Skip to content

Commit

Permalink
Get chopn_inplace working, and puzzle over strlen/bufused differences.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/branches/strings@35630 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
simon committed Jan 16, 2009
1 parent 2de1fe8 commit 287b2c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 3 additions & 1 deletion pseudocode/Encodings.pm
@@ -1,6 +1,6 @@
class ParrotEncoding::Base::Fixed {
our $.width;
method string_length($str) { return $str.bufused / $str.encoding.width; }
method string_length($str) { return $str.strlen / $str.encoding.width; }

method string_char_iterate($str, $callback, $parameter) {
for (0..self.string_length($str)-1) {
Expand All @@ -20,6 +20,8 @@ class ParrotEncoding::Base::Fixed {
$callback($str.encoding.grapheme_at_index($str,$_), $parameter);
}
}

method chopn_inplace($str, $n) { $str.strlen -= $n * $.width }
}

class ParrotEncoding::Base::Variable {
Expand Down
4 changes: 3 additions & 1 deletion pseudocode/ParrotString.pm
Expand Up @@ -85,7 +85,9 @@ sub Parrot_string_grapheme_substr($src, $offset, $len) { ... }
sub Parrot_string_replace($src, $offset, $len, $replacement) { ... }
sub Parrot_string_grapheme_replace($src, $offset, $len, $replacement) { ... }
sub Parrot_string_chopn($src, $count) { ... }
sub Parrot_string_chopn_inplace($src, $count) { ... }
sub Parrot_string_chopn_inplace($str, $count) {
return $str.encoding.chopn_inplace($str, $count);
}

sub Parrot_string_grapheme_chopn($src, $count) {
return Parrot_string_replace($src, Parrot_string_grapheme_length($src) - $count, $count, undef);
Expand Down
7 changes: 5 additions & 2 deletions pseudocode/t/create.t
@@ -1,12 +1,15 @@
use Test;
use ParrotString;
plan 8;
plan 10;

my $str = Parrot_string_new_init("flurble", 4, ParrotCharset::ASCII, ParrotEncoding::Byte);
ok($str.charset ~~ ParrotCharset::ASCII, "Charset set properly");
is(Parrot_string_grapheme_length($str), 4, "String length correct");
is(Parrot_string_grapheme_length($str), 4, "Grapheme length correct");
is(Parrot_string_byte_length($str), 4, "String length correct");
is(Parrot_string_index($str, 1), ord("l"), "String indexing");
Parrot_string_chopn_inplace($str, 2);
is(Parrot_string_grapheme_length($str), 2, "Grapheme length correct post-chop");
is(Parrot_string_byte_length($str), 2, "String length correct post-chop");

$str = Parrot_string_new_init("\xce\xb3\xce\xb5\xce\xb9\xce\xac \xcf\x83\xce\xbf\xcf\x85 \xce\xba\xcf\x8c\xcf\x83\xce\xbc\xce\xbf\xcf\x82", 28, ParrotCharset::Unicode, ParrotEncoding::UTF8);
ok($str.charset ~~ ParrotCharset::Unicode, "We're unicode");
Expand Down

0 comments on commit 287b2c5

Please sign in to comment.