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
This commit is contained in:
parent
ebd9f1d0e4
commit
a77206582e
12
ChangeLog
12
ChangeLog
@ -1,3 +1,15 @@
|
|||||||
|
Wed Mar 26 08:45:00 2014 Sam Rawlins <sam.rawlins@gmail.com>
|
||||||
|
|
||||||
|
* 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
|
||||||
|
|
||||||
|
[GH-578] [Bug #9676] [ruby-core:61682]
|
||||||
|
|
||||||
Wed Mar 26 08:29:43 2014 mo khan <mo@mokhan.ca>
|
Wed Mar 26 08:29:43 2014 mo khan <mo@mokhan.ca>
|
||||||
|
|
||||||
* lib/rubygem.rb: fix spelling of Jim Weirich. [Fixes GH-577]
|
* lib/rubygem.rb: fix spelling of Jim Weirich. [Fixes GH-577]
|
||||||
|
@ -978,6 +978,9 @@ VALUE rb_gcd_normal(VALUE self, VALUE other);
|
|||||||
VALUE rb_gcd_gmp(VALUE x, VALUE y);
|
VALUE rb_gcd_gmp(VALUE x, VALUE y);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* re.c */
|
||||||
|
long rb_reg_search0(VALUE, VALUE, long, int, int);
|
||||||
|
|
||||||
/* util.c */
|
/* util.c */
|
||||||
extern const signed char ruby_digit36_to_number_table[];
|
extern const signed char ruby_digit36_to_number_table[];
|
||||||
|
|
||||||
|
13
re.c
13
re.c
@ -1375,7 +1375,7 @@ rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
|
|||||||
|
|
||||||
/* returns byte offset */
|
/* returns byte offset */
|
||||||
long
|
long
|
||||||
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
|
rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
|
||||||
{
|
{
|
||||||
long result;
|
long result;
|
||||||
VALUE match;
|
VALUE match;
|
||||||
@ -1450,17 +1450,26 @@ rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
|
|||||||
FL_UNSET(match, FL_TAINT);
|
FL_UNSET(match, FL_TAINT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (set_backref_str) {
|
||||||
RMATCH(match)->str = rb_str_new4(str);
|
RMATCH(match)->str = rb_str_new4(str);
|
||||||
|
OBJ_INFECT(match, str);
|
||||||
|
}
|
||||||
|
|
||||||
RMATCH(match)->regexp = re;
|
RMATCH(match)->regexp = re;
|
||||||
RMATCH(match)->rmatch->char_offset_updated = 0;
|
RMATCH(match)->rmatch->char_offset_updated = 0;
|
||||||
rb_backref_set(match);
|
rb_backref_set(match);
|
||||||
|
|
||||||
OBJ_INFECT(match, re);
|
OBJ_INFECT(match, re);
|
||||||
OBJ_INFECT(match, str);
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long
|
||||||
|
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
|
||||||
|
{
|
||||||
|
return rb_reg_search0(re, str, pos, reverse, 1);
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_reg_nth_defined(int nth, VALUE match)
|
rb_reg_nth_defined(int nth, VALUE match)
|
||||||
{
|
{
|
||||||
|
8
string.c
8
string.c
@ -4021,6 +4021,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
int iter = 0;
|
int iter = 0;
|
||||||
char *sp, *cp;
|
char *sp, *cp;
|
||||||
int tainted = 0;
|
int tainted = 0;
|
||||||
|
int str_replace;
|
||||||
rb_encoding *str_enc;
|
rb_encoding *str_enc;
|
||||||
|
|
||||||
switch (argc) {
|
switch (argc) {
|
||||||
@ -4041,7 +4042,8 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pat = get_pat(argv[0], 1);
|
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 (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);
|
||||||
@ -4064,7 +4066,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
regs = RMATCH_REGS(match);
|
regs = RMATCH_REGS(match);
|
||||||
beg0 = BEG(0);
|
beg0 = BEG(0);
|
||||||
end0 = END(0);
|
end0 = END(0);
|
||||||
if (iter || !NIL_P(hash)) {
|
if (!str_replace) {
|
||||||
if (iter) {
|
if (iter) {
|
||||||
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)));
|
||||||
}
|
}
|
||||||
@ -4104,7 +4106,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
|
|||||||
}
|
}
|
||||||
cp = RSTRING_PTR(str) + offset;
|
cp = RSTRING_PTR(str) + offset;
|
||||||
if (offset > RSTRING_LEN(str)) break;
|
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);
|
} while (beg >= 0);
|
||||||
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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user