From 51b4cc11d179106d4b7b9e8baf84e8e55c7b49f7 Mon Sep 17 00:00:00 2001 From: matz Date: Tue, 28 Aug 2007 06:45:32 +0000 Subject: [PATCH] * string.c (rb_str_subseq): retrieve substring based on byte offset. * string.c (rb_str_rindex_m): was confusing character offset and byte offset. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13295 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ include/ruby/intern.h | 1 + re.c | 10 +++++----- string.c | 42 +++++++++++++++++++++++++++--------------- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/ChangeLog b/ChangeLog index d9a02c5b79..c04c3783ea 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Tue Aug 28 15:42:09 2007 Yukihiro Matsumoto + + * string.c (rb_str_subseq): retrieve substring based on byte offset. + + * string.c (rb_str_rindex_m): was confusing character offset and + byte offset. + Tue Aug 28 14:23:31 2007 Nobuyoshi Nakada * string.c (rb_str_splice_0): should check to modify. [ruby-dev:31665] diff --git a/include/ruby/intern.h b/include/ruby/intern.h index 7b1ab6aebe..78ac3f9d00 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -500,6 +500,7 @@ VALUE rb_str_plus(VALUE, VALUE); VALUE rb_str_times(VALUE, VALUE); int rb_str_sublen(VALUE, int); VALUE rb_str_substr(VALUE, long, long); +VALUE rb_str_subseq(VALUE, long, long); void rb_str_modify(VALUE); VALUE rb_str_freeze(VALUE); void rb_str_set_len(VALUE, long); diff --git a/re.c b/re.c index 15f229c1f9..f6cb2b2a41 100644 --- a/re.c +++ b/re.c @@ -1114,7 +1114,7 @@ rb_reg_nth_match(int nth, VALUE match) if (start == -1) return Qnil; end = RMATCH(match)->END(nth); len = end - start; - str = rb_str_substr(RMATCH(match)->str, start, len); + str = rb_str_subseq(RMATCH(match)->str, start, len); OBJ_INFECT(str, match); return str; } @@ -1144,7 +1144,7 @@ rb_reg_match_pre(VALUE match) if (NIL_P(match)) return Qnil; if (RMATCH(match)->BEG(0) == -1) return Qnil; - str = rb_str_substr(RMATCH(match)->str, 0, RMATCH(match)->BEG(0)); + str = rb_str_subseq(RMATCH(match)->str, 0, RMATCH(match)->BEG(0)); if (OBJ_TAINTED(match)) OBJ_TAINT(str); return str; } @@ -1171,7 +1171,7 @@ rb_reg_match_post(VALUE match) if (RMATCH(match)->BEG(0) == -1) return Qnil; str = RMATCH(match)->str; pos = RMATCH(match)->END(0); - str = rb_str_substr(str, pos, RSTRING_LEN(str) - pos); + str = rb_str_subseq(str, pos, RSTRING_LEN(str) - pos); if (OBJ_TAINTED(match)) OBJ_TAINT(str); return str; } @@ -1228,7 +1228,7 @@ match_array(VALUE match, int start) rb_ary_push(ary, Qnil); } else { - VALUE str = rb_str_substr(target, regs->beg[i], regs->end[i]-regs->beg[i]); + VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]); if (taint) OBJ_TAINT(str); rb_ary_push(ary, str); } @@ -1423,7 +1423,7 @@ match_select(int argc, VALUE *argv, VALUE match) int taint = OBJ_TAINTED(match); for (i=0; inum_regs; i++) { - VALUE str = rb_str_substr(target, regs->beg[i], regs->end[i]-regs->beg[i]); + VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]); if (taint) OBJ_TAINT(str); if (RTEST(rb_yield(str))) { rb_ary_push(result, str); diff --git a/string.c b/string.c index 95d48289eb..dc0796b130 100644 --- a/string.c +++ b/string.c @@ -400,10 +400,10 @@ rb_str_init(int argc, VALUE *argv, VALUE str) return str; } -static int +static long str_strlen(VALUE str, rb_encoding *enc) { - int len; + long len; if (!enc) enc = rb_enc_get(str); len = rb_enc_strlen(RSTRING_PTR(str), RSTRING_END(str), enc); @@ -696,14 +696,14 @@ str_offset(const char *p, const char *e, int nth, rb_encoding *enc) return pp - p; } -static int -str_sublen(VALUE str, int pos, rb_encoding *enc) +static long +str_sublen(VALUE str, long pos, rb_encoding *enc) { if (rb_enc_mbmaxlen(enc) == 1 || pos < 0) return pos; else { char *p = RSTRING_PTR(str); char *e = p + pos; - int i; + long i; i = 0; while (p < e) { @@ -720,6 +720,17 @@ rb_str_sublen(VALUE str, int len) return str_sublen(str, len, rb_enc_get(str)); } +VALUE +rb_str_subseq(VALUE str, long beg, long len) +{ + VALUE str2 = rb_str_new5(str, RSTRING_PTR(str)+beg, len); + + rb_enc_copy(str2, str); + OBJ_INFECT(str2, str); + + return str2; +} + VALUE rb_str_substr(VALUE str, long beg, long len) { @@ -1439,12 +1450,13 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) { VALUE sub; VALUE vpos; - long pos; + rb_encoding *enc = rb_enc_get(str); + long pos, len = str_strlen(str, enc); if (rb_scan_args(argc, argv, "11", &sub, &vpos) == 2) { pos = NUM2LONG(vpos); if (pos < 0) { - pos += RSTRING_LEN(str); + pos += len; if (pos < 0) { if (TYPE(sub) == T_REGEXP) { rb_backref_set(Qnil); @@ -1452,17 +1464,15 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) return Qnil; } } - if (pos > RSTRING_LEN(str)) pos = RSTRING_LEN(str); + if (pos > len) pos = len; } else { - pos = RSTRING_LEN(str); + pos = len; } switch (TYPE(sub)) { case T_REGEXP: /* enc = rb_get_check(str, sub); */ - pos++; /* xxx adjust for Oniguruma 5.x */ - pos = str_offset(RSTRING_PTR(str), RSTRING_END(str), pos, rb_enc_get(str)); if (RREGEXP(sub)->len) { pos = rb_reg_adjust_startpos(sub, str, pos, 1); pos = rb_reg_search(sub, str, pos, 1); @@ -1483,6 +1493,7 @@ rb_str_rindex_m(int argc, VALUE *argv, VALUE str) } /* fall through */ case T_STRING: + pos = str_sublen(str, pos, enc); pos = rb_str_rindex(str, sub, pos); if (pos >= 0) return LONG2NUM(pos); break; @@ -2076,6 +2087,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str) for (i=0; i