Permalink
Browse files

String() and Array() will respectively convert NSStrings and NSArrays…

… to RubyStrings and RubyArrays
  • Loading branch information...
1 parent a6b0db5 commit f03ba947afa2a665f2b71e59ef007f88c73e7d7c Laurent Sansonetti committed May 27, 2011
Showing with 50 additions and 39 deletions.
  1. +20 −0 array.c
  2. +1 −0 array.h
  3. +1 −0 encoding.h
  4. +5 −39 object.c
  5. +23 −0 string.c
View
@@ -3665,6 +3665,26 @@ imp_rary_objectAtIndex(void *rcv, SEL sel, CFIndex idx)
return RB2OC(rary_elt((VALUE)rcv, idx));
}
+/*
+ * call-seq:
+ * Array(arg) => array
+ *
+ * Returns <i>arg</i> as an <code>Array</code>. First tries to call
+ * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
+ *
+ * Array(1..5) #=> [1, 2, 3, 4, 5]
+ */
+
+VALUE
+rb_f_array(VALUE obj, SEL sel, VALUE arg)
+{
+ VALUE ary = rb_Array(arg);
+ if (!IS_RARY(ary)) {
+ ary = rary_copy(ary, rb_cRubyArray);
+ }
+ return ary;
+}
+
static void
imp_rary_insertObjectAtIndex(void *rcv, SEL sel, void *obj, CFIndex idx)
{
View
@@ -185,6 +185,7 @@ VALUE rary_and(VALUE ary1, SEL sel, VALUE ary2);
VALUE rary_or(VALUE ary1, SEL sel, VALUE ary2);
unsigned long rb_ary_hash(VALUE ary);
+VALUE rb_f_array(VALUE, SEL, VALUE);
#if defined(__cplusplus)
} // extern "C"
View
@@ -376,6 +376,7 @@ str_check_ascii_compatible(VALUE str)
}
}
+VALUE rb_f_string(VALUE, SEL, VALUE);
VALUE rstr_new_path(const char *path);
const char *nsstr_cstr(VALUE str);
View
@@ -2890,30 +2890,6 @@ rb_str2cstr(VALUE str, long *len)
}
VALUE
-rb_String(VALUE val)
-{
- return rb_convert_type(val, T_STRING, "String", "to_s");
-}
-
-/*
- * call-seq:
- * String(arg) => string
- *
- * Converts <i>arg</i> to a <code>String</code> by calling its
- * <code>to_s</code> method.
- *
- * String(self) #=> "main"
- * String(self.class) #=> "Object"
- * String(123456) #=> "123456"
- */
-
-static VALUE
-rb_f_string(VALUE obj, SEL sel, VALUE arg)
-{
- return rb_String(arg);
-}
-
-VALUE
rb_Array(VALUE val)
{
VALUE tmp = rb_check_array_type(val);
@@ -2927,20 +2903,10 @@ rb_Array(VALUE val)
return tmp;
}
-/*
- * call-seq:
- * Array(arg) => array
- *
- * Returns <i>arg</i> as an <code>Array</code>. First tries to call
- * <i>arg</i><code>.to_ary</code>, then <i>arg</i><code>.to_a</code>.
- *
- * Array(1..5) #=> [1, 2, 3, 4, 5]
- */
-
-static VALUE
-rb_f_array(VALUE obj, SEL sel, VALUE arg)
+VALUE
+rb_String(VALUE val)
{
- return rb_Array(arg);
+ return rb_convert_type(val, T_STRING, "String", "to_s");
}
static VALUE
@@ -3159,8 +3125,8 @@ Init_Object(void)
rb_objc_define_module_function(rb_mKernel, "Integer", rb_f_integer, -1);
rb_objc_define_module_function(rb_mKernel, "Float", rb_f_float, 1);
- rb_objc_define_module_function(rb_mKernel, "String", rb_f_string, 1);
- rb_objc_define_module_function(rb_mKernel, "Array", rb_f_array, 1);
+ rb_objc_define_module_function(rb_mKernel, "String", rb_f_string, 1); /* in string.c */
+ rb_objc_define_module_function(rb_mKernel, "Array", rb_f_array, 1); /* in array.c */
rb_const_set(rb_cObject, rb_intern("NSNull"), (VALUE)objc_getClass("NSNull"));
View
@@ -5979,6 +5979,29 @@ nsdata_to_str(VALUE data, SEL sel)
CFDataGetLength(dataref));
}
+
+/*
+ * call-seq:
+ * String(arg) => string
+ *
+ * Converts <i>arg</i> to a <code>String</code> by calling its
+ * <code>to_s</code> method.
+ *
+ * String(self) #=> "main"
+ * String(self.class) #=> "Object"
+ * String(123456) #=> "123456"
+ */
+
+VALUE
+rb_f_string(VALUE obj, SEL sel, VALUE arg)
+{
+ VALUE str = rb_String(arg);
+ if (!IS_RSTR(str)) {
+ str = (VALUE)str_new_from_cfstring((CFStringRef)str);
+ }
+ return str;
+}
+
void Init_NSString(void);
void

0 comments on commit f03ba94

Please sign in to comment.