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
RE2 Class Reference
Collaboration diagram for RE2:

Classes

class  Options
class  Arg
class  Set

Public Types

enum  ErrorCode {
  NoError = 0 , ErrorInternal , ErrorBadEscape , ErrorBadCharClass ,
  ErrorBadCharRange , ErrorMissingBracket , ErrorMissingParen , ErrorUnexpectedParen ,
  ErrorTrailingBackslash , ErrorRepeatArgument , ErrorRepeatSize , ErrorRepeatOp ,
  ErrorBadPerlOp , ErrorBadUTF8 , ErrorBadNamedCapture , ErrorPatternTooLarge
}
enum  CannedOptions { DefaultOptions = 0 , Latin1 , POSIX , Quiet }
enum  Anchor { UNANCHORED , ANCHOR_START , ANCHOR_BOTH }

Public Member Functions

 RE2 (const char *pattern)
 RE2 (const std::string &pattern)
 RE2 (const StringPiece &pattern)
 RE2 (const StringPiece &pattern, const Options &options)
 RE2 (const RE2 &)=delete
 RE2 (RE2 &&)=delete
RE2 & operator= (const RE2 &)=delete
RE2 & operator= (RE2 &&)=delete
bool ok () const
const std::string & pattern () const
const std::string & error () const
ErrorCode error_code () const
const std::string & error_arg () const
int ProgramSize () const
int ReverseProgramSize () const
int ProgramFanout (std::vector< int > *histogram) const
int ReverseProgramFanout (std::vector< int > *histogram) const
duckdb_re2::Regexp * Regexp () const
bool PossibleMatchRange (std::string *min, std::string *max, int maxlen) const
int NumberOfCapturingGroups () const
const std::map< std::string, int > & NamedCapturingGroups () const
const std::map< int, std::string > & CapturingGroupNames () const
bool Match (const StringPiece &text, size_t startpos, size_t endpos, Anchor re_anchor, StringPiece *submatch, int nsubmatch) const
bool CheckRewriteString (const StringPiece &rewrite, std::string *error) const
bool Rewrite (std::string *out, const StringPiece &rewrite, const StringPiece *vec, int veclen) const
const Options & options () const

Static Public Member Functions

static bool FullMatchN (const StringPiece &text, const RE2 &re, const Arg *const args[], int n)
static bool PartialMatchN (const StringPiece &text, const RE2 &re, const Arg *const args[], int n)
static bool ConsumeN (StringPiece *input, const RE2 &re, const Arg *const args[], int n)
static bool FindAndConsumeN (StringPiece *input, const RE2 &re, const Arg *const args[], int n)
template<typename... A>
static bool FullMatch (const StringPiece &text, const RE2 &re, A &&... a)
template<typename... A>
static bool PartialMatch (const StringPiece &text, const RE2 &re, A &&... a)
template<typename... A>
static bool Consume (StringPiece *input, const RE2 &re, A &&... a)
template<typename... A>
static bool FindAndConsume (StringPiece *input, const RE2 &re, A &&... a)
static bool Replace (std::string *str, const RE2 &re, const StringPiece &rewrite)
static int GlobalReplace (std::string *str, const RE2 &re, const StringPiece &rewrite)
static bool Extract (const StringPiece &text, const RE2 &re, const StringPiece &rewrite, std::string *out)
static std::string QuoteMeta (const StringPiece &unquoted)
static int MaxSubmatch (const StringPiece &rewrite)
template<typename T>
static Arg CRadix (T *ptr)
template<typename T>
static Arg Hex (T *ptr)
template<typename T>
static Arg Octal (T *ptr)
static void FUZZING_ONLY_set_maximum_global_replace_count (int i)

Private Member Functions

void Init (const StringPiece &pattern, const Options &options)
bool DoMatch (const StringPiece &text, Anchor re_anchor, size_t *consumed, const Arg *const args[], int n) const
duckdb_re2::Prog * ReverseProg () const

Static Private Member Functions

template<typename F, typename SP>
static bool Apply (F f, SP sp, const RE2 &re)
template<typename F, typename SP, typename... A>
static bool Apply (F f, SP sp, const RE2 &re, const A &... a)

Private Attributes

const std::string * pattern_
Options options_
duckdb_re2::Regexp * entire_regexp_
duckdb_re2::Regexp * suffix_regexp_
const std::string * error_
const std::string * error_arg_
int num_captures_
ErrorCode error_code_: 29
bool longest_match_: 1
bool is_one_pass_: 1
bool prefix_foldcase_: 1
std::string prefix_
duckdb_re2::Prog * prog_
duckdb_re2::Prog * rprog_
const std::map< std::string, int > * named_groups_
const std::map< int, std::string > * group_names_
std::once_flag rprog_once_
std::once_flag named_groups_once_
std::once_flag group_names_once_

Member Enumeration Documentation

◆ ErrorCode

enum duckdb_re2::RE2::ErrorCode
97987 {
97988 NoError = 0,
97989
97990 // Unexpected error
97991 ErrorInternal,
97992
97993 // Parse errors
97994 ErrorBadEscape, // bad escape sequence
97995 ErrorBadCharClass, // bad character class
97996 ErrorBadCharRange, // bad character class range
97997 ErrorMissingBracket, // missing closing ]
97998 ErrorMissingParen, // missing closing )
97999 ErrorUnexpectedParen, // unexpected closing )
98000 ErrorTrailingBackslash, // trailing \ at end of regexp
98001 ErrorRepeatArgument, // repeat argument missing, e.g. "*"
98002 ErrorRepeatSize, // bad repetition argument
98003 ErrorRepeatOp, // bad repetition operator
98004 ErrorBadPerlOp, // bad perl operator
98005 ErrorBadUTF8, // invalid UTF-8 in regexp
98006 ErrorBadNamedCapture, // bad named capture group
98007 ErrorPatternTooLarge // pattern too large (compile failed)
98008 };

◆ CannedOptions

enum duckdb_re2::RE2::CannedOptions
98015 {
98016 DefaultOptions = 0,
98017 Latin1, // treat input as Latin-1 (default UTF-8)
98018 POSIX, // POSIX syntax, leftmost-longest match
98019 Quiet // do not log about regexp parse errors
98020 };

◆ Anchor

enum duckdb_re2::RE2::Anchor
98277 {
98278 UNANCHORED, // No anchoring
98279 ANCHOR_START, // Anchor at start only
98280 ANCHOR_BOTH // Anchor at start and end
98281 };

Constructor & Destructor Documentation

◆ RE2() [1/4]

duckdb_re2::RE2::RE2 ( const char * pattern)
471710 {
471711 Init(pattern, DefaultOptions);
471712}

◆ RE2() [2/4]

duckdb_re2::RE2::RE2 ( const std::string & pattern)
471714 {
471715 Init(pattern, DefaultOptions);
471716}

◆ RE2() [3/4]

duckdb_re2::RE2::RE2 ( const StringPiece & pattern)
471718 {
471719 Init(pattern, DefaultOptions);
471720}

◆ RE2() [4/4]

duckdb_re2::RE2::RE2 ( const StringPiece & pattern,
const Options & options )
471722 {
471723 Init(pattern, options);
471724}

◆ ~RE2()

duckdb_re2::RE2::~RE2 ( )
471865 {
471866 if (group_names_ != empty_group_names())
471867 delete group_names_;
471868 if (named_groups_ != empty_named_groups())
471869 delete named_groups_;
471870 delete rprog_;
471871 delete prog_;
471872 if (error_arg_ != empty_string())
471873 delete error_arg_;
471874 if (error_ != empty_string())
471875 delete error_;
471876 if (suffix_regexp_)
471877 suffix_regexp_->Decref();
471878 if (entire_regexp_)
471879 entire_regexp_->Decref();
471880 delete pattern_;
471881}

Member Function Documentation

◆ ok()

bool duckdb_re2::RE2::ok ( ) const
inline
98046{ return error_code() == NoError; }

◆ pattern()

const std::string & duckdb_re2::RE2::pattern ( ) const
inline
98051{ return *pattern_; }

◆ error()

const std::string & duckdb_re2::RE2::error ( ) const
inline
98055{ return *error_; }

◆ error_code()

ErrorCode duckdb_re2::RE2::error_code ( ) const
inline
98059{ return error_code_; }

◆ error_arg()

const std::string & duckdb_re2::RE2::error_arg ( ) const
inline
98063{ return *error_arg_; }

◆ ProgramSize()

int duckdb_re2::RE2::ProgramSize ( ) const
471883 {
471884 if (prog_ == NULL)
471885 return -1;
471886 return prog_->size();
471887}

◆ ReverseProgramSize()

int duckdb_re2::RE2::ReverseProgramSize ( ) const
471889 {
471890 if (prog_ == NULL)
471891 return -1;
471892 Prog* prog = ReverseProg();
471893 if (prog == NULL)
471894 return -1;
471895 return prog->size();
471896}

◆ ProgramFanout()

int duckdb_re2::RE2::ProgramFanout ( std::vector< int > * histogram) const
471939 {
471940 if (prog_ == NULL)
471941 return -1;
471942 return Fanout(prog_, histogram);
471943}

◆ ReverseProgramFanout()

int duckdb_re2::RE2::ReverseProgramFanout ( std::vector< int > * histogram) const
471945 {
471946 if (prog_ == NULL)
471947 return -1;
471948 Prog* prog = ReverseProg();
471949 if (prog == NULL)
471950 return -1;
471951 return Fanout(prog, histogram);
471952}

◆ Regexp()

duckdb_re2::Regexp * duckdb_re2::RE2::Regexp ( ) const
inline
98079{ return entire_regexp_; }

◆ FullMatchN()

bool duckdb_re2::RE2::FullMatchN ( const StringPiece & text,
const RE2 & re,
const Arg *const args[],
int n )
static
471979 {
471980 return re.DoMatch(text, ANCHOR_BOTH, NULL, args, n);
471981}

◆ PartialMatchN()

bool duckdb_re2::RE2::PartialMatchN ( const StringPiece & text,
const RE2 & re,
const Arg *const args[],
int n )
static
471984 {
471985 return re.DoMatch(text, UNANCHORED, NULL, args, n);
471986}

◆ ConsumeN()

bool duckdb_re2::RE2::ConsumeN ( StringPiece * input,
const RE2 & re,
const Arg *const args[],
int n )
static
471989 {
471990 size_t consumed;
471991 if (re.DoMatch(*input, ANCHOR_START, &consumed, args, n)) {
471992 input->remove_prefix(consumed);
471993 return true;
471994 } else {
471995 return false;
471996 }
471997}

◆ FindAndConsumeN()

bool duckdb_re2::RE2::FindAndConsumeN ( StringPiece * input,
const RE2 & re,
const Arg *const args[],
int n )
static
472000 {
472001 size_t consumed;
472002 if (re.DoMatch(*input, UNANCHORED, &consumed, args, n)) {
472003 input->remove_prefix(consumed);
472004 return true;
472005 } else {
472006 return false;
472007 }
472008}

◆ Apply() [1/2]

template<typename F, typename SP>
bool duckdb_re2::RE2::Apply ( F f,
SP sp,
const RE2 & re )
inlinestaticprivate
98098 {
98099 return f(sp, re, NULL, 0);
98100 }

◆ Apply() [2/2]

template<typename F, typename SP, typename... A>
bool duckdb_re2::RE2::Apply ( F f,
SP sp,
const RE2 & re,
const A &... a )
inlinestaticprivate
98103 {
98104 const Arg* const args[] = {&a...};
98105 const int n = sizeof...(a);
98106 return f(sp, re, args, n);
98107 }
Definition duckdb.cpp:98582

◆ FullMatch()

template<typename... A>
bool duckdb_re2::RE2::FullMatch ( const StringPiece & text,
const RE2 & re,
A &&... a )
inlinestatic
98146 {
98147 return Apply(FullMatchN, text, re, Arg(std::forward<A>(a))...);
98148 }

◆ PartialMatch()

template<typename... A>
bool duckdb_re2::RE2::PartialMatch ( const StringPiece & text,
const RE2 & re,
A &&... a )
inlinestatic
98162 {
98163 return Apply(PartialMatchN, text, re, Arg(std::forward<A>(a))...);
98164 }

◆ Consume()

template<typename... A>
bool duckdb_re2::RE2::Consume ( StringPiece * input,
const RE2 & re,
A &&... a )
inlinestatic
98180 {
98181 return Apply(ConsumeN, input, re, Arg(std::forward<A>(a))...);
98182 }

◆ FindAndConsume()

template<typename... A>
bool duckdb_re2::RE2::FindAndConsume ( StringPiece * input,
const RE2 & re,
A &&... a )
inlinestatic
98198 {
98199 return Apply(FindAndConsumeN, input, re, Arg(std::forward<A>(a))...);
98200 }

◆ Replace()

bool duckdb_re2::RE2::Replace ( std::string * str,
const RE2 & re,
const StringPiece & rewrite )
static
472012 {
472013 StringPiece vec[kVecSize];
472014 int nvec = 1 + MaxSubmatch(rewrite);
472015 if (nvec > 1 + re.NumberOfCapturingGroups())
472016 return false;
472017 if (nvec > static_cast<int>(arraysize(vec)))
472018 return false;
472019 if (!re.Match(*str, 0, str->size(), UNANCHORED, vec, nvec))
472020 return false;
472021
472022 std::string s;
472023 if (!re.Rewrite(&s, rewrite, vec, nvec))
472024 return false;
472025
472026 assert(vec[0].data() >= str->data());
472027 assert(vec[0].data() + vec[0].size() <= str->data() + str->size());
472028 str->replace(vec[0].data() - str->data(), vec[0].size(), s);
472029 return true;
472030}
#define assert(x)
Definition duckdb.cpp:486071
GOpaque< Size > size(const GMat &src)

◆ GlobalReplace()

int duckdb_re2::RE2::GlobalReplace ( std::string * str,
const RE2 & re,
const StringPiece & rewrite )
static
472034 {
472035 StringPiece vec[kVecSize];
472036 int nvec = 1 + MaxSubmatch(rewrite);
472037 if (nvec > 1 + re.NumberOfCapturingGroups())
472038 return false;
472039 if (nvec > static_cast<int>(arraysize(vec)))
472040 return false;
472041
472042 const char* p = str->data();
472043 const char* ep = p + str->size();
472044 const char* lastend = NULL;
472045 std::string out;
472046 int count = 0;
472047 while (p <= ep) {
472048 if (maximum_global_replace_count != -1 &&
472049 count >= maximum_global_replace_count)
472050 break;
472051 if (!re.Match(*str, static_cast<size_t>(p - str->data()),
472052 str->size(), UNANCHORED, vec, nvec))
472053 break;
472054 if (p < vec[0].data())
472055 out.append(p, vec[0].data() - p);
472056 if (vec[0].data() == lastend && vec[0].empty()) {
472057 // Disallow empty match at end of last match: skip ahead.
472058 //
472059 // fullrune() takes int, not ptrdiff_t. However, it just looks
472060 // at the leading byte and treats any length >= 4 the same.
472061 if (re.options().encoding() == RE2::Options::EncodingUTF8 &&
472062 fullrune(p, static_cast<int>(std::min(ptrdiff_t{4}, ep - p)))) {
472063 // re is in UTF-8 mode and there is enough left of str
472064 // to allow us to advance by up to UTFmax bytes.
472065 Rune r;
472066 int n = chartorune(&r, p);
472067 // Some copies of chartorune have a bug that accepts
472068 // encodings of values in (10FFFF, 1FFFFF] as valid.
472069 if (r > Runemax) {
472070 n = 1;
472071 r = Runeerror;
472072 }
472073 if (!(n == 1 && r == Runeerror)) { // no decoding error
472074 out.append(p, n);
472075 p += n;
472076 continue;
472077 }
472078 }
472079 // Most likely, re is in Latin-1 mode. If it is in UTF-8 mode,
472080 // we fell through from above and the GIGO principle applies.
472081 if (p < ep)
472082 out.append(p, 1);
472083 p++;
472084 continue;
472085 }
472086 re.Rewrite(&out, rewrite, vec, nvec);
472087 p = vec[0].data() + vec[0].size();
472088 lastend = p;
472089 count++;
472090 }
472091
472092 if (count == 0)
472093 return 0;
472094
472095 if (p < ep)
472096 out.append(p, ep - p);
472097 using std::swap;
472098 swap(out, *str);
472099 return count;
472100}
void swap(Mat &a, Mat &b)

◆ Extract()

bool duckdb_re2::RE2::Extract ( const StringPiece & text,
const RE2 & re,
const StringPiece & rewrite,
std::string * out )
static
472105 {
472106 StringPiece vec[kVecSize];
472107 int nvec = 1 + MaxSubmatch(rewrite);
472108 if (nvec > 1 + re.NumberOfCapturingGroups())
472109 return false;
472110 if (nvec > static_cast<int>(arraysize(vec)))
472111 return false;
472112 if (!re.Match(text, 0, text.size(), UNANCHORED, vec, nvec))
472113 return false;
472114
472115 out->clear();
472116 return re.Rewrite(out, rewrite, vec, nvec);
472117}

◆ QuoteMeta()

std::string duckdb_re2::RE2::QuoteMeta ( const StringPiece & unquoted)
static
472119 {
472120 std::string result;
472121 result.reserve(unquoted.size() << 1);
472122
472123 // Escape any ascii character not in [A-Za-z_0-9].
472124 //
472125 // Note that it's legal to escape a character even if it has no
472126 // special meaning in a regular expression -- so this function does
472127 // that. (This also makes it identical to the perl function of the
472128 // same name except for the null-character special case;
472129 // see `perldoc -f quotemeta`.)
472130 for (size_t ii = 0; ii < unquoted.size(); ++ii) {
472131 // Note that using 'isalnum' here raises the benchmark time from
472132 // 32ns to 58ns:
472133 if ((unquoted[ii] < 'a' || unquoted[ii] > 'z') &&
472134 (unquoted[ii] < 'A' || unquoted[ii] > 'Z') &&
472135 (unquoted[ii] < '0' || unquoted[ii] > '9') &&
472136 unquoted[ii] != '_' &&
472137 // If this is the part of a UTF8 or Latin1 character, we need
472138 // to copy this byte without escaping. Experimentally this is
472139 // what works correctly with the regexp library.
472140 !(unquoted[ii] & 128)) {
472141 if (unquoted[ii] == '\0') { // Special handling for null chars.
472142 // Note that this special handling is not strictly required for RE2,
472143 // but this quoting is required for other regexp libraries such as
472144 // PCRE.
472145 // Can't use "\\0" since the next character might be a digit.
472146 result += "\\x00";
472147 continue;
472148 }
472149 result += '\\';
472150 }
472151 result += unquoted[ii];
472152 }
472153
472154 return result;
472155}

◆ PossibleMatchRange()

bool duckdb_re2::RE2::PossibleMatchRange ( std::string * min,
std::string * max,
int maxlen ) const
472158 {
472159 if (prog_ == NULL)
472160 return false;
472161
472162 int n = static_cast<int>(prefix_.size());
472163 if (n > maxlen)
472164 n = maxlen;
472165
472166 // Determine initial min max from prefix_ literal.
472167 *min = prefix_.substr(0, n);
472168 *max = prefix_.substr(0, n);
472169 if (prefix_foldcase_) {
472170 // prefix is ASCII lowercase; change *min to uppercase.
472171 for (int i = 0; i < n; i++) {
472172 char& c = (*min)[i];
472173 if ('a' <= c && c <= 'z')
472174 c += 'A' - 'a';
472175 }
472176 }
472177
472178 // Add to prefix min max using PossibleMatchRange on regexp.
472179 std::string dmin, dmax;
472180 maxlen -= n;
472181 if (maxlen > 0 && prog_->PossibleMatchRange(&dmin, &dmax, maxlen)) {
472182 min->append(dmin);
472183 max->append(dmax);
472184 } else if (!max->empty()) {
472185 // prog_->PossibleMatchRange has failed us,
472186 // but we still have useful information from prefix_.
472187 // Round up *max to allow any possible suffix.
472188 PrefixSuccessor(max);
472189 } else {
472190 // Nothing useful.
472191 *min = "";
472192 *max = "";
472193 return false;
472194 }
472195
472196 return true;
472197}
static softfloat min()
static softfloat max()

◆ NumberOfCapturingGroups()

int duckdb_re2::RE2::NumberOfCapturingGroups ( ) const
inline
98286{ return num_captures_; }

◆ NamedCapturingGroups()

const std::map< std::string, int > & duckdb_re2::RE2::NamedCapturingGroups ( ) const
471955 {
471956 std::call_once(named_groups_once_, [](const RE2* re) {
471957 if (re->suffix_regexp_ != NULL)
471958 re->named_groups_ = re->suffix_regexp_->NamedCaptures();
471959 if (re->named_groups_ == NULL)
471960 re->named_groups_ = empty_named_groups();
471961 }, this);
471962 return *named_groups_;
471963}

◆ CapturingGroupNames()

const std::map< int, std::string > & duckdb_re2::RE2::CapturingGroupNames ( ) const
471966 {
471967 std::call_once(group_names_once_, [](const RE2* re) {
471968 if (re->suffix_regexp_ != NULL)
471969 re->group_names_ = re->suffix_regexp_->CaptureNames();
471970 if (re->group_names_ == NULL)
471971 re->group_names_ = empty_group_names();
471972 }, this);
471973 return *group_names_;
471974}

◆ Match()

bool duckdb_re2::RE2::Match ( const StringPiece & text,
size_t startpos,
size_t endpos,
Anchor re_anchor,
StringPiece * submatch,
int nsubmatch ) const
472223 {
472224 if (!ok()) {
472225 if (options_.log_errors())
472226 LOG(ERROR) << "Invalid RE2: " << *error_;
472227 return false;
472228 }
472229
472230 if (startpos > endpos || endpos > text.size()) {
472231 if (options_.log_errors())
472232 LOG(ERROR) << "RE2: invalid startpos, endpos pair. ["
472233 << "startpos: " << startpos << ", "
472234 << "endpos: " << endpos << ", "
472235 << "text size: " << text.size() << "]";
472236 return false;
472237 }
472238
472239 StringPiece subtext = text;
472240 subtext.remove_prefix(startpos);
472241 subtext.remove_suffix(text.size() - endpos);
472242
472243 // Use DFAs to find exact location of match, filter out non-matches.
472244
472245 // Don't ask for the location if we won't use it.
472246 // SearchDFA can do extra optimizations in that case.
472247 StringPiece match;
472248 StringPiece* matchp = &match;
472249 if (nsubmatch == 0)
472250 matchp = NULL;
472251
472252 int ncap = 1 + NumberOfCapturingGroups();
472253 if (ncap > nsubmatch)
472254 ncap = nsubmatch;
472255
472256 // If the regexp is anchored explicitly, must not be in middle of text.
472257 if (prog_->anchor_start() && startpos != 0)
472258 return false;
472259 if (prog_->anchor_end() && endpos != text.size())
472260 return false;
472261
472262 // If the regexp is anchored explicitly, update re_anchor
472263 // so that we can potentially fall into a faster case below.
472264 if (prog_->anchor_start() && prog_->anchor_end())
472265 re_anchor = ANCHOR_BOTH;
472266 else if (prog_->anchor_start() && re_anchor != ANCHOR_BOTH)
472267 re_anchor = ANCHOR_START;
472268
472269 // Check for the required prefix, if any.
472270 size_t prefixlen = 0;
472271 if (!prefix_.empty()) {
472272 if (startpos != 0)
472273 return false;
472274 prefixlen = prefix_.size();
472275 if (prefixlen > subtext.size())
472276 return false;
472277 if (prefix_foldcase_) {
472278 if (ascii_strcasecmp(&prefix_[0], subtext.data(), prefixlen) != 0)
472279 return false;
472280 } else {
472281 if (memcmp(&prefix_[0], subtext.data(), prefixlen) != 0)
472282 return false;
472283 }
472284 subtext.remove_prefix(prefixlen);
472285 // If there is a required prefix, the anchor must be at least ANCHOR_START.
472286 if (re_anchor != ANCHOR_BOTH)
472287 re_anchor = ANCHOR_START;
472288 }
472289
472290 Prog::Anchor anchor = Prog::kUnanchored;
472291 Prog::MatchKind kind =
472292 longest_match_ ? Prog::kLongestMatch : Prog::kFirstMatch;
472293
472294 bool can_one_pass = is_one_pass_ && ncap <= Prog::kMaxOnePassCapture;
472295 bool can_bit_state = prog_->CanBitState();
472296 size_t bit_state_text_max_size = prog_->bit_state_text_max_size();
472297
472298#ifdef RE2_HAVE_THREAD_LOCAL
472299 hooks::context = this;
472300#endif
472301 bool dfa_failed = false;
472302 bool skipped_test = false;
472303 switch (re_anchor) {
472304 default:
472305 LOG(DFATAL) << "Unexpected re_anchor value: " << re_anchor;
472306 return false;
472307
472308 case UNANCHORED: {
472309 if (prog_->anchor_end()) {
472310 // This is a very special case: we don't need the forward DFA because
472311 // we already know where the match must end! Instead, the reverse DFA
472312 // can say whether there is a match and (optionally) where it starts.
472313 Prog* prog = ReverseProg();
472314 if (prog == NULL) {
472315 // Fall back to NFA below.
472316 skipped_test = true;
472317 break;
472318 }
472319 if (!prog->SearchDFA(subtext, text, Prog::kAnchored,
472320 Prog::kLongestMatch, matchp, &dfa_failed, NULL)) {
472321 if (dfa_failed) {
472322 if (options_.log_errors())
472323 LOG(ERROR) << "DFA out of memory: "
472324 << "pattern length " << pattern_->size() << ", "
472325 << "program size " << prog->size() << ", "
472326 << "list count " << prog->list_count() << ", "
472327 << "bytemap range " << prog->bytemap_range();
472328 // Fall back to NFA below.
472329 skipped_test = true;
472330 break;
472331 }
472332 return false;
472333 }
472334 if (matchp == NULL) // Matched. Don't care where.
472335 return true;
472336 break;
472337 }
472338
472339 if (!prog_->SearchDFA(subtext, text, anchor, kind,
472340 matchp, &dfa_failed, NULL)) {
472341 if (dfa_failed) {
472342 if (options_.log_errors())
472343 LOG(ERROR) << "DFA out of memory: "
472344 << "pattern length " << pattern_->size() << ", "
472345 << "program size " << prog_->size() << ", "
472346 << "list count " << prog_->list_count() << ", "
472347 << "bytemap range " << prog_->bytemap_range();
472348 // Fall back to NFA below.
472349 skipped_test = true;
472350 break;
472351 }
472352 return false;
472353 }
472354 if (matchp == NULL) // Matched. Don't care where.
472355 return true;
472356 // SearchDFA set match.end() but didn't know where the
472357 // match started. Run the regexp backward from match.end()
472358 // to find the longest possible match -- that's where it started.
472359 Prog* prog = ReverseProg();
472360 if (prog == NULL) {
472361 // Fall back to NFA below.
472362 skipped_test = true;
472363 break;
472364 }
472365 if (!prog->SearchDFA(match, text, Prog::kAnchored,
472366 Prog::kLongestMatch, &match, &dfa_failed, NULL)) {
472367 if (dfa_failed) {
472368 if (options_.log_errors())
472369 LOG(ERROR) << "DFA out of memory: "
472370 << "pattern length " << pattern_->size() << ", "
472371 << "program size " << prog->size() << ", "
472372 << "list count " << prog->list_count() << ", "
472373 << "bytemap range " << prog->bytemap_range();
472374 // Fall back to NFA below.
472375 skipped_test = true;
472376 break;
472377 }
472378 if (options_.log_errors())
472379 LOG(ERROR) << "SearchDFA inconsistency";
472380 return false;
472381 }
472382 break;
472383 }
472384
472385 case ANCHOR_BOTH:
472386 case ANCHOR_START:
472387 if (re_anchor == ANCHOR_BOTH)
472388 kind = Prog::kFullMatch;
472389 anchor = Prog::kAnchored;
472390
472391 // If only a small amount of text and need submatch
472392 // information anyway and we're going to use OnePass or BitState
472393 // to get it, we might as well not even bother with the DFA:
472394 // OnePass or BitState will be fast enough.
472395 // On tiny texts, OnePass outruns even the DFA, and
472396 // it doesn't have the shared state and occasional mutex that
472397 // the DFA does.
472398 if (can_one_pass && text.size() <= 4096 &&
472399 (ncap > 1 || text.size() <= 16)) {
472400 skipped_test = true;
472401 break;
472402 }
472403 if (can_bit_state && text.size() <= bit_state_text_max_size &&
472404 ncap > 1) {
472405 skipped_test = true;
472406 break;
472407 }
472408 if (!prog_->SearchDFA(subtext, text, anchor, kind,
472409 &match, &dfa_failed, NULL)) {
472410 if (dfa_failed) {
472411 if (options_.log_errors())
472412 LOG(ERROR) << "DFA out of memory: "
472413 << "pattern length " << pattern_->size() << ", "
472414 << "program size " << prog_->size() << ", "
472415 << "list count " << prog_->list_count() << ", "
472416 << "bytemap range " << prog_->bytemap_range();
472417 // Fall back to NFA below.
472418 skipped_test = true;
472419 break;
472420 }
472421 return false;
472422 }
472423 break;
472424 }
472425
472426 if (!skipped_test && ncap <= 1) {
472427 // We know exactly where it matches. That's enough.
472428 if (ncap == 1)
472429 submatch[0] = match;
472430 } else {
472431 StringPiece subtext1;
472432 if (skipped_test) {
472433 // DFA ran out of memory or was skipped:
472434 // need to search in entire original text.
472435 subtext1 = subtext;
472436 } else {
472437 // DFA found the exact match location:
472438 // let NFA run an anchored, full match search
472439 // to find submatch locations.
472440 subtext1 = match;
472441 anchor = Prog::kAnchored;
472442 kind = Prog::kFullMatch;
472443 }
472444
472445 if (can_one_pass && anchor != Prog::kUnanchored) {
472446 if (!prog_->SearchOnePass(subtext1, text, anchor, kind, submatch, ncap)) {
472447 if (!skipped_test && options_.log_errors())
472448 LOG(ERROR) << "SearchOnePass inconsistency";
472449 return false;
472450 }
472451 } else if (can_bit_state && subtext1.size() <= bit_state_text_max_size) {
472452 if (!prog_->SearchBitState(subtext1, text, anchor,
472453 kind, submatch, ncap)) {
472454 if (!skipped_test && options_.log_errors())
472455 LOG(ERROR) << "SearchBitState inconsistency";
472456 return false;
472457 }
472458 } else {
472459 if (!prog_->SearchNFA(subtext1, text, anchor, kind, submatch, ncap)) {
472460 if (!skipped_test && options_.log_errors())
472461 LOG(ERROR) << "SearchNFA inconsistency";
472462 return false;
472463 }
472464 }
472465 }
472466
472467 // Adjust overall match for required prefix that we stripped off.
472468 if (prefixlen > 0 && nsubmatch > 0)
472469 submatch[0] = StringPiece(submatch[0].data() - prefixlen,
472470 submatch[0].size() + prefixlen);
472471
472472 // Zero submatches that don't exist in the regexp.
472473 for (int i = ncap; i < nsubmatch; i++)
472474 submatch[i] = StringPiece();
472475 return true;
472476}

◆ CheckRewriteString()

bool duckdb_re2::RE2::CheckRewriteString ( const StringPiece & rewrite,
std::string * error ) const
472544 {
472545 int max_token = -1;
472546 for (const char *s = rewrite.data(), *end = s + rewrite.size();
472547 s < end; s++) {
472548 int c = *s;
472549 if (c != '\\') {
472550 continue;
472551 }
472552 if (++s == end) {
472553 *error = "Rewrite schema error: '\\' not allowed at end.";
472554 return false;
472555 }
472556 c = *s;
472557 if (c == '\\') {
472558 continue;
472559 }
472560 if (!isdigit(c)) {
472561 *error = "Rewrite schema error: "
472562 "'\\' must be followed by a digit or '\\'.";
472563 return false;
472564 }
472565 int n = (c - '0');
472566 if (max_token < n) {
472567 max_token = n;
472568 }
472569 }
472570
472571 if (max_token > NumberOfCapturingGroups()) {
472572 *error = StringPrintf(
472573 "Rewrite schema requests %d matches, but the regexp only has %d "
472574 "parenthesized subexpressions.",
472575 max_token, NumberOfCapturingGroups());
472576 return false;
472577 }
472578 return true;
472579}

◆ MaxSubmatch()

int duckdb_re2::RE2::MaxSubmatch ( const StringPiece & rewrite)
static
472583 {
472584 int max = 0;
472585 for (const char *s = rewrite.data(), *end = s + rewrite.size();
472586 s < end; s++) {
472587 if (*s == '\\') {
472588 s++;
472589 int c = (s < end) ? *s : -1;
472590 if (isdigit(c)) {
472591 int n = (c - '0');
472592 if (n > max)
472593 max = n;
472594 }
472595 }
472596 }
472597 return max;
472598}

◆ Rewrite()

bool duckdb_re2::RE2::Rewrite ( std::string * out,
const StringPiece & rewrite,
const StringPiece * vec,
int veclen ) const
472605 {
472606 for (const char *s = rewrite.data(), *end = s + rewrite.size();
472607 s < end; s++) {
472608 if (*s != '\\') {
472609 out->push_back(*s);
472610 continue;
472611 }
472612 s++;
472613 int c = (s < end) ? *s : -1;
472614 if (isdigit(c)) {
472615 int n = (c - '0');
472616 if (n >= veclen) {
472617 if (options_.log_errors()) {
472618 LOG(ERROR) << "invalid substitution \\" << n
472619 << " from " << veclen << " groups";
472620 }
472621 return false;
472622 }
472623 StringPiece snip = vec[n];
472624 if (!snip.empty())
472625 out->append(snip.data(), snip.size());
472626 } else if (c == '\\') {
472627 out->push_back('\\');
472628 } else {
472629 if (options_.log_errors())
472630 LOG(ERROR) << "invalid rewrite pattern: " << rewrite.data();
472631 return false;
472632 }
472633 }
472634 return true;
472635}

◆ options()

const Options & duckdb_re2::RE2::options ( ) const
inline
98493{ return options_; }

◆ CRadix()

template<typename T>
RE2::Arg duckdb_re2::RE2::CRadix ( T * ptr)
inlinestatic
98655 {
98656 return RE2::Arg(ptr, [](const char* str, size_t n, void* dest) -> bool {
98657 return re2_internal::Parse(str, n, reinterpret_cast<T*>(dest), 0);
98658 });
98659}
Definition duckdb.cpp:98582

◆ Hex()

template<typename T>
RE2::Arg duckdb_re2::RE2::Hex ( T * ptr)
inlinestatic
98662 {
98663 return RE2::Arg(ptr, [](const char* str, size_t n, void* dest) -> bool {
98664 return re2_internal::Parse(str, n, reinterpret_cast<T*>(dest), 16);
98665 });
98666}

◆ Octal()

template<typename T>
RE2::Arg duckdb_re2::RE2::Octal ( T * ptr)
inlinestatic
98669 {
98670 return RE2::Arg(ptr, [](const char* str, size_t n, void* dest) -> bool {
98671 return re2_internal::Parse(str, n, reinterpret_cast<T*>(dest), 8);
98672 });
98673}

◆ FUZZING_ONLY_set_maximum_global_replace_count()

void duckdb_re2::RE2::FUZZING_ONLY_set_maximum_global_replace_count ( int i)
static
471613 {
471614 maximum_global_replace_count = i;
471615}

◆ Init()

void duckdb_re2::RE2::Init ( const StringPiece & pattern,
const Options & options )
private
471770 {
471771 static std::once_flag empty_once;
471772 std::call_once(empty_once, []() {
471773 (void) new (empty_storage) EmptyStorage;
471774 });
471775
471776 pattern_ = new std::string(pattern);
471777 options_.Copy(options);
471778 entire_regexp_ = NULL;
471779 suffix_regexp_ = NULL;
471780 error_ = empty_string();
471781 error_arg_ = empty_string();
471782
471783 num_captures_ = -1;
471784 error_code_ = NoError;
471785 longest_match_ = options_.longest_match();
471786 is_one_pass_ = false;
471787 prefix_foldcase_ = false;
471788 prefix_.clear();
471789 prog_ = NULL;
471790
471791 rprog_ = NULL;
471792 named_groups_ = NULL;
471793 group_names_ = NULL;
471794
471795 RegexpStatus status;
471796 entire_regexp_ = Regexp::Parse(
471797 *pattern_,
471798 static_cast<Regexp::ParseFlags>(options_.ParseFlags()),
471799 &status);
471800 if (entire_regexp_ == NULL) {
471801 if (options_.log_errors()) {
471802 LOG(ERROR) << "Error parsing '" << trunc(*pattern_) << "': "
471803 << status.Text();
471804 }
471805 error_ = new std::string(status.Text());
471806 error_code_ = RegexpErrorToRE2(status.code());
471807 error_arg_ = new std::string(status.error_arg());
471808 return;
471809 }
471810
471811 bool foldcase;
471812 duckdb_re2::Regexp* suffix;
471813 if (entire_regexp_->RequiredPrefix(&prefix_, &foldcase, &suffix)) {
471814 prefix_foldcase_ = foldcase;
471815 suffix_regexp_ = suffix;
471816 }
471817 else {
471818 suffix_regexp_ = entire_regexp_->Incref();
471819 }
471820
471821 // Two thirds of the memory goes to the forward Prog,
471822 // one third to the reverse prog, because the forward
471823 // Prog has two DFAs but the reverse prog has one.
471824 prog_ = suffix_regexp_->CompileToProg(options_.max_mem()*2/3);
471825 if (prog_ == NULL) {
471826 if (options_.log_errors())
471827 LOG(ERROR) << "Error compiling '" << trunc(*pattern_) << "'";
471828 error_ = new std::string("pattern too large - compile failed");
471829 error_code_ = RE2::ErrorPatternTooLarge;
471830 return;
471831 }
471832
471833 // We used to compute this lazily, but it's used during the
471834 // typical control flow for a match call, so we now compute
471835 // it eagerly, which avoids the overhead of std::once_flag.
471836 num_captures_ = suffix_regexp_->NumCaptures();
471837
471838 // Could delay this until the first match call that
471839 // cares about submatch information, but the one-pass
471840 // machine's memory gets cut from the DFA memory budget,
471841 // and that is harder to do if the DFA has already
471842 // been built.
471843 is_one_pass_ = prog_->IsOnePass();
471844}

◆ DoMatch()

bool duckdb_re2::RE2::DoMatch ( const StringPiece & text,
Anchor re_anchor,
size_t * consumed,
const Arg *const args[],
int n ) const
private
472483 {
472484 if (!ok()) {
472485 if (options_.log_errors())
472486 LOG(ERROR) << "Invalid RE2: " << *error_;
472487 return false;
472488 }
472489
472490 if (NumberOfCapturingGroups() < n) {
472491 // RE has fewer capturing groups than number of Arg pointers passed in.
472492 return false;
472493 }
472494
472495 // Count number of capture groups needed.
472496 int nvec;
472497 if (n == 0 && consumed == NULL)
472498 nvec = 0;
472499 else
472500 nvec = n+1;
472501
472502 StringPiece* vec;
472503 StringPiece stkvec[kVecSize];
472504 StringPiece* heapvec = NULL;
472505
472506 if (nvec <= static_cast<int>(arraysize(stkvec))) {
472507 vec = stkvec;
472508 } else {
472509 vec = new StringPiece[nvec];
472510 heapvec = vec;
472511 }
472512
472513 if (!Match(text, 0, text.size(), re_anchor, vec, nvec)) {
472514 delete[] heapvec;
472515 return false;
472516 }
472517
472518 if (consumed != NULL)
472519 *consumed = static_cast<size_t>(EndPtr(vec[0]) - BeginPtr(text));
472520
472521 if (n == 0 || args == NULL) {
472522 // We are not interested in results
472523 delete[] heapvec;
472524 return true;
472525 }
472526
472527 // If we got here, we must have matched the whole pattern.
472528 for (int i = 0; i < n; i++) {
472529 const StringPiece& s = vec[i+1];
472530 if (!args[i]->Parse(s.data(), s.size())) {
472531 // TODO: Should we indicate what the error was?
472532 delete[] heapvec;
472533 return false;
472534 }
472535 }
472536
472537 delete[] heapvec;
472538 return true;
472539}

◆ ReverseProg()

duckdb_re2::Prog * duckdb_re2::RE2::ReverseProg ( ) const
private
471847 {
471848 std::call_once(rprog_once_, [](const RE2* re) {
471849 re->rprog_ =
471850 re->suffix_regexp_->CompileToReverseProg(re->options_.max_mem() / 3);
471851 if (re->rprog_ == NULL) {
471852 if (re->options_.log_errors())
471853 LOG(ERROR) << "Error reverse compiling '" << trunc(*re->pattern_)
471854 << "'";
471855 // We no longer touch error_ and error_code_ because failing to compile
471856 // the reverse Prog is not a showstopper: falling back to NFA execution
471857 // is fine. More importantly, an RE2 object is supposed to be logically
471858 // immutable: whatever ok() would have returned after Init() completed,
471859 // it should continue to return that no matter what ReverseProg() does.
471860 }
471861 }, this);
471862 return rprog_;
471863}

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