[svn:parrot] r35630 - in branches/strings/pseudocode: . t

[email protected]
Newsgroups perl.cvs.parrot
Message-ID <[email protected]>
Author: simon
Date: Fri Jan 16 05:55:37 2009
New Revision: 35630

Modified:
   branches/strings/pseudocode/Encodings.pm
   branches/strings/pseudocode/ParrotString.pm
   branches/strings/pseudocode/t/create.t

Log:
Get chopn_inplace working, and puzzle over strlen/bufused differences.


Modified: branches/strings/pseudocode/Encodings.pm
==============================================================================
--- branches/strings/pseudocode/Encodings.pm	(original)
+++ branches/strings/pseudocode/Encodings.pm	Fri Jan 16 05:55:37 2009
@@ -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) { 
@@ -20,6 +20,8 @@
             $callback($str.encoding.grapheme_at_index($str,$_), $parameter); 
         }
     }
+
+    method chopn_inplace($str, $n) { $str.strlen -= $n * $.width }
 }
 
 class ParrotEncoding::Base::Variable {

Modified: branches/strings/pseudocode/ParrotString.pm
==============================================================================
--- branches/strings/pseudocode/ParrotString.pm	(original)
+++ branches/strings/pseudocode/ParrotString.pm	Fri Jan 16 05:55:37 2009
@@ -85,7 +85,9 @@
 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);

Modified: branches/strings/pseudocode/t/create.t
==============================================================================
--- branches/strings/pseudocode/t/create.t	(original)
+++ branches/strings/pseudocode/t/create.t	Fri Jan 16 05:55:37 2009
@@ -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");
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.