Returns true if input matched the pattern using method Const::SCAN, Const::FIND, Const::SPLIT, or Const::MATCH.
| 40 | |
| 41 | /// Returns true if input matched the pattern using method Const::SCAN, Const::FIND, Const::SPLIT, or Const::MATCH. |
| 42 | size_t Matcher::match(Method method) |
| 43 | { |
| 44 | DBGLOG("BEGIN Matcher::match()"); |
| 45 | reset_text(); |
| 46 | len_ = 0; // split text length starts with 0 |
| 47 | size_t retry = 0; // retry regex match at lookback positions for predicted matches |
| 48 | if (method == Const::FIND) |
| 49 | { |
| 50 | // advance to find a possible match at or after cur in the buffer |
| 51 | txt_ = buf_ + cur_; |
| 52 | if ((this->*adv_)(cur_)) |
| 53 | { |
| 54 | if (pat_->lbk_ > 0) |
| 55 | { |
| 56 | // go back over lookback chars (never includes \n) from cur-1 back to txt (at most) |
| 57 | const char *s = buf_ + cur_; |
| 58 | if (s > txt_) |
| 59 | { |
| 60 | size_t n = std::min<size_t>(pat_->lbk_ == 0xffff ? SIZE_MAX : pat_->lbk_, s - txt_); |
| 61 | while (n-- > 0 && pat_->cbk_.test(static_cast<unsigned char>(*--s))) |
| 62 | ++retry; |
| 63 | cur_ -= retry; |
| 64 | // don't retry at minimal look back distances that are too short for pattern to match |
| 65 | if (retry > pat_->lbm_) |
| 66 | retry -= pat_->lbm_; |
| 67 | else |
| 68 | retry = 0; |
| 69 | } |
| 70 | } |
| 71 | else if (pat_->one_) |
| 72 | { |
| 73 | // one string match, no need to perform a regex match |
| 74 | size_t k = cur_ + pat_->len_; |
| 75 | int ch = k < end_ ? static_cast<unsigned char>(buf_[k]) : EOF; |
| 76 | if (!opt_.W || (at_wb() && (at_end() || at_we(ch, k)))) |
| 77 | { |
| 78 | txt_ = buf_ + cur_; |
| 79 | len_ = pat_->len_; |
| 80 | set_current(k); |
| 81 | return cap_ = 1; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | set_current(cur_); |
| 86 | } |
| 87 | scan: |
| 88 | txt_ = buf_ + cur_; |
| 89 | #if !defined(WITH_NO_INDENT) |
| 90 | mrk_ = false; |
| 91 | ind_ = pos_; // ind scans input in buf[] in newline() up to pos - 1 |
| 92 | col_ = 0; // count columns for indent matching |
| 93 | #endif |
| 94 | find: |
| 95 | int ch = got_; |
| 96 | bool bol = at_bol(); // at begin of line? |
| 97 | #if !defined(WITH_NO_CODEGEN) |
| 98 | if (pat_->fsm_ != NULL) |
| 99 | fsm_.ch = ch; |