From: nobu.nokada@... Date: 2002-12-14T02:03:31+09:00 Subject: Re: [RCR] Global Regexp Match Mechanism (//g) Hi, At Sat, 14 Dec 2002 01:25:21 +0900, Matt Armstrong wrote: > >> Freezing the string is inconvenient in my application. I examine a > >> string in detail before deciding whether to append more data to it > >> (String#<<) from a file or start a new string. > > > > Modification of target string will cause the character boundary > > issue even with your String#gpos. It must be recalculated. > > It is possible to be fancy and avoid recalculation of byte offset B of > character position C. E.g. if you insert 10 bytes before B, increment > B by 10. It means all destructive String methods must pay that cost. It depends on frequencies of index and other methods, but I doubt if it acceptable. > But your point about thread safety is good. String#index together > with \G is a good solution. Though Regexp#match with a character > offset may be handy too, since it returns the MatchData. I also want this extension. Index: re.c =================================================================== RCS file: /cvs/ruby/src/ruby/re.c,v retrieving revision 1.86 diff -u -2 -p -r1.86 re.c --- re.c 12 Dec 2002 09:17:32 -0000 1.86 +++ re.c 12 Dec 2002 09:31:57 -0000 @@ -1130,10 +1130,18 @@ rb_reg_match2(re) static VALUE -rb_reg_match_m(re, str) - VALUE re, str; +rb_reg_match_m(argc, argv, re) + int argc; + VALUE *argv; + VALUE re; { - VALUE result = rb_reg_match(re, str); + VALUE str, initpos, result; + long pos = 0; - if (NIL_P(result)) return Qnil; + if (rb_scan_args(argc, argv, "11", &str, &initpos) == 2) { + pos = NUM2LONG(initpos); + } + if (NIL_P(str)) return Qnil; + StringValue(str); + if (rb_reg_search(re, str, pos, 0) < 0) return Qnil; result = rb_backref_get(); rb_match_busy(result); @@ -1586,5 +1683,5 @@ Init_Regexp() rb_define_method(rb_cRegexp, "===", rb_reg_match, 1); rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0); - rb_define_method(rb_cRegexp, "match", rb_reg_match_m, 1); + rb_define_method(rb_cRegexp, "match", rb_reg_match_m, -1); rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0); rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0); -- Nobu Nakada