Merge pull request #6303 from hasumikin/fix/return-value-of-String_aset

mrb_str_aset_m() should return replace instead of str
This commit is contained in:
Yukihiro "Matz" Matsumoto
2024-07-17 09:49:34 +09:00
committed by GitHub
2 changed files with 17 additions and 4 deletions
+2 -1
View File
@@ -1452,6 +1452,7 @@ mrb_str_aset(mrb_state *mrb, mrb_value str, mrb_value indx, mrb_value alen, mrb_
*
* Modify +self+ by replacing the content of +self+.
* The portion of the string affected is determined using the same criteria as +String#[]+.
* The return value of this expression is +replace+.
*/
static mrb_value
mrb_str_aset_m(mrb_state *mrb, mrb_value str)
@@ -1467,7 +1468,7 @@ mrb_str_aset_m(mrb_state *mrb, mrb_value str)
break;
}
mrb_str_aset(mrb, str, indx, alen, replace);
return str;
return replace;
}
/* 15.2.10.5.8 */
+15 -3
View File
@@ -129,7 +129,7 @@ end
assert('String#[]=') do
# length of args is 1
a = 'abc'
a[0] = 'X'
assert_equal 'X', (a[0] = 'X')
assert_equal 'Xbc', a
b = 'abc'
@@ -152,6 +152,10 @@ assert('String#[]=') do
assert_equal 'aXc', e
end
f = 'abc'
assert_equal 'X', f.[]=(0, 'X')
assert_equal 'Xbc', f
assert_raise(TypeError) { 'a'[0] = 1 }
assert_raise(TypeError) { 'a'[:a] = '1' }
@@ -176,15 +180,19 @@ assert('String#[]=') do
assert_equal 'Xabc', d1
e1 = 'abc'
e1[1, 3] = 'X'
assert_equal 'X', (e1[1, 3] = 'X')
assert_equal 'aX', e1
f1 = 'abc'
assert_equal 'X', f1.[]=(0, 1, 'X')
assert_equal 'Xbc', f1
# args is RegExp
# It will be tested in mrbgems.
# args is String
a3 = 'abc'
a3['bc'] = 'X'
assert_equal 'X', (a3['bc'] = 'X')
assert_equal a3, 'aX'
b3 = 'abc'
@@ -192,6 +200,10 @@ assert('String#[]=') do
b3['XX'] = 'Y'
end
c3 = 'abc'
assert_equal 'X', c3.[]=('bc', 'X')
assert_equal 'aX', c3
assert_raise(TypeError) { 'a'[:a, 0] = '1' }
assert_raise(TypeError) { 'a'[0, :a] = '1' }
assert_raise(TypeError) { 'a'[0, 1] = 1 }