ext/re2/re2.cc in re2-1.3.0 vs ext/re2/re2.cc in re2-1.4.0

- old
+ new

@@ -1156,10 +1156,11 @@ * matches returned (padded with nils if necessary). * * @param [String] text the text to search * @param [Fixnum] number_of_matches the number of matches to return * @return [RE2::MatchData] the matches + * @raise [ArgumentError] if given a negative number of matches * @raise [NoMemoryError] if there was not enough memory to allocate the matches * @example * r = RE2::Regexp.new('w(o)(o)') * r.match('woo', 1) #=> #<RE2::MatchData "woo" 1:"o"> * r.match('woo', 3) #=> #<RE2::MatchData "woo" 1:"o" 2:"o" 3:nil> @@ -1178,11 +1179,19 @@ Data_Get_Struct(self, re2_pattern, p); if (RTEST(number_of_matches)) { n = NUM2INT(number_of_matches); + + if (n < 0) { + rb_raise(rb_eArgError, "number of matches should be >= 0"); + } } else { + if (!p->pattern->ok()) { + return Qnil; + } + n = p->pattern->NumberOfCapturingGroups(); } if (n == 0) { matched = match(p->pattern, StringValuePtr(text), 0, @@ -1249,10 +1258,16 @@ Data_Get_Struct(scanner, re2_scanner, c); c->input = new(nothrow) re2::StringPiece(StringValuePtr(text)); c->regexp = self; c->text = text; - c->number_of_capturing_groups = p->pattern->NumberOfCapturingGroups(); + + if (p->pattern->ok()) { + c->number_of_capturing_groups = p->pattern->NumberOfCapturingGroups(); + } else { + c->number_of_capturing_groups = 0; + } + c->eof = false; return scanner; }