Skip to content

Commit

Permalink
Merge pull request #43 from ferrous26/master
Browse files Browse the repository at this point in the history
A couple of fixes for cfstr_to_str()
  • Loading branch information
alloy committed Jan 7, 2013
2 parents 161ed66 + 4ae1f11 commit 954b782
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions ext/xcodeproj/xcodeproj_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,18 @@ static VALUE
cfstr_to_str(const void *cfstr) {
CFDataRef data = CFStringCreateExternalRepresentation(NULL, cfstr, kCFStringEncodingUTF8, 0);
assert(data != NULL);
long len = (long)CFDataGetLength(data);
char *buf = (char *)malloc(len+1);
assert(buf != NULL);
CFDataGetBytes(data, CFRangeMake(0, len), buf);
long len = (long)CFDataGetLength(data);
char *buf = (char *)CFDataGetBytePtr(data);

register VALUE str = rb_str_new(buf, len);
free(buf);

// force UTF-8 encoding in Ruby 1.9+
ID forceEncodingId = rb_intern("force_encoding");
if (rb_respond_to(str, forceEncodingId)) {
rb_funcall(str, forceEncodingId, 1, rb_str_new("UTF-8", 5));
}

CFRelease(data);
return str;
}

Expand Down

0 comments on commit 954b782

Please sign in to comment.