Permalink
Browse files

migrating bytestrings + misc cleanup

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/branches/icu@3563 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
lrz committed Feb 17, 2010
1 parent a5bdca9 commit cb8cef0ccd8fdef8ab83318648bb527e19f7e1c3
Showing with 241 additions and 168 deletions.
  1. +12 −0 encoding.h
  2. +0 −12 include/ruby/ruby.h
  3. +52 −64 io.c
  4. +6 −5 marshal.c
  5. +46 −47 pack.c
  6. +2 −1 random.c
  7. +12 −22 ruby.c
  8. +102 −7 string.c
  9. +9 −10 time.c
View
@@ -267,6 +267,18 @@ str_set_valid_encoding(rb_str_t *self, bool status)
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
View
@@ -396,18 +396,6 @@ char *rb_string_value_cstr(volatile VALUE*);
#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 {\
View
116 io.c
@@ -16,8 +16,7 @@
#include "vm.h"
#include "objc.h"
#include "id.h"
-
-extern VALUE rb_cByteString; // TODO it does not exist anymore
+#include "encoding.h"
#include <errno.h>
#include <paths.h>
@@ -409,36 +408,24 @@ prep_io(int fd, int mode, VALUE klass)
*/
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 @@ rb_io_read_internal(rb_io_t *io_struct, UInt8 *buffer, long len)
}
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,12 +1113,7 @@ io_read(VALUE io, SEL sel, int argc, VALUE *argv)
rb_io_assert_readable(io_struct);
if (NIL_P(outbuf)) {
- outbuf = rb_bytestring_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.");
+ outbuf = bstr_new();
}
if (NIL_P(len)) {
@@ -1148,15 +1132,15 @@ io_read(VALUE io, SEL sel, int argc, VALUE *argv)
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 @@ rb_io_gets_m(VALUE io, SEL sel, int argc, VALUE *argv)
}
}
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 @@ rb_io_gets_m(VALUE io, SEL sel, int argc, VALUE *argv)
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);
-
- UInt8 *buf = CFDataGetMutableBytePtr(data);
- UInt8 *tmp_buf = alloca(seplen);
+ bstr_resize(bstr, s);
+ 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 @@ rb_io_gets_m(VALUE io, SEL sel, int argc, VALUE *argv)
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 @@ rb_f_backquote(VALUE obj, SEL sel, VALUE str)
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 @@ rb_io_s_readlines(VALUE recv, SEL sel, int argc, VALUE *argv)
}
}
- 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 @@ rb_io_s_readlines(VALUE recv, SEL sel, int argc, VALUE *argv)
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;
}
}
View
@@ -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 @@ marshal_dump(VALUE self, SEL sel, int argc, VALUE *argv)
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 @@ r_bytes0(long len, struct load_arg *arg)
}
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;
Oops, something went wrong.

0 comments on commit cb8cef0

Please sign in to comment.