Project

General

Profile

Bug #9676 ยป ruby-578.diff

patch for String#gsub - srawlins (Sam Rawlins), 03/25/2014 10:55 PM

View differences:

include/ruby/re.h
52 52
#define RMATCH_REGS(obj)  (&(R_CAST(RMatch)(obj))->rmatch->regs)
53 53

  
54 54
VALUE rb_reg_regcomp(VALUE);
55
long rb_reg_search0(VALUE, VALUE, long, int, int);
55 56
long rb_reg_search(VALUE, VALUE, long, int);
56 57
VALUE rb_reg_regsub(VALUE, VALUE, struct re_registers *, VALUE);
57 58
long rb_reg_adjust_startpos(VALUE, VALUE, long, int);
re.c
1375 1375

  
1376 1376
/* returns byte offset */
1377 1377
long
1378
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1378
rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
1379 1379
{
1380 1380
    long result;
1381 1381
    VALUE match;
......
1450 1450
	    FL_UNSET(match, FL_TAINT);
1451 1451
    }
1452 1452

  
1453
    RMATCH(match)->str = rb_str_new4(str);
1453
    if (set_backref_str) {
1454
	RMATCH(match)->str = rb_str_new4(str);
1455
	OBJ_INFECT(match, str);
1456
    }
1457

  
1454 1458
    RMATCH(match)->regexp = re;
1455 1459
    RMATCH(match)->rmatch->char_offset_updated = 0;
1456 1460
    rb_backref_set(match);
1457 1461

  
1458 1462
    OBJ_INFECT(match, re);
1459
    OBJ_INFECT(match, str);
1460 1463

  
1461 1464
    return result;
1462 1465
}
1463 1466

  
1467
long
1468
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1469
{
1470
	return rb_reg_search0(re, str, pos, reverse, 1);
1471
}
1472

  
1464 1473
VALUE
1465 1474
rb_reg_nth_defined(int nth, VALUE match)
1466 1475
{
string.c
4021 4021
    int iter = 0;
4022 4022
    char *sp, *cp;
4023 4023
    int tainted = 0;
4024
    int str_replace;
4024 4025
    rb_encoding *str_enc;
4025 4026

  
4026 4027
    switch (argc) {
......
4041 4042
    }
4042 4043

  
4043 4044
    pat = get_pat(argv[0], 1);
4044
    beg = rb_reg_search(pat, str, 0, 0);
4045
    str_replace = !iter && NIL_P(hash);
4046
    beg = rb_reg_search0(pat, str, 0, 0, !str_replace);
4045 4047
    if (beg < 0) {
4046 4048
	if (bang) return Qnil;	/* no match, no substitution */
4047 4049
	return rb_str_dup(str);
......
4064 4066
	regs = RMATCH_REGS(match);
4065 4067
	beg0 = BEG(0);
4066 4068
	end0 = END(0);
4067
	if (iter || !NIL_P(hash)) {
4069
	if (!str_replace) {
4068 4070
            if (iter) {
4069 4071
                val = rb_obj_as_string(rb_yield(rb_reg_nth_match(0, match)));
4070 4072
            }
......
4104 4106
	}
4105 4107
	cp = RSTRING_PTR(str) + offset;
4106 4108
	if (offset > RSTRING_LEN(str)) break;
4107
	beg = rb_reg_search(pat, str, offset, 0);
4109
	beg = rb_reg_search0(pat, str, offset, 0, !str_replace);
4108 4110
    } while (beg >= 0);
4109 4111
    if (RSTRING_LEN(str) > offset) {
4110 4112
        rb_enc_str_buf_cat(dest, cp, RSTRING_LEN(str) - offset, str_enc);