Skip to content

Instantly share code, notes, and snippets.

@patrickt
Created May 27, 2009 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickt/118795 to your computer and use it in GitHub Desktop.
Save patrickt/118795 to your computer and use it in GitHub Desktop.
static VALUE
str_new(VALUE klass, const char *ptr, long len)
{
VALUE str;
if (len < 0) {
rb_raise(rb_eArgError, "negative string size (or size too big)");
}
if (ptr != NULL) {
str = rb_string_new_with_encoded_data(klass, ptr, len, kCFStringEncodingUTF8);
}
else if (len == 0) {
str = str_alloc(klass);
}
else {
str = rb_bytestring_new();
rb_bytestring_resize(str, len);
}
return str;
}
VALUE rb_string_new_with_encoded_data(VALUE klass, const UInt8 *buf, long size, CFStringEncoding enc)
{
VALUE s = str_alloc(klass);
CFStringRef to_append = CFStringCreateWithBytes(NULL, buf, (CFIndex)size, enc, false);
if (to_append != NULL) {
CFStringAppend((CFMutableStringRef)s, to_append);
return s;
}
// encoding FAIL, let's use a bytestring instead.
return rb_bytestring_new_with_data(buf, size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment