diff --git a/ChangeLog b/ChangeLog index 5de093ed22..fb5e9291fd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Mon Dec 10 13:50:33 2007 Nobuyoshi Nakada + + * re.c (rb_reg_search): return byte offset. [ruby-dev:32452] + + * re.c (rb_reg_match, rb_reg_match2, rb_reg_match_m): convert byte + offset to char index. + + * string.c (rb_str_index): return byte offset. [ruby-dev:32472] + + * string.c (rb_str_split_m): calculate in byte offset. + Mon Dec 10 09:56:29 2007 Koichi Sasada * benchmark/bm_vm1_neq.rb, bm_vm1_not.rb: added. diff --git a/re.c b/re.c index 563acbf3af..36493f3785 100644 --- a/re.c +++ b/re.c @@ -1070,7 +1070,7 @@ rb_reg_search(VALUE re, VALUE str, int pos, int reverse) OBJ_INFECT(match, re); OBJ_INFECT(match, str); - return rb_str_sublen(RMATCH(match)->str, result); + return result; } VALUE @@ -2123,28 +2123,24 @@ reg_operand(VALUE s, int check) } } -static VALUE +static long rb_reg_match_pos(VALUE re, VALUE str, long pos) { if (NIL_P(str)) { rb_backref_set(Qnil); - return Qnil; + return -1; } str = reg_operand(str, Qtrue); if (pos != 0) { if (pos < 0) { pos += RSTRING_LEN(str); if (pos < 0) { - return Qnil; + return pos; } } pos = rb_reg_adjust_startpos(re, str, pos, 0); } - pos = rb_reg_search(re, str, pos, 0); - if (pos < 0) { - return Qnil; - } - return LONG2FIX(pos); + return rb_reg_search(re, str, pos, 0); } /* @@ -2160,7 +2156,10 @@ rb_reg_match_pos(VALUE re, VALUE str, long pos) VALUE rb_reg_match(VALUE re, VALUE str) { - return rb_reg_match_pos(re, str, 0); + long pos = rb_reg_match_pos(re, str, 0); + if (pos < 0) return Qnil; + pos = rb_str_sublen(str, pos); + return LONG2FIX(pos); } /* @@ -2225,6 +2224,7 @@ rb_reg_match2(VALUE re) if (start < 0) { return Qnil; } + start = rb_str_sublen(line, start); return LONG2FIX(start); } @@ -2270,8 +2270,8 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re) pos = 0; } - result = rb_reg_match_pos(re, str, pos); - if (NIL_P(result)) { + pos = rb_reg_match_pos(re, str, pos); + if (pos < 0) { rb_backref_set(Qnil); return Qnil; } diff --git a/string.c b/string.c index a6f8bf1160..7111578256 100644 --- a/string.c +++ b/string.c @@ -1412,7 +1412,7 @@ static long rb_str_index(VALUE str, VALUE sub, long offset) { long pos; - char *s; + char *s, *sptr; long len, slen; rb_encoding *enc; @@ -1424,12 +1424,27 @@ rb_str_index(VALUE str, VALUE sub, long offset) if (offset < 0) return -1; } if (len - offset < slen) return -1; + s = RSTRING_PTR(str); + if (offset) { + s = str_nth(s, RSTRING_END(str), offset, enc); + offset = s - RSTRING_PTR(str); + } if (slen == 0) return offset; - s = offset ? str_nth(RSTRING_PTR(str), RSTRING_END(str), offset, enc) : RSTRING_PTR(str); /* need proceed one character at a time */ - pos = rb_memsearch(RSTRING_PTR(sub), RSTRING_LEN(sub), - s, RSTRING_LEN(str)-(s - RSTRING_PTR(str))); - if (pos < 0) return pos; + sptr = RSTRING_PTR(sub); + slen = RSTRING_LEN(sub); + len = RSTRING_LEN(str) - offset; + for (;;) { + char *t; + pos = rb_memsearch(sptr, slen, s, len); + if (pos < 0) return pos; + t = (char *)onigenc_get_right_adjust_char_head(enc, (const UChar *)s, + (const UChar *)s + pos); + if (t == s) break; + if ((len -= t - s) <= 0) return -1; + offset += t - s; + s = t; + } return pos + offset; } @@ -4024,34 +4039,35 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str) if (awk_split) { char *ptr = RSTRING_PTR(str); char *eptr = RSTRING_END(str); + char *bptr = ptr; int skip = 1; int c; end = beg; while (ptr < eptr) { c = rb_enc_codepoint(ptr, eptr, enc); + ptr += rb_enc_mbclen(ptr, eptr, enc); if (skip) { if (rb_enc_isspace(c, enc)) { - beg++; + beg = ptr - bptr; } else { - end = beg+1; + end = ptr - bptr; skip = 0; if (!NIL_P(limit) && lim <= i) break; } } else { if (rb_enc_isspace(c, enc)) { - rb_ary_push(result, rb_str_substr(str, beg, end-beg)); + rb_ary_push(result, rb_str_subseq(str, beg, end-beg)); skip = 1; - beg = end + 1; + beg = ptr - bptr; if (!NIL_P(limit)) ++i; } else { - end++; + end = ptr - bptr; } } - ptr += rb_enc_codelen(c, enc); } } else { @@ -5114,14 +5130,13 @@ rb_str_partition(VALUE str, VALUE sep) failed: return rb_ary_new3(3, str, rb_str_new(0,0),rb_str_new(0,0)); } - pos = rb_str_sublen(str, pos); if (regex) { sep = rb_str_subpat(str, sep, 0); if (pos == 0 && RSTRING_LEN(sep) == 0) goto failed; } - return rb_ary_new3(3, rb_str_substr(str, 0, pos), + return rb_ary_new3(3, rb_str_subseq(str, 0, pos), sep, - rb_str_substr(str, pos+RSTRING_LEN(sep), + rb_str_subseq(str, pos+RSTRING_LEN(sep), RSTRING_LEN(str)-pos-RSTRING_LEN(sep))); } @@ -5162,13 +5177,12 @@ rb_str_rpartition(VALUE str, VALUE sep) if (pos < 0) { return rb_ary_new3(3, rb_str_new(0,0),rb_str_new(0,0), str); } - pos = rb_str_sublen(str, pos); if (regex) { sep = rb_reg_nth_match(0, rb_backref_get()); } - return rb_ary_new3(3, rb_str_substr(str, 0, pos), + return rb_ary_new3(3, rb_str_subseq(str, 0, pos), sep, - rb_str_substr(str, pos+RSTRING_LEN(sep), + rb_str_subseq(str, pos+RSTRING_LEN(sep), RSTRING_LEN(str)-pos-RSTRING_LEN(sep))); }