[svn:parrot] r48779 - in trunk: src/pmc t/pmc
chromatic at svn.parrot.org
chromatic at svn.parrot.org
Sat Sep 4 15:03:45 UTC 2010
Author: chromatic
Date: Sat Sep 4 15:03:45 2010
New Revision: 48779
URL: https://trac.parrot.org/parrot/changeset/48779
Log:
[PMC] Added init_pmc() to StringBuilder PMC.
Modified:
trunk/src/pmc/stringbuilder.pmc
trunk/t/pmc/stringbuilder.t
Modified: trunk/src/pmc/stringbuilder.pmc
==============================================================================
--- trunk/src/pmc/stringbuilder.pmc Sat Sep 4 11:48:35 2010 (r48778)
+++ trunk/src/pmc/stringbuilder.pmc Sat Sep 4 15:03:45 2010 (r48779)
@@ -37,6 +37,7 @@
pmclass StringBuilder provides string auto_attrs {
ATTR STRING *buffer; /* Mutable string to gather results */
+
/*
=item C<void init()>
@@ -51,6 +52,7 @@
STATICSELF.init_int(INITIAL_STRING_CAPACITY);
}
+
/*
=item C<void init_int()>
@@ -63,19 +65,53 @@
VTABLE void init_int(INTVAL initial_size) {
STRING * const buffer = mem_gc_allocate_zeroed_typed(INTERP, STRING);
- buffer->encoding = Parrot_default_encoding_ptr;
- buffer->charset = Parrot_default_charset_ptr;
- /* We need all string flags here because we use this buffer in substr_str */
- buffer->flags = PObj_is_string_FLAG | PObj_external_FLAG;
- buffer->_bufstart = buffer->strstart = mem_gc_allocate_n_typed(INTERP,
- initial_size, char);
- buffer->_buflen = initial_size;
+
+ buffer->encoding = Parrot_default_encoding_ptr;
+ buffer->charset = Parrot_default_charset_ptr;
+ buffer->_buflen = initial_size;
+ buffer->_bufstart = buffer->strstart
+ = mem_gc_allocate_n_typed(INTERP,
+ initial_size, char);
+
+ /* We need these string flags to use this buffer in substr_str */
+ buffer->flags = PObj_is_string_FLAG | PObj_external_FLAG;
SET_ATTR_buffer(INTERP, SELF, buffer);
PObj_custom_destroy_SET(SELF);
}
+
+/*
+
+=item C<void init_pmc()>
+
+Initializes the StringBuilder with an array of STRINGs.
+
+=cut
+
+*/
+
+ VTABLE void init_pmc(PMC *ar) {
+ const INTVAL count = VTABLE_elements(INTERP, ar);
+
+ if (!count)
+ STATICSELF.init_int(INITIAL_STRING_CAPACITY);
+ else {
+ STRING * const first = VTABLE_get_string_keyed_int(INTERP, ar, 0);
+ const INTVAL size = Parrot_str_byte_length(INTERP, first);
+ INTVAL i;
+
+ /* it's just an estimate, but estimates help */
+ STATICSELF.init_int(size * count);
+ SELF.push_string(first);
+
+ for (i = 1; i < count; ++i)
+ SELF.push_string(VTABLE_get_string_keyed_int(INTERP, ar, i));
+ }
+ }
+
+
/*
=item C<void destroy()>
Modified: trunk/t/pmc/stringbuilder.t
==============================================================================
--- trunk/t/pmc/stringbuilder.t Sat Sep 4 11:48:35 2010 (r48778)
+++ trunk/t/pmc/stringbuilder.t Sat Sep 4 15:03:45 2010 (r48779)
@@ -21,6 +21,7 @@
.include 'test_more.pir'
test_create() # 2 tests
+ test_init_pmc()
test_push_string()
test_push_pmc() # 4 tests
test_push_string_unicode() # 1 test
@@ -110,7 +111,6 @@
push sb, $S0
$I0 = sb
is( $I0, 16384, "push a null string does nothing" )
-
.end
.sub 'test_push_pmc'
@@ -305,6 +305,34 @@
ok( $S0, "Pushing unicode strings doesn't kill StringBuilder")
.end
+.sub 'test_init_pmc'
+ .local pmc ar
+ ar = new ['ResizableStringArray']
+
+ push ar, "foo"
+ push ar, "bar"
+
+ $S99 = repeat "x", 12
+ push ar, $S99
+ $S1 = 'foobar' . $S99
+
+ $S99 = repeat "y", 13
+ push ar, $S99
+ $S1 = $S1 . $S99
+
+ $S99 = repeat "z", 14
+ push ar, $S99
+ $S1 = $S1 . $S99
+
+ null $S0
+ push ar, $S0
+
+ .local pmc sb
+ sb = new ["StringBuilder"], ar
+ $S0 = sb
+ is( $S0, $S1, 'init_pmc() should join all passed strings' )
+.end
+
# Local Variables:
# mode: pir
# fill-column: 100
More information about the parrot-commits
mailing list