Skip to content

Commit

Permalink
Privatize alternative string constructor and make its name more descr…
Browse files Browse the repository at this point in the history
…iptive
  • Loading branch information
ferrous26 committed Apr 29, 2011
1 parent f6908b2 commit d7b52bd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
1 change: 0 additions & 1 deletion include/ruby/intern.h
Expand Up @@ -533,7 +533,6 @@ 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_data2(const uint8_t *bytes, long len);
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);
Expand Down
31 changes: 17 additions & 14 deletions string.c
Expand Up @@ -5939,6 +5939,21 @@ rstr_imp_replaceCharactersInRangeWithString(void *rcv, SEL sel, CFRange range,
str_splice(RSTR(rcv), range.location, range.length, spat);
}

static VALUE
rb_bstr_new_using_encoding(const uint8_t *bytes, long len, int encoding)
{
rb_str_t *str = str_alloc(rb_cRubyString);
str_replace_with_bytes(str, (char *)bytes, len,
rb_encodings[encoding]);
return (VALUE)str;
}

static VALUE
rb_bstr_new_utf8_with_data(const uint8_t *bytes, long len)
{
return rb_bstr_new_using_encoding(bytes, len, ENCODING_UTF8);
}

/*
* call-seq:
* data.to_str => String
Expand All @@ -5951,7 +5966,7 @@ static VALUE
nsdata_to_str(VALUE data, SEL sel)
{
CFDataRef dataref = (CFDataRef)data;
return rb_bstr_new_with_data2(CFDataGetBytePtr(dataref),
return rb_bstr_new_utf8_with_data(CFDataGetBytePtr(dataref),
CFDataGetLength(dataref));
}

Expand Down Expand Up @@ -6164,22 +6179,10 @@ rb_bstr_bytes(VALUE str)
return (uint8_t *)RSTR(str)->bytes;
}

VALUE
rb_bstr_new_with_data2(const uint8_t *bytes, long len)
{
rb_str_t *str = str_alloc(rb_cRubyString);
str_replace_with_bytes(str, (char *)bytes, len,
rb_encodings[ENCODING_UTF8]);
return (VALUE)str;
}

VALUE
rb_bstr_new_with_data(const uint8_t *bytes, long len)
{
rb_str_t *str = str_alloc(rb_cRubyString);
str_replace_with_bytes(str, (char *)bytes, len,
rb_encodings[ENCODING_BINARY]);
return (VALUE)str;
return rb_bstr_new_using_encoding(bytes, len, ENCODING_BINARY);
}

VALUE
Expand Down

0 comments on commit d7b52bd

Please sign in to comment.