/* * 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 rb_str_insert(str, idx, str2) VALUE str, idx, str2; { long pos = NUM2LONG(idx); if (pos == -1) { pos = RSTRING(str)->len; } else if (pos < 0) { pos++; } rb_str_splice(str, pos, 0, str2); return str; }