1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

string.c: byte offset

* string.c (rb_pat_search): advance by byte offset but not by char
  offset.  [ruby-core:62669] [Bug #9849]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46013 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2014-05-19 07:29:51 +00:00
parent 72c773b15d
commit 8fb925dfcd
3 changed files with 15 additions and 5 deletions

View file

@ -1,3 +1,8 @@
Mon May 19 16:29:48 2014 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <her@sorah.jp> Mon May 19 14:06:18 2014 Shota Fukumori <her@sorah.jp>
* bin/testrb: Removed. Forgot to remove in r45971. * bin/testrb: Removed. Forgot to remove in r45971.

View file

@ -2569,8 +2569,10 @@ rb_str_casecmp(VALUE str1, VALUE str2)
return INT2FIX(-1); return INT2FIX(-1);
} }
#define rb_str_index(str, sub, offset) rb_strseq_index(str, sub, offset, 0)
static long 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; const char *s, *sptr, *e;
long pos, len, slen; long pos, len, slen;
@ -2580,8 +2582,8 @@ rb_str_index(VALUE str, VALUE sub, long offset)
enc = rb_enc_check(str, sub); enc = rb_enc_check(str, sub);
if (is_broken_string(sub)) return -1; if (is_broken_string(sub)) return -1;
len = single_byte ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */ len = (in_byte || single_byte) ? RSTRING_LEN(str) : str_strlen(str, enc); /* rb_enc_check */
slen = str_strlen(sub, enc); /* rb_enc_check */ slen = in_byte ? RSTRING_LEN(sub) : str_strlen(sub, enc); /* rb_enc_check */
if (offset < 0) { if (offset < 0) {
offset += len; offset += len;
if (offset < 0) return -1; if (offset < 0) return -1;
@ -2591,7 +2593,7 @@ rb_str_index(VALUE str, VALUE sub, long offset)
s = RSTRING_PTR(str); s = RSTRING_PTR(str);
e = RSTRING_END(str); e = RSTRING_END(str);
if (offset) { 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; s += offset;
} }
if (slen == 0) return 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) rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
{ {
if (BUILTIN_TYPE(pat) == T_STRING) { 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 (set_backref_str) {
if (pos >= 0) { if (pos >= 0) {
VALUE match; VALUE match;

View file

@ -834,6 +834,9 @@ class TestString < Test::Unit::TestCase
assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding assert_equal Encoding::UTF_8, a.gsub(/world/, c).encoding
assert_equal S("a\u{e9}apos&lt;"), S("a\u{e9}'&lt;").gsub("'", "apos") assert_equal S("a\u{e9}apos&lt;"), S("a\u{e9}'&lt;").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 end
def test_gsub! def test_gsub!