diff options
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | string.c | 12 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 3 |
3 files changed, 15 insertions, 5 deletions
@@ -1,3 +1,8 @@ +Mon May 19 16:29:48 2014 Nobuyoshi Nakada <[email protected]> + + * string.c (rb_pat_search): advance by byte offset but not by char + offset. [ruby-core:62669] [Bug #9849] + Mon May 19 14:06:18 2014 Shota Fukumori <[email protected]> * bin/testrb: Removed. Forgot to remove in r45971. @@ -2569,8 +2569,10 @@ rb_str_casecmp(VALUE str1, VALUE str2) return INT2FIX(-1); } +#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0) + static long -rb_str_index(VALUE str, VALUE sub, long offset) +rb_strseq_index(VALUE str, VALUE sub, long offset, int in_byte) { const char *s, *sptr, *e; long pos, len, slen; @@ -2580,8 +2582,8 @@ rb_str_index(VALUE str, VALUE sub, long offset) enc = rb_enc_check(str, sub); if (is_broken_string(sub)) return -1; - len = single_byte ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */ - slen = str_strlen(sub, enc); /* rb_enc_check */ + len = (in_byte || single_byte) ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */ + slen = in_byte ? RSTRING_LEN(sub) : str_strlen(sub, enc); /* rb_enc_check */ if (offset < 0) { offset += len; if (offset < 0) return -1; @@ -2591,7 +2593,7 @@ rb_str_index(VALUE str, VALUE sub, long offset) s = RSTRING_PTR(str); e = RSTRING_END(str); if (offset) { - offset = str_offset(s, e, offset, enc, single_byte); + if (!in_byte) offset = str_offset(s, e, offset, enc, single_byte); s += offset; } if (slen == 0) return offset; @@ -3873,7 +3875,7 @@ static long rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str) { if (BUILTIN_TYPE(pat) == T_STRING) { - pos = rb_str_index(str, pat, pos); + pos = rb_strseq_index(str, pat, pos, 1); if (set_backref_str) { if (pos >= 0) { VALUE match; diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 3539a3eace..ed1ca895df 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -834,6 +834,9 @@ class TestString < Test::Unit::TestCase assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding assert_equal S("a\u{e9}apos<"), S("a\u{e9}'<").gsub("'", "apos") + + bug9849 = '[ruby-core:62669] [Bug #9849]' + assert_equal S("\u{3042 3042 3042}!foo!"), S("\u{3042 3042 3042}/foo/").gsub("/", "!"), bug9849 end def test_gsub! |