Skip to content

Commit

Permalink
expose the bstr APIs
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/branches/icu@3716 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
lrz committed Mar 9, 2010
1 parent b4141cc commit adfb972
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 132 deletions.
13 changes: 0 additions & 13 deletions encoding.h
Expand Up @@ -306,19 +306,6 @@ unsigned long rb_str_hash_uchars(const UChar *chars, long chars_len);
long rb_uchar_strtol(UniChar *chars, long chars_len, long pos,
long *end_offset);

// 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);
void bstr_concat(VALUE str, const uint8_t *bytes, long len);
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
Expand Down
14 changes: 14 additions & 0 deletions include/ruby/intern.h
Expand Up @@ -606,6 +606,20 @@ VALUE rb_str_inspect(VALUE);
#if WITH_OBJC
bool rb_objc_str_is_pure(VALUE);
#endif

// 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 rb_bstr_new(void);
VALUE rb_bstr_new_with_data(const uint8_t *bytes, long len);
uint8_t *rb_bstr_bytes(VALUE str);
void rb_bstr_concat(VALUE str, const uint8_t *bytes, long len);
long rb_bstr_length(VALUE str);
void rb_bstr_set_length(VALUE str, long len);
void rb_bstr_resize(VALUE str, long capa);

/* struct.c */
VALUE rb_struct_new(VALUE, ...);
VALUE rb_struct_define(const char*, ...);
Expand Down
44 changes: 22 additions & 22 deletions io.c
Expand Up @@ -424,8 +424,8 @@ io_write(VALUE io, SEL sel, VALUE data)
data = rb_obj_as_string(data);

data = rb_str_bstr(data);
const uint8_t *buffer = bstr_bytes(data);
const long length = bstr_length(data);
const uint8_t *buffer = rb_bstr_bytes(data);
const long length = rb_bstr_length(data);

if (length == 0) {
return INT2FIX(0);
Expand Down Expand Up @@ -988,19 +988,19 @@ rb_io_read_all(rb_io_t *io_struct, VALUE outbuf)

const long BUFSIZE = 512;
long bytes_read = 0;
const long original_position = bstr_length(outbuf);
const long original_position = rb_bstr_length(outbuf);

while (true) {
bstr_resize(outbuf, original_position + bytes_read + BUFSIZE);
uint8_t *bytes = bstr_bytes(outbuf) + original_position + bytes_read;
rb_bstr_resize(outbuf, original_position + bytes_read + BUFSIZE);
uint8_t *bytes = rb_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;
}
}

bstr_set_length(outbuf, original_position + bytes_read);
rb_bstr_set_length(outbuf, original_position + bytes_read);
return outbuf;
}

Expand Down Expand Up @@ -1119,7 +1119,7 @@ io_read(VALUE io, SEL sel, int argc, VALUE *argv)
rb_io_assert_readable(io_struct);

if (NIL_P(outbuf)) {
outbuf = bstr_new();
outbuf = rb_bstr_new();
}

if (NIL_P(len)) {
Expand All @@ -1139,14 +1139,14 @@ io_read(VALUE io, SEL sel, int argc, VALUE *argv)
}

outbuf = rb_str_bstr(outbuf);
bstr_resize(outbuf, size);
uint8_t *bytes = bstr_bytes(outbuf);
rb_bstr_resize(outbuf, size);
uint8_t *bytes = rb_bstr_bytes(outbuf);

const long data_read = rb_io_read_internal(io_struct, bytes, size);
if (data_read == 0) {
return Qnil;
}
bstr_set_length(outbuf, data_read);
rb_bstr_set_length(outbuf, data_read);

return outbuf;
}
Expand Down Expand Up @@ -1281,10 +1281,10 @@ rb_io_gets_m(VALUE io, SEL sel, int argc, VALUE *argv)
}
const long line_limit = NIL_P(limit) ? -1 : FIX2LONG(limit);

VALUE bstr = bstr_new();
VALUE bstr = rb_bstr_new();
if (line_limit != -1) {
bstr_resize(bstr, line_limit);
uint8_t *bytes = bstr_bytes(bstr);
rb_bstr_resize(bstr, line_limit);
uint8_t *bytes = rb_bstr_bytes(bstr);
rb_io_read_internal(io_struct, bytes, line_limit);
#if 0 // TODO
CFRange r = CFStringFind((CFStringRef)bstr, (CFStringRef)sep, 0);
Expand Down Expand Up @@ -1334,18 +1334,18 @@ rb_io_gets_m(VALUE io, SEL sel, int argc, VALUE *argv)
// Read from IO (slow).
long s = 512;
long data_read = 0;
bstr_resize(bstr, s);
rb_bstr_resize(bstr, s);

uint8_t *buf = bstr_bytes(bstr);
uint8_t *buf = rb_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;
bstr_resize(bstr, s);
buf = bstr_bytes(bstr);
rb_bstr_resize(bstr, s);
buf = rb_bstr_bytes(bstr);
}
memcpy(&buf[data_read], tmp_buf, seplen);
data_read += seplen;
Expand All @@ -1359,7 +1359,7 @@ rb_io_gets_m(VALUE io, SEL sel, int argc, VALUE *argv)
if (data_read == 0) {
return Qnil;
}
bstr_set_length(bstr, data_read);
rb_bstr_set_length(bstr, data_read);
}
}
OBJ_TAINT(bstr);
Expand Down Expand Up @@ -3343,7 +3343,7 @@ rb_f_backquote(VALUE obj, SEL sel, VALUE str)
io_s->pid = -1;
}

VALUE outbuf = bstr_new();
VALUE outbuf = rb_bstr_new();
rb_io_read_all(ExtractIOStruct(io), outbuf);
rb_io_close(io);

Expand Down Expand Up @@ -3840,8 +3840,8 @@ rb_io_s_readlines(VALUE recv, SEL sel, int argc, VALUE *argv)
}

outbuf = rb_str_bstr(outbuf);
uint8_t *bytes = bstr_bytes(outbuf);
const long length = bstr_length(outbuf);
uint8_t *bytes = rb_bstr_bytes(outbuf);
const long length = rb_bstr_length(outbuf);

VALUE ary = rb_ary_new();

Expand All @@ -3852,7 +3852,7 @@ rb_io_s_readlines(VALUE recv, SEL sel, int argc, VALUE *argv)
void *ptr;
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));
rb_ary_push(ary, rb_bstr_new_with_data(&bytes[pos], s));
pos += s;
}
}
Expand Down
14 changes: 7 additions & 7 deletions marshal.c
Expand Up @@ -219,7 +219,7 @@ static void
w_nbyte(const char *s, int n, struct dump_arg *arg)
{
VALUE buf = arg->str;
bstr_concat(buf, (const uint8_t *)s, n);
rb_bstr_concat(buf, (const uint8_t *)s, n);
if (arg->dest && RSTRING_LEN(buf) >= BUFSIZ) {
if (arg->taint) OBJ_TAINT(buf);
rb_io_write(arg->dest, 0, buf);
Expand Down Expand Up @@ -898,7 +898,7 @@ dump(struct dump_call_arg *arg)
w_object(arg->obj, arg->arg, arg->limit);
if (arg->arg->dest) {
rb_io_write(arg->arg->dest, 0, arg->arg->str);
bstr_resize(arg->arg->str, 0);
rb_bstr_resize(arg->arg->str, 0);
}
return 0;
}
Expand Down Expand Up @@ -982,14 +982,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, bstr_new());
GC_WB(&arg->str, rb_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 = bstr_new();
port = rb_bstr_new();
GC_WB(&arg->str, port);
}

Expand Down Expand Up @@ -1121,9 +1121,9 @@ r_bytes0(long len, struct load_arg *arg)
}
if (TYPE(arg->src) == T_STRING) {
if (RSTRING_LEN(arg->src) - arg->offset >= len) {
str = bstr_new();
bstr_resize(str, len + 1);
uint8_t *data = bstr_bytes(str);
str = rb_bstr_new();
rb_bstr_resize(str, len + 1);
uint8_t *data = rb_bstr_bytes(str);
memcpy(data, (UInt8 *)RSTRING_PTR(arg->src) + arg->offset, len);
data[len] = '\0';
arg->offset += len;
Expand Down

0 comments on commit adfb972

Please sign in to comment.