Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb_re2::Prog Class Reference
Collaboration diagram for duckdb_re2::Prog:

Classes

class  Inst

Public Types

enum  Anchor { kUnanchored , kAnchored }
enum  MatchKind { kFirstMatch , kLongestMatch , kFullMatch , kManyMatch }
using DFAStateCallback = std::function<void(const int* next, bool match)>

Public Member Functions

Inst * inst (int id)
int start ()
void set_start (int start)
int start_unanchored ()
void set_start_unanchored (int start)
int size ()
bool reversed ()
void set_reversed (bool reversed)
int list_count ()
int inst_count (InstOp op)
uint16_t * list_heads ()
size_t bit_state_text_max_size ()
int64_t dfa_mem ()
void set_dfa_mem (int64_t dfa_mem)
bool anchor_start ()
void set_anchor_start (bool b)
bool anchor_end ()
void set_anchor_end (bool b)
int bytemap_range ()
const uint8_t * bytemap ()
bool can_prefix_accel ()
const void * PrefixAccel (const void *data, size_t size)
void ConfigurePrefixAccel (const std::string &prefix, bool prefix_foldcase)
const void * PrefixAccel_ShiftDFA (const void *data, size_t size)
const void * PrefixAccel_FrontAndBack (const void *data, size_t size)
std::string Dump ()
std::string DumpUnanchored ()
std::string DumpByteMap ()
bool SearchNFA (const StringPiece &text, const StringPiece &context, Anchor anchor, MatchKind kind, StringPiece *match, int nmatch)
bool SearchDFA (const StringPiece &text, const StringPiece &context, Anchor anchor, MatchKind kind, StringPiece *match0, bool *failed, SparseSet *matches)
int BuildEntireDFA (MatchKind kind, const DFAStateCallback &cb)
void ComputeByteMap ()
void Optimize ()
bool IsOnePass ()
bool SearchOnePass (const StringPiece &text, const StringPiece &context, Anchor anchor, MatchKind kind, StringPiece *match, int nmatch)
bool CanBitState ()
bool SearchBitState (const StringPiece &text, const StringPiece &context, Anchor anchor, MatchKind kind, StringPiece *match, int nmatch)
bool UnsafeSearchBacktrack (const StringPiece &text, const StringPiece &context, Anchor anchor, MatchKind kind, StringPiece *match, int nmatch)
bool PossibleMatchRange (std::string *min, std::string *max, int maxlen)
void Fanout (SparseArray< int > *fanout)
void Flatten ()
void MarkSuccessors (SparseArray< int > *rootmap, SparseArray< int > *predmap, std::vector< std::vector< int > > *predvec, SparseSet *reachable, std::vector< int > *stk)
void MarkDominator (int root, SparseArray< int > *rootmap, SparseArray< int > *predmap, std::vector< std::vector< int > > *predvec, SparseSet *reachable, std::vector< int > *stk)
void EmitList (int root, SparseArray< int > *rootmap, std::vector< Inst > *flat, SparseSet *reachable, std::vector< int > *stk)
void ComputeHints (std::vector< Inst > *flat, int begin, int end)

Static Public Member Functions

static uint32_t EmptyFlags (const StringPiece &context, const char *p)
static bool IsWordChar (uint8_t c)
static Prog * CompileSet (Regexp *re, RE2::Anchor anchor, int64_t max_mem)
static void TESTING_ONLY_set_dfa_should_bail_when_slow (bool b)

Static Public Attributes

static const int kMaxOnePassCapture = 5

Private Member Functions

DFA * GetDFA (MatchKind kind)
void DeleteDFA (DFA *dfa)
 Prog (const Prog &)=delete
Prog & operator= (const Prog &)=delete

Private Attributes

bool anchor_start_
bool anchor_end_
bool reversed_
bool did_flatten_
bool did_onepass_
int start_
int start_unanchored_
int size_
int bytemap_range_
bool prefix_foldcase_
size_t prefix_size_
union { 
   uint64_t *   prefix_dfa_ 
   struct { 
      int   prefix_front_ 
      int   prefix_back_ 
   }   prefix_front_back
}; 
int list_count_
int inst_count_ [kNumInst]
PODArray< uint16_t > list_heads_
size_t bit_state_text_max_size_
PODArray< Inst > inst_
PODArray< uint8_t > onepass_nodes_
int64_t dfa_mem_
DFA * dfa_first_
DFA * dfa_longest_
uint8_t bytemap_ [256]
std::once_flag dfa_first_once_
std::once_flag dfa_longest_once_

Friends

class Compiler

Member Enumeration Documentation

◆ Anchor

enum duckdb_re2::Prog::Anchor
459943 {
459944 kUnanchored, // match anywhere
459945 kAnchored, // match only starting at beginning of text
459946 };

◆ MatchKind

enum duckdb_re2::Prog::MatchKind
459960 {
459961 kFirstMatch, // like Perl, PCRE
459962 kLongestMatch, // like egrep or POSIX
459963 kFullMatch, // match only entire text; implies anchor==kAnchored
459964 kManyMatch // for SearchDFA, records set of matches
459965 };

Constructor & Destructor Documentation

◆ Prog()

duckdb_re2::Prog::Prog ( )
470492 : anchor_start_(false),
470493 anchor_end_(false),
470494 reversed_(false),
470495 did_flatten_(false),
470496 did_onepass_(false),
470497 start_(0),
470498 start_unanchored_(0),
470499 size_(0),
470500 bytemap_range_(0),
470501 prefix_foldcase_(false),
470502 prefix_size_(0),
470503 list_count_(0),
470504 bit_state_text_max_size_(0),
470505 dfa_mem_(0),
470506 dfa_first_(NULL),
470507 dfa_longest_(NULL) {
470508}

◆ ~Prog()

duckdb_re2::Prog::~Prog ( )
470510 {
470511 DeleteDFA(dfa_longest_);
470512 DeleteDFA(dfa_first_);
470513 if (prefix_foldcase_)
470514 delete[] prefix_dfa_;
470515}

Member Function Documentation

◆ inst()

Inst * duckdb_re2::Prog::inst ( int id)
inline
459967{ return &inst_[id]; }

◆ start()

int duckdb_re2::Prog::start ( )
inline
459968{ return start_; }

◆ set_start()

void duckdb_re2::Prog::set_start ( int start)
inline
459969{ start_ = start; }

◆ start_unanchored()

int duckdb_re2::Prog::start_unanchored ( )
inline
459970{ return start_unanchored_; }

◆ set_start_unanchored()

void duckdb_re2::Prog::set_start_unanchored ( int start)
inline
459971{ start_unanchored_ = start; }

◆ size()

int duckdb_re2::Prog::size ( )
inline
459972{ return size_; }

◆ reversed()

bool duckdb_re2::Prog::reversed ( )
inline
459973{ return reversed_; }

◆ set_reversed()

void duckdb_re2::Prog::set_reversed ( bool reversed)
inline
459974{ reversed_ = reversed; }

◆ list_count()

int duckdb_re2::Prog::list_count ( )
inline
459975{ return list_count_; }

◆ inst_count()

int duckdb_re2::Prog::inst_count ( InstOp op)
inline
459976{ return inst_count_[op]; }

◆ list_heads()

uint16_t * duckdb_re2::Prog::list_heads ( )
inline
459977{ return list_heads_.data(); }

◆ bit_state_text_max_size()

size_t duckdb_re2::Prog::bit_state_text_max_size ( )
inline
459978{ return bit_state_text_max_size_; }

◆ dfa_mem()

int64_t duckdb_re2::Prog::dfa_mem ( )
inline
459979{ return dfa_mem_; }

◆ set_dfa_mem()

void duckdb_re2::Prog::set_dfa_mem ( int64_t dfa_mem)
inline
459980{ dfa_mem_ = dfa_mem; }

◆ anchor_start()

bool duckdb_re2::Prog::anchor_start ( )
inline
459981{ return anchor_start_; }

◆ set_anchor_start()

void duckdb_re2::Prog::set_anchor_start ( bool b)
inline
459982{ anchor_start_ = b; }

◆ anchor_end()

bool duckdb_re2::Prog::anchor_end ( )
inline
459983{ return anchor_end_; }

◆ set_anchor_end()

void duckdb_re2::Prog::set_anchor_end ( bool b)
inline
459984{ anchor_end_ = b; }

◆ bytemap_range()

int duckdb_re2::Prog::bytemap_range ( )
inline
459985{ return bytemap_range_; }

◆ bytemap()

const uint8_t * duckdb_re2::Prog::bytemap ( )
inline
459986{ return bytemap_; }

◆ can_prefix_accel()

bool duckdb_re2::Prog::can_prefix_accel ( )
inline
459987{ return prefix_size_ != 0; }

◆ PrefixAccel()

const void * duckdb_re2::Prog::PrefixAccel ( const void * data,
size_t size )
inline
459991 {
459992 DCHECK(can_prefix_accel());
459993 if (prefix_foldcase_) {
459994 return PrefixAccel_ShiftDFA(data, size);
459995 } else if (prefix_size_ != 1) {
459996 return PrefixAccel_FrontAndBack(data, size);
459997 } else {
459998 return memchr(data, prefix_front_back.prefix_front_, size);
459999 }
460000 }

◆ ConfigurePrefixAccel()

void duckdb_re2::Prog::ConfigurePrefixAccel ( const std::string & prefix,
bool prefix_foldcase )
471398 {
471399 prefix_foldcase_ = prefix_foldcase;
471400 prefix_size_ = prefix.size();
471401 if (prefix_foldcase_) {
471402 // Use PrefixAccel_ShiftDFA().
471403 // ... and no more than nine bytes of the prefix. (See above for details.)
471404 prefix_size_ = std::min(prefix_size_, kShiftDFAFinal);
471405 prefix_dfa_ = BuildShiftDFA(prefix.substr(0, prefix_size_));
471406 } else if (prefix_size_ != 1) {
471407 // Use PrefixAccel_FrontAndBack().
471408 prefix_front_back.prefix_front_ = prefix.front();
471409 prefix_front_back.prefix_back_ = prefix.back();
471410 } else {
471411 // Use memchr(3).
471412 prefix_front_back.prefix_front_ = prefix.front();
471413 }
471414}

◆ PrefixAccel_ShiftDFA()

const void * duckdb_re2::Prog::PrefixAccel_ShiftDFA ( const void * data,
size_t size )
471416 {
471417 if (size < prefix_size_)
471418 return NULL;
471419
471420 uint64_t curr = 0;
471421
471422 // At the time of writing, rough benchmarks on a Broadwell machine showed
471423 // that this unroll factor (i.e. eight) achieves a speedup factor of two.
471424 if (size >= 8) {
471425 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
471426 const uint8_t* endp = p + (size&~7);
471427 do {
471428 uint8_t b0 = p[0];
471429 uint8_t b1 = p[1];
471430 uint8_t b2 = p[2];
471431 uint8_t b3 = p[3];
471432 uint8_t b4 = p[4];
471433 uint8_t b5 = p[5];
471434 uint8_t b6 = p[6];
471435 uint8_t b7 = p[7];
471436
471437 uint64_t next0 = prefix_dfa_[b0];
471438 uint64_t next1 = prefix_dfa_[b1];
471439 uint64_t next2 = prefix_dfa_[b2];
471440 uint64_t next3 = prefix_dfa_[b3];
471441 uint64_t next4 = prefix_dfa_[b4];
471442 uint64_t next5 = prefix_dfa_[b5];
471443 uint64_t next6 = prefix_dfa_[b6];
471444 uint64_t next7 = prefix_dfa_[b7];
471445
471446 uint64_t curr0 = next0 >> (curr & 63);
471447 uint64_t curr1 = next1 >> (curr0 & 63);
471448 uint64_t curr2 = next2 >> (curr1 & 63);
471449 uint64_t curr3 = next3 >> (curr2 & 63);
471450 uint64_t curr4 = next4 >> (curr3 & 63);
471451 uint64_t curr5 = next5 >> (curr4 & 63);
471452 uint64_t curr6 = next6 >> (curr5 & 63);
471453 uint64_t curr7 = next7 >> (curr6 & 63);
471454
471455 if ((curr7 & 63) == kShiftDFAFinal * 6) {
471456 // At the time of writing, using the same masking subexpressions from
471457 // the preceding lines caused Clang to clutter the hot loop computing
471458 // them - even though they aren't actually needed for shifting! Hence
471459 // these rewritten conditions, which achieve a speedup factor of two.
471460 if (((curr7-curr0) & 63) == 0) return p+1-prefix_size_;
471461 if (((curr7-curr1) & 63) == 0) return p+2-prefix_size_;
471462 if (((curr7-curr2) & 63) == 0) return p+3-prefix_size_;
471463 if (((curr7-curr3) & 63) == 0) return p+4-prefix_size_;
471464 if (((curr7-curr4) & 63) == 0) return p+5-prefix_size_;
471465 if (((curr7-curr5) & 63) == 0) return p+6-prefix_size_;
471466 if (((curr7-curr6) & 63) == 0) return p+7-prefix_size_;
471467 if (((curr7-curr7) & 63) == 0) return p+8-prefix_size_;
471468 }
471469
471470 curr = curr7;
471471 p += 8;
471472 } while (p != endp);
471473 data = p;
471474 size = size&7;
471475 }
471476
471477 const uint8_t* p = reinterpret_cast<const uint8_t*>(data);
471478 const uint8_t* endp = p + size;
471479 while (p != endp) {
471480 uint8_t b = *p++;
471481 uint64_t next = prefix_dfa_[b];
471482 curr = next >> (curr & 63);
471483 if ((curr & 63) == kShiftDFAFinal * 6)
471484 return p-prefix_size_;
471485 }
471486 return NULL;
471487}
::uint8_t uint8_t
::uint64_t uint64_t

◆ PrefixAccel_FrontAndBack()

const void * duckdb_re2::Prog::PrefixAccel_FrontAndBack ( const void * data,
size_t size )
471513 {
471514 DCHECK_GE(prefix_size_, 2);
471515 if (size < prefix_size_)
471516 return NULL;
471517 // Don't bother searching the last prefix_size_-1 bytes for prefix_front_.
471518 // This also means that probing for prefix_back_ doesn't go out of bounds.
471519 size -= prefix_size_-1;
471520
471521#if defined(__AVX2__)
471522 // Use AVX2 to look for prefix_front_ and prefix_back_ 32 bytes at a time.
471523 if (size >= sizeof(__m256i)) {
471524 const __m256i* fp = reinterpret_cast<const __m256i*>(
471525 reinterpret_cast<const char*>(data));
471526 const __m256i* bp = reinterpret_cast<const __m256i*>(
471527 reinterpret_cast<const char*>(data) + prefix_size_-1);
471528 const __m256i* endfp = fp + size/sizeof(__m256i);
471529 const __m256i f_set1 = _mm256_set1_epi8(prefix_front_back.prefix_front_);
471530 const __m256i b_set1 = _mm256_set1_epi8(prefix_front_back.prefix_back_);
471531 do {
471532 const __m256i f_loadu = _mm256_loadu_si256(fp++);
471533 const __m256i b_loadu = _mm256_loadu_si256(bp++);
471534 const __m256i f_cmpeq = _mm256_cmpeq_epi8(f_set1, f_loadu);
471535 const __m256i b_cmpeq = _mm256_cmpeq_epi8(b_set1, b_loadu);
471536 const int fb_testz = _mm256_testz_si256(f_cmpeq, b_cmpeq);
471537 if (fb_testz == 0) { // ZF: 1 means zero, 0 means non-zero.
471538 const __m256i fb_and = _mm256_and_si256(f_cmpeq, b_cmpeq);
471539 const int fb_movemask = _mm256_movemask_epi8(fb_and);
471540 const int fb_ctz = FindLSBSet(fb_movemask);
471541 return reinterpret_cast<const char*>(fp-1) + fb_ctz;
471542 }
471543 } while (fp != endfp);
471544 data = fp;
471545 size = size%sizeof(__m256i);
471546 }
471547#endif
471548
471549 const char* p0 = reinterpret_cast<const char*>(data);
471550 for (const char* p = p0;; p++) {
471551 DCHECK_GE(size, static_cast<size_t>(p-p0));
471552 p = reinterpret_cast<const char*>(memchr(p, prefix_front_back.prefix_front_, size - (p-p0)));
471553 if (p == NULL || p[prefix_size_-1] == prefix_front_back.prefix_back_)
471554 return p;
471555 }
471556}
cv::Point2i p0

◆ Dump()

std::string duckdb_re2::Prog::Dump ( )
470549 {
470550 if (did_flatten_)
470551 return FlattenedProgToString(this, start_);
470552
470553 Workq q(size_);
470554 AddToQueue(&q, start_);
470555 return ProgToString(this, &q);
470556}

◆ DumpUnanchored()

std::string duckdb_re2::Prog::DumpUnanchored ( )
470558 {
470559 if (did_flatten_)
470560 return FlattenedProgToString(this, start_unanchored_);
470561
470562 Workq q(size_);
470563 AddToQueue(&q, start_unanchored_);
470564 return ProgToString(this, &q);
470565}

◆ DumpByteMap()

std::string duckdb_re2::Prog::DumpByteMap ( )
470567 {
470568 std::string map;
470569 for (int c = 0; c < 256; c++) {
470570 int b = bytemap_[c];
470571 int lo = c;
470572 while (c < 256-1 && bytemap_[c+1] == b)
470573 c++;
470574 int hi = c;
470575 map += StringPrintf("[%02x-%02x] -> %d\n", lo, hi, b);
470576 }
470577 return map;
470578}

◆ EmptyFlags()

uint32_t duckdb_re2::Prog::EmptyFlags ( const StringPiece & context,
const char * p )
static
470670 {
470671 int flags = 0;
470672
470673 // ^ and \A
470674 if (p == text.data())
470675 flags |= kEmptyBeginText | kEmptyBeginLine;
470676 else if (p[-1] == '\n')
470677 flags |= kEmptyBeginLine;
470678
470679 // $ and \z
470680 if (p == text.data() + text.size())
470681 flags |= kEmptyEndText | kEmptyEndLine;
470682 else if (p < text.data() + text.size() && p[0] == '\n')
470683 flags |= kEmptyEndLine;
470684
470685 // \b and \B
470686 if (p == text.data() && p == text.data() + text.size()) {
470687 // no word boundary here
470688 } else if (p == text.data()) {
470689 if (IsWordChar(p[0]))
470690 flags |= kEmptyWordBoundary;
470691 } else if (p == text.data() + text.size()) {
470692 if (IsWordChar(p[-1]))
470693 flags |= kEmptyWordBoundary;
470694 } else {
470695 if (IsWordChar(p[-1]) != IsWordChar(p[0]))
470696 flags |= kEmptyWordBoundary;
470697 }
470698 if (!(flags & kEmptyWordBoundary))
470699 flags |= kEmptyNonWordBoundary;
470700
470701 return flags;
470702}

◆ IsWordChar()

bool duckdb_re2::Prog::IsWordChar ( uint8_t c)
inlinestatic
460029 {
460030 return ('A' <= c && c <= 'Z') ||
460031 ('a' <= c && c <= 'z') ||
460032 ('0' <= c && c <= '9') ||
460033 c == '_';
460034 }

◆ SearchNFA()

bool duckdb_re2::Prog::SearchNFA ( const StringPiece & text,
const StringPiece & context,
Anchor anchor,
MatchKind kind,
StringPiece * match,
int nmatch )
465832 {
465833
465834 NFA nfa(this);
465835 StringPiece sp;
465836 if (kind == kFullMatch) {
465837 anchor = kAnchored;
465838 if (nmatch == 0) {
465839 match = &sp;
465840 nmatch = 1;
465841 }
465842 }
465843 if (!nfa.Search(text, context, anchor == kAnchored, kind != kFirstMatch, match, nmatch))
465844 return false;
465845 if (kind == kFullMatch && EndPtr(match[0]) != EndPtr(text))
465846 return false;
465847 return true;
465848}

◆ SearchDFA()

bool duckdb_re2::Prog::SearchDFA ( const StringPiece & text,
const StringPiece & context,
Anchor anchor,
MatchKind kind,
StringPiece * match0,
bool * failed,
SparseSet * matches )
464167 {
464168 *failed = false;
464169
464170 StringPiece context = const_context;
464171 if (context.data() == NULL)
464172 context = text;
464173 bool caret = anchor_start();
464174 bool dollar = anchor_end();
464175 if (reversed_) {
464176 using std::swap;
464177 swap(caret, dollar);
464178 }
464179 if (caret && BeginPtr(context) != BeginPtr(text))
464180 return false;
464181 if (dollar && EndPtr(context) != EndPtr(text))
464182 return false;
464183
464184 // Handle full match by running an anchored longest match
464185 // and then checking if it covers all of text.
464186 bool anchored = anchor == kAnchored || anchor_start() || kind == kFullMatch;
464187 bool endmatch = false;
464188 if (kind == kManyMatch) {
464189 // This is split out in order to avoid clobbering kind.
464190 } else if (kind == kFullMatch || anchor_end()) {
464191 endmatch = true;
464192 kind = kLongestMatch;
464193 }
464194
464195 // If the caller doesn't care where the match is (just whether one exists),
464196 // then we can stop at the very first match we find, the so-called
464197 // "earliest match".
464198 bool want_earliest_match = false;
464199 if (kind == kManyMatch) {
464200 // This is split out in order to avoid clobbering kind.
464201 if (matches == NULL) {
464202 want_earliest_match = true;
464203 }
464204 } else if (match0 == NULL && !endmatch) {
464205 want_earliest_match = true;
464206 kind = kLongestMatch;
464207 }
464208
464209 DFA* dfa = GetDFA(kind);
464210 const char* ep;
464211 bool matched = dfa->Search(text, context, anchored,
464212 want_earliest_match, !reversed_,
464213 failed, &ep, matches);
464214 if (*failed) {
464215 hooks::GetDFASearchFailureHook()({
464216 // Nothing yet...
464217 });
464218 return false;
464219 }
464220 if (!matched)
464221 return false;
464222 if (endmatch && ep != (reversed_ ? text.data() : text.data() + text.size()))
464223 return false;
464224
464225 // If caller cares, record the boundary of the match.
464226 // We only know where it ends, so use the boundary of text
464227 // as the beginning.
464228 if (match0) {
464229 if (reversed_)
464230 *match0 =
464231 StringPiece(ep, static_cast<size_t>(text.data() + text.size() - ep));
464232 else
464233 *match0 =
464234 StringPiece(text.data(), static_cast<size_t>(ep - text.data()));
464235 }
464236 return true;
464237}
void swap(Mat &a, Mat &b)

◆ BuildEntireDFA()

int duckdb_re2::Prog::BuildEntireDFA ( MatchKind kind,
const DFAStateCallback & cb )
464308 {
464309 return GetDFA(kind)->BuildAllStates(cb);
464310}

◆ ComputeByteMap()

void duckdb_re2::Prog::ComputeByteMap ( )
470829 {
470830 // Fill in bytemap with byte classes for the program.
470831 // Ranges of bytes that are treated indistinguishably
470832 // will be mapped to a single byte class.
470833 ByteMapBuilder builder;
470834
470835 // Don't repeat the work for ^ and $.
470836 bool marked_line_boundaries = false;
470837 // Don't repeat the work for \b and \B.
470838 bool marked_word_boundaries = false;
470839
470840 for (int id = 0; id < size(); id++) {
470841 Inst* ip = inst(id);
470842 if (ip->opcode() == kInstByteRange) {
470843 int lo = ip->lo();
470844 int hi = ip->hi();
470845 builder.Mark(lo, hi);
470846 if (ip->foldcase() && lo <= 'z' && hi >= 'a') {
470847 int foldlo = lo;
470848 int foldhi = hi;
470849 if (foldlo < 'a')
470850 foldlo = 'a';
470851 if (foldhi > 'z')
470852 foldhi = 'z';
470853 if (foldlo <= foldhi) {
470854 foldlo += 'A' - 'a';
470855 foldhi += 'A' - 'a';
470856 builder.Mark(foldlo, foldhi);
470857 }
470858 }
470859 // If this Inst is not the last Inst in its list AND the next Inst is
470860 // also a ByteRange AND the Insts have the same out, defer the merge.
470861 if (!ip->last() &&
470862 inst(id+1)->opcode() == kInstByteRange &&
470863 ip->out() == inst(id+1)->out())
470864 continue;
470865 builder.Merge();
470866 } else if (ip->opcode() == kInstEmptyWidth) {
470867 if (ip->empty() & (kEmptyBeginLine|kEmptyEndLine) &&
470868 !marked_line_boundaries) {
470869 builder.Mark('\n', '\n');
470870 builder.Merge();
470871 marked_line_boundaries = true;
470872 }
470873 if (ip->empty() & (kEmptyWordBoundary|kEmptyNonWordBoundary) &&
470874 !marked_word_boundaries) {
470875 // We require two batches here: the first for ranges that are word
470876 // characters, the second for ranges that are not word characters.
470877 for (bool isword : {true, false}) {
470878 int j;
470879 for (int i = 0; i < 256; i = j) {
470880 for (j = i + 1; j < 256 &&
470881 Prog::IsWordChar(static_cast<uint8_t>(i)) ==
470882 Prog::IsWordChar(static_cast<uint8_t>(j));
470883 j++)
470884 ;
470885 if (Prog::IsWordChar(static_cast<uint8_t>(i)) == isword)
470886 builder.Mark(i, j - 1);
470887 }
470888 builder.Merge();
470889 }
470890 marked_word_boundaries = true;
470891 }
470892 }
470893 }
470894
470895 builder.Build(bytemap_, &bytemap_range_);
470896
470897 if ((0)) { // For debugging, use trivial bytemap.
470898 LOG(ERROR) << "Using trivial bytemap.";
470899 for (int i = 0; i < 256; i++)
470900 bytemap_[i] = static_cast<uint8_t>(i);
470901 bytemap_range_ = 256;
470902 }
470903}
Definition duckdb.cpp:459830

◆ Optimize()

void duckdb_re2::Prog::Optimize ( )
470607 {
470608 Workq q(size_);
470609
470610 // Eliminate nops. Most are taken out during compilation
470611 // but a few are hard to avoid.
470612 q.clear();
470613 AddToQueue(&q, start_);
470614 for (Workq::iterator i = q.begin(); i != q.end(); ++i) {
470615 int id = *i;
470616
470617 Inst* ip = inst(id);
470618 int j = ip->out();
470619 Inst* jp;
470620 while (j != 0 && (jp=inst(j))->opcode() == kInstNop) {
470621 j = jp->out();
470622 }
470623 ip->set_out(j);
470624 AddToQueue(&q, ip->out());
470625
470626 if (ip->opcode() == kInstAlt) {
470627 j = ip->out1();
470628 while (j != 0 && (jp=inst(j))->opcode() == kInstNop) {
470629 j = jp->out();
470630 }
470631 ip->out1_ = j;
470632 AddToQueue(&q, ip->out1());
470633 }
470634 }
470635
470636 // Insert kInstAltMatch instructions
470637 // Look for
470638 // ip: Alt -> j | k
470639 // j: ByteRange [00-FF] -> ip
470640 // k: Match
470641 // or the reverse (the above is the greedy one).
470642 // Rewrite Alt to AltMatch.
470643 q.clear();
470644 AddToQueue(&q, start_);
470645 for (Workq::iterator i = q.begin(); i != q.end(); ++i) {
470646 int id = *i;
470647 Inst* ip = inst(id);
470648 AddToQueue(&q, ip->out());
470649 if (ip->opcode() == kInstAlt)
470650 AddToQueue(&q, ip->out1());
470651
470652 if (ip->opcode() == kInstAlt) {
470653 Inst* j = inst(ip->out());
470654 Inst* k = inst(ip->out1());
470655 if (j->opcode() == kInstByteRange && j->out() == id &&
470656 j->lo() == 0x00 && j->hi() == 0xFF &&
470657 IsMatch(this, k)) {
470658 ip->set_opcode(kInstAltMatch);
470659 continue;
470660 }
470661 if (IsMatch(this, j) &&
470662 k->opcode() == kInstByteRange && k->out() == id &&
470663 k->lo() == 0x00 && k->hi() == 0xFF) {
470664 ip->set_opcode(kInstAltMatch);
470665 }
470666 }
470667 }
470668}

◆ IsOnePass()

bool duckdb_re2::Prog::IsOnePass ( )
466301 {
466302 if (did_onepass_)
466303 return onepass_nodes_.data() != NULL;
466304 did_onepass_ = true;
466305
466306 if (start() == 0) // no match
466307 return false;
466308
466309 // Steal memory for the one-pass NFA from the overall DFA budget.
466310 // Willing to use at most 1/4 of the DFA budget (heuristic).
466311 // Limit max node count to 65000 as a conservative estimate to
466312 // avoid overflowing 16-bit node index in encoding.
466313 int maxnodes = 2 + inst_count(kInstByteRange);
466314 int statesize = sizeof(uint32_t) + bytemap_range()*sizeof(uint32_t);
466315 if (maxnodes >= 65000 || dfa_mem_ / 4 / statesize < maxnodes)
466316 return false;
466317
466318 // Flood the graph starting at the start state, and check
466319 // that in each reachable state, each possible byte leads
466320 // to a unique next state.
466321 int stacksize = inst_count(kInstCapture) +
466322 inst_count(kInstEmptyWidth) +
466323 inst_count(kInstNop) + 1; // + 1 for start inst
466324 PODArray<InstCond> stack(stacksize);
466325
466326 int size = this->size();
466327 PODArray<int> nodebyid(size); // indexed by ip
466328 memset(nodebyid.data(), 0xFF, size*sizeof nodebyid[0]);
466329
466330 // Originally, nodes was a uint8_t[maxnodes*statesize], but that was
466331 // unnecessarily optimistic: why allocate a large amount of memory
466332 // upfront for a large program when it is unlikely to be one-pass?
466333 std::vector<uint8_t> nodes;
466334
466335 Instq tovisit(size), workq(size);
466336 AddQ(&tovisit, start());
466337 nodebyid[start()] = 0;
466338 int nalloc = 1;
466339 nodes.insert(nodes.end(), statesize, 0);
466340 for (Instq::iterator it = tovisit.begin(); it != tovisit.end(); ++it) {
466341 int id = *it;
466342 int nodeindex = nodebyid[id];
466343 OneState* node = IndexToNode(nodes.data(), statesize, nodeindex);
466344
466345 // Flood graph using manual stack, filling in actions as found.
466346 // Default is none.
466347 for (int b = 0; b < bytemap_range_; b++)
466348 node->action[b] = kImpossible;
466349 node->matchcond = kImpossible;
466350
466351 workq.clear();
466352 bool matched = false;
466353 int nstack = 0;
466354 stack[nstack].id = id;
466355 stack[nstack++].cond = 0;
466356 while (nstack > 0) {
466357 int id = stack[--nstack].id;
466358 uint32_t cond = stack[nstack].cond;
466359
466360 Loop:
466361 Prog::Inst* ip = inst(id);
466362 switch (ip->opcode()) {
466363 default:
466364 LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
466365 break;
466366
466367 case kInstAltMatch:
466368 // TODO(rsc): Ignoring kInstAltMatch optimization.
466369 // Should implement it in this engine, but it's subtle.
466370 DCHECK(!ip->last());
466371 // If already on work queue, (1) is violated: bail out.
466372 if (!AddQ(&workq, id+1))
466373 goto fail;
466374 id = id+1;
466375 goto Loop;
466376
466377 case kInstByteRange: {
466378 int nextindex = nodebyid[ip->out()];
466379 if (nextindex == -1) {
466380 if (nalloc >= maxnodes) {
466381 goto fail;
466382 }
466383 nextindex = nalloc;
466384 AddQ(&tovisit, ip->out());
466385 nodebyid[ip->out()] = nalloc;
466386 nalloc++;
466387 nodes.insert(nodes.end(), statesize, 0);
466388 // Update node because it might have been invalidated.
466389 node = IndexToNode(nodes.data(), statesize, nodeindex);
466390 }
466391 for (int c = ip->lo(); c <= ip->hi(); c++) {
466392 int b = bytemap_[c];
466393 // Skip any bytes immediately after c that are also in b.
466394 while (c < 256-1 && bytemap_[c+1] == b)
466395 c++;
466396 uint32_t act = node->action[b];
466397 uint32_t newact = (nextindex << kIndexShift) | cond;
466398 if (matched)
466399 newact |= kMatchWins;
466400 if ((act & kImpossible) == kImpossible) {
466401 node->action[b] = newact;
466402 } else if (act != newact) {
466403 goto fail;
466404 }
466405 }
466406 if (ip->foldcase()) {
466407 Rune lo = std::max<Rune>(ip->lo(), 'a') + 'A' - 'a';
466408 Rune hi = std::min<Rune>(ip->hi(), 'z') + 'A' - 'a';
466409 for (int c = lo; c <= hi; c++) {
466410 int b = bytemap_[c];
466411 // Skip any bytes immediately after c that are also in b.
466412 while (c < 256-1 && bytemap_[c+1] == b)
466413 c++;
466414 uint32_t act = node->action[b];
466415 uint32_t newact = (nextindex << kIndexShift) | cond;
466416 if (matched)
466417 newact |= kMatchWins;
466418 if ((act & kImpossible) == kImpossible) {
466419 node->action[b] = newact;
466420 } else if (act != newact) {
466421 goto fail;
466422 }
466423 }
466424 }
466425
466426 if (ip->last())
466427 break;
466428 // If already on work queue, (1) is violated: bail out.
466429 if (!AddQ(&workq, id+1))
466430 goto fail;
466431 id = id+1;
466432 goto Loop;
466433 }
466434
466435 case kInstCapture:
466436 case kInstEmptyWidth:
466437 case kInstNop:
466438 if (!ip->last()) {
466439 // If already on work queue, (1) is violated: bail out.
466440 if (!AddQ(&workq, id+1))
466441 goto fail;
466442 stack[nstack].id = id+1;
466443 stack[nstack++].cond = cond;
466444 }
466445
466446 if (ip->opcode() == kInstCapture && ip->cap() < kMaxCap)
466447 cond |= (1 << kCapShift) << ip->cap();
466448 if (ip->opcode() == kInstEmptyWidth)
466449 cond |= ip->empty();
466450
466451 // kInstCapture and kInstNop always proceed to ip->out().
466452 // kInstEmptyWidth only sometimes proceeds to ip->out(),
466453 // but as a conservative approximation we assume it always does.
466454 // We could be a little more precise by looking at what c
466455 // is, but that seems like overkill.
466456
466457 // If already on work queue, (1) is violated: bail out.
466458 if (!AddQ(&workq, ip->out())) {
466459 goto fail;
466460 }
466461 id = ip->out();
466462 goto Loop;
466463
466464 case kInstMatch:
466465 if (matched) {
466466 // (3) is violated
466467 goto fail;
466468 }
466469 matched = true;
466470 node->matchcond = cond;
466471
466472 if (ip->last())
466473 break;
466474 // If already on work queue, (1) is violated: bail out.
466475 if (!AddQ(&workq, id+1))
466476 goto fail;
466477 id = id+1;
466478 goto Loop;
466479
466480 case kInstFail:
466481 break;
466482 }
466483 }
466484 }
466485
466486 dfa_mem_ -= nalloc*statesize;
466487 onepass_nodes_ = PODArray<uint8_t>(nalloc*statesize);
466488 memmove(onepass_nodes_.data(), nodes.data(), nalloc*statesize);
466489 return true;
466490
466491fail:
466492 return false;
466493}
::uint32_t uint32_t

◆ SearchOnePass()

bool duckdb_re2::Prog::SearchOnePass ( const StringPiece & text,
const StringPiece & context,
Anchor anchor,
MatchKind kind,
StringPiece * match,
int nmatch )
466133 {
466134 if (anchor != kAnchored && kind != kFullMatch) {
466135 LOG(DFATAL) << "Cannot use SearchOnePass for unanchored matches.";
466136 return false;
466137 }
466138
466139 // Make sure we have at least cap[1],
466140 // because we use it to tell if we matched.
466141 int ncap = 2*nmatch;
466142 if (ncap < 2)
466143 ncap = 2;
466144
466145 const char* cap[kMaxCap];
466146 for (int i = 0; i < ncap; i++)
466147 cap[i] = NULL;
466148
466149 const char* matchcap[kMaxCap];
466150 for (int i = 0; i < ncap; i++)
466151 matchcap[i] = NULL;
466152
466153 StringPiece context = const_context;
466154 if (context.data() == NULL)
466155 context = text;
466156 if (anchor_start() && BeginPtr(context) != BeginPtr(text))
466157 return false;
466158 if (anchor_end() && EndPtr(context) != EndPtr(text))
466159 return false;
466160 if (anchor_end())
466161 kind = kFullMatch;
466162
466163 uint8_t* nodes = onepass_nodes_.data();
466164 int statesize = sizeof(uint32_t) + bytemap_range()*sizeof(uint32_t);
466165
466166 // start() is always mapped to the zeroth OneState.
466167 OneState* state = IndexToNode(nodes, statesize, 0);
466168 uint8_t* bytemap = bytemap_;
466169 const char* bp = text.data();
466170 const char* ep = text.data() + text.size();
466171 const char* p;
466172 bool matched = false;
466173 matchcap[0] = bp;
466174 cap[0] = bp;
466175 uint32_t nextmatchcond = state->matchcond;
466176 for (p = bp; p < ep; p++) {
466177 int c = bytemap[*p & 0xFF];
466178 uint32_t matchcond = nextmatchcond;
466179 uint32_t cond = state->action[c];
466180
466181 // Determine whether we can reach act->next.
466182 // If so, advance state and nextmatchcond.
466183 if ((cond & kEmptyAllFlags) == 0 || Satisfy(cond, context, p)) {
466184 uint32_t nextindex = cond >> kIndexShift;
466185 state = IndexToNode(nodes, statesize, nextindex);
466186 nextmatchcond = state->matchcond;
466187 } else {
466188 state = NULL;
466189 nextmatchcond = kImpossible;
466190 }
466191
466192 // This code section is carefully tuned.
466193 // The goto sequence is about 10% faster than the
466194 // obvious rewrite as a large if statement in the
466195 // ASCIIMatchRE2 and DotMatchRE2 benchmarks.
466196
466197 // Saving the match capture registers is expensive.
466198 // Is this intermediate match worth thinking about?
466199
466200 // Not if we want a full match.
466201 if (kind == kFullMatch)
466202 goto skipmatch;
466203
466204 // Not if it's impossible.
466205 if (matchcond == kImpossible)
466206 goto skipmatch;
466207
466208 // Not if the possible match is beaten by the certain
466209 // match at the next byte. When this test is useless
466210 // (e.g., HTTPPartialMatchRE2) it slows the loop by
466211 // about 10%, but when it avoids work (e.g., DotMatchRE2),
466212 // it cuts the loop execution by about 45%.
466213 if ((cond & kMatchWins) == 0 && (nextmatchcond & kEmptyAllFlags) == 0)
466214 goto skipmatch;
466215
466216 // Finally, the match conditions must be satisfied.
466217 if ((matchcond & kEmptyAllFlags) == 0 || Satisfy(matchcond, context, p)) {
466218 for (int i = 2; i < 2*nmatch; i++)
466219 matchcap[i] = cap[i];
466220 if (nmatch > 1 && (matchcond & kCapMask))
466221 ApplyCaptures(matchcond, p, matchcap, ncap);
466222 matchcap[1] = p;
466223 matched = true;
466224
466225 // If we're in longest match mode, we have to keep
466226 // going and see if we find a longer match.
466227 // In first match mode, we can stop if the match
466228 // takes priority over the next state for this input byte.
466229 // That bit is per-input byte and thus in cond, not matchcond.
466230 if (kind == kFirstMatch && (cond & kMatchWins))
466231 goto done;
466232 }
466233
466234 skipmatch:
466235 if (state == NULL)
466236 goto done;
466237 if ((cond & kCapMask) && nmatch > 1)
466238 ApplyCaptures(cond, p, cap, ncap);
466239 }
466240
466241 // Look for match at end of input.
466242 {
466243 uint32_t matchcond = state->matchcond;
466244 if (matchcond != kImpossible &&
466245 ((matchcond & kEmptyAllFlags) == 0 || Satisfy(matchcond, context, p))) {
466246 if (nmatch > 1 && (matchcond & kCapMask))
466247 ApplyCaptures(matchcond, p, cap, ncap);
466248 for (int i = 2; i < ncap; i++)
466249 matchcap[i] = cap[i];
466250 matchcap[1] = p;
466251 matched = true;
466252 }
466253 }
466254
466255done:
466256 if (!matched)
466257 return false;
466258 for (int i = 0; i < nmatch; i++)
466259 match[i] =
466260 StringPiece(matchcap[2 * i],
466261 static_cast<size_t>(matchcap[2 * i + 1] - matchcap[2 * i]));
466262 return true;
466263}

◆ CanBitState()

bool duckdb_re2::Prog::CanBitState ( )
inline
460098{ return list_heads_.data() != NULL; }

◆ SearchBitState()

bool duckdb_re2::Prog::SearchBitState ( const StringPiece & text,
const StringPiece & context,
Anchor anchor,
MatchKind kind,
StringPiece * match,
int nmatch )
460572 {
460573 // If full match, we ask for an anchored longest match
460574 // and then check that match[0] == text.
460575 // So make sure match[0] exists.
460576 StringPiece sp0;
460577 if (kind == kFullMatch) {
460578 anchor = kAnchored;
460579 if (nmatch < 1) {
460580 match = &sp0;
460581 nmatch = 1;
460582 }
460583 }
460584
460585 // Run the search.
460586 BitState b(this);
460587 bool anchored = anchor == kAnchored;
460588 bool longest = kind != kFirstMatch;
460589 if (!b.Search(text, context, anchored, longest, match, nmatch))
460590 return false;
460591 if (kind == kFullMatch && EndPtr(match[0]) != EndPtr(text))
460592 return false;
460593 return true;
460594}

◆ PossibleMatchRange()

bool duckdb_re2::Prog::PossibleMatchRange ( std::string * min,
std::string * max,
int maxlen )
464451 {
464452 // Have to use dfa_longest_ to get all strings for full matches.
464453 // For example, (a|aa) never matches aa in first-match mode.
464454 return GetDFA(kLongestMatch)->PossibleMatchRange(min, max, maxlen);
464455}
static softfloat min()
static softfloat max()

◆ Fanout()

void duckdb_re2::Prog::Fanout ( SparseArray< int > * fanout)
465856 {
465857 DCHECK_EQ(fanout->max_size(), size());
465858 SparseSet reachable(size());
465859 fanout->clear();
465860 fanout->set_new(start(), 0);
465861 for (SparseArray<int>::iterator i = fanout->begin(); i != fanout->end(); ++i) {
465862 int* count = &i->value();
465863 reachable.clear();
465864 reachable.insert(i->index());
465865 for (SparseSet::iterator j = reachable.begin(); j != reachable.end(); ++j) {
465866 int id = *j;
465867 Prog::Inst* ip = inst(id);
465868 switch (ip->opcode()) {
465869 default:
465870 LOG(DFATAL) << "unhandled " << ip->opcode() << " in Prog::Fanout()";
465871 break;
465872
465873 case kInstByteRange:
465874 if (!ip->last())
465875 reachable.insert(id+1);
465876
465877 (*count)++;
465878 if (!fanout->has_index(ip->out())) {
465879 fanout->set_new(ip->out(), 0);
465880 }
465881 break;
465882
465883 case kInstAltMatch:
465884 DCHECK(!ip->last());
465885 reachable.insert(id+1);
465886 break;
465887
465888 case kInstCapture:
465889 case kInstEmptyWidth:
465890 case kInstNop:
465891 if (!ip->last())
465892 reachable.insert(id+1);
465893
465894 reachable.insert(ip->out());
465895 break;
465896
465897 case kInstMatch:
465898 if (!ip->last())
465899 reachable.insert(id+1);
465900 break;
465901
465902 case kInstFail:
465903 break;
465904 }
465905 }
465906 }
465907}

◆ CompileSet()

Prog * duckdb_re2::Prog::CompileSet ( Regexp * re,
RE2::Anchor anchor,
int64_t max_mem )
static
462118 {
462119 return Compiler::CompileSet(re, anchor, max_mem);
462120}

◆ Flatten()

void duckdb_re2::Prog::Flatten ( )
470940 {
470941 if (did_flatten_)
470942 return;
470943 did_flatten_ = true;
470944
470945 // Scratch structures. It's important that these are reused by functions
470946 // that we call in loops because they would thrash the heap otherwise.
470947 SparseSet reachable(size());
470948 std::vector<int> stk;
470949 stk.reserve(size());
470950
470951 // First pass: Marks "successor roots" and predecessors.
470952 // Builds the mapping from inst-ids to root-ids.
470953 SparseArray<int> rootmap(size());
470954 SparseArray<int> predmap(size());
470955 std::vector<std::vector<int>> predvec;
470956 MarkSuccessors(&rootmap, &predmap, &predvec, &reachable, &stk);
470957
470958 // Second pass: Marks "dominator roots".
470959 SparseArray<int> sorted(rootmap);
470960 std::sort(sorted.begin(), sorted.end(), sorted.less);
470961 for (SparseArray<int>::const_iterator i = sorted.end() - 1;
470962 i != sorted.begin();
470963 --i) {
470964 if (i->index() != start_unanchored() && i->index() != start())
470965 MarkDominator(i->index(), &rootmap, &predmap, &predvec, &reachable, &stk);
470966 }
470967
470968 // Third pass: Emits "lists". Remaps outs to root-ids.
470969 // Builds the mapping from root-ids to flat-ids.
470970 std::vector<int> flatmap(rootmap.size());
470971 std::vector<Inst> flat;
470972 flat.reserve(size());
470973 for (SparseArray<int>::const_iterator i = rootmap.begin();
470974 i != rootmap.end();
470975 ++i) {
470976 flatmap[i->value()] = static_cast<int>(flat.size());
470977 EmitList(i->index(), &rootmap, &flat, &reachable, &stk);
470978 flat.back().set_last();
470979 // We have the bounds of the "list", so this is the
470980 // most convenient point at which to compute hints.
470981 ComputeHints(&flat, flatmap[i->value()], static_cast<int>(flat.size()));
470982 }
470983
470984 list_count_ = static_cast<int>(flatmap.size());
470985 for (int i = 0; i < kNumInst; i++)
470986 inst_count_[i] = 0;
470987
470988 // Fourth pass: Remaps outs to flat-ids.
470989 // Counts instructions by opcode.
470990 for (int id = 0; id < static_cast<int>(flat.size()); id++) {
470991 Inst* ip = &flat[id];
470992 if (ip->opcode() != kInstAltMatch) // handled in EmitList()
470993 ip->set_out(flatmap[ip->out()]);
470994 inst_count_[ip->opcode()]++;
470995 }
470996
470997#if !defined(NDEBUG)
470998 // Address a `-Wunused-but-set-variable' warning from Clang 13.x.
470999 size_t total = 0;
471000 for (int i = 0; i < kNumInst; i++)
471001 total += inst_count_[i];
471002 CHECK_EQ(total, flat.size());
471003#endif
471004
471005 // Remap start_unanchored and start.
471006 if (start_unanchored() == 0) {
471007 DCHECK_EQ(start(), 0);
471008 } else if (start_unanchored() == start()) {
471009 set_start_unanchored(flatmap[1]);
471010 set_start(flatmap[1]);
471011 } else {
471012 set_start_unanchored(flatmap[1]);
471013 set_start(flatmap[2]);
471014 }
471015
471016 // Finally, replace the old instructions with the new instructions.
471017 size_ = static_cast<int>(flat.size());
471018 inst_ = PODArray<Inst>(size_);
471019 memmove(inst_.data(), flat.data(), size_*sizeof inst_[0]);
471020
471021 // Populate the list heads for BitState.
471022 // 512 instructions limits the memory footprint to 1KiB.
471023 if (size_ <= 512) {
471024 list_heads_ = PODArray<uint16_t>(size_);
471025 // 0xFF makes it more obvious if we try to look up a non-head.
471026 memset(list_heads_.data(), 0xFF, size_*sizeof list_heads_[0]);
471027 for (int i = 0; i < list_count_; ++i)
471028 list_heads_[flatmap[i]] = i;
471029 }
471030
471031 // BitState allocates a bitmap of size list_count_ * (text.size()+1)
471032 // for tracking pairs of possibilities that it has already explored.
471033 const size_t kBitStateBitmapMaxSize = 256*1024; // max size in bits
471034 bit_state_text_max_size_ = kBitStateBitmapMaxSize / list_count_ - 1;
471035}
flat

◆ MarkSuccessors()

void duckdb_re2::Prog::MarkSuccessors ( SparseArray< int > * rootmap,
SparseArray< int > * predmap,
std::vector< std::vector< int > > * predvec,
SparseSet * reachable,
std::vector< int > * stk )
471040 {
471041 // Mark the kInstFail instruction.
471042 rootmap->set_new(0, rootmap->size());
471043
471044 // Mark the start_unanchored and start instructions.
471045 if (!rootmap->has_index(start_unanchored()))
471046 rootmap->set_new(start_unanchored(), rootmap->size());
471047 if (!rootmap->has_index(start()))
471048 rootmap->set_new(start(), rootmap->size());
471049
471050 reachable->clear();
471051 stk->clear();
471052 stk->push_back(start_unanchored());
471053 while (!stk->empty()) {
471054 int id = stk->back();
471055 stk->pop_back();
471056 Loop:
471057 if (reachable->contains(id))
471058 continue;
471059 reachable->insert_new(id);
471060
471061 Inst* ip = inst(id);
471062 switch (ip->opcode()) {
471063 default:
471064 LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
471065 break;
471066
471067 case kInstAltMatch:
471068 case kInstAlt:
471069 // Mark this instruction as a predecessor of each out.
471070 for (int out : {ip->out(), ip->out1()}) {
471071 if (!predmap->has_index(out)) {
471072 predmap->set_new(out, static_cast<int>(predvec->size()));
471073 predvec->emplace_back();
471074 }
471075 (*predvec)[predmap->get_existing(out)].emplace_back(id);
471076 }
471077 stk->push_back(ip->out1());
471078 id = ip->out();
471079 goto Loop;
471080
471081 case kInstByteRange:
471082 case kInstCapture:
471083 case kInstEmptyWidth:
471084 // Mark the out of this instruction as a "root".
471085 if (!rootmap->has_index(ip->out()))
471086 rootmap->set_new(ip->out(), rootmap->size());
471087 id = ip->out();
471088 goto Loop;
471089
471090 case kInstNop:
471091 id = ip->out();
471092 goto Loop;
471093
471094 case kInstMatch:
471095 case kInstFail:
471096 break;
471097 }
471098 }
471099}

◆ MarkDominator()

void duckdb_re2::Prog::MarkDominator ( int root,
SparseArray< int > * rootmap,
SparseArray< int > * predmap,
std::vector< std::vector< int > > * predvec,
SparseSet * reachable,
std::vector< int > * stk )
471104 {
471105 reachable->clear();
471106 stk->clear();
471107 stk->push_back(root);
471108 while (!stk->empty()) {
471109 int id = stk->back();
471110 stk->pop_back();
471111 Loop:
471112 if (reachable->contains(id))
471113 continue;
471114 reachable->insert_new(id);
471115
471116 if (id != root && rootmap->has_index(id)) {
471117 // We reached another "tree" via epsilon transition.
471118 continue;
471119 }
471120
471121 Inst* ip = inst(id);
471122 switch (ip->opcode()) {
471123 default:
471124 LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
471125 break;
471126
471127 case kInstAltMatch:
471128 case kInstAlt:
471129 stk->push_back(ip->out1());
471130 id = ip->out();
471131 goto Loop;
471132
471133 case kInstByteRange:
471134 case kInstCapture:
471135 case kInstEmptyWidth:
471136 break;
471137
471138 case kInstNop:
471139 id = ip->out();
471140 goto Loop;
471141
471142 case kInstMatch:
471143 case kInstFail:
471144 break;
471145 }
471146 }
471147
471148 for (SparseSet::const_iterator i = reachable->begin();
471149 i != reachable->end();
471150 ++i) {
471151 int id = *i;
471152 if (predmap->has_index(id)) {
471153 for (int pred : (*predvec)[predmap->get_existing(id)]) {
471154 if (!reachable->contains(pred)) {
471155 // id has a predecessor that cannot be reached from root!
471156 // Therefore, id must be a "root" too - mark it as such.
471157 if (!rootmap->has_index(id))
471158 rootmap->set_new(id, rootmap->size());
471159 }
471160 }
471161 }
471162 }
471163}

◆ EmitList()

void duckdb_re2::Prog::EmitList ( int root,
SparseArray< int > * rootmap,
std::vector< Inst > * flat,
SparseSet * reachable,
std::vector< int > * stk )
471167 {
471168 reachable->clear();
471169 stk->clear();
471170 stk->push_back(root);
471171 while (!stk->empty()) {
471172 int id = stk->back();
471173 stk->pop_back();
471174 Loop:
471175 if (reachable->contains(id))
471176 continue;
471177 reachable->insert_new(id);
471178
471179 if (id != root && rootmap->has_index(id)) {
471180 // We reached another "tree" via epsilon transition. Emit a kInstNop
471181 // instruction so that the Prog does not become quadratically larger.
471182 flat->emplace_back();
471183 flat->back().set_opcode(kInstNop);
471184 flat->back().set_out(rootmap->get_existing(id));
471185 continue;
471186 }
471187
471188 Inst* ip = inst(id);
471189 switch (ip->opcode()) {
471190 default:
471191 LOG(DFATAL) << "unhandled opcode: " << ip->opcode();
471192 break;
471193
471194 case kInstAltMatch:
471195 flat->emplace_back();
471196 flat->back().set_opcode(kInstAltMatch);
471197 flat->back().set_out(static_cast<int>(flat->size()));
471198 flat->back().out1_ = static_cast<uint32_t>(flat->size())+1;
471199 FALLTHROUGH_INTENDED;
471200
471201 case kInstAlt:
471202 stk->push_back(ip->out1());
471203 id = ip->out();
471204 goto Loop;
471205
471206 case kInstByteRange:
471207 case kInstCapture:
471208 case kInstEmptyWidth:
471209 flat->emplace_back();
471210 memmove(&flat->back(), ip, sizeof *ip);
471211 flat->back().set_out(rootmap->get_existing(ip->out()));
471212 break;
471213
471214 case kInstNop:
471215 id = ip->out();
471216 goto Loop;
471217
471218 case kInstMatch:
471219 case kInstFail:
471220 flat->emplace_back();
471221 memmove(&flat->back(), ip, sizeof *ip);
471222 break;
471223 }
471224 }
471225}

◆ ComputeHints()

void duckdb_re2::Prog::ComputeHints ( std::vector< Inst > * flat,
int begin,
int end )
471235 {
471236 Bitmap256 splits;
471237 int colors[256];
471238
471239 bool dirty = false;
471240 for (int id = end; id >= begin; --id) {
471241 if (id == end ||
471242 (*flat)[id].opcode() != kInstByteRange) {
471243 if (dirty) {
471244 dirty = false;
471245 splits.Clear();
471246 }
471247 splits.Set(255);
471248 colors[255] = id;
471249 // At this point, the [0-255] range is colored with id.
471250 // Thus, hints cannot point beyond id; and if id == end,
471251 // hints that would have pointed to id will be 0 instead.
471252 continue;
471253 }
471254 dirty = true;
471255
471256 // We recolor the [lo-hi] range with id. Note that first ratchets backwards
471257 // from end to the nearest conflict (if any) during recoloring.
471258 int first = end;
471259 auto Recolor = [&](int lo, int hi) {
471260 // Like ByteMapBuilder, we split at lo-1 and at hi.
471261 --lo;
471262
471263 if (0 <= lo && !splits.Test(lo)) {
471264 splits.Set(lo);
471265 int next = splits.FindNextSetBit(lo+1);
471266 colors[lo] = colors[next];
471267 }
471268 if (!splits.Test(hi)) {
471269 splits.Set(hi);
471270 int next = splits.FindNextSetBit(hi+1);
471271 colors[hi] = colors[next];
471272 }
471273
471274 int c = lo+1;
471275 while (c < 256) {
471276 int next = splits.FindNextSetBit(c);
471277 // Ratchet backwards...
471278 first = std::min(first, colors[next]);
471279 // Recolor with id - because it's the new nearest conflict!
471280 colors[next] = id;
471281 if (next == hi)
471282 break;
471283 c = next+1;
471284 }
471285 };
471286
471287 Inst* ip = &(*flat)[id];
471288 int lo = ip->lo();
471289 int hi = ip->hi();
471290 Recolor(lo, hi);
471291 if (ip->foldcase() && lo <= 'z' && hi >= 'a') {
471292 int foldlo = lo;
471293 int foldhi = hi;
471294 if (foldlo < 'a')
471295 foldlo = 'a';
471296 if (foldhi > 'z')
471297 foldhi = 'z';
471298 if (foldlo <= foldhi) {
471299 foldlo += 'A' - 'a';
471300 foldhi += 'A' - 'a';
471301 Recolor(foldlo, foldhi);
471302 }
471303 }
471304
471305 if (first != end) {
471306 uint16_t hint = static_cast<uint16_t>(std::min(first - id, 32767));
471307 ip->byte_range.hint_foldcase_ |= hint<<1;
471308 }
471309 }
471310}
::uint16_t uint16_t

◆ TESTING_ONLY_set_dfa_should_bail_when_slow()

void duckdb_re2::Prog::TESTING_ONLY_set_dfa_should_bail_when_slow ( bool b)
static
462444 {
462445 dfa_should_bail_when_slow = b;
462446}

◆ GetDFA()

DFA * duckdb_re2::Prog::GetDFA ( MatchKind kind)
private
464122 {
464123 // For a forward DFA, half the memory goes to each DFA.
464124 // However, if it is a "many match" DFA, then there is
464125 // no counterpart with which the memory must be shared.
464126 //
464127 // For a reverse DFA, all the memory goes to the
464128 // "longest match" DFA, because RE2 never does reverse
464129 // "first match" searches.
464130 if (kind == kFirstMatch) {
464131 std::call_once(dfa_first_once_, [](Prog* prog) {
464132 prog->dfa_first_ = new DFA(prog, kFirstMatch, prog->dfa_mem_ / 2);
464133 }, this);
464134 return dfa_first_;
464135 } else if (kind == kManyMatch) {
464136 std::call_once(dfa_first_once_, [](Prog* prog) {
464137 prog->dfa_first_ = new DFA(prog, kManyMatch, prog->dfa_mem_);
464138 }, this);
464139 return dfa_first_;
464140 } else {
464141 std::call_once(dfa_longest_once_, [](Prog* prog) {
464142 if (!prog->reversed_)
464143 prog->dfa_longest_ = new DFA(prog, kLongestMatch, prog->dfa_mem_ / 2);
464144 else
464145 prog->dfa_longest_ = new DFA(prog, kLongestMatch, prog->dfa_mem_);
464146 }, this);
464147 return dfa_longest_;
464148 }
464149}

◆ DeleteDFA()

void duckdb_re2::Prog::DeleteDFA ( DFA * dfa)
private
464151 {
464152 delete dfa;
464153}

The documentation for this class was generated from the following file: