MCPcopy Create free account
hub / github.com/Genivia/ugrep / advance_pattern_mink

Method advance_pattern_mink

lib/matcher.cpp:2091–2187  ·  view source on GitHub ↗

Minimal k byte long patterns (MIN>=k) using bitap hashed pairs and PMk+PMx

Source from the content-addressed store, hash-verified

2089
2090/// Minimal k byte long patterns (MIN>=k) using bitap hashed pairs and PMk+PMx
2091bool Matcher::advance_pattern_mink(size_t loc)
2092{
2093 const Pattern::Bitap *tap = pat_->tap_;
2094 const uint16_t min = pat_->min_;
2095 const uint16_t sub = (min < Pattern::Const::PM_M + 2 ? Pattern::Const::PM_M + 2 - min : 0);
2096 const uint32_t mask = 1 << (min - 1);
2097 uint32_t state1 = ~0;
2098 uint32_t state2 = ~0;
2099 while (true)
2100 {
2101 const char *s = buf_ + loc;
2102 const char *e = buf_ + end_ - sub;
2103 uint8_t c0 = static_cast<uint8_t>(*s);
2104 while (s < e)
2105 {
2106 uint8_t c1 = static_cast<uint8_t>(*++s);
2107 state2 = (state1 << 1) | tap[Pattern::bihash(c0, c1)];
2108 c0 = static_cast<uint8_t>(*++s);
2109 state1 = (state2 << 1) | tap[Pattern::bihash(c1, c0)];
2110 if ((state2 & mask) == 0 && pat_->predict_match_min(s - min - 1))
2111 {
2112 size_t k = s - buf_ - min - 1;
2113 set_current(k);
2114 return true;
2115 }
2116 if ((state1 & mask) == 0 && pat_->predict_match_min(s - min))
2117 {
2118 size_t k = s - buf_ - min;
2119 set_current(k);
2120 return true;
2121 }
2122 }
2123 loc = s - buf_;
2124 size_t m = std::min<size_t>(min - 1, loc); // to clamp loc - min + 1
2125 set_current_and_peek_more(loc - m); // clamp loc - min + 1
2126 loc = cur_ + m;
2127 if (loc + sub >= end_ && eof_)
2128 {
2129 // keep going, we may have a match in the last bytes
2130 s = buf_ + loc;
2131 e = buf_ + end_ - 2;
2132 c0 = static_cast<uint8_t>(s[0]);
2133 while (s < e)
2134 {
2135 uint8_t c1 = static_cast<uint8_t>(*++s);
2136 state2 = (state1 << 1) | tap[Pattern::bihash(c0, c1)];
2137 c0 = static_cast<uint8_t>(*++s);
2138 state1 = (state2 << 1) | tap[Pattern::bihash(c1, c0)];
2139 if ((state2 & mask) == 0)
2140 {
2141 size_t k = s - buf_ - min - 1;
2142 set_current(k);
2143 return true;
2144 }
2145 if ((state1 & mask) == 0)
2146 {
2147 size_t k = s - buf_ - min;
2148 set_current(k);

Callers

nothing calls this directly

Calls 1

predict_match_minMethod · 0.80

Tested by

no test coverage detected