[svn:parrot] r46241 - branches/codestring/src/pmc

darbelo at svn.parrot.org darbelo at svn.parrot.org
Mon May 3 17:42:05 UTC 2010


Author: darbelo
Date: Mon May  3 17:42:05 2010
New Revision: 46241
URL: https://trac.parrot.org/parrot/changeset/46241

Log:
Merge non-codestring changes from trunk.

Modified:
   branches/codestring/src/pmc/codestring.pmc

Modified: branches/codestring/src/pmc/codestring.pmc
==============================================================================
--- branches/codestring/src/pmc/codestring.pmc	Mon May  3 17:37:33 2010	(r46240)
+++ branches/codestring/src/pmc/codestring.pmc	Mon May  3 17:42:05 2010	(r46241)
@@ -39,8 +39,7 @@
 /* HEADERIZER END: static */
 
 pmclass CodeString extends String provides string auto_attrs {
-    ATTR PMC *linepos;  /* start of line positions */
-    ATTR PMC *strings;  /* array of strings... */
+    ATTR PMC *linepos;            /* start of line positions */
 
 /*
 
@@ -53,8 +52,8 @@
 */
 
     VTABLE void init() {
+        SUPER();
         SET_ATTR_linepos(INTERP, SELF, PMCNULL);
-        SET_ATTR_strings(INTERP, SELF, pmc_new(INTERP, enum_class_ResizableStringArray));
         PObj_custom_mark_SET(SELF);
     }
 
@@ -69,19 +68,13 @@
 */
 
     VTABLE void mark() {
+        SUPER();
         if (PMC_data(SELF)) {
-            PMC *linepos, *strings;
-
-            /* force the join to avoid marking the RPA and lots of STRINGs */
-            STRING *contents = SELF.get_string();
-            UNUSED(contents);
+            PMC *linepos;
 
             GET_ATTR_linepos(INTERP, SELF, linepos);
-            Parrot_gc_mark_PMC_alive(INTERP, linepos);
 
-            /* all that's in here now is a single STRING */
-            GET_ATTR_strings(INTERP, SELF, strings);
-            Parrot_gc_mark_PMC_alive(INTERP, strings);
+            Parrot_gc_mark_PMC_alive(INTERP, linepos);
         }
     }
 
@@ -104,8 +97,7 @@
 A percent-sign followed by any other character that is a hash
 key receives the value of the hash element.
 
-A newline is automatically added to the end of the fmt, if one
-is not already present.
+A newline is automatically added to the end of the fmt.
 
 =cut
 
@@ -116,131 +108,60 @@
     STRING * const comma       = CONST_STRING(INTERP, ",");
     STRING * const comma_space = CONST_STRING(INTERP, ", ");
     STRING * const newline     = CONST_STRING(INTERP, "\n");
-    STRING *key;
-    PMC    *strings;
-    INTVAL percentPos;
-    INTVAL pos = 0;
+    PMC           *parts       = PMCNULL;
+    STRING *key, *repl, *S1;
+    INTVAL pos          = 0;
+    INTVAL replen       = 0;
 
-    GET_ATTR_strings(INTERP, SELF, strings);
-
-    /* Loop over the format string, splitting it into chunks
-     * for the string builder. */
     while (pos >= 0) {
-        /* Find the next % */
-        percentPos = Parrot_str_find_index(INTERP, fmt, percent, pos);
-
-        if (percentPos < 0) {
-            if (pos == 0) {
-                VTABLE_push_string(INTERP, strings, fmt);
-            }
-            else {
-                /* remaining string can be added as is. */
-                VTABLE_push_string(INTERP, strings,
-                    Parrot_str_substr(INTERP, fmt, pos,
-                        Parrot_str_byte_length(INTERP, fmt) -pos));
-                }
+        pos += replen;
+        pos = Parrot_str_find_index(INTERP, fmt, percent, pos);
+        if (pos < 0)
             break;
-        }
-        else {
-            /* slurp up to just before the % sign... */
-            VTABLE_push_string(INTERP, strings,
-                Parrot_str_substr(INTERP, fmt, pos, percentPos - pos));
-            /* skip the % sign */
-            pos = percentPos + 1 ;
-        }
 
-        /* key is always a single character */
-        key = Parrot_str_substr(INTERP, fmt, pos++, 1);
+        key = Parrot_str_substr(INTERP, fmt, pos+1, 1);
 
         if (VTABLE_exists_keyed_str(INTERP, hash, key)) {
-            VTABLE_push_string(INTERP, strings,
-                    VTABLE_get_string_keyed_str(INTERP, hash, key));
+            repl = VTABLE_get_string_keyed_str(INTERP, hash, key);
         }
-        else if (Parrot_str_is_cclass(INTERP, enum_cclass_numeric, key, 0)) {
-            VTABLE_push_string(INTERP, strings,
-                VTABLE_get_string_keyed_int(INTERP, args,
-                    Parrot_str_to_int(INTERP, key)));
+        else if (Parrot_str_is_cclass(INTERP, enum_cclass_numeric, fmt, (UINTVAL)pos + 1)) {
+            const INTVAL I0 = Parrot_str_to_int(INTERP, key);
+            repl = VTABLE_get_string_keyed_int(INTERP, args, I0);
         }
         else if (Parrot_str_equal(INTERP, key, comma)) {
-            INTVAL num_args = VTABLE_elements(INTERP, args);
-            INTVAL pos_args = 1;
-
-            VTABLE_push_string(INTERP, strings,
-                VTABLE_get_string_keyed_int(INTERP, args, 0));
-
-            while (pos_args < num_args) {
-                VTABLE_push_string(INTERP, strings, comma_space);
-                VTABLE_push_string(INTERP, strings,
-                    VTABLE_get_string_keyed_int(INTERP, args, pos_args));
-                pos_args++;
-            }
+            repl = Parrot_str_join(INTERP, comma_space, args);
         }
         else if (Parrot_str_equal(INTERP, key, percent)) {
-            VTABLE_push_string(INTERP, strings, percent);
+            repl = percent;
         }
         else {
-            /* %foo has no special meaning, pass it through unchanged */
-            VTABLE_push_string(INTERP, strings,
-                key = Parrot_str_substr(INTERP, fmt, pos-2, 2));
+            /* No substitution is necessary */
+            replen = 2;
+            continue;
         }
-    }
-
-    /* Add a newline if necessary */
-    if ('\n' != Parrot_str_indexed(INTERP, fmt, Parrot_str_byte_length(INTERP, fmt) - 1))
-        VTABLE_push_string(INTERP, strings, newline);
-
-    RETURN(PMC *SELF);
-}
-
-/*
 
-=item get_string()
-
-Stringify ourselves.
-
-=cut
-
-*/
-
-
-    VTABLE STRING *get_string() {
-        PMC    *strings;
-
-        GET_ATTR_strings(INTERP, SELF, strings);
-
-        /* avoid unnecessary joins */
-        if (VTABLE_elements(INTERP, strings) <= 1)
-            return VTABLE_get_string_keyed_int(INTERP, strings, 0);
-        else {
-            STRING const *empty  = CONST_STRING(INTERP, "");
-            STRING const *result = Parrot_str_join(INTERP, empty, strings);
-
-            /* cache the result to avoid a join the next time. */
-            SELF.set_string_native(result);
-
-            return result;
-        }
+        fmt    = Parrot_str_replace(INTERP, fmt, pos, 2, repl);
+        replen = Parrot_str_byte_length(INTERP, repl);
     }
 
-/*
-
-=item set_string_native()
+    GET_ATTR_str_val(INTERP, SELF, S1);
 
-Save our value as a single entry in the StringBuilder.
+    parts = Parrot_pmc_new_temporary(INTERP, enum_class_ResizableStringArray);
+    VTABLE_set_integer_native(INTERP, parts, 3);
+    VTABLE_set_string_keyed_int(INTERP, parts, 0, S1);
+    VTABLE_set_string_keyed_int(INTERP, parts, 1, fmt);
 
-=cut
+    /* Add a newline if necessary */
+    if ('\n' != Parrot_str_indexed(INTERP, fmt, Parrot_str_byte_length(INTERP, fmt) - 1))
+        VTABLE_set_string_keyed_int(INTERP, parts, 2, newline);
 
-*/
+    S1 = Parrot_str_join(INTERP, STRINGNULL, parts);
+    VTABLE_set_string_native(INTERP, SELF, S1);
+    Parrot_pmc_free_temporary(INTERP, parts);
 
+    RETURN(PMC *SELF);
+  }
 
-    VTABLE void set_string_native(STRING *value) {
-        /* reuse our old stringBuilder */
-        PMC *strings;
-
-        GET_ATTR_strings(INTERP, SELF, strings);
-        VTABLE_set_integer_native(INTERP, strings, 0);
-        VTABLE_push_string(INTERP, strings, value);
-    }
 
 /*
 
@@ -268,7 +189,7 @@
 
         linepos = Parrot_pmc_new(INTERP, enum_class_ResizableIntegerArray);
         /* get the string itself */
-        str = SELF.get_string();
+        GET_ATTR_str_val(INTERP, SELF, str);
         eos  = Parrot_str_byte_length(INTERP, str);
         /* find the first newline, if any */
         jpos = Parrot_str_find_cclass(INTERP, enum_cclass_newline,


More information about the parrot-commits mailing list