Permalink
Browse files

Handle multibyte strings for String#insert

Fixes #2268
  • Loading branch information...
1 parent bd7a5f1 commit 3c191e31d06b1f1a2082de14dbcdd8ecd306f714 @dbussink dbussink committed Apr 12, 2013
Showing with 9 additions and 6 deletions.
  1. +9 −6 kernel/common/string.rb
View
@@ -384,20 +384,23 @@ def include?(needle)
def insert(index, other)
other = StringValue(other)
index = Rubinius::Type.coerce_to index, Fixnum, :to_int
+ index = length + 1 + index if index < 0
- osize = other.size
+ if index > length or index < 0 then
+ raise IndexError, "index #{index} out of string"
+ end
+
+ osize = other.bytesize
size = @num_bytes + osize
str = self.class.pattern size, "\0"
- m = Rubinius::Mirror.reflect str
- index = @num_bytes + 1 + index if index < 0
- if index > @num_bytes or index < 0 then
- raise IndexError, "index #{index} out of string"
- end
+ self_m = Rubinius::Mirror.reflect self
+ index = self_m.character_to_byte_index index
Rubinius.check_frozen
@hash_value = nil
+ m = Rubinius::Mirror.reflect str
if index == @num_bytes
m.copy_from self, 0, @num_bytes, 0
m.copy_from other, 0, osize, @num_bytes

0 comments on commit 3c191e3

Please sign in to comment.