Permalink
Browse files

String#insert should be destructive

  • Loading branch information...
ksss committed Jul 14, 2016
1 parent 72b28f1 commit d83d9cd164d934c57ab19b6022d29f150c12b3a2
Showing with 11 additions and 8 deletions.
  1. +7 −8 mrbgems/mruby-string-ext/mrblib/string.rb
  2. +4 −0 mrbgems/mruby-string-ext/test/string.rb
@@ -254,14 +254,13 @@ def slice!(arg1, arg2=nil)
# "abcd".insert(-1, 'X') #=> "abcdX"
#
def insert(idx, str)
pos = idx.to_i
pos += self.size + 1 if pos < 0
raise IndexError, "index #{idx.to_i} out of string" if pos < 0 || pos > self.size
return self + str if pos == -1
return str + self if pos == 0
return self[0..pos - 1] + str + self[pos..-1]
if idx == -1
return self << str
elsif idx < 0
idx += 1
end
self[idx, 0] = str
self
end
##
@@ -406,6 +406,10 @@ def o.to_str
assert_equal "abcdX", "abcd".insert(-1, 'X')
assert_raise(IndexError) { "abcd".insert(5, 'X') }
assert_raise(IndexError) { "abcd".insert(-6, 'X') }
a = "abcd"
a.insert(0, 'X')
assert_equal "Xabcd", a
end
assert('String#prepend') do

0 comments on commit d83d9cd

Please sign in to comment.