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

NotFound at svn.parrot.org NotFound at svn.parrot.org
Wed Jun 9 01:02:18 UTC 2010


Author: NotFound
Date: Wed Jun  9 01:02:17 2010
New Revision: 47498
URL: https://trac.parrot.org/parrot/changeset/47498

Log:
ByteBuffer set_integer_keyed_int

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

Modified: trunk/src/pmc/bytebuffer.pmc
==============================================================================
--- trunk/src/pmc/bytebuffer.pmc	Tue Jun  8 23:58:15 2010	(r47497)
+++ trunk/src/pmc/bytebuffer.pmc	Wed Jun  9 01:02:17 2010	(r47498)
@@ -132,6 +132,58 @@
         return (position >= 0 && position < size) ? content[position] : (INTVAL) 0;
     }
 
+/*
+
+=item C<void set_integer_keyed_int()>
+Set the value of the byte at position.
+
+=cut
+
+*/
+
+    VTABLE void set_integer_keyed_int(INTVAL position, INTVAL value) {
+        unsigned char *content;
+        INTVAL size, allocated_size;
+        if (position < 0)
+            Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
+                "Negative position not allowed");
+
+        GET_ATTR_allocated_size(INTERP, SELF, allocated_size);
+        if (position >= allocated_size) {
+            INTVAL newsize = position + 1;
+            if (allocated_size == 0) {
+                INTVAL copysize = newsize;
+                STRING * source;
+                content = (unsigned char *)Parrot_gc_allocate_memory_chunk(INTERP, newsize);
+                GET_ATTR_source(INTERP, SELF, source);
+                if (!STRING_IS_NULL(source)) {
+                    INTVAL srclen = Parrot_str_byte_length(INTERP, source);
+                    if (srclen < copysize)
+                        copysize = srclen;
+                    memcpy(content, source->strstart, copysize);
+                    SET_ATTR_source(INTERP, SELF, STRINGNULL);
+                }
+            }
+            else {
+                GET_ATTR_content(INTERP, SELF, content);
+                content = (unsigned char *)
+                    Parrot_gc_reallocate_memory_chunk(INTERP, content, newsize);
+            }
+
+            SET_ATTR_content(INTERP, SELF, content);
+            SET_ATTR_allocated_size(INTERP, SELF, newsize);
+        }
+        else
+            GET_ATTR_content(INTERP, SELF, content);
+
+        GET_ATTR_size(INTERP, SELF, size);
+        if (position >= size) {
+            size = position + 1;
+            SET_ATTR_size(INTERP, SELF, size);
+        }
+        content[position] = value;
+    }
+
 } /* pmclass end */
 
 /*

Modified: trunk/t/pmc/bytebuffer.t
==============================================================================
--- trunk/t/pmc/bytebuffer.t	Tue Jun  8 23:58:15 2010	(r47497)
+++ trunk/t/pmc/bytebuffer.t	Wed Jun  9 01:02:17 2010	(r47498)
@@ -18,10 +18,11 @@
 
 .sub 'main' :main
     .include 'test_more.pir'
-    plan(7)
+    plan(12)
 
     test_init()
-    test_set()
+    test_set_string()
+    test_set_byte()
 .end
 
 .sub test_init
@@ -37,7 +38,7 @@
 
 .end
 
-.sub test_set
+.sub test_set_string
     .local pmc bb
     .local string s
     .local int n, c
@@ -58,6 +59,32 @@
     is(n, 0, "byte at negative index is 0")
 .end
 
+.sub test_set_byte
+    .local pmc bb
+    .local int n
+    bb = new ['ByteBuffer']
+    bb[255] = 42
+    n = elements bb
+    is(n, 256, "setting a byte resize empty buffer")
+
+    .local string s
+    s = 'Hi'
+    bb = s
+    bb[2] = 42
+    n = elements bb
+    is(n, 3, "setting a byte resize buffer initialized from string")
+
+    bb = new ['ByteBuffer'], 42
+    bb[41] = 9
+    n = elements bb
+    is(n, 42, "setting a byte within range does not resize")
+    bb[42] = 7
+    n = elements bb
+    is(n, 43, "setting a byte resize buffer with initial size")
+    n = bb[41]
+    is(n, 9, "resized buffer preserve old value")
+.end
+
 # Local Variables:
 #   mode: pir
 #   fill-column: 100


More information about the parrot-commits mailing list