[macruby-changes] [3563] MacRuby/branches/icu

source_changes at macosforge.org source_changes at macosforge.org
Tue Feb 16 17:55:10 PST 2010


Revision: 3563
          http://trac.macosforge.org/projects/ruby/changeset/3563
Author:   lsansonetti at apple.com
Date:     2010-02-16 17:55:10 -0800 (Tue, 16 Feb 2010)
Log Message:
-----------
migrating bytestrings + misc cleanup

Modified Paths:
--------------
    MacRuby/branches/icu/encoding.h
    MacRuby/branches/icu/include/ruby/ruby.h
    MacRuby/branches/icu/io.c
    MacRuby/branches/icu/marshal.c
    MacRuby/branches/icu/pack.c
    MacRuby/branches/icu/random.c
    MacRuby/branches/icu/ruby.c
    MacRuby/branches/icu/string.c
    MacRuby/branches/icu/time.c

Modified: MacRuby/branches/icu/encoding.h
===================================================================
--- MacRuby/branches/icu/encoding.h	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/encoding.h	2010-02-17 01:55:10 UTC (rev 3563)
@@ -267,6 +267,18 @@
 	    STRING_VALID_ENCODING);
 }
 
+// Return a string object appropriate for bstr_ calls. This does nothing for
+// data/binary RubyStrings.
+VALUE rb_str_bstr(VALUE str);
+
+// Byte strings APIs. Use this only when dealing with raw data.
+VALUE bstr_new(void);
+VALUE bstr_new_with_data(const uint8_t *bytes, long len);
+uint8_t *bstr_bytes(VALUE str);
+long bstr_length(VALUE str);
+void bstr_set_length(VALUE str, long len);
+void bstr_resize(VALUE str, long capa);
+
 #if defined(__cplusplus)
 } // extern "C"
 #endif

Modified: MacRuby/branches/icu/include/ruby/ruby.h
===================================================================
--- MacRuby/branches/icu/include/ruby/ruby.h	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/include/ruby/ruby.h	2010-02-17 01:55:10 UTC (rev 3563)
@@ -396,18 +396,6 @@
 #define StringValuePtr(v) rb_string_value_ptr(&(v))
 #define StringValueCStr(v) rb_string_value_cstr(&(v))
 
-VALUE rb_bytestring_new();
-VALUE rb_bytestring_new_with_data(const UInt8 *buf, long size);
-VALUE rb_bytestring_new_with_cfdata(CFMutableDataRef data);
-VALUE rb_bytestring_copy(VALUE str);
-CFMutableDataRef rb_bytestring_wrapped_data(VALUE);
-CFStringRef rb_bytestring_resolve_cfstring(VALUE str);
-UInt8 *rb_bytestring_byte_pointer(VALUE);
-VALUE rb_coerce_to_bytestring(VALUE);
-long rb_bytestring_length(VALUE str);
-void rb_bytestring_resize(VALUE str, long newsize);
-void rb_bytestring_append_bytes(VALUE str, const UInt8* bytes, long len);
-
 void rb_check_safe_obj(VALUE);
 void rb_check_safe_str(VALUE);
 #define SafeStringValue(v) do {\

Modified: MacRuby/branches/icu/io.c
===================================================================
--- MacRuby/branches/icu/io.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/io.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -16,9 +16,8 @@
 #include "vm.h"
 #include "objc.h"
 #include "id.h"
+#include "encoding.h"
 
-extern VALUE rb_cByteString; // TODO it does not exist anymore
-
 #include <errno.h>
 #include <paths.h>
 #include <fcntl.h>
@@ -409,36 +408,24 @@
  */
 
 static VALUE
-io_write(VALUE io, SEL sel, VALUE to_write)
+io_write(VALUE io, SEL sel, VALUE data)
 {
     rb_secure(4);
 
     VALUE tmp = rb_io_check_io(io);
     if (NIL_P(tmp)) {
 	// receiver is not IO, dispatch the write method on it
-	return rb_vm_call(io, selWrite, 1, &to_write, false);
+	return rb_vm_call(io, selWrite, 1, &data, false);
     }
     io = tmp;
     
     rb_io_t *io_struct = ExtractIOStruct(io);
     rb_io_assert_writable(io_struct);
-    to_write = rb_obj_as_string(to_write);
+    data = rb_obj_as_string(data);
 
-    UInt8 *buffer;
-    size_t length;
-    if (CLASS_OF(to_write) == rb_cByteString) {
-	CFMutableDataRef data = rb_bytestring_wrapped_data(to_write);
-	buffer = CFDataGetMutableBytePtr(data);
-	length = CFDataGetLength(data);
-    }
-    else {
-	buffer = (UInt8 *)RSTRING_PTR(to_write);
-	if (buffer == NULL) {
-	    rb_raise(rb_eRuntimeError,
-		    "could not extract a string from the read data.");
-	}
-	length = strlen((char *)buffer);
-    }
+    data = rb_str_bstr(data);
+    const uint8_t *buffer = bstr_bytes(data);
+    const long length = bstr_length(data);
 
     if (length == 0) {
         return INT2FIX(0);
@@ -989,24 +976,26 @@
 }
 
 static VALUE 
-rb_io_read_all(rb_io_t *io_struct, VALUE bytestring_buffer) 
+rb_io_read_all(rb_io_t *io_struct, VALUE outbuf) 
 {
+    outbuf = rb_str_bstr(outbuf);
+
     const long BUFSIZE = 512;
-    CFMutableDataRef data = rb_bytestring_wrapped_data(bytestring_buffer);
     long bytes_read = 0;
-    const long original_position = (long)CFDataGetLength(data);
-    for (;;) {
-        CFDataIncreaseLength(data, BUFSIZE);
-        UInt8 *b = CFDataGetMutableBytePtr(data) + original_position
-	    + bytes_read;
-        const long last_read = rb_io_read_internal(io_struct, b, BUFSIZE);
+    const long original_position = bstr_length(outbuf);
+
+    while (true) {
+	bstr_resize(outbuf, BUFSIZE);
+	uint8_t *bytes = bstr_bytes(outbuf) + original_position + bytes_read;
+        const long last_read = rb_io_read_internal(io_struct, bytes, BUFSIZE);
         bytes_read += last_read;
 	if (last_read == 0) {
 	    break;
 	}
     }
-    CFDataSetLength(data, original_position + bytes_read);
-    return bytestring_buffer; 
+
+    bstr_set_length(outbuf, original_position + bytes_read);
+    return outbuf; 
 }
 
 long
@@ -1124,13 +1113,8 @@
     rb_io_assert_readable(io_struct);
 
     if (NIL_P(outbuf)) {
-	outbuf = rb_bytestring_new();
+	outbuf = bstr_new();
     }
-    else if (CLASS_OF(outbuf) != rb_cByteString) {
-	// TODO: Get the magical pointer incantations right.
-	rb_raise(rb_eIOError,
-		"writing to non-bytestrings is not supported at this time.");
-    }
 
     if (NIL_P(len)) {
         return rb_io_read_all(io_struct, outbuf);
@@ -1148,15 +1132,15 @@
 	rb_raise(rb_eArgError, "given size `%ld' is too big", size);
     }
 
-    CFMutableDataRef data = rb_bytestring_wrapped_data(outbuf);
-    CFDataIncreaseLength(data, size);
-    UInt8 *buf = CFDataGetMutableBytePtr(data);
+    outbuf = rb_str_bstr(outbuf);
+    bstr_resize(outbuf, size);
+    uint8_t *bytes = bstr_bytes(outbuf);
 
-    const long data_read = rb_io_read_internal(io_struct, buf, size);
+    const long data_read = rb_io_read_internal(io_struct, bytes, size);
     if (data_read == 0) {
 	return Qnil;
     }
-    CFDataSetLength(data, data_read);
+    bstr_set_length(outbuf, data_read);
 
     return outbuf;
 }
@@ -1290,24 +1274,25 @@
 	}
     }
     const long line_limit = NIL_P(limit) ? -1 : FIX2LONG(limit);
-    // now that we've got our parameters, let's get down to business.
 
-    VALUE bstr = rb_bytestring_new();
-    CFMutableDataRef data = rb_bytestring_wrapped_data(bstr);
+    VALUE bstr = bstr_new();
     if (line_limit != -1) {
-	CFDataIncreaseLength(data, line_limit);
-	UInt8 *b = CFDataGetMutableBytePtr(data);
-	rb_io_read_internal(io_struct, b, line_limit);
+	bstr_resize(bstr, line_limit);
+	uint8_t *bytes = bstr_bytes(bstr);
+	rb_io_read_internal(io_struct, bytes, line_limit);
+#if 0 // TODO
 	CFRange r = CFStringFind((CFStringRef)bstr, (CFStringRef)sep, 0);
 	if (r.location != kCFNotFound) {
 	    CFDataSetLength(data, r.location);
 	}
+#endif
     }
     else {
 	const char *sepstr = RSTRING_PTR(sep);
 	const long seplen = RSTRING_LEN(sep);
 	assert(seplen > 0);
 
+#if 0 // TODO
 	// Pre-cache if possible.
 	rb_io_read_internal(io_struct, NULL, 0);
 	if (io_struct->buf != NULL && CFDataGetLength(io_struct->buf) > 0) {
@@ -1337,23 +1322,24 @@
 	    CFDataAppendBytes(data, cache, data_read);
 	    rb_io_read_update(io_struct, data_read);
 	}
-	else {
+	else 
+#endif
+	{
 	    // Read from IO (slow).
 	    long s = 512;
 	    long data_read = 0;
-	    CFDataSetLength(data, s);
+	    bstr_resize(bstr, s);
 
-	    UInt8 *buf = CFDataGetMutableBytePtr(data);
-	    UInt8 *tmp_buf = alloca(seplen);
-
+	    uint8_t *buf = bstr_bytes(bstr);
+	    uint8_t *tmp_buf = (uint8_t *)malloc(seplen);
 	    while (true) {
 		if (rb_io_read_internal(io_struct, tmp_buf, seplen) != seplen) {
 		    break;
 		}
 		if (data_read >= s) {
 		    s += s;
-		    CFDataSetLength(data, s);
-		    buf = CFDataGetMutableBytePtr(data);
+		    bstr_resize(bstr, s);
+		    buf = bstr_bytes(bstr);
 		}
 		memcpy(&buf[data_read], tmp_buf, seplen);
 		data_read += seplen;
@@ -1362,10 +1348,12 @@
 		    break;
 		}
 	    }
+	    free(tmp_buf);
+
 	    if (data_read == 0) {
 		return Qnil;
 	    }
-	    CFDataSetLength(data, data_read);
+	    bstr_set_length(bstr, data_read);
 	}
     }
     OBJ_TAINT(bstr);
@@ -3349,11 +3337,11 @@
 	io_s->pid = -1;
     }
 
-    VALUE bstr = rb_bytestring_new();
-    rb_io_read_all(ExtractIOStruct(io), bstr);
+    VALUE outbuf = bstr_new();
+    rb_io_read_all(ExtractIOStruct(io), outbuf);
     rb_io_close(io);
 
-    return bstr;
+    return outbuf;
 }
 
 /*
@@ -3845,9 +3833,9 @@
 	}
     }
 
-    CFMutableDataRef data = rb_bytestring_wrapped_data(outbuf);
-    UInt8 *buf = CFDataGetMutableBytePtr(data);
-    const long length = CFDataGetLength(data);
+    outbuf = rb_str_bstr(outbuf);
+    uint8_t *bytes = bstr_bytes(outbuf);
+    const long length = bstr_length(outbuf);
 
     VALUE ary = rb_ary_new();
 
@@ -3856,9 +3844,9 @@
 
 	long pos = 0;
 	void *ptr;
-	while ((ptr = memchr(&buf[pos], byte, length - pos)) != NULL) {
-	    const long s =  (long)ptr - (long)&buf[pos] + 1;
-	    rb_ary_push(ary, rb_bytestring_new_with_data(&buf[pos], s));
+	while ((ptr = memchr(&bytes[pos], byte, length - pos)) != NULL) {
+	    const long s = (long)ptr - (long)&bytes[pos] + 1;
+	    rb_ary_push(ary, bstr_new_with_data(&bytes[pos], s));
 	    pos += s; 
 	}
     }

Modified: MacRuby/branches/icu/marshal.c
===================================================================
--- MacRuby/branches/icu/marshal.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/marshal.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -14,6 +14,7 @@
 #include "ruby/st.h"
 #include "ruby/util.h"
 #include "ruby/encoding.h"
+#include "encoding.h"
 #include "id.h"
 
 #include <math.h>
@@ -980,14 +981,14 @@
 type_error:
 	    rb_raise(rb_eTypeError, "instance of IO needed");
 	}
-	GC_WB(&arg->str, rb_bytestring_new());
+	GC_WB(&arg->str, bstr_new());
 	GC_WB(&arg->dest, port);
 	if (rb_obj_respond_to(port, s_binmode, Qtrue)) {
 	    rb_funcall2(port, s_binmode, 0, 0);
 	}
     }
     else {
-	port = rb_bytestring_new();
+	port = bstr_new();
 	GC_WB(&arg->str, port);
     }
 
@@ -1119,9 +1120,9 @@
     }
     if (TYPE(arg->src) == T_STRING) {
 	if (RSTRING_LEN(arg->src) - arg->offset >= len) {
-	    str = rb_bytestring_new();
-	    rb_bytestring_resize(str, len + 1);
-	    UInt8 *data = rb_bytestring_byte_pointer(str);
+	    str = bstr_new();
+	    bstr_resize(str, len + 1);
+	    uint8_t *data = bstr_bytes(str);
 	    memcpy(data, (UInt8 *)RSTRING_PTR(arg->src) + arg->offset, len);
 	    data[len] = '\0';
 	    arg->offset += len;

Modified: MacRuby/branches/icu/pack.c
===================================================================
--- MacRuby/branches/icu/pack.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/pack.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -9,13 +9,15 @@
 
 **********************************************************************/
 
-#include "ruby/ruby.h"
-#include "ruby/node.h"
-#include "vm.h"
 #include <sys/types.h>
 #include <ctype.h>
 #include <errno.h>
 
+#include "ruby/ruby.h"
+#include "ruby/node.h"
+#include "vm.h"
+#include "encoding.h"
+
 #define SIZE16 2
 #define SIZE32 4
 
@@ -452,26 +454,12 @@
     p = RSTRING_PTR(fmt);
     pend = p + RSTRING_LEN(fmt);
 
-    VALUE bres = rb_bytestring_new();
-    CFMutableDataRef data = rb_bytestring_wrapped_data(bres);
+    CFMutableDataRef data = CFDataCreateMutable(NULL, 0);
+    CFMakeCollectable(data);
 
     items = RARRAY_LEN(ary);
     idx = 0;
 
-    // Taint the ByteString accordingly.
-    if (OBJ_TAINTED(fmt)) {
-	OBJ_TAINT(bres);
-    }
-    else {
-	long i;
-	for (i = 0; i < items; i++) {
-	    if (OBJ_TAINTED(RARRAY_AT(ary, i))) {
-		OBJ_TAINT(bres);
-		break;
-	    }
-	}
-    }
-
 #define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
 #define THISFROM (items > 0 ? RARRAY_AT(ary, idx) : TOO_FEW)
 #define NEXTFROM (items-- > 0 ? RARRAY_AT(ary, idx++) : TOO_FEW)
@@ -1029,10 +1017,21 @@
 	}
     }
 
-    if (associates) {
-	rb_str_associate(bres, associates);
-    }
+    VALUE bres = bstr_new_with_data(CFDataGetBytePtr(data),
+	    CFDataGetLength(data));
 
+    // Taint the ByteString accordingly.
+    if (OBJ_TAINTED(fmt)) {
+	OBJ_TAINT(bres);
+    }
+    else {
+	for (long i = 0; i < items; i++) {
+	    if (OBJ_TAINTED(RARRAY_AT(ary, i))) {
+		OBJ_TAINT(bres);
+		break;
+	    }
+	}
+    }
     return bres;
 }
 
@@ -1453,10 +1452,10 @@
 		if (p[-1] == '*' || len > (send - s) * 8)
 		    len = (send - s) * 8;
 		bits = 0;
-		bitstr = rb_bytestring_new();
-		rb_bytestring_resize(bitstr, len);
+		bitstr = bstr_new();
+		bstr_resize(bitstr, len);
 		UNPACK_PUSH(bitstr);
-		t = (char *)rb_bytestring_byte_pointer(bitstr);
+		t = (char *)bstr_bytes(bitstr);
 		for (i=0; i<len; i++) {
 		    if (i & 7) {
 			bits >>= 1;
@@ -1479,10 +1478,10 @@
 		if (p[-1] == '*' || len > (send - s) * 8)
 		    len = (send - s) * 8;
 		bits = 0;
-		bitstr = rb_bytestring_new();
-		rb_bytestring_resize(bitstr, len);
+		bitstr = bstr_new();
+		bstr_resize(bitstr, len);
 		UNPACK_PUSH(bitstr);
-		t = (char *)rb_bytestring_byte_pointer(bitstr);
+		t = (char *)bstr_bytes(bitstr);
 		for (i=0; i<len; i++) {
 		    if (i & 7) {
 			bits <<= 1;
@@ -1505,10 +1504,10 @@
 		if (p[-1] == '*' || len > (send - s) * 2)
 		    len = (send - s) * 2;
 		bits = 0;
-		bitstr = rb_bytestring_new();
-		rb_bytestring_resize(bitstr, len);
+		bitstr = bstr_new();
+		bstr_resize(bitstr, len);
 		UNPACK_PUSH(bitstr);
-		t = (char *)rb_bytestring_byte_pointer(bitstr);
+		t = (char *)bstr_bytes(bitstr);
 		for (i=0; i<len; i++) {
 		    if (i & 1) {
 			bits >>= 4;
@@ -1531,10 +1530,10 @@
 		if (p[-1] == '*' || len > (send - s) * 2)
 		    len = (send - s) * 2;
 		bits = 0;
-		bitstr = rb_bytestring_new();
-		rb_bytestring_resize(bitstr, len);
+		bitstr = bstr_new();
+		bstr_resize(bitstr, len);
 		UNPACK_PUSH(bitstr);
-		t = (char *)rb_bytestring_byte_pointer(bitstr);
+		t = (char *)bstr_bytes(bitstr);
 		for (i=0; i<len; i++) {
 		    if (i & 1) {
 			bits <<= 4;
@@ -1789,11 +1788,11 @@
 
 	  case 'u':
 	    {
-		VALUE buf = rb_bytestring_new();
-		rb_bytestring_resize(buf, (send - s)*3/4);
-		char *ptr = (char *)rb_bytestring_byte_pointer(buf);
+		VALUE buf = bstr_new();
+		bstr_resize(buf, (send - s)*3/4);
+		char *ptr = (char *)bstr_bytes(buf);
 		long total = 0;
-		const long buflen = rb_bytestring_length(buf);
+		const long buflen = bstr_length(buf);
 
 		while (s < send && *s > ' ' && *s < 'a') {
 		    long a,b,c,d;
@@ -1852,16 +1851,16 @@
 		    }
 		}
 
-		rb_bytestring_resize(buf, total);
+		bstr_resize(buf, total);
 		UNPACK_PUSH(buf);
 	    }
 	    break;
 
 	  case 'm':
 	    {
-		VALUE buf = rb_bytestring_new();
-		rb_bytestring_resize(buf, (send - s)*3/4);
-		char *ptr = (char *)rb_bytestring_byte_pointer(buf);
+		VALUE buf = bstr_new();
+		bstr_resize(buf, (send - s)*3/4);
+		char *ptr = (char *)bstr_bytes(buf);
 		char *ptr_beg = ptr;
 		int a = -1,b = -1,c = 0,d;
 		static int first = 1;
@@ -1905,16 +1904,16 @@
 			*ptr++ = b << 4 | c >> 2;
 		    }
 		}
-		rb_bytestring_resize(buf, ptr - ptr_beg);
+		bstr_resize(buf, ptr - ptr_beg);
 		UNPACK_PUSH(buf);
 	    }
 	    break;
 
 	  case 'M':
 	    {
-		VALUE buf = rb_bytestring_new();
-		rb_bytestring_resize(buf, send - s);
-		char *ptr = (char *)rb_bytestring_byte_pointer(buf);
+		VALUE buf = bstr_new();
+		bstr_resize(buf, send - s);
+		char *ptr = (char *)bstr_bytes(buf);
 		char *ptr_beg = ptr;
 		int c1, c2;
 
@@ -1935,7 +1934,7 @@
 		    }
 		    s++;
 		}
-		rb_bytestring_resize(buf, ptr - ptr_beg);
+		bstr_resize(buf, ptr - ptr_beg);
 		UNPACK_PUSH(buf);
 	    }
 	    break;

Modified: MacRuby/branches/icu/random.c
===================================================================
--- MacRuby/branches/icu/random.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/random.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -11,6 +11,7 @@
 #include "ruby/node.h"
 #include "vm.h"
 #include "id.h"
+#include "encoding.h"
 
 #include <unistd.h>
 #include <time.h>
@@ -563,7 +564,7 @@
 	ptr[i] = (char)r;
 	r >>= CHAR_BIT;
     }
-    VALUE bytes = rb_bytestring_new_with_data(ptr, n);
+    VALUE bytes = bstr_new_with_data(ptr, n);
     free(ptr);
     return bytes;
 }

Modified: MacRuby/branches/icu/ruby.c
===================================================================
--- MacRuby/branches/icu/ruby.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/ruby.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -11,30 +11,19 @@
 
 **********************************************************************/
 
-#include "ruby/ruby.h"
-#include "ruby/node.h"
-#include "ruby/encoding.h"
-#include "dln.h"
 #include <stdio.h>
 #include <sys/types.h>
 #include <ctype.h>
-#include "vm.h"
-
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
-#if defined(HAVE_FCNTL_H)
 #include <fcntl.h>
-#elif defined(HAVE_SYS_FCNTL_H)
-#include <sys/fcntl.h>
-#endif
-#ifdef HAVE_SYS_PARAM_H
-# include <sys/param.h>
-#endif
-#ifndef MAXPATHLEN
-# define MAXPATHLEN 1024
-#endif
+#include <sys/param.h>
 
+#include "ruby/ruby.h"
+#include "ruby/node.h"
+#include "ruby/encoding.h"
+#include "dln.h"
+#include "vm.h"
+#include "encoding.h"
 #include "ruby/util.h"
 
 #ifndef HAVE_STDLIB_H
@@ -1099,8 +1088,9 @@
 		    char *path;
 		    char *pend;
 
-		    p = (char *)rb_bytestring_byte_pointer(line);
-		    pend = p + rb_bytestring_length(line);
+		    line = rb_str_bstr(line);
+		    p = (char *)bstr_bytes(line);
+		    pend = p + bstr_length(line);
 
 		    if (pend[-1] == '\n') {
 			pend--;	/* chomp line */
@@ -1134,8 +1124,8 @@
 	      start_read:
 		p += 4;
 
-		char *linebuf = (char *)rb_bytestring_byte_pointer(line);
-		long linebuflen = rb_bytestring_length(line);
+		char *linebuf = (char *)bstr_bytes(line);
+		const long linebuflen = bstr_length(line);
 
 		linebuf[linebuflen - 1] = '\0';
 		if (linebuf[linebuflen - 2] == '\r') {

Modified: MacRuby/branches/icu/string.c
===================================================================
--- MacRuby/branches/icu/string.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/string.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -765,6 +765,18 @@
 }
 
 static void
+str_resize_bytes(rb_str_t *self, long new_capacity)
+{
+    if (self->capacity_in_bytes < new_capacity) {
+	char *bytes = xrealloc(self->data.bytes, new_capacity);
+	if (bytes != self->data.bytes) {
+	    GC_WB(&self->data.bytes, bytes);
+	}
+	self->capacity_in_bytes = new_capacity;
+    }
+}
+
+static void
 str_concat_string(rb_str_t *self, rb_str_t *str)
 {
     if (str->length_in_bytes == 0) {
@@ -783,12 +795,7 @@
     // (if both are ASCII-only, the concatenation is ASCII-only,
     //  though I'm not sure all the tests required are worth doing)
     str_unset_facultative_flags(self);
-    if (self->capacity_in_bytes < new_length_in_bytes) {
-	uint8_t *bytes = xmalloc(new_length_in_bytes);
-	memcpy(bytes, self->data.bytes, self->length_in_bytes);
-	GC_WB(&self->data.bytes, bytes);
-	self->capacity_in_bytes = new_length_in_bytes;
-    }
+    str_resize_bytes(self, new_length_in_bytes);
     memcpy(self->data.bytes + self->length_in_bytes, str->data.bytes,
 	    str->length_in_bytes);
     self->length_in_bytes = new_length_in_bytes;
@@ -1364,6 +1371,87 @@
     rb_define_variable("$-F", &rb_fs);
 }
 
+bool
+rb_objc_str_is_pure(VALUE str)
+{
+    VALUE k = *(VALUE *)str;
+    while (RCLASS_SINGLETON(k)) {
+        k = RCLASS_SUPER(k);
+    }
+    if (k == rb_cRubyString) {
+        return true;
+    }
+    while (k != 0) {
+        if (k == rb_cRubyString) {
+            return false;
+        }
+        k = RCLASS_SUPER(k);
+    }
+    return true;
+}
+
+void
+rb_objc_install_string_primitives(Class klass)
+{
+    // TODO
+}
+
+// ByteString emulation.
+
+VALUE
+rb_str_bstr(VALUE str)
+{
+    if (IS_RSTR(str)) {
+	str_make_data_binary(RSTR(str));
+	return str;
+    }
+    abort(); // TODO
+}
+
+uint8_t *
+bstr_bytes(VALUE str)
+{
+    assert(IS_RSTR(str));
+    return (uint8_t *)RSTR(str)->data.bytes;
+}
+
+VALUE
+bstr_new_with_data(const uint8_t *bytes, long len)
+{
+    rb_str_t *str = str_alloc();
+    str_replace_with_bytes(str, (char *)bytes, len, ENCODING_BINARY);
+    return (VALUE)str;
+}
+
+VALUE
+bstr_new(void)
+{
+    return bstr_new_with_data(NULL, 0);
+}
+
+long
+bstr_length(VALUE str)
+{
+    assert(IS_RSTR(str));
+    return RSTR(str)->length_in_bytes;
+}
+
+void
+bstr_resize(VALUE str, long capa)
+{
+    assert(IS_RSTR(str));
+    str_resize_bytes(RSTR(str), capa);
+}
+
+void
+bstr_set_length(VALUE str, long len)
+{
+    assert(IS_RSTR(str));
+    assert(len < RSTR(str)->capacity_in_bytes);
+    assert(len < RSTR(str)->length_in_bytes);
+    RSTR(str)->length_in_bytes = len;
+}
+
 // MRI C-API compatibility.
 
 VALUE
@@ -1636,7 +1724,13 @@
 VALUE
 rb_str_resize(VALUE str, long len)
 {
-    abort(); // TODO
+    if (IS_RSTR(str)) {
+	str_resize_bytes(RSTR(str), len);
+    }
+    else {
+	abort(); // TODO
+    }
+    return str;
 }
 
 VALUE
@@ -1665,3 +1759,4 @@
     CFRelease((CFTypeRef)data);
     return code;
 }
+

Modified: MacRuby/branches/icu/time.c
===================================================================
--- MacRuby/branches/icu/time.c	2010-02-17 01:47:04 UTC (rev 3562)
+++ MacRuby/branches/icu/time.c	2010-02-17 01:55:10 UTC (rev 3563)
@@ -9,18 +9,16 @@
 
 **********************************************************************/
 
-#include "ruby/ruby.h"
 #include <sys/types.h>
 #include <time.h>
 #include <errno.h>
-#include "ruby/encoding.h"
-
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
-
 #include <math.h>
 
+#include "ruby/ruby.h"
+#include "ruby/encoding.h"
+#include "encoding.h"
+
 VALUE rb_cTime;
 static VALUE time_utc_offset _((VALUE, SEL));
 
@@ -2171,7 +2169,7 @@
 	s = RSHIFT(s, 8);
     }
 
-    str = rb_bytestring_new_with_data(buf, 8);
+    str = bstr_new_with_data(buf, 8);
     rb_copy_generic_ivar(str, time);
     if (nsec) {
         /*
@@ -2190,7 +2188,7 @@
         buf[0] |= (nsec % 10) << 4;
         if (buf[1] == 0)
             len = 1;
-        rb_ivar_set(str, id_submicro, rb_bytestring_new_with_data(buf, len));
+        rb_ivar_set(str, id_submicro, bstr_new_with_data(buf, len));
     }
     return str;
 }
@@ -2238,9 +2236,10 @@
     rb_copy_generic_ivar(time, str);
 
     StringValue(str);
+    str = rb_str_bstr(str);
 
-    unsigned char *buf = (unsigned char *)rb_bytestring_byte_pointer(str);
-    const size_t buflen = rb_bytestring_length(str); 
+    uint8_t *buf = bstr_bytes(str);
+    const long buflen = bstr_length(str); 
     if (buflen != 8 && buflen != 9) {
 	rb_raise(rb_eTypeError, "marshaled time format differ");
     }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100216/3fa8330e/attachment-0001.html>


More information about the macruby-changes mailing list