472223 {
472224 if (!ok()) {
472225 if (options_.log_errors())
472226 LOG(ERROR) << "Invalid RE2: " << *error_;
472227 return false;
472228 }
472229
472230 if (startpos > endpos || endpos > text.size()) {
472231 if (options_.log_errors())
472232 LOG(ERROR) << "RE2: invalid startpos, endpos pair. ["
472233 << "startpos: " << startpos << ", "
472234 << "endpos: " << endpos << ", "
472235 << "text size: " << text.size() << "]";
472236 return false;
472237 }
472238
472239 StringPiece subtext = text;
472240 subtext.remove_prefix(startpos);
472241 subtext.remove_suffix(text.size() - endpos);
472242
472243
472244
472245
472246
472247 StringPiece match;
472248 StringPiece* matchp = &match;
472249 if (nsubmatch == 0)
472250 matchp = NULL;
472251
472252 int ncap = 1 + NumberOfCapturingGroups();
472253 if (ncap > nsubmatch)
472254 ncap = nsubmatch;
472255
472256
472257 if (prog_->anchor_start() && startpos != 0)
472258 return false;
472259 if (prog_->anchor_end() && endpos != text.size())
472260 return false;
472261
472262
472263
472264 if (prog_->anchor_start() && prog_->anchor_end())
472265 re_anchor = ANCHOR_BOTH;
472266 else if (prog_->anchor_start() && re_anchor != ANCHOR_BOTH)
472267 re_anchor = ANCHOR_START;
472268
472269
472270 size_t prefixlen = 0;
472271 if (!prefix_.empty()) {
472272 if (startpos != 0)
472273 return false;
472274 prefixlen = prefix_.size();
472275 if (prefixlen > subtext.size())
472276 return false;
472277 if (prefix_foldcase_) {
472278 if (ascii_strcasecmp(&prefix_[0], subtext.data(), prefixlen) != 0)
472279 return false;
472280 } else {
472281 if (memcmp(&prefix_[0], subtext.data(), prefixlen) != 0)
472282 return false;
472283 }
472284 subtext.remove_prefix(prefixlen);
472285
472286 if (re_anchor != ANCHOR_BOTH)
472287 re_anchor = ANCHOR_START;
472288 }
472289
472290 Prog::Anchor anchor = Prog::kUnanchored;
472291 Prog::MatchKind kind =
472292 longest_match_ ? Prog::kLongestMatch : Prog::kFirstMatch;
472293
472294 bool can_one_pass = is_one_pass_ && ncap <= Prog::kMaxOnePassCapture;
472295 bool can_bit_state = prog_->CanBitState();
472296 size_t bit_state_text_max_size = prog_->bit_state_text_max_size();
472297
472298#ifdef RE2_HAVE_THREAD_LOCAL
472299 hooks::context = this;
472300#endif
472301 bool dfa_failed = false;
472302 bool skipped_test = false;
472303 switch (re_anchor) {
472304 default:
472305 LOG(DFATAL) << "Unexpected re_anchor value: " << re_anchor;
472306 return false;
472307
472308 case UNANCHORED: {
472309 if (prog_->anchor_end()) {
472310
472311
472312
472313 Prog* prog = ReverseProg();
472314 if (prog == NULL) {
472315
472316 skipped_test = true;
472317 break;
472318 }
472319 if (!prog->SearchDFA(subtext, text, Prog::kAnchored,
472320 Prog::kLongestMatch, matchp, &dfa_failed, NULL)) {
472321 if (dfa_failed) {
472322 if (options_.log_errors())
472323 LOG(ERROR) << "DFA out of memory: "
472324 << "pattern length " << pattern_->size() << ", "
472325 << "program size " << prog->size() << ", "
472326 << "list count " << prog->list_count() << ", "
472327 << "bytemap range " << prog->bytemap_range();
472328
472329 skipped_test = true;
472330 break;
472331 }
472332 return false;
472333 }
472334 if (matchp == NULL)
472335 return true;
472336 break;
472337 }
472338
472339 if (!prog_->SearchDFA(subtext, text, anchor, kind,
472340 matchp, &dfa_failed, NULL)) {
472341 if (dfa_failed) {
472342 if (options_.log_errors())
472343 LOG(ERROR) << "DFA out of memory: "
472344 << "pattern length " << pattern_->size() << ", "
472345 << "program size " << prog_->size() << ", "
472346 << "list count " << prog_->list_count() << ", "
472347 << "bytemap range " << prog_->bytemap_range();
472348
472349 skipped_test = true;
472350 break;
472351 }
472352 return false;
472353 }
472354 if (matchp == NULL)
472355 return true;
472356
472357
472358
472359 Prog* prog = ReverseProg();
472360 if (prog == NULL) {
472361
472362 skipped_test = true;
472363 break;
472364 }
472365 if (!prog->SearchDFA(match, text, Prog::kAnchored,
472366 Prog::kLongestMatch, &match, &dfa_failed, NULL)) {
472367 if (dfa_failed) {
472368 if (options_.log_errors())
472369 LOG(ERROR) << "DFA out of memory: "
472370 << "pattern length " << pattern_->size() << ", "
472371 << "program size " << prog->size() << ", "
472372 << "list count " << prog->list_count() << ", "
472373 << "bytemap range " << prog->bytemap_range();
472374
472375 skipped_test = true;
472376 break;
472377 }
472378 if (options_.log_errors())
472379 LOG(ERROR) << "SearchDFA inconsistency";
472380 return false;
472381 }
472382 break;
472383 }
472384
472385 case ANCHOR_BOTH:
472386 case ANCHOR_START:
472387 if (re_anchor == ANCHOR_BOTH)
472388 kind = Prog::kFullMatch;
472389 anchor = Prog::kAnchored;
472390
472391
472392
472393
472394
472395
472396
472397
472398 if (can_one_pass && text.size() <= 4096 &&
472399 (ncap > 1 || text.size() <= 16)) {
472400 skipped_test = true;
472401 break;
472402 }
472403 if (can_bit_state && text.size() <= bit_state_text_max_size &&
472404 ncap > 1) {
472405 skipped_test = true;
472406 break;
472407 }
472408 if (!prog_->SearchDFA(subtext, text, anchor, kind,
472409 &match, &dfa_failed, NULL)) {
472410 if (dfa_failed) {
472411 if (options_.log_errors())
472412 LOG(ERROR) << "DFA out of memory: "
472413 << "pattern length " << pattern_->size() << ", "
472414 << "program size " << prog_->size() << ", "
472415 << "list count " << prog_->list_count() << ", "
472416 << "bytemap range " << prog_->bytemap_range();
472417
472418 skipped_test = true;
472419 break;
472420 }
472421 return false;
472422 }
472423 break;
472424 }
472425
472426 if (!skipped_test && ncap <= 1) {
472427
472428 if (ncap == 1)
472429 submatch[0] = match;
472430 } else {
472431 StringPiece subtext1;
472432 if (skipped_test) {
472433
472434
472435 subtext1 = subtext;
472436 } else {
472437
472438
472439
472440 subtext1 = match;
472441 anchor = Prog::kAnchored;
472442 kind = Prog::kFullMatch;
472443 }
472444
472445 if (can_one_pass && anchor != Prog::kUnanchored) {
472446 if (!prog_->SearchOnePass(subtext1, text, anchor, kind, submatch, ncap)) {
472447 if (!skipped_test && options_.log_errors())
472448 LOG(ERROR) << "SearchOnePass inconsistency";
472449 return false;
472450 }
472451 } else if (can_bit_state && subtext1.size() <= bit_state_text_max_size) {
472452 if (!prog_->SearchBitState(subtext1, text, anchor,
472453 kind, submatch, ncap)) {
472454 if (!skipped_test && options_.log_errors())
472455 LOG(ERROR) << "SearchBitState inconsistency";
472456 return false;
472457 }
472458 } else {
472459 if (!prog_->SearchNFA(subtext1, text, anchor, kind, submatch, ncap)) {
472460 if (!skipped_test && options_.log_errors())
472461 LOG(ERROR) << "SearchNFA inconsistency";
472462 return false;
472463 }
472464 }
472465 }
472466
472467
472468 if (prefixlen > 0 && nsubmatch > 0)
472469 submatch[0] = StringPiece(submatch[0].data() - prefixlen,
472470 submatch[0].
size() + prefixlen);
472471
472472
472473 for (int i = ncap; i < nsubmatch; i++)
472474 submatch[i] = StringPiece();
472475 return true;
472476}