Permalink
Browse files

introduce String#transform, to apply ICU transformations to a given s…

…tring

git-svn-id: http://svn.macosforge.org/repository/ruby/MacRuby/trunk@245 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 26fb676 commit 558238c0e1411e814e0a281c75d6eb50344156f3 @lrz lrz committed Jun 3, 2008
Showing with 36 additions and 0 deletions.
  1. +36 −0 string.c
View
@@ -8079,6 +8079,37 @@ rb_str_is_ascii_only_p(VALUE str)
#endif
}
+#if WITH_OBJC
+static VALUE
+rb_str_transform_bang(VALUE str, VALUE transform_name)
+{
+ CFRange range;
+
+ rb_str_modify(str);
+ StringValue(transform_name);
+
+ range = CFRangeMake(0, RSTRING_CLEN(str));
+
+ if (!CFStringTransform((CFMutableStringRef)str,
+ &range,
+ (CFStringRef)transform_name,
+ false))
+ rb_raise(rb_eRuntimeError, "cannot apply transformation `%s' to `%s'",
+ RSTRING_CPTR(transform_name), RSTRING_CPTR(str));
+
+ return range.length == kCFNotFound ? Qnil : str;
+}
+
+static VALUE
+rb_str_transform(VALUE str, VALUE transform_name)
+{
+ str = rb_str_dup(str);
+ rb_str_transform_bang(str, transform_name);
+ return str;
+}
+
+#endif
+
/**********************************************************************
* Document-class: Symbol
*
@@ -8630,6 +8661,11 @@ Init_String(void)
rb_define_method(rb_cString, "valid_encoding?", rb_str_valid_encoding_p, 0);
rb_define_method(rb_cString, "ascii_only?", rb_str_is_ascii_only_p, 0);
+#if WITH_OBJC
+ rb_define_method(rb_cString, "transform", rb_str_transform, 1);
+ rb_define_method(rb_cString, "transform!", rb_str_transform_bang, 1);
+#endif
+
id_to_s = rb_intern("to_s");
rb_fs = Qnil;

0 comments on commit 558238c

Please sign in to comment.