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::Compiler Class Reference
Inheritance diagram for duckdb_re2::Compiler:
Collaboration diagram for duckdb_re2::Compiler:

Public Member Functions

Frag PreVisit (Regexp *re, Frag parent_arg, bool *stop)
Frag PostVisit (Regexp *re, Frag parent_arg, Frag pre_arg, Frag *child_args, int nchild_args)
Frag ShortVisit (Regexp *re, Frag parent_arg)
Frag Copy (Frag arg)
Frag Plus (Frag a, bool nongreedy)
Frag Star (Frag a, bool nongreedy)
Frag Quest (Frag a, bool nongreedy)
Frag Capture (Frag a, int n)
Frag Cat (Frag a, Frag b)
Frag Alt (Frag a, Frag b)
Frag NoMatch ()
Frag Match (int32_t id)
Frag Nop ()
Frag ByteRange (int lo, int hi, bool foldcase)
Frag EmptyWidth (EmptyOp op)
int AllocInst (int n)
void BeginRange ()
void AddRuneRange (Rune lo, Rune hi, bool foldcase)
void AddRuneRangeLatin1 (Rune lo, Rune hi, bool foldcase)
void AddRuneRangeUTF8 (Rune lo, Rune hi, bool foldcase)
void Add_80_10ffff ()
int UncachedRuneByteSuffix (uint8_t lo, uint8_t hi, bool foldcase, int next)
int CachedRuneByteSuffix (uint8_t lo, uint8_t hi, bool foldcase, int next)
bool IsCachedRuneByteSuffix (int id)
void AddSuffix (int id)
int AddSuffixRecursive (int root, int id)
Frag FindByteRange (int root, int id)
bool ByteRangeEqual (int id1, int id2)
Frag EndRange ()
Frag Literal (Rune r, bool foldcase)
void Setup (Regexp::ParseFlags flags, int64_t max_mem, RE2::Anchor anchor)
Prog * Finish (Regexp *re)
Frag DotStar ()
Public Member Functions inherited from duckdb_re2::Regexp::Walker< Frag >
virtual Frag PreVisit (Regexp *re, Frag parent_arg, bool *stop)
virtual Frag PostVisit (Regexp *re, Frag parent_arg, Frag pre_arg, Frag *child_args, int nchild_args)
virtual Frag Copy (Frag arg)
Frag Walk (Regexp *re, Frag top_arg)
Frag WalkExponential (Regexp *re, Frag top_arg, int max_visits)
void Reset ()
bool stopped_early ()

Static Public Member Functions

static Prog * Compile (Regexp *re, bool reversed, int64_t max_mem)
static Prog * CompileSet (Regexp *re, RE2::Anchor anchor, int64_t max_mem)

Private Member Functions

 Compiler (const Compiler &)=delete
Compiler & operator= (const Compiler &)=delete

Private Attributes

Prog * prog_
bool failed_
Encoding encoding_
bool reversed_
PODArray< Prog::Inst > inst_
int ninst_
int max_ninst_
int64_t max_mem_
std::unordered_map< uint64_t, int > rune_cache_
Frag rune_range_
RE2::Anchor anchor_

Constructor & Destructor Documentation

◆ Compiler()

duckdb_re2::Compiler::Compiler ( )
explicit
461084 {
461085 prog_ = new Prog();
461086 failed_ = false;
461087 encoding_ = kEncodingUTF8;
461088 reversed_ = false;
461089 ninst_ = 0;
461090 max_ninst_ = 1; // make AllocInst for fail instruction okay
461091 max_mem_ = 0;
461092 int fail = AllocInst(1);
461093 inst_[fail].InitFail();
461094 max_ninst_ = 0; // Caller must change
461095}

◆ ~Compiler()

duckdb_re2::Compiler::~Compiler ( )
461097 {
461098 delete prog_;
461099}

Member Function Documentation

◆ Compile()

Prog * duckdb_re2::Compiler::Compile ( Regexp * re,
bool reversed,
int64_t max_mem )
static
461972 {
461973 Compiler c;
461974 c.Setup(re->parse_flags(), max_mem, RE2::UNANCHORED /* unused */);
461975 c.reversed_ = reversed;
461976
461977 // Simplify to remove things like counted repetitions
461978 // and character classes like \d.
461979 Regexp* sre = re->Simplify();
461980 if (sre == NULL)
461981 return NULL;
461982
461983 // Record whether prog is anchored, removing the anchors.
461984 // (They get in the way of other optimizations.)
461985 bool is_anchor_start = IsAnchorStart(&sre, 0);
461986 bool is_anchor_end = IsAnchorEnd(&sre, 0);
461987
461988 // Generate fragment for entire regexp.
461989 Frag all = c.WalkExponential(sre, Frag(), 2*c.max_ninst_);
461990 sre->Decref();
461991 if (c.failed_)
461992 return NULL;
461993
461994 // Success! Finish by putting Match node at end, and record start.
461995 // Turn off c.reversed_ (if it is set) to force the remaining concatenations
461996 // to behave normally.
461997 c.reversed_ = false;
461998 all = c.Cat(all, c.Match(0));
461999
462000 c.prog_->set_reversed(reversed);
462001 if (c.prog_->reversed()) {
462002 c.prog_->set_anchor_start(is_anchor_end);
462003 c.prog_->set_anchor_end(is_anchor_start);
462004 } else {
462005 c.prog_->set_anchor_start(is_anchor_start);
462006 c.prog_->set_anchor_end(is_anchor_end);
462007 }
462008
462009 c.prog_->set_start(all.begin);
462010 if (!c.prog_->anchor_start()) {
462011 // Also create unanchored version, which starts with a .*? loop.
462012 all = c.Cat(c.DotStar(), all);
462013 }
462014 c.prog_->set_start_unanchored(all.begin);
462015
462016 // Hand ownership of prog_ to caller.
462017 return c.Finish(re);
462018}

◆ CompileSet()

Prog * duckdb_re2::Compiler::CompileSet ( Regexp * re,
RE2::Anchor anchor,
int64_t max_mem )
static
462076 {
462077 Compiler c;
462078 c.Setup(re->parse_flags(), max_mem, anchor);
462079
462080 Regexp* sre = re->Simplify();
462081 if (sre == NULL)
462082 return NULL;
462083
462084 Frag all = c.WalkExponential(sre, Frag(), 2*c.max_ninst_);
462085 sre->Decref();
462086 if (c.failed_)
462087 return NULL;
462088
462089 c.prog_->set_anchor_start(true);
462090 c.prog_->set_anchor_end(true);
462091
462092 if (anchor == RE2::UNANCHORED) {
462093 // Prepend .* or else the expression will effectively be anchored.
462094 // Complemented by the ANCHOR_BOTH case in PostVisit().
462095 all = c.Cat(c.DotStar(), all);
462096 }
462097 c.prog_->set_start(all.begin);
462098 c.prog_->set_start_unanchored(all.begin);
462099
462100 Prog* prog = c.Finish(re);
462101 if (prog == NULL)
462102 return NULL;
462103
462104 // Make sure DFA has enough memory to operate,
462105 // since we're not going to fall back to the NFA.
462106 bool dfa_failed = false;
462107 StringPiece sp = "hello, world";
462108 prog->SearchDFA(sp, sp, Prog::kAnchored, Prog::kManyMatch,
462109 NULL, &dfa_failed, NULL);
462110 if (dfa_failed) {
462111 delete prog;
462112 return NULL;
462113 }
462114
462115 return prog;
462116}

◆ PreVisit()

Frag duckdb_re2::Compiler::PreVisit ( Regexp * re,
Frag parent_arg,
bool * stop )
461666 {
461667 // Cut off walk if we've already failed.
461668 if (failed_)
461669 *stop = true;
461670
461671 return Frag(); // not used by caller
461672}

◆ PostVisit()

Frag duckdb_re2::Compiler::PostVisit ( Regexp * re,
Frag parent_arg,
Frag pre_arg,
Frag * child_args,
int nchild_args )
461698 {
461699 // If a child failed, don't bother going forward, especially
461700 // since the child_frags might contain Frags with NULLs in them.
461701 if (failed_)
461702 return NoMatch();
461703
461704 // Given the child fragments, return the fragment for this node.
461705 switch (re->op()) {
461706 case kRegexpRepeat:
461707 // Should not see; code at bottom of function will print error
461708 break;
461709
461710 case kRegexpNoMatch:
461711 return NoMatch();
461712
461713 case kRegexpEmptyMatch:
461714 return Nop();
461715
461716 case kRegexpHaveMatch: {
461717 Frag f = Match(re->match_id());
461718 if (anchor_ == RE2::ANCHOR_BOTH) {
461719 // Append \z or else the subexpression will effectively be unanchored.
461720 // Complemented by the UNANCHORED case in CompileSet().
461721 f = Cat(EmptyWidth(kEmptyEndText), f);
461722 }
461723 return f;
461724 }
461725
461726 case kRegexpConcat: {
461727 Frag f = child_frags[0];
461728 for (int i = 1; i < nchild_frags; i++)
461729 f = Cat(f, child_frags[i]);
461730 return f;
461731 }
461732
461733 case kRegexpAlternate: {
461734 Frag f = child_frags[0];
461735 for (int i = 1; i < nchild_frags; i++)
461736 f = Alt(f, child_frags[i]);
461737 return f;
461738 }
461739
461740 case kRegexpStar:
461741 return Star(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
461742
461743 case kRegexpPlus:
461744 return Plus(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
461745
461746 case kRegexpQuest:
461747 return Quest(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
461748
461749 case kRegexpLiteral:
461750 return Literal(re->rune(), (re->parse_flags()&Regexp::FoldCase) != 0);
461751
461752 case kRegexpLiteralString: {
461753 // Concatenation of literals.
461754 if (re->nrunes() == 0)
461755 return Nop();
461756 Frag f;
461757 for (int i = 0; i < re->nrunes(); i++) {
461758 Frag f1 = Literal(re->runes()[i],
461759 (re->parse_flags()&Regexp::FoldCase) != 0);
461760 if (i == 0)
461761 f = f1;
461762 else
461763 f = Cat(f, f1);
461764 }
461765 return f;
461766 }
461767
461768 case kRegexpAnyChar:
461769 BeginRange();
461770 AddRuneRange(0, Runemax, false);
461771 return EndRange();
461772
461773 case kRegexpAnyByte:
461774 return ByteRange(0x00, 0xFF, false);
461775
461776 case kRegexpCharClass: {
461777 CharClass* cc = re->cc();
461778 if (cc->empty()) {
461779 // This can't happen.
461780 failed_ = true;
461781 LOG(DFATAL) << "No ranges in char class";
461782 return NoMatch();
461783 }
461784
461785 // ASCII case-folding optimization: if the char class
461786 // behaves the same on A-Z as it does on a-z,
461787 // discard any ranges wholly contained in A-Z
461788 // and mark the other ranges as foldascii.
461789 // This reduces the size of a program for
461790 // (?i)abc from 3 insts per letter to 1 per letter.
461791 bool foldascii = cc->FoldsASCII();
461792
461793 // Character class is just a big OR of the different
461794 // character ranges in the class.
461795 BeginRange();
461796 for (CharClass::iterator i = cc->begin(); i != cc->end(); ++i) {
461797 // ASCII case-folding optimization (see above).
461798 if (foldascii && 'A' <= i->lo && i->hi <= 'Z')
461799 continue;
461800
461801 // If this range contains all of A-Za-z or none of it,
461802 // the fold flag is unnecessary; don't bother.
461803 bool fold = foldascii;
461804 if ((i->lo <= 'A' && 'z' <= i->hi) || i->hi < 'A' || 'z' < i->lo ||
461805 ('Z' < i->lo && i->hi < 'a'))
461806 fold = false;
461807
461808 AddRuneRange(i->lo, i->hi, fold);
461809 }
461810 return EndRange();
461811 }
461812
461813 case kRegexpCapture:
461814 // If this is a non-capturing parenthesis -- (?:foo) --
461815 // just use the inner expression.
461816 if (re->cap() < 0)
461817 return child_frags[0];
461818 return Capture(child_frags[0], re->cap());
461819
461820 case kRegexpBeginLine:
461821 return EmptyWidth(reversed_ ? kEmptyEndLine : kEmptyBeginLine);
461822
461823 case kRegexpEndLine:
461824 return EmptyWidth(reversed_ ? kEmptyBeginLine : kEmptyEndLine);
461825
461826 case kRegexpBeginText:
461827 return EmptyWidth(reversed_ ? kEmptyEndText : kEmptyBeginText);
461828
461829 case kRegexpEndText:
461830 return EmptyWidth(reversed_ ? kEmptyBeginText : kEmptyEndText);
461831
461832 case kRegexpWordBoundary:
461833 return EmptyWidth(kEmptyWordBoundary);
461834
461835 case kRegexpNoWordBoundary:
461836 return EmptyWidth(kEmptyNonWordBoundary);
461837 }
461838 failed_ = true;
461839 LOG(DFATAL) << "Missing case in Compiler: " << re->op();
461840 return NoMatch();
461841}

◆ ShortVisit()

Frag duckdb_re2::Compiler::ShortVisit ( Regexp * re,
Frag parent_arg )
virtual

Implements duckdb_re2::Regexp::Walker< Frag >.

461660 {
461661 failed_ = true;
461662 return NoMatch();
461663}

◆ Copy()

Frag duckdb_re2::Compiler::Copy ( Frag arg)
461651 {
461652 // We're using WalkExponential; there should be no copying.
461653 failed_ = true;
461654 LOG(DFATAL) << "Compiler::Copy called!";
461655 return NoMatch();
461656}

◆ Plus()

Frag duckdb_re2::Compiler::Plus ( Frag a,
bool nongreedy )
461188 {
461189 int id = AllocInst(1);
461190 if (id < 0)
461191 return NoMatch();
461192 PatchList pl;
461193 if (nongreedy) {
461194 inst_[id].InitAlt(0, a.begin);
461195 pl = PatchList::Mk(id << 1);
461196 } else {
461197 inst_[id].InitAlt(a.begin, 0);
461198 pl = PatchList::Mk((id << 1) | 1);
461199 }
461200 PatchList::Patch(inst_.data(), a.end, id);
461201 return Frag(a.begin, pl, a.nullable);
461202}

◆ Star()

Frag duckdb_re2::Compiler::Star ( Frag a,
bool nongreedy )
461205 {
461206 // When the subexpression is nullable, one Alt isn't enough to guarantee
461207 // correct priority ordering within the transitive closure. The simplest
461208 // solution is to handle it as (a+)? instead, which adds the second Alt.
461209 if (a.nullable)
461210 return Quest(Plus(a, nongreedy), nongreedy);
461211
461212 int id = AllocInst(1);
461213 if (id < 0)
461214 return NoMatch();
461215 PatchList pl;
461216 if (nongreedy) {
461217 inst_[id].InitAlt(0, a.begin);
461218 pl = PatchList::Mk(id << 1);
461219 } else {
461220 inst_[id].InitAlt(a.begin, 0);
461221 pl = PatchList::Mk((id << 1) | 1);
461222 }
461223 PatchList::Patch(inst_.data(), a.end, id);
461224 return Frag(id, pl, true);
461225}

◆ Quest()

Frag duckdb_re2::Compiler::Quest ( Frag a,
bool nongreedy )
461228 {
461229 if (IsNoMatch(a))
461230 return Nop();
461231 int id = AllocInst(1);
461232 if (id < 0)
461233 return NoMatch();
461234 PatchList pl;
461235 if (nongreedy) {
461236 inst_[id].InitAlt(0, a.begin);
461237 pl = PatchList::Mk(id << 1);
461238 } else {
461239 inst_[id].InitAlt(a.begin, 0);
461240 pl = PatchList::Mk((id << 1) | 1);
461241 }
461242 return Frag(id, PatchList::Append(inst_.data(), pl, a.end), true);
461243}

◆ Capture()

Frag duckdb_re2::Compiler::Capture ( Frag a,
int n )
461282 {
461283 if (IsNoMatch(a))
461284 return NoMatch();
461285 int id = AllocInst(2);
461286 if (id < 0)
461287 return NoMatch();
461288 inst_[id].InitCapture(2*n, a.begin);
461289 inst_[id+1].InitCapture(2*n+1, 0);
461290 PatchList::Patch(inst_.data(), a.end, id+1);
461291
461292 return Frag(id, PatchList::Mk((id+1) << 1), a.nullable);
461293}

◆ Cat()

Frag duckdb_re2::Compiler::Cat ( Frag a,
Frag b )
461139 {
461140 if (IsNoMatch(a) || IsNoMatch(b))
461141 return NoMatch();
461142
461143 // Elide no-op.
461144 Prog::Inst* begin = &inst_[a.begin];
461145 if (begin->opcode() == kInstNop &&
461146 a.end.head == (a.begin << 1) &&
461147 begin->out() == 0) {
461148 // in case refs to a somewhere
461149 PatchList::Patch(inst_.data(), a.end, b.begin);
461150 return b;
461151 }
461152
461153 // To run backward over string, reverse all concatenations.
461154 if (reversed_) {
461155 PatchList::Patch(inst_.data(), b.end, a.begin);
461156 return Frag(b.begin, a.end, b.nullable && a.nullable);
461157 }
461158
461159 PatchList::Patch(inst_.data(), a.end, b.begin);
461160 return Frag(a.begin, b.end, a.nullable && b.nullable);
461161}

◆ Alt()

Frag duckdb_re2::Compiler::Alt ( Frag a,
Frag b )
461164 {
461165 // Special case for convenience in loops.
461166 if (IsNoMatch(a))
461167 return b;
461168 if (IsNoMatch(b))
461169 return a;
461170
461171 int id = AllocInst(1);
461172 if (id < 0)
461173 return NoMatch();
461174
461175 inst_[id].InitAlt(a.begin, b.begin);
461176 return Frag(id, PatchList::Append(inst_.data(), a.end, b.end),
461177 a.nullable || b.nullable);
461178}

◆ NoMatch()

Frag duckdb_re2::Compiler::NoMatch ( )
461129 {
461130 return Frag();
461131}

◆ Match()

Frag duckdb_re2::Compiler::Match ( int32_t id)
461264 {
461265 int id = AllocInst(1);
461266 if (id < 0)
461267 return NoMatch();
461268 inst_[id].InitMatch(match_id);
461269 return Frag(id, kNullPatchList, false);
461270}

◆ Nop()

Frag duckdb_re2::Compiler::Nop ( )
461255 {
461256 int id = AllocInst(1);
461257 if (id < 0)
461258 return NoMatch();
461259 inst_[id].InitNop(0);
461260 return Frag(id, PatchList::Mk(id << 1), true);
461261}

◆ ByteRange()

Frag duckdb_re2::Compiler::ByteRange ( int lo,
int hi,
bool foldcase )
461246 {
461247 int id = AllocInst(1);
461248 if (id < 0)
461249 return NoMatch();
461250 inst_[id].InitByteRange(lo, hi, foldcase, 0);
461251 return Frag(id, PatchList::Mk(id << 1), false);
461252}

◆ EmptyWidth()

Frag duckdb_re2::Compiler::EmptyWidth ( EmptyOp op)
461273 {
461274 int id = AllocInst(1);
461275 if (id < 0)
461276 return NoMatch();
461277 inst_[id].InitEmptyWidth(empty, 0);
461278 return Frag(id, PatchList::Mk(id << 1), true);
461279}

◆ AllocInst()

int duckdb_re2::Compiler::AllocInst ( int n)
461101 {
461102 if (failed_ || ninst_ + n > max_ninst_) {
461103 failed_ = true;
461104 return -1;
461105 }
461106
461107 if (ninst_ + n > inst_.size()) {
461108 int cap = inst_.size();
461109 if (cap == 0)
461110 cap = 8;
461111 while (ninst_ + n > cap)
461112 cap *= 2;
461113 PODArray<Prog::Inst> inst(cap);
461114 if (inst_.data() != NULL)
461115 memmove(inst.data(), inst_.data(), ninst_*sizeof inst_[0]);
461116 memset(inst.data() + ninst_, 0, (cap - ninst_)*sizeof inst_[0]);
461117 inst_ = std::move(inst);
461118 }
461119 int id = ninst_;
461120 ninst_ += n;
461121 return id;
461122}

◆ BeginRange()

void duckdb_re2::Compiler::BeginRange ( )
461314 {
461315 rune_cache_.clear();
461316 rune_range_.begin = 0;
461317 rune_range_.end = kNullPatchList;
461318}

◆ AddRuneRange()

void duckdb_re2::Compiler::AddRuneRange ( Rune lo,
Rune hi,
bool foldcase )
461490 {
461491 switch (encoding_) {
461492 default:
461493 case kEncodingUTF8:
461494 AddRuneRangeUTF8(lo, hi, foldcase);
461495 break;
461496 case kEncodingLatin1:
461497 AddRuneRangeLatin1(lo, hi, foldcase);
461498 break;
461499 }
461500}

◆ AddRuneRangeLatin1()

void duckdb_re2::Compiler::AddRuneRangeLatin1 ( Rune lo,
Rune hi,
bool foldcase )
461502 {
461503 // Latin-1 is easy: runes *are* bytes.
461504 if (lo > hi || lo > 0xFF)
461505 return;
461506 if (hi > 0xFF)
461507 hi = 0xFF;
461508 AddSuffix(UncachedRuneByteSuffix(static_cast<uint8_t>(lo),
461509 static_cast<uint8_t>(hi), foldcase, 0));
461510}
::uint8_t uint8_t

◆ AddRuneRangeUTF8()

void duckdb_re2::Compiler::AddRuneRangeUTF8 ( Rune lo,
Rune hi,
bool foldcase )
461552 {
461553 if (lo > hi)
461554 return;
461555
461556 // Pick off 80-10FFFF as a common special case.
461557 if (lo == 0x80 && hi == 0x10ffff) {
461558 Add_80_10ffff();
461559 return;
461560 }
461561
461562 // Split range into same-length sized ranges.
461563 for (int i = 1; i < UTFmax; i++) {
461564 Rune max = MaxRune(i);
461565 if (lo <= max && max < hi) {
461566 AddRuneRangeUTF8(lo, max, foldcase);
461567 AddRuneRangeUTF8(max+1, hi, foldcase);
461568 return;
461569 }
461570 }
461571
461572 // ASCII range is always a special case.
461573 if (hi < Runeself) {
461574 AddSuffix(UncachedRuneByteSuffix(static_cast<uint8_t>(lo),
461575 static_cast<uint8_t>(hi), foldcase, 0));
461576 return;
461577 }
461578
461579 // Split range into sections that agree on leading bytes.
461580 for (int i = 1; i < UTFmax; i++) {
461581 uint32_t m = (1<<(6*i)) - 1; // last i bytes of a UTF-8 sequence
461582 if ((lo & ~m) != (hi & ~m)) {
461583 if ((lo & m) != 0) {
461584 AddRuneRangeUTF8(lo, lo|m, foldcase);
461585 AddRuneRangeUTF8((lo|m)+1, hi, foldcase);
461586 return;
461587 }
461588 if ((hi & m) != m) {
461589 AddRuneRangeUTF8(lo, (hi&~m)-1, foldcase);
461590 AddRuneRangeUTF8(hi&~m, hi, foldcase);
461591 return;
461592 }
461593 }
461594 }
461595
461596 // Finally. Generate byte matching equivalent for lo-hi.
461597 uint8_t ulo[UTFmax], uhi[UTFmax];
461598 int n = runetochar(reinterpret_cast<char*>(ulo), &lo);
461599 int m = runetochar(reinterpret_cast<char*>(uhi), &hi);
461600 (void)m; // USED(m)
461601 DCHECK_EQ(n, m);
461602
461603 // The logic below encodes this thinking:
461604 //
461605 // 1. When we have built the whole suffix, we know that it cannot
461606 // possibly be a suffix of anything longer: in forward mode, nothing
461607 // else can occur before the leading byte; in reverse mode, nothing
461608 // else can occur after the last continuation byte or else the leading
461609 // byte would have to change. Thus, there is no benefit to caching
461610 // the first byte of the suffix whereas there is a cost involved in
461611 // cloning it if it begins a common prefix, which is fairly likely.
461612 //
461613 // 2. Conversely, the last byte of the suffix cannot possibly be a
461614 // prefix of anything because next == 0, so we will never want to
461615 // clone it, but it is fairly likely to be a common suffix. Perhaps
461616 // more so in reverse mode than in forward mode because the former is
461617 // "converging" towards lower entropy, but caching is still worthwhile
461618 // for the latter in cases such as 80-BF.
461619 //
461620 // 3. Handling the bytes between the first and the last is less
461621 // straightforward and, again, the approach depends on whether we are
461622 // "converging" towards lower entropy: in forward mode, a single byte
461623 // is unlikely to be part of a common suffix whereas a byte range
461624 // is more likely so; in reverse mode, a byte range is unlikely to
461625 // be part of a common suffix whereas a single byte is more likely
461626 // so. The same benefit versus cost argument applies here.
461627 int id = 0;
461628 if (reversed_) {
461629 for (int i = 0; i < n; i++) {
461630 // In reverse UTF-8 mode: cache the leading byte; don't cache the last
461631 // continuation byte; cache anything else iff it's a single byte (XX-XX).
461632 if (i == 0 || (ulo[i] == uhi[i] && i != n-1))
461633 id = CachedRuneByteSuffix(ulo[i], uhi[i], false, id);
461634 else
461635 id = UncachedRuneByteSuffix(ulo[i], uhi[i], false, id);
461636 }
461637 } else {
461638 for (int i = n-1; i >= 0; i--) {
461639 // In forward UTF-8 mode: don't cache the leading byte; cache the last
461640 // continuation byte; cache anything else iff it's a byte range (XX-YY).
461641 if (i == n-1 || (ulo[i] < uhi[i] && i != 0))
461642 id = CachedRuneByteSuffix(ulo[i], uhi[i], false, id);
461643 else
461644 id = UncachedRuneByteSuffix(ulo[i], uhi[i], false, id);
461645 }
461646 }
461647 AddSuffix(id);
461648}
static softfloat max()
::uint32_t uint32_t

◆ Add_80_10ffff()

void duckdb_re2::Compiler::Add_80_10ffff ( )
461512 {
461513 // The 80-10FFFF (Runeself-Runemax) rune range occurs frequently enough
461514 // (for example, for /./ and /[^a-z]/) that it is worth simplifying: by
461515 // permitting overlong encodings in E0 and F0 sequences and code points
461516 // over 10FFFF in F4 sequences, the size of the bytecode and the number
461517 // of equivalence classes are reduced significantly.
461518 int id;
461519 if (reversed_) {
461520 // Prefix factoring matters, but we don't have to handle it here
461521 // because the rune range trie logic takes care of that already.
461522 id = UncachedRuneByteSuffix(0xC2, 0xDF, false, 0);
461523 id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
461524 AddSuffix(id);
461525
461526 id = UncachedRuneByteSuffix(0xE0, 0xEF, false, 0);
461527 id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
461528 id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
461529 AddSuffix(id);
461530
461531 id = UncachedRuneByteSuffix(0xF0, 0xF4, false, 0);
461532 id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
461533 id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
461534 id = UncachedRuneByteSuffix(0x80, 0xBF, false, id);
461535 AddSuffix(id);
461536 } else {
461537 // Suffix factoring matters - and we do have to handle it here.
461538 int cont1 = UncachedRuneByteSuffix(0x80, 0xBF, false, 0);
461539 id = UncachedRuneByteSuffix(0xC2, 0xDF, false, cont1);
461540 AddSuffix(id);
461541
461542 int cont2 = UncachedRuneByteSuffix(0x80, 0xBF, false, cont1);
461543 id = UncachedRuneByteSuffix(0xE0, 0xEF, false, cont2);
461544 AddSuffix(id);
461545
461546 int cont3 = UncachedRuneByteSuffix(0x80, 0xBF, false, cont2);
461547 id = UncachedRuneByteSuffix(0xF0, 0xF4, false, cont3);
461548 AddSuffix(id);
461549 }
461550}

◆ UncachedRuneByteSuffix()

int duckdb_re2::Compiler::UncachedRuneByteSuffix ( uint8_t lo,
uint8_t hi,
bool foldcase,
int next )
461321 {
461322 Frag f = ByteRange(lo, hi, foldcase);
461323 if (next != 0) {
461324 PatchList::Patch(inst_.data(), f.end, next);
461325 } else {
461326 rune_range_.end = PatchList::Append(inst_.data(), rune_range_.end, f.end);
461327 }
461328 return f.begin;
461329}

◆ CachedRuneByteSuffix()

int duckdb_re2::Compiler::CachedRuneByteSuffix ( uint8_t lo,
uint8_t hi,
bool foldcase,
int next )
461340 {
461341 uint64_t key = MakeRuneCacheKey(lo, hi, foldcase, next);
461342 std::unordered_map<uint64_t, int>::const_iterator it = rune_cache_.find(key);
461343 if (it != rune_cache_.end())
461344 return it->second;
461345 int id = UncachedRuneByteSuffix(lo, hi, foldcase, next);
461346 rune_cache_[key] = id;
461347 return id;
461348}
::uint64_t uint64_t

◆ IsCachedRuneByteSuffix()

bool duckdb_re2::Compiler::IsCachedRuneByteSuffix ( int id)
461350 {
461351 uint8_t lo = inst_[id].byte_range.lo_;
461352 uint8_t hi = inst_[id].byte_range.hi_;
461353 bool foldcase = inst_[id].foldcase() != 0;
461354 int next = inst_[id].out();
461355
461356 uint64_t key = MakeRuneCacheKey(lo, hi, foldcase, next);
461357 return rune_cache_.find(key) != rune_cache_.end();
461358}

◆ AddSuffix()

void duckdb_re2::Compiler::AddSuffix ( int id)
461360 {
461361 if (failed_)
461362 return;
461363
461364 if (rune_range_.begin == 0) {
461365 rune_range_.begin = id;
461366 return;
461367 }
461368
461369 if (encoding_ == kEncodingUTF8) {
461370 // Build a trie in order to reduce fanout.
461371 rune_range_.begin = AddSuffixRecursive(rune_range_.begin, id);
461372 return;
461373 }
461374
461375 int alt = AllocInst(1);
461376 if (alt < 0) {
461377 rune_range_.begin = 0;
461378 return;
461379 }
461380 inst_[alt].InitAlt(rune_range_.begin, id);
461381 rune_range_.begin = alt;
461382}

◆ AddSuffixRecursive()

int duckdb_re2::Compiler::AddSuffixRecursive ( int root,
int id )
461384 {
461385 DCHECK(inst_[root].opcode() == kInstAlt ||
461386 inst_[root].opcode() == kInstByteRange);
461387
461388 Frag f = FindByteRange(root, id);
461389 if (IsNoMatch(f)) {
461390 int alt = AllocInst(1);
461391 if (alt < 0)
461392 return 0;
461393 inst_[alt].InitAlt(root, id);
461394 return alt;
461395 }
461396
461397 int br;
461398 if (f.end.head == 0)
461399 br = root;
461400 else if (f.end.head&1)
461401 br = inst_[f.begin].out1();
461402 else
461403 br = inst_[f.begin].out();
461404
461405 if (IsCachedRuneByteSuffix(br)) {
461406 // We can't fiddle with cached suffixes, so make a clone of the head.
461407 int byterange = AllocInst(1);
461408 if (byterange < 0)
461409 return 0;
461410 inst_[byterange].InitByteRange(inst_[br].lo(), inst_[br].hi(),
461411 inst_[br].foldcase(), inst_[br].out());
461412
461413 // Ensure that the parent points to the clone, not to the original.
461414 // Note that this could leave the head unreachable except via the cache.
461415 br = byterange;
461416 if (f.end.head == 0)
461417 root = br;
461418 else if (f.end.head&1)
461419 inst_[f.begin].out1_ = br;
461420 else
461421 inst_[f.begin].set_out(br);
461422 }
461423
461424 int out = inst_[id].out();
461425 if (!IsCachedRuneByteSuffix(id)) {
461426 // The head should be the instruction most recently allocated, so free it
461427 // instead of leaving it unreachable.
461428 DCHECK_EQ(id, ninst_-1);
461429 inst_[id].out_opcode_ = 0;
461430 inst_[id].out1_ = 0;
461431 ninst_--;
461432 }
461433
461434 out = AddSuffixRecursive(inst_[br].out(), out);
461435 if (out == 0)
461436 return 0;
461437
461438 inst_[br].set_out(out);
461439 return root;
461440}

◆ FindByteRange()

Frag duckdb_re2::Compiler::FindByteRange ( int root,
int id )
461448 {
461449 if (inst_[root].opcode() == kInstByteRange) {
461450 if (ByteRangeEqual(root, id))
461451 return Frag(root, kNullPatchList, false);
461452 else
461453 return NoMatch();
461454 }
461455
461456 while (inst_[root].opcode() == kInstAlt) {
461457 int out1 = inst_[root].out1();
461458 if (ByteRangeEqual(out1, id))
461459 return Frag(root, PatchList::Mk((root << 1) | 1), false);
461460
461461 // CharClass is a sorted list of ranges, so if out1 of the root Alt wasn't
461462 // what we're looking for, then we can stop immediately. Unfortunately, we
461463 // can't short-circuit the search in reverse mode.
461464 if (!reversed_)
461465 return NoMatch();
461466
461467 int out = inst_[root].out();
461468 if (inst_[out].opcode() == kInstAlt)
461469 root = out;
461470 else if (ByteRangeEqual(out, id))
461471 return Frag(root, PatchList::Mk(root << 1), false);
461472 else
461473 return NoMatch();
461474 }
461475
461476 LOG(DFATAL) << "should never happen";
461477 return NoMatch();
461478}

◆ ByteRangeEqual()

bool duckdb_re2::Compiler::ByteRangeEqual ( int id1,
int id2 )
461442 {
461443 return inst_[id1].lo() == inst_[id2].lo() &&
461444 inst_[id1].hi() == inst_[id2].hi() &&
461445 inst_[id1].foldcase() == inst_[id2].foldcase();
461446}

◆ EndRange()

Frag duckdb_re2::Compiler::EndRange ( )
461480 {
461481 return rune_range_;
461482}

◆ Literal()

Frag duckdb_re2::Compiler::Literal ( Rune r,
bool foldcase )
461674 {
461675 switch (encoding_) {
461676 default:
461677 return Frag();
461678
461679 case kEncodingLatin1:
461680 return ByteRange(r, r, foldcase);
461681
461682 case kEncodingUTF8: {
461683 if (r < Runeself) // Make common case fast.
461684 return ByteRange(r, r, foldcase);
461685 uint8_t buf[UTFmax];
461686 int n = runetochar(reinterpret_cast<char*>(buf), &r);
461687 Frag f = ByteRange((uint8_t)buf[0], buf[0], false);
461688 for (int i = 1; i < n; i++)
461689 f = Cat(f, ByteRange((uint8_t)buf[i], buf[i], false));
461690 return f;
461691 }
461692 }
461693}

◆ Setup()

void duckdb_re2::Compiler::Setup ( Regexp::ParseFlags flags,
int64_t max_mem,
RE2::Anchor anchor )
461938 {
461939 if (flags & Regexp::Latin1)
461940 encoding_ = kEncodingLatin1;
461941 max_mem_ = max_mem;
461942 if (max_mem <= 0) {
461943 max_ninst_ = 100000; // more than enough
461944 } else if (static_cast<size_t>(max_mem) <= sizeof(Prog)) {
461945 // No room for anything.
461946 max_ninst_ = 0;
461947 } else {
461948 int64_t m = (max_mem - sizeof(Prog)) / sizeof(Prog::Inst);
461949 // Limit instruction count so that inst->id() fits nicely in an int.
461950 // SparseArray also assumes that the indices (inst->id()) are ints.
461951 // The call to WalkExponential uses 2*max_ninst_ below,
461952 // and other places in the code use 2 or 3 * prog->size().
461953 // Limiting to 2^24 should avoid overflow in those places.
461954 // (The point of allowing more than 32 bits of memory is to
461955 // have plenty of room for the DFA states, not to use it up
461956 // on the program.)
461957 if (m >= 1<<24)
461958 m = 1<<24;
461959 // Inst imposes its own limit (currently bigger than 2^24 but be safe).
461960 if (m > Prog::Inst::kMaxInst)
461961 m = Prog::Inst::kMaxInst;
461962 max_ninst_ = static_cast<int>(m);
461963 }
461964 anchor_ = anchor;
461965}
::int64_t int64_t

◆ Finish()

Prog * duckdb_re2::Compiler::Finish ( Regexp * re)
462020 {
462021 if (failed_)
462022 return NULL;
462023
462024 if (prog_->start() == 0 && prog_->start_unanchored() == 0) {
462025 // No possible matches; keep Fail instruction only.
462026 ninst_ = 1;
462027 }
462028
462029 // Hand off the array to Prog.
462030 prog_->inst_ = std::move(inst_);
462031 prog_->size_ = ninst_;
462032
462033 prog_->Optimize();
462034 prog_->Flatten();
462035 prog_->ComputeByteMap();
462036
462037 if (!prog_->reversed()) {
462038 std::string prefix;
462039 bool prefix_foldcase;
462040 if (re->RequiredPrefixForAccel(&prefix, &prefix_foldcase))
462041 prog_->ConfigurePrefixAccel(prefix, prefix_foldcase);
462042 }
462043
462044 // Record remaining memory for DFA.
462045 if (max_mem_ <= 0) {
462046 prog_->set_dfa_mem(1<<20);
462047 } else {
462048 int64_t m = max_mem_ - sizeof(Prog);
462049 m -= prog_->size_*sizeof(Prog::Inst); // account for inst_
462050 if (prog_->CanBitState())
462051 m -= prog_->size_*sizeof(uint16_t); // account for list_heads_
462052 if (m < 0)
462053 m = 0;
462054 prog_->set_dfa_mem(m);
462055 }
462056
462057 Prog* p = prog_;
462058 prog_ = NULL;
462059 return p;
462060}
::uint16_t uint16_t

◆ DotStar()

Frag duckdb_re2::Compiler::DotStar ( )
462071 {
462072 return Star(ByteRange(0x00, 0xff, false), true);
462073}

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