Project

General

Profile

Feature #14417 » string_sub_with_symbol_key.patch

osyo (manga osyo), 01/29/2018 09:34 AM

View differences:

string.c
repl = rb_obj_as_string(rb_yield(match0));
}
else {
repl = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
VALUE key = rb_str_subseq(str, beg0, end0 - beg0);
repl = rb_hash_aref(hash, key);
if (NIL_P(repl)) {
repl = rb_hash_aref(hash, rb_to_symbol(key));
}
repl = rb_obj_as_string(repl);
}
str_mod_check(str, p, len);
......
val = rb_obj_as_string(rb_yield(match0));
}
else {
val = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
VALUE key = rb_str_subseq(str, beg0, end0 - beg0);
val = rb_hash_aref(hash, key);
if (NIL_P(val)) {
val = rb_hash_aref(hash, rb_to_symbol(key));
}
val = rb_obj_as_string(val);
}
str_mod_check(str, sp, slen);
test/-ext-/string/test_gsub.rb
require 'test/unit'
require "-test-/string"
class Test_StringGsub < Test::Unit::TestCase
def test_gsub_with_hash
hash = {'b'=>'B', 'c'=>'C'}
assert_equal("abcabc".gsub(/[bc]/, hash), "aBCaBC")
assert_equal("cbacba".gsub(/[bc]/, hash), "CBaCBa")
assert_equal("abcabc".gsub(/[ac]/, hash), "bCbC")
end
def test_gsub_with_symbol_key_hash
hash = {b: 'B', c: 'C'}
assert_equal("abcabc".gsub(/[bc]/, hash), "aBCaBC")
assert_equal("cbacba".gsub(/[bc]/, hash), "CBaCBa")
assert_equal("abcabc".gsub(/[ac]/, hash), "bCbC")
end
end
test/-ext-/string/test_sub.rb
require 'test/unit'
require "-test-/string"
class Test_StringSub < Test::Unit::TestCase
def test_sub_with_hash
hash = {'b'=>'B', 'c'=>'C'}
assert_equal("abcabc".sub(/[bc]/, hash), "aBcabc")
assert_equal("cbacba".sub(/[bc]/, hash), "Cbacba")
assert_equal("abcabc".sub(/[ac]/, hash), "bcabc")
end
def test_sub_with_symbol_key_hash
hash = {b: 'B', c: 'C'}
assert_equal("abcabc".sub(/[bc]/, hash), "aBcabc")
assert_equal("cbacba".sub(/[bc]/, hash), "Cbacba")
assert_equal("abcabc".sub(/[ac]/, hash), "bcabc")
end
end
    (1-1/1)