Permalink
Browse files

better RSTRING_{PTR,LEN] for pure NSStrings

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@5223 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 55b12f3 commit 5819dc0b74206a4333525145300fbcca1b4a796a @lrz lrz committed Feb 9, 2011
Showing with 17 additions and 19 deletions.
  1. +12 −0 NSString.m
  2. +3 −0 encoding.h
  3. +2 −19 string.c
View
@@ -427,3 +427,15 @@
rb_objc_define_method(rb_cString, "to_data", rstr_only, 0);
rb_objc_define_method(rb_cString, "pointer", rstr_only, 0);
}
+
+const char *
+nsstr_cstr(VALUE str)
+{
+ return [(NSString *)str UTF8String];
+}
+
+long
+nsstr_clen(VALUE str)
+{
+ return [(NSString *)str lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
+}
View
@@ -378,6 +378,9 @@ str_check_ascii_compatible(VALUE str)
VALUE rstr_new_path(const char *path);
+const char *nsstr_cstr(VALUE str);
+long nsstr_clen(VALUE str);
+
#if defined(__cplusplus)
} // extern "C"
#endif
View
@@ -6333,24 +6333,7 @@ rb_str_cstr(VALUE str)
str_ensure_null_terminator(RSTR(str));
return RSTR(str)->bytes;
}
-
- // CFString code path, hopefully this should not happen very often.
- const char *cptr = (const char *)CFStringGetCStringPtr((CFStringRef)str, 0);
- if (cptr != NULL) {
- return cptr;
- }
-
- const long max = CFStringGetMaximumSizeForEncoding(
- CFStringGetLength((CFStringRef)str),
- kCFStringEncodingUTF8);
- char *cptr2 = (char *)xmalloc(max + 1);
- if (!CFStringGetCString((CFStringRef)str, cptr2, max + 1,
- kCFStringEncodingUTF8)) {
- // Probably an UTF16 string...
- xfree(cptr2);
- return NULL;
- }
- return cptr2;
+ return nsstr_cstr(str);
}
long
@@ -6359,7 +6342,7 @@ rb_str_clen(VALUE str)
if (IS_RSTR(str)) {
return RSTR(str)->length_in_bytes;
}
- return CFStringGetLength((CFStringRef)str);
+ return nsstr_clen(str);
}
char *

0 comments on commit 5819dc0

Please sign in to comment.