diff options
author | charliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-03-25 23:46:05 +0000 |
---|---|---|
committer | charliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-03-25 23:46:05 +0000 |
commit | a77206582e9372d748db3bbc85c9c3ab0c6681db (patch) | |
tree | 1610591c9696fb751b8d31eec1e24b985cf68d59 /string.c | |
parent | ebd9f1d0e485972ef778f5c7251b0e52694f3979 (diff) |
Stop allocating backref strings within gsub's search loop
* internal.h: add prototype for rb_reg_search0
* re.c: rename rb_reg_search to rb_reg_search0, add set_backref_str
argument to allow callers to indicate that they don't require the
backref string to be allocated
* string.c: don't allocate backref str if replacement string is provided
Closes GH-578. [Bug #9676] [ruby-core:61682]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45414 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 8 |
1 files changed, 5 insertions, 3 deletions
@@ -4021,6 +4021,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) int iter = 0; char *sp, *cp; int tainted = 0; + int str_replace; rb_encoding *str_enc; switch (argc) { @@ -4041,7 +4042,8 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) } pat = get_pat(argv[0], 1); - beg = rb_reg_search(pat, str, 0, 0); + str_replace = !iter && NIL_P(hash); + beg = rb_reg_search0(pat, str, 0, 0, !str_replace); if (beg < 0) { if (bang) return Qnil; /* no match, no substitution */ return rb_str_dup(str); @@ -4064,7 +4066,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) regs = RMATCH_REGS(match); beg0 = BEG(0); end0 = END(0); - if (iter || !NIL_P(hash)) { + if (!str_replace) { if (iter) { val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match))); } @@ -4104,7 +4106,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang) } cp = RSTRING_PTR(str) + offset; if (offset > RSTRING_LEN(str)) break; - beg = rb_reg_search(pat, str, offset, 0); + beg = rb_reg_search0(pat, str, offset, 0, !str_replace); } while (beg >= 0); if (RSTRING_LEN(str) > offset) { rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc); |