[svn:parrot] r46346 - in trunk: src/pmc t/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Thu May 6 12:30:33 UTC 2010


Author: bacek
Date: Thu May  6 12:30:33 2010
New Revision: 46346
URL: https://trac.parrot.org/parrot/changeset/46346

Log:
Add converting of strigns in different encodings to StringBuilder

Modified:
   trunk/src/pmc/stringbuilder.pmc
   trunk/t/pmc/stringbuilder.t

Modified: trunk/src/pmc/stringbuilder.pmc
==============================================================================
--- trunk/src/pmc/stringbuilder.pmc	Thu May  6 12:30:11 2010	(r46345)
+++ trunk/src/pmc/stringbuilder.pmc	Thu May  6 12:30:33 2010	(r46346)
@@ -18,6 +18,8 @@
 
 */
 
+#include "parrot/string_funcs.h"
+
 /* HEADERIZER HFILE: none */
 /* HEADERIZER BEGIN: static */
 /* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
@@ -117,10 +119,32 @@
     VTABLE void push_string(STRING *s) {
         STRING          *buffer;
         size_t           total_size;
+        const CHARSET   *cs;
+        const ENCODING  *enc = NULL;
+
+        /* Early return on NULL strings */
+        if (STRING_IS_NULL(s))
+            return;
 
         GET_ATTR_buffer(INTERP, SELF, buffer);
 
-        /* TODO If strings are incompatible - convert them */
+        /* If strings are incompatible - convert them */
+        /* TODO Ask chromatic why in Parrot_str_join he ignored charset */
+        cs = Parrot_str_rep_compatible(interp, buffer, s, &enc);
+        if (!cs) {
+            cs = Parrot_unicode_charset_ptr;
+            enc = (buffer->encoding == Parrot_utf16_encoding_ptr
+               ||  s->encoding == Parrot_utf16_encoding_ptr
+               ||  buffer->encoding == Parrot_ucs2_encoding_ptr
+               ||  s->encoding == Parrot_ucs2_encoding_ptr)
+                ? Parrot_utf16_encoding_ptr
+                : Parrot_utf8_encoding_ptr;
+
+            buffer = Parrot_unicode_charset_ptr->to_charset(interp, buffer);
+            s      = Parrot_unicode_charset_ptr->to_charset(interp, s);
+
+            SET_ATTR_buffer(INTERP, SELF, buffer);
+        }
 
         /* Calculate (possibly new) total size */
         total_size = calculate_capacity(INTERP, buffer->bufused, s->bufused);

Modified: trunk/t/pmc/stringbuilder.t
==============================================================================
--- trunk/t/pmc/stringbuilder.t	Thu May  6 12:30:11 2010	(r46345)
+++ trunk/t/pmc/stringbuilder.t	Thu May  6 12:30:33 2010	(r46346)
@@ -20,9 +20,10 @@
 .sub 'main' :main
     .include 'test_more.pir'
 
-    plan(12)
-    test_create()       # 2 tests
-    test_push_string()
+    plan(13)
+    test_create()               # 2 tests
+    test_push_string()          # 9 tests
+    test_push_string_unicode()  # 1 test
 
     # END_OF_TESTS
 .end
@@ -90,6 +91,18 @@
 
 .end
 
+.sub 'test_push_string_unicode'
+    .local pmc sb
+    sb = new ["StringBuilder"]
+
+    push sb, "le"
+    push sb, unicode:"o "
+    push sb, iso-8859-1:"tötsch"
+
+    $S0 = sb
+    is( $S0, iso-8859-1:"leo tötsch", "Unicode strings appened")
+.end
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100


More information about the parrot-commits mailing list