* string.c (str_gsub): should preserve last successful match
data. [ruby-dev:35182] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17447 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
e929f49e56
commit
5336cbdfd3
@ -1,3 +1,8 @@
|
|||||||
|
Fri Jun 20 02:11:01 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
|
||||||
|
|
||||||
|
* string.c (str_gsub): should preserve last successful match
|
||||||
|
data. [ruby-dev:35182]
|
||||||
|
|
||||||
Fri Jun 20 01:07:28 2008 Koichi Sasada <ko1@atdot.net>
|
Fri Jun 20 01:07:28 2008 Koichi Sasada <ko1@atdot.net>
|
||||||
|
|
||||||
* KNOWNBUGS.rb, bootstraptest/pending.rb: move a bug (?) to pending.
|
* KNOWNBUGS.rb, bootstraptest/pending.rb: move a bug (?) to pending.
|
||||||
|
@ -392,6 +392,5 @@ assert_equal 'ok', %q{
|
|||||||
|
|
||||||
f = Foo.new
|
f = Foo.new
|
||||||
a_proc = give_it
|
a_proc = give_it
|
||||||
p :call_it
|
|
||||||
f.call_it(&give_it())
|
f.call_it(&give_it())
|
||||||
}, '[ruby-core:15711]'
|
}, '[ruby-core:15711]'
|
||||||
|
35
string.c
35
string.c
@ -3084,8 +3084,7 @@ get_pat(VALUE pat, int quote)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
||||||
{
|
{
|
||||||
VALUE pat, repl, match, hash = Qnil;
|
VALUE pat, repl, hash = Qnil;
|
||||||
struct re_registers *regs;
|
|
||||||
int iter = 0;
|
int iter = 0;
|
||||||
int tainted = 0;
|
int tainted = 0;
|
||||||
long plen;
|
long plen;
|
||||||
@ -3109,15 +3108,13 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
|||||||
if (rb_reg_search(pat, str, 0, 0) >= 0) {
|
if (rb_reg_search(pat, str, 0, 0) >= 0) {
|
||||||
rb_encoding *enc;
|
rb_encoding *enc;
|
||||||
int cr = ENC_CODERANGE(str);
|
int cr = ENC_CODERANGE(str);
|
||||||
|
VALUE match = rb_backref_get();
|
||||||
match = rb_backref_get();
|
struct re_registers *regs = RMATCH_REGS(match);
|
||||||
regs = RMATCH_REGS(match);
|
|
||||||
|
|
||||||
if (iter || !NIL_P(hash)) {
|
if (iter || !NIL_P(hash)) {
|
||||||
char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);
|
char *p = RSTRING_PTR(str); long len = RSTRING_LEN(str);
|
||||||
|
|
||||||
if (iter) {
|
if (iter) {
|
||||||
rb_match_busy(match);
|
|
||||||
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
repl = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -3126,7 +3123,6 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
|
|||||||
}
|
}
|
||||||
str_mod_check(str, p, len);
|
str_mod_check(str, p, len);
|
||||||
str_frozen_check(str);
|
str_frozen_check(str);
|
||||||
if (iter) rb_backref_set(match);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
repl = rb_reg_regsub(repl, str, regs, pat);
|
repl = rb_reg_regsub(repl, str, regs, pat);
|
||||||
@ -3217,7 +3213,8 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
VALUE pat, val, repl, match, dest, hash = Qnil;
|
VALUE pat, val, repl, match, dest, hash = Qnil;
|
||||||
struct re_registers *regs;
|
struct re_registers *regs;
|
||||||
long beg, n;
|
long beg, n;
|
||||||
long offset, blen, slen, len;
|
long beg0, end0;
|
||||||
|
long offset, blen, slen, len, last;
|
||||||
int iter = 0;
|
int iter = 0;
|
||||||
char *sp, *cp;
|
char *sp, *cp;
|
||||||
int tainted = 0;
|
int tainted = 0;
|
||||||
@ -3241,13 +3238,14 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pat = get_pat(argv[0], 1);
|
pat = get_pat(argv[0], 1);
|
||||||
offset=0; n=0;
|
|
||||||
beg = rb_reg_search(pat, str, 0, 0);
|
beg = rb_reg_search(pat, str, 0, 0);
|
||||||
if (beg < 0) {
|
if (beg < 0) {
|
||||||
if (bang) return Qnil; /* no match, no substitution */
|
if (bang) return Qnil; /* no match, no substitution */
|
||||||
return rb_str_dup(str);
|
return rb_str_dup(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
offset = 0;
|
||||||
|
n = 0;
|
||||||
blen = RSTRING_LEN(str) + 30; /* len + margin */
|
blen = RSTRING_LEN(str) + 30; /* len + margin */
|
||||||
dest = rb_str_buf_new(blen);
|
dest = rb_str_buf_new(blen);
|
||||||
sp = RSTRING_PTR(str);
|
sp = RSTRING_PTR(str);
|
||||||
@ -3259,9 +3257,10 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
n++;
|
n++;
|
||||||
match = rb_backref_get();
|
match = rb_backref_get();
|
||||||
regs = RMATCH_REGS(match);
|
regs = RMATCH_REGS(match);
|
||||||
|
beg0 = BEG(0);
|
||||||
|
end0 = END(0);
|
||||||
if (iter || !NIL_P(hash)) {
|
if (iter || !NIL_P(hash)) {
|
||||||
if (iter) {
|
if (iter) {
|
||||||
rb_match_busy(match);
|
|
||||||
val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -3273,7 +3272,6 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
if (val == dest) { /* paranoid check [ruby-dev:24827] */
|
if (val == dest) { /* paranoid check [ruby-dev:24827] */
|
||||||
rb_raise(rb_eRuntimeError, "block should not cheat");
|
rb_raise(rb_eRuntimeError, "block should not cheat");
|
||||||
}
|
}
|
||||||
if (iter) rb_backref_set(match);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
val = rb_reg_regsub(repl, str, regs, pat);
|
val = rb_reg_regsub(repl, str, regs, pat);
|
||||||
@ -3288,16 +3286,17 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
|
|
||||||
rb_str_buf_append(dest, val);
|
rb_str_buf_append(dest, val);
|
||||||
|
|
||||||
offset = END(0);
|
last = offset;
|
||||||
if (BEG(0) == END(0)) {
|
offset = end0;
|
||||||
|
if (beg0 == end0) {
|
||||||
/*
|
/*
|
||||||
* Always consume at least one character of the input string
|
* Always consume at least one character of the input string
|
||||||
* in order to prevent infinite loops.
|
* in order to prevent infinite loops.
|
||||||
*/
|
*/
|
||||||
if (RSTRING_LEN(str) <= END(0)) break;
|
if (RSTRING_LEN(str) <= end0) break;
|
||||||
len = rb_enc_mbclen(RSTRING_PTR(str)+END(0), RSTRING_END(str), str_enc);
|
len = rb_enc_mbclen(RSTRING_PTR(str)+end0, RSTRING_END(str), str_enc);
|
||||||
rb_enc_str_buf_cat(dest, RSTRING_PTR(str)+END(0), len, str_enc);
|
rb_enc_str_buf_cat(dest, RSTRING_PTR(str)+end0, len, str_enc);
|
||||||
offset = END(0) + len;
|
offset = end0 + len;
|
||||||
}
|
}
|
||||||
cp = RSTRING_PTR(str) + offset;
|
cp = RSTRING_PTR(str) + offset;
|
||||||
if (offset > RSTRING_LEN(str)) break;
|
if (offset > RSTRING_LEN(str)) break;
|
||||||
@ -3306,7 +3305,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
if (RSTRING_LEN(str) > offset) {
|
if (RSTRING_LEN(str) > offset) {
|
||||||
rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
|
rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);
|
||||||
}
|
}
|
||||||
rb_backref_set(match);
|
rb_reg_search(pat, str, last, 0);
|
||||||
if (bang) {
|
if (bang) {
|
||||||
rb_str_shared_replace(str, dest);
|
rb_str_shared_replace(str, dest);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user