[svn:parrot] r46334 - trunk/src/pmc

bacek at svn.parrot.org bacek at svn.parrot.org
Thu May 6 12:03:08 UTC 2010


Author: bacek
Date: Thu May  6 12:03:08 2010
New Revision: 46334
URL: https://trac.parrot.org/parrot/changeset/46334

Log:
Add init/init_pmc/mark/get_string scaffolding.

Modified:
   trunk/src/pmc/stringbuilder.pmc

Modified: trunk/src/pmc/stringbuilder.pmc
==============================================================================
--- trunk/src/pmc/stringbuilder.pmc	Thu May  6 11:42:34 2010	(r46333)
+++ trunk/src/pmc/stringbuilder.pmc	Thu May  6 12:03:08 2010	(r46334)
@@ -22,7 +22,79 @@
 /* HEADERIZER BEGIN: static */
 /* HEADERIZER END: static */
 
+#define INITIAL_STRING_CAPACITY 128
+
 pmclass StringBuilder provides string auto_attrs {
+    ATTR STRING *buffer;    /* Mutable string to gather results */
+
+/*
+
+=item C<void init()>
+
+Initializes the StringBuilder.
+
+=cut
+
+*/
+
+    VTABLE void init() {
+        STATICSELF.init_int(INITIAL_STRING_CAPACITY);
+    }
+
+/*
+
+=item C<void init_int()>
+
+Initializes the StringBuilder with initial size of buffer.
+
+=cut
+
+*/
+
+    VTABLE void init_int(INTVAL initial_size) {
+        STRING * const buffer = Parrot_str_new_init(INTERP, NULL, initial_size,
+                Parrot_default_encoding_ptr, Parrot_default_charset_ptr, 0);
+        SET_ATTR_buffer(INTERP, SELF, buffer);
+
+        PObj_custom_mark_SET(SELF);
+    }
+
+/*
+
+=item C<void mark()>
+
+Marks the StringBuilder as live.
+
+=cut
+
+*/
+
+    VTABLE void mark() {
+        STRING *buffer;
+
+        if (!PMC_data(SELF))
+            return;
+
+        GET_ATTR_buffer(INTERP, SELF, buffer);
+        Parrot_gc_mark_STRING_alive(INTERP, buffer);
+    }
+
+/*
+
+=item C<STRING *get_string()>
+
+Clones and returns current buffer. We need clone because outside of
+StringBuilder strings are immutable.
+
+=cut
+
+*/
+
+    VTABLE STRING *get_string() {
+        STRING *buffer;
+        GET_ATTR_buffer(INTERP, SELF, buffer);
+        return Parrot_str_clone(INTERP, buffer);
+    }
 
 
 /*


More information about the parrot-commits mailing list