Project

General

Profile

Feature #14417 » string_sub_with_symbol_key.patch

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

View differences:

string.c
4965 4965
                repl = rb_obj_as_string(rb_yield(match0));
4966 4966
            }
4967 4967
            else {
4968
                repl = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
4968
                VALUE key = rb_str_subseq(str, beg0, end0 - beg0);
4969
                repl = rb_hash_aref(hash, key);
4970
                if (NIL_P(repl)) {
4971
                    repl = rb_hash_aref(hash, rb_to_symbol(key));
4972
                }
4969 4973
                repl = rb_obj_as_string(repl);
4970 4974
            }
4971 4975
	    str_mod_check(str, p, len);
......
5140 5144
                val = rb_obj_as_string(rb_yield(match0));
5141 5145
            }
5142 5146
            else {
5143
                val = rb_hash_aref(hash, rb_str_subseq(str, beg0, end0 - beg0));
5147
                VALUE key = rb_str_subseq(str, beg0, end0 - beg0);
5148
                val = rb_hash_aref(hash, key);
5149
                if (NIL_P(val)) {
5150
                    val = rb_hash_aref(hash, rb_to_symbol(key));
5151
                }
5144 5152
                val = rb_obj_as_string(val);
5145 5153
            }
5146 5154
	    str_mod_check(str, sp, slen);
test/-ext-/string/test_gsub.rb
1
require 'test/unit'
2
require "-test-/string"
3

  
4
class Test_StringGsub < Test::Unit::TestCase
5
  def test_gsub_with_hash
6
    hash = {'b'=>'B', 'c'=>'C'}
7
    assert_equal("abcabc".gsub(/[bc]/, hash), "aBCaBC")
8
    assert_equal("cbacba".gsub(/[bc]/, hash), "CBaCBa")
9
    assert_equal("abcabc".gsub(/[ac]/, hash), "bCbC")
10
  end
11

  
12
  def test_gsub_with_symbol_key_hash
13
    hash = {b: 'B', c: 'C'}
14
    assert_equal("abcabc".gsub(/[bc]/, hash), "aBCaBC")
15
    assert_equal("cbacba".gsub(/[bc]/, hash), "CBaCBa")
16
    assert_equal("abcabc".gsub(/[ac]/, hash), "bCbC")
17
  end
18
end
test/-ext-/string/test_sub.rb
1
require 'test/unit'
2
require "-test-/string"
3

  
4
class Test_StringSub < Test::Unit::TestCase
5
  def test_sub_with_hash
6
    hash = {'b'=>'B', 'c'=>'C'}
7
    assert_equal("abcabc".sub(/[bc]/, hash), "aBcabc")
8
    assert_equal("cbacba".sub(/[bc]/, hash), "Cbacba")
9
    assert_equal("abcabc".sub(/[ac]/, hash), "bcabc")
10
  end
11

  
12
  def test_sub_with_symbol_key_hash
13
    hash = {b: 'B', c: 'C'}
14
    assert_equal("abcabc".sub(/[bc]/, hash), "aBcabc")
15
    assert_equal("cbacba".sub(/[bc]/, hash), "Cbacba")
16
    assert_equal("abcabc".sub(/[ac]/, hash), "bcabc")
17
  end
18
end