[macruby-changes] [3677] MacRuby/branches/icu/string.c

source_changes at macosforge.org source_changes at macosforge.org
Tue Mar 2 19:36:55 PST 2010


Revision: 3677
          http://trac.macosforge.org/projects/ruby/changeset/3677
Author:   lsansonetti at apple.com
Date:     2010-03-02 19:36:52 -0800 (Tue, 02 Mar 2010)
Log Message:
-----------
added #insert, cleanup a few things

Modified Paths:
--------------
    MacRuby/branches/icu/string.c

Modified: MacRuby/branches/icu/string.c
===================================================================
--- MacRuby/branches/icu/string.c	2010-03-03 03:22:13 UTC (rev 3676)
+++ MacRuby/branches/icu/string.c	2010-03-03 03:36:52 UTC (rev 3677)
@@ -1141,6 +1141,8 @@
 static void
 rstr_splice(VALUE self, long beg, long len, VALUE str)
 {
+    rb_str_t *strstr = str_need_string(str);
+
     if (len < 0) {
 	rb_raise(rb_eIndexError, "negative length %ld", len);
     }
@@ -1162,13 +1164,23 @@
 
     rstr_modify(self);
 
-    str_splice(RSTR(self), beg, len, str_need_string(str), false);
+    str_splice(RSTR(self), beg, len, strstr, false);
 
-    if (OBJ_TAINTED(str)) {
+    if (OBJ_TAINTED(strstr)) {
 	OBJ_TAINT(self);
     }
 }
 
+static void
+rstr_append(VALUE str, VALUE substr)
+{
+    str_concat_string(RSTR(str), str_need_string(substr));
+
+    if (OBJ_TAINTED(substr)) {
+	OBJ_TAINT(str);
+    }
+}
+
 static VALUE
 str_trim(VALUE str)
 {
@@ -1756,6 +1768,39 @@
 
 /*
  *  call-seq:
+ *     str.insert(index, other_str)   => str
+ *  
+ *  Inserts <i>other_str</i> before the character at the given
+ *  <i>index</i>, modifying <i>str</i>. Negative indices count from the
+ *  end of the string, and insert <em>after</em> the given character.
+ *  The intent is insert <i>aString</i> so that it starts at the given
+ *  <i>index</i>.
+ *     
+ *     "abcd".insert(0, 'X')    #=> "Xabcd"
+ *     "abcd".insert(3, 'X')    #=> "abcXd"
+ *     "abcd".insert(4, 'X')    #=> "abcdX"
+ *     "abcd".insert(-3, 'X')   #=> "abXcd"
+ *     "abcd".insert(-1, 'X')   #=> "abcdX"
+ */
+
+static VALUE
+rstr_insert(VALUE str, SEL sel, VALUE idx, VALUE substr)
+{
+    long pos = NUM2LONG(idx);
+    if (pos == -1) {
+	rstr_append(str, substr);
+    }
+    else {
+	if (pos < 0) {
+	    pos++;
+	}
+	rstr_splice(str, pos, 0, substr);
+    }
+    return str;
+}
+
+/*
+ *  call-seq:
  *     str.index(substring [, offset])   => fixnum or nil
  *     str.index(fixnum [, offset])      => fixnum or nil
  *     str.index(regexp [, offset])      => fixnum or nil
@@ -1901,7 +1946,7 @@
 {
     rb_str_t *newstr = str_dup(RSTR(self));
     str_concat_string(newstr, str_need_string(other));
-    if (OBJ_TAINTED(self) || OBJ_TAINTED(other)) {
+    if (OBJ_TAINTED(self)) {
 	OBJ_TAINT(newstr);
     }
     return (VALUE)newstr;
@@ -1995,7 +2040,7 @@
 	    break;
 	    
 	default:
-	    str_concat_string(RSTR(self), str_need_string(other));
+	    rstr_append(self, other);
 	    return self;
     }
 
@@ -4155,6 +4200,7 @@
     rb_objc_define_method(rb_cRubyString, "[]", rstr_aref, -1);
     rb_objc_define_method(rb_cRubyString, "[]=", rstr_aset, -1);
     rb_objc_define_method(rb_cRubyString, "slice", rstr_aref, -1);
+    rb_objc_define_method(rb_cRubyString, "insert", rstr_insert, 2);
     rb_objc_define_method(rb_cRubyString, "index", rstr_index, -1);
     rb_objc_define_method(rb_cRubyString, "rindex", rstr_rindex, -1);
     rb_objc_define_method(rb_cRubyString, "+", rstr_plus, 1);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20100302/f393613b/attachment.html>


More information about the macruby-changes mailing list