Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Include the BOM when converting CFString to String.

  • Loading branch information...
commit b6f3d3f90f8bcffe4522282c0c5ebcc47fd35ac4 1 parent 8d4f987
@alloy alloy authored
Showing with 24 additions and 2 deletions.
  1. +16 −2 ext/xcodeproj/xcodeproj_ext.c
  2. +8 −0 spec/xcodeproj_ext_spec.rb
View
18 ext/xcodeproj/xcodeproj_ext.c
@@ -17,12 +17,26 @@ VALUE Xcodeproj = Qnil;
static VALUE
cfstr_to_str(const void *cfstr) {
- long len = (long)CFStringGetLength(cfstr);
+ long len;
+
+ len = (long)CFStringGetLength(cfstr);
char *buf = (char *)malloc(len+1);
assert(buf != NULL);
- CFStringGetCString(cfstr, buf, len+1, kCFStringEncodingUTF8);
+
+ CFDataRef data = CFStringCreateExternalRepresentation(NULL, cfstr, kCFStringEncodingUTF8, 0);
+ assert(data != NULL);
+ len = (long)CFDataGetLength(data);
+ CFDataGetBytes(data, CFRangeMake(0, len), buf);
+
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));
+ }
+
return str;
}
View
8 spec/xcodeproj_ext_spec.rb
@@ -1,3 +1,5 @@
+# encoding: UTF-8
+
require File.expand_path('../spec_helper', __FILE__)
require 'xcodeproj/xcodeproj_ext'
@@ -55,6 +57,12 @@ def o.to_hash; { 'from' => 'object' }; end
Xcodeproj.read_plist(@plist).should == { '1' => '1', 'symbol' => 'symbol' }
end
+ it "handles unicode characters in paths and strings" do
+ plist = @plist.to_s + 'øµ'
+ Xcodeproj.write_plist({ 'café' => 'før yoµ' }, plist)
+ Xcodeproj.read_plist(plist).should == { 'café' => 'før yoµ' }
+ end
+
it "raises if a plist contains any other object type as value than string, dictionary, and array" do
@plist.open('w') do |f|
f.write <<-EOS
Please sign in to comment.
Something went wrong with that request. Please try again.