465677 {
465678 if (start_ == 0)
465679 return false;
465680
465681 StringPiece context = const_context;
465682 if (context.data() == NULL)
465683 context = text;
465684
465685
465686 if (BeginPtr(text) < BeginPtr(context) || EndPtr(text) > EndPtr(context)) {
465687 LOG(DFATAL) << "context does not contain text";
465688 return false;
465689 }
465690
465691 if (prog_->anchor_start() && BeginPtr(context) != BeginPtr(text))
465692 return false;
465693 if (prog_->anchor_end() && EndPtr(context) != EndPtr(text))
465694 return false;
465695 anchored |= prog_->anchor_start();
465696 if (prog_->anchor_end()) {
465697 longest = true;
465698 endmatch_ = true;
465699 }
465700
465701 if (nsubmatch < 0) {
465702 LOG(DFATAL) << "Bad args: nsubmatch=" << nsubmatch;
465703 return false;
465704 }
465705
465706
465707 ncapture_ = 2*nsubmatch;
465708 longest_ = longest;
465709
465710 if (nsubmatch == 0) {
465711
465712
465713
465714 ncapture_ = 2;
465715 }
465716
465717 match_ = new const char*[ncapture_];
465718 memset(match_, 0, ncapture_*sizeof match_[0]);
465719 matched_ = false;
465720
465721
465722 btext_ = context.data();
465723
465724 etext_ = text.data() + text.size();
465725
465726
465727 Threadq* runq = &q0_;
465728 Threadq* nextq = &q1_;
465729 runq->clear();
465730 nextq->clear();
465731
465732
465733 for (const char* p = text.data();; p++) {
465734
465735 int id = Step(runq, nextq, p < etext_ ? p[0] & 0xFF : -1, context, p);
465736 DCHECK_EQ(runq->size(), 0);
465737 using std::swap;
465738 swap(nextq, runq);
465739 nextq->clear();
465740 if (id != 0) {
465741
465742 p = etext_;
465743 for (;;) {
465744 Prog::Inst* ip = prog_->inst(id);
465745 switch (ip->opcode()) {
465746 default:
465747 LOG(DFATAL) << "Unexpected opcode in short circuit: " << ip->opcode();
465748 break;
465749
465750 case kInstCapture:
465751 if (ip->cap() < ncapture_)
465752 match_[ip->cap()] = p;
465753 id = ip->out();
465754 continue;
465755
465756 case kInstNop:
465757 id = ip->out();
465758 continue;
465759
465760 case kInstMatch:
465761 match_[1] = p;
465762 matched_ = true;
465763 break;
465764 }
465765 break;
465766 }
465767 break;
465768 }
465769
465770 if (p > etext_)
465771 break;
465772
465773
465774
465775
465776
465777 if (!matched_ && (!anchored || p == text.data())) {
465778
465779
465780
465781 if (!anchored && runq->size() == 0 &&
465782 p < etext_ && prog_->can_prefix_accel()) {
465783 p = reinterpret_cast<const char*>(prog_->PrefixAccel(p, etext_ - p));
465784 if (p == NULL)
465785 p = etext_;
465786 }
465787
465788 Thread* t = AllocThread();
465789 CopyCapture(t->capture, match_);
465790 t->capture[0] = p;
465791 AddToThreadq(runq, start_, p < etext_ ? p[0] & 0xFF : -1, context, p,
465792 t);
465793 Decref(t);
465794 }
465795
465796
465797 if (runq->size() == 0) {
465798 break;
465799 }
465800
465801
465802
465803
465804 if (p == NULL) {
465805 (void) Step(runq, nextq, -1, context, p);
465806 DCHECK_EQ(runq->size(), 0);
465807 using std::swap;
465808 swap(nextq, runq);
465809 nextq->clear();
465810 break;
465811 }
465812 }
465813
465814 for (Threadq::iterator i = runq->begin(); i != runq->end(); ++i) {
465815 if (i->value() != NULL)
465816 Decref(i->value());
465817 }
465818
465819 if (matched_) {
465820 for (int i = 0; i < nsubmatch; i++)
465821 submatch[i] =
465822 StringPiece(match_[2 * i],
465823 static_cast<size_t>(match_[2 * i + 1] - match_[2 * i]));
465824 return true;
465825 }
465826 return false;
465827}
void swap(Mat &a, Mat &b)