Created
May 27, 2009 18:17
-
-
Save patrickt/118795 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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