@@ -287,6 +287,8 @@ str_substr(str, start, len)
287
287
VALUE str ;
288
288
int start , len ;
289
289
{
290
+ VALUE str2 ;
291
+
290
292
if (start < 0 ) {
291
293
start = RSTRING (str )-> len + start ;
292
294
}
@@ -297,7 +299,10 @@ str_substr(str, start, len)
297
299
len = RSTRING (str )-> len - start ;
298
300
}
299
301
300
- return str_new (RSTRING (str )-> ptr + start , len );
302
+ str2 = str_new (RSTRING (str )-> ptr + start , len );
303
+ if (str_tainted (str )) str_taint (str2 );
304
+
305
+ return str2 ;
301
306
}
302
307
303
308
static VALUE
@@ -972,7 +977,7 @@ str_sub_iter_s(str, pat, once)
972
977
VALUE pat ;
973
978
int once ;
974
979
{
975
- VALUE val , result ;
980
+ VALUE val , match , result ;
976
981
int beg , offset , n ;
977
982
struct re_registers * regs ;
978
983
@@ -998,10 +1003,11 @@ str_sub_iter_s(str, pat, once)
998
1003
while ((beg = reg_search (pat , str , offset , 0 )) >= 0 ) {
999
1004
1000
1005
n ++ ;
1001
- regs = RMATCH (backref_get ())-> regs ;
1006
+ match = backref_get ();
1007
+ regs = RMATCH (match )-> regs ;
1002
1008
str_cat (result , RSTRING (str )-> ptr + offset , beg - offset );
1003
1009
1004
- val = obj_as_string (rb_yield (reg_nth_match (0 , backref_get () )));
1010
+ val = obj_as_string (rb_yield (reg_nth_match (0 , match )));
1005
1011
str_cat (result , RSTRING (val )-> ptr , RSTRING (val )-> len );
1006
1012
1007
1013
if (BEG (0 ) == END (0 )) {
@@ -2391,29 +2397,25 @@ scan_once(str, pat, start)
2391
2397
VALUE str , pat ;
2392
2398
int * start ;
2393
2399
{
2394
- VALUE result ;
2400
+ VALUE result , match ;
2395
2401
struct re_registers * regs ;
2396
2402
int i ;
2397
2403
2398
2404
if (reg_search (pat , str , * start , 0 ) >= 0 ) {
2399
- regs = RMATCH (backref_get ())-> regs ;
2405
+ match = backref_get ();
2406
+ regs = RMATCH (match )-> regs ;
2400
2407
if (END (0 ) == * start ) {
2401
2408
* start = END (0 )+ 1 ;
2402
2409
}
2403
2410
else {
2404
2411
* start = END (0 );
2405
2412
}
2406
2413
if (regs -> num_regs == 1 ) {
2407
- return str_substr ( str , BEG ( 0 ), END ( 0 ) - BEG ( 0 ) );
2414
+ return reg_nth_match ( 0 , match );
2408
2415
}
2409
2416
result = ary_new2 (regs -> num_regs );
2410
2417
for (i = 1 ; i < regs -> num_regs ; i ++ ) {
2411
- if (BEG (i ) == -1 ) {
2412
- ary_push (result , Qnil );
2413
- }
2414
- else {
2415
- ary_push (result , str_substr (str , BEG (i ), END (i )- BEG (i )));
2416
- }
2418
+ ary_push (result , reg_nth_match (i , match ));
2417
2419
}
2418
2420
2419
2421
return result ;
0 commit comments