Commit 4a8b88f7 authored by KOBAYASHI Shuji's avatar KOBAYASHI Shuji

Add type check (conversion) in `String#[]=`

Before this patch:

  'a'[0] = 1        #=> 1
  'a'[:a] = '1'     #=> ArgumentError
  'a'[:a, 0] = '1'  #=> ArgumentError
  'a'[0, :a] = '1'  #=> ArgumentError
  'a'[0, 1] = 1     #=> 1

After this patch / Ruby:

  'a'[0] = 1        #=> TypeError
  'a'[:a] = '1'     #=> TypeError
  'a'[:a, 0] = '1'  #=> TypeError
  'a'[0, :a] = '1'  #=> TypeError
  'a'[0, 1] = 1     #=> TypeError
parent 05f65d2d
...@@ -197,12 +197,12 @@ class String ...@@ -197,12 +197,12 @@ class String
def []=(*args) def []=(*args)
anum = args.size anum = args.size
if anum == 2 if anum == 2
pos, value = args pos, value = args[0], args[1].__to_str
case pos case pos
when String when String
posnum = self.index(pos) posnum = self.index(pos)
if posnum if posnum
b = self[0, posnum.to_i] b = self[0, posnum]
a = self[(posnum + pos.length)..-1] a = self[(posnum + pos.length)..-1]
self.replace([b, value, a].join('')) self.replace([b, value, a].join(''))
else else
...@@ -217,17 +217,18 @@ class String ...@@ -217,17 +217,18 @@ class String
end end
return self[head, tail-head]=value return self[head, tail-head]=value
else else
pos = pos.__to_int
pos += self.length if pos < 0 pos += self.length if pos < 0
if pos < 0 || pos > self.length if pos < 0 || pos > self.length
raise IndexError, "index #{args[0]} out of string" raise IndexError, "index #{args[0]} out of string"
end end
b = self[0, pos.to_i] b = self[0, pos]
a = self[pos + 1..-1] a = self[pos + 1..-1]
self.replace([b, value, a].join('')) self.replace([b, value, a].join(''))
end end
return value return value
elsif anum == 3 elsif anum == 3
pos, len, value = args pos, len, value = args[0].__to_int, args[1].__to_int, args[2].__to_str
pos += self.length if pos < 0 pos += self.length if pos < 0
if pos < 0 || pos > self.length if pos < 0 || pos > self.length
raise IndexError, "index #{args[0]} out of string" raise IndexError, "index #{args[0]} out of string"
...@@ -235,7 +236,7 @@ class String ...@@ -235,7 +236,7 @@ class String
if len < 0 if len < 0
raise IndexError, "negative length #{len}" raise IndexError, "negative length #{len}"
end end
b = self[0, pos.to_i] b = self[0, pos]
a = self[pos + len..-1] a = self[pos + len..-1]
self.replace([b, value, a].join('')) self.replace([b, value, a].join(''))
return value return value
......
...@@ -161,6 +161,9 @@ assert('String#[]=') do ...@@ -161,6 +161,9 @@ assert('String#[]=') do
assert_equal 'aXc', e assert_equal 'aXc', e
end end
assert_raise(TypeError) { 'a'[0] = 1 }
assert_raise(TypeError) { 'a'[:a] = '1' }
# length of args is 2 # length of args is 2
a1 = 'abc' a1 = 'abc'
assert_raise(IndexError) do assert_raise(IndexError) do
...@@ -197,6 +200,10 @@ assert('String#[]=') do ...@@ -197,6 +200,10 @@ assert('String#[]=') do
assert_raise(IndexError) do assert_raise(IndexError) do
b3['XX'] = 'Y' b3['XX'] = 'Y'
end end
assert_raise(TypeError) { 'a'[:a, 0] = '1' }
assert_raise(TypeError) { 'a'[0, :a] = '1' }
assert_raise(TypeError) { 'a'[0, 1] = 1 }
end end
assert('String#capitalize', '15.2.10.5.7') do assert('String#capitalize', '15.2.10.5.7') do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment