RE2-compatible drop-in for RE2. Backed by real::regex — linear-time, ReDoS-safe.
More...
#include <re2.hpp>
|
| enum | CannedOptions : std::uint8_t { DefaultOptions = 0
, Latin1
, POSIX
, Quiet
} |
| | RE2's canned option sets, convertible to Options as RE2's are, so RE2 re(p, RE2::Quiet) reads as it does there. Latin1 and POSIX select what this layer rejects at construction (see Options). More...
|
| |
| enum class | ErrorCode : std::uint8_t { NoError
, ErrorSyntax
, ErrorUnsupported
, ErrorPatternTooLarge
} |
| | A coarse error taxonomy, not RE2's 14 codes (REAL's parser classifies differently); error() carries the detail. More...
|
| |
| enum class | Anchor : std::uint8_t { UNANCHORED
, ANCHOR_START
, ANCHOR_BOTH
} |
| | Where a Set member (or the primitive Match, not exposed here) is anchored. More...
|
| |
|
| | RE2 (const char *pattern) |
| | Compiles pattern with default options. Implicit, like real RE2's own pattern constructors — so FullMatch(text, "a.*b", &arg) builds a temporary RE2 in place.
|
| |
| | RE2 (const std::string &pattern) |
| | Compiles pattern with default options. Implicit; see the const char* overload.
|
| |
| | RE2 (std::string_view pattern) |
| | Compiles pattern with default options. Implicit; see the const char* overload.
|
| |
| | RE2 (std::string_view pattern, const Options &options) |
| | Compiles pattern with options.
|
| |
| bool | ok () const noexcept |
| | Whether construction succeeded (error_code() == ErrorCode::NoError).
|
| |
| const std::string & | pattern () const noexcept |
| | The pattern text this RE2 was built from.
|
| |
| const std::string & | error () const noexcept |
| | The rejection reason, or an empty string if ok().
|
| |
| ErrorCode | error_code () const noexcept |
| | The coarse rejection category, or ErrorCode::NoError if ok().
|
| |
| const Options & | options () const noexcept |
| | The construction options this RE2 was built with.
|
| |
| int | NumberOfCapturingGroups () const noexcept |
| | The number of capturing groups (excluding group 0), or 0 if !ok().
|
| |
|
| template<typename ... Args> |
| static bool | FullMatch (std::string_view text, const RE2 &re, Args &&... args) |
| | Anchored-both match: the whole text must match re.
|
| |
| template<typename ... Args> |
| static bool | PartialMatch (std::string_view text, const RE2 &re, Args &&... args) |
| | Unanchored match: re must match some substring of text.
|
| |
| template<typename ... Args> |
| static bool | Consume (std::string_view *input, const RE2 &re, Args &&... args) |
| | Anchored-start match against *input; on success, removes the matched prefix from *input.
|
| |
| template<typename ... Args> |
| static bool | FindAndConsume (std::string_view *input, const RE2 &re, Args &&... args) |
| | Unanchored match anywhere in *input; on success, removes everything up to and including the match from *input.
|
| |
| static bool | Replace (std::string *str, const RE2 &re, std::string_view rewrite) |
| | Replaces the first match of re in *str with rewrite.
|
| |
| static int | GlobalReplace (std::string *str, const RE2 &re, std::string_view rewrite) |
| | Replaces every non-overlapping match of re in *str with rewrite.
|
| |
| static std::string | QuoteMeta (std::string_view unquoted) |
| | Escapes every regex metacharacter in unquoted so the result matches it literally.
|
| |
|
| void | init (std::string_view pattern, const Options &options) |
| | Compiles pattern_ under options, or records why it could not be compiled.
|
| |
|
| static std::size_t | program_bytes (const real::regex &re) noexcept |
| | The bytes of re's compiled program: every table it holds, as RE2 counts its program (the pattern's own copy and the tables' spare capacity are not counted).
|
| |
| static constexpr bool | within_budget (std::size_t bytes, std::size_t instructions, std::int64_t max_mem) noexcept |
| | Whether a program of bytes and instructions fits max_mem as RE2 bounds it: two thirds of the budget for the program, and 100 000 instructions when those two thirds are not positive.
|
| |
| static real::flags | options_to_flags (const Options &options) |
| | Translates the Options fields this layer honors into real::flags.
|
| |
| static std::string_view | unsupported_option (const Options &options) |
| | The first unsupported-option rejection reason for options, if any.
|
| |
| static void | log_error (const Options &options, std::string_view what, std::string_view pattern, std::string_view detail) |
| | Writes the message RE2 writes to stderr on a failed compile, once an explicit set_log_errors(true) armed the logging: "Error parsing" with the pattern and the error for a pattern it cannot parse, "Error compiling" with the pattern for one too large.
|
| |
| static std::string | trunc (std::string_view pattern) |
| | The pattern as RE2 prints it in a compile error: its first 100 bytes and ... past that.
|
| |
| template<typename Result , typename ... Args> |
| static bool | extract (const Result &match, Args &&... args) |
| | Extracts args from a match result, RE2's own DoMatch contract: too few groups, no match, or any one extraction failing fails the whole call.
|
| |
| template<typename Result > |
| static bool | expand_rewrite (std::string &out, std::string_view rewrite, const Result &match) |
| | Expands an RE2-style rewrite template (\0…\9, \\) against match, appending to out.
|
| |
|
|
std::optional< real::regex > | regex_ |
| | Engaged only if compilation succeeded.
|
| |
|
std::string | pattern_ |
| | The original pattern text.
|
| |
|
std::string | error_ |
| | The rejection reason, if any.
|
| |
|
ErrorCode | error_code_ {ErrorCode::NoError} |
| | The rejection category.
|
| |
|
int | num_captures_ {} |
| | Capturing-group count (excl. group 0).
|
| |
|
bool | longest_match_ {} |
| | Cached options_.longest_match().
|
| |
|
Options | options_ |
| | The construction options.
|
| |
|
|
static constexpr std::string_view | pattern_too_large {"pattern too large - compile failed"} |
| | RE2's text for ErrorPatternTooLarge.
|
| |
RE2-compatible drop-in for RE2. Backed by real::regex — linear-time, ReDoS-safe.
Copyable and movable, where RE2 deletes both (real::regex copies cheaply). A rejected pattern does not throw: ok(), error() and error_code() report it, as RE2 does.
◆ Anchor
Where a Set member (or the primitive Match, not exposed here) is anchored.
| Enumerator |
|---|
| UNANCHORED | Match anywhere in the text.
|
| ANCHOR_START | Match must start at the beginning of the text.
|
| ANCHOR_BOTH | Match must span the entire text.
|
◆ CannedOptions
RE2's canned option sets, convertible to Options as RE2's are, so RE2 re(p, RE2::Quiet) reads as it does there. Latin1 and POSIX select what this layer rejects at construction (see Options).
| Enumerator |
|---|
| DefaultOptions | The default options.
|
| Latin1 | EncodingLatin1: rejected at construction.
|
| POSIX | posix_syntax and longest_match: rejected at construction.
|
| Quiet | log_errors off.
|
◆ ErrorCode
A coarse error taxonomy, not RE2's 14 codes (REAL's parser classifies differently); error() carries the detail.
| Enumerator |
|---|
| NoError | ok() == true.
|
| ErrorSyntax | The pattern is malformed (rejected by RE2 too).
|
| ErrorUnsupported | Well-formed, but outside this layer's supported subset (see error()).
|
| ErrorPatternTooLarge | The compiled pattern exceeds max_mem, or REAL's own program bound.
|
◆ RE2() [1/4]
| real::compat::re2::RE2::RE2 |
( |
const char * |
pattern | ) |
|
|
inline |
Compiles pattern with default options. Implicit, like real RE2's own pattern constructors — so FullMatch(text, "a.*b", &arg) builds a temporary RE2 in place.
- Parameters
-
| [in] | pattern | The pattern text (NUL-terminated). |
◆ RE2() [2/4]
| real::compat::re2::RE2::RE2 |
( |
const std::string & |
pattern | ) |
|
|
inline |
Compiles pattern with default options. Implicit; see the const char* overload.
Not mergeable into the std::string_view overload: a std::string would then need two user-defined conversions to become an RE2, and FullMatch(text, some_string, &arg) would stop compiling.
- Parameters
-
| [in] | pattern | The pattern text. |
◆ RE2() [3/4]
| real::compat::re2::RE2::RE2 |
( |
std::string_view |
pattern | ) |
|
|
inline |
Compiles pattern with default options. Implicit; see the const char* overload.
- Parameters
-
| [in] | pattern | The pattern text. |
◆ RE2() [4/4]
| real::compat::re2::RE2::RE2 |
( |
std::string_view |
pattern, |
|
|
const Options & |
options |
|
) |
| |
|
inline |
Compiles pattern with options.
- Parameters
-
| [in] | pattern | The pattern text. |
| [in] | options | The construction options. |
◆ Consume()
template<typename ... Args>
| static bool real::compat::re2::RE2::Consume |
( |
std::string_view * |
input, |
|
|
const RE2 & |
re, |
|
|
Args &&... |
args |
|
) |
| |
|
inlinestatic |
Anchored-start match against *input; on success, removes the matched prefix from *input.
- Template Parameters
-
| Args | Destination pointer types (deduced), one per submatch extracted. |
- Parameters
-
| [in,out] | input | The subject text; shrunk from the front on a successful match. |
| [in] | re | The pattern (an RE2, or a pattern text implicitly converted to one). |
| [out] | args | Destinations for the first sizeof...(Args) capturing groups (see Arg). |
- Returns
true on a match with every args extraction succeeding.
◆ error()
| const std::string & real::compat::re2::RE2::error |
( |
| ) |
const |
|
inlinenoexcept |
The rejection reason, or an empty string if ok().
- Returns
- The message, empty when construction succeeded.
◆ error_code()
| ErrorCode real::compat::re2::RE2::error_code |
( |
| ) |
const |
|
inlinenoexcept |
◆ expand_rewrite()
template<typename Result >
| static bool real::compat::re2::RE2::expand_rewrite |
( |
std::string & |
out, |
|
|
std::string_view |
rewrite, |
|
|
const Result & |
match |
|
) |
| |
|
inlinestaticprivate |
Expands an RE2-style rewrite template (\0…\9, \\) against match, appending to out.
- Template Parameters
-
- Parameters
-
| [out] | out | Appended to on success (left unspecified on failure). |
| [in] | rewrite | The rewrite template. |
| [in] | match | The match whose groups \N refers to. |
- Returns
true if rewrite was well-formed (every \N in range, no trailing backslash).
◆ extract()
template<typename Result , typename ... Args>
| static bool real::compat::re2::RE2::extract |
( |
const Result & |
match, |
|
|
Args &&... |
args |
|
) |
| |
|
inlinestaticprivate |
Extracts args from a match result, RE2's own DoMatch contract: too few groups, no match, or any one extraction failing fails the whole call.
- Template Parameters
-
- Parameters
-
| [in] | match | The match attempt's result. |
| [out] | args | Destinations for the first sizeof...(Args) capturing groups. |
- Returns
true on a match with every extraction succeeding.
◆ FindAndConsume()
template<typename ... Args>
| static bool real::compat::re2::RE2::FindAndConsume |
( |
std::string_view * |
input, |
|
|
const RE2 & |
re, |
|
|
Args &&... |
args |
|
) |
| |
|
inlinestatic |
Unanchored match anywhere in *input; on success, removes everything up to and including the match from *input.
- Template Parameters
-
| Args | Destination pointer types (deduced), one per submatch extracted. |
- Parameters
-
| [in,out] | input | The subject text; shrunk from the front on a successful match. |
| [in] | re | The pattern (an RE2, or a pattern text implicitly converted to one). |
| [out] | args | Destinations for the first sizeof...(Args) capturing groups (see Arg). |
- Returns
true on a match with every args extraction succeeding.
◆ FullMatch()
template<typename ... Args>
| static bool real::compat::re2::RE2::FullMatch |
( |
std::string_view |
text, |
|
|
const RE2 & |
re, |
|
|
Args &&... |
args |
|
) |
| |
|
inlinestatic |
Anchored-both match: the whole text must match re.
- Template Parameters
-
| Args | Destination pointer types (deduced), one per submatch extracted. |
- Parameters
-
| [in] | text | The subject text. |
| [in] | re | The pattern (an RE2, or a pattern text implicitly converted to one). |
| [out] | args | Destinations for the first sizeof...(Args) capturing groups (see Arg); pass none to just test for a match. |
- Returns
true on a full match with every args extraction succeeding.
◆ GlobalReplace()
| static int real::compat::re2::RE2::GlobalReplace |
( |
std::string * |
str, |
|
|
const RE2 & |
re, |
|
|
std::string_view |
rewrite |
|
) |
| |
|
inlinestatic |
Replaces every non-overlapping match of re in *str with rewrite.
rewrite may reference groups RE2-style: \0 (whole match), \1…\9, \\ for a literal backslash.
As in RE2, an empty match starting where the previous match ended is skipped (a* over aa gives one replacement); other empty matches are replaced (a* over bbb gives #b#b#b#).
- Parameters
-
| [in,out] | str | The subject; rewritten in place only if every match's rewrite expansion succeeds. |
| [in] | re | The pattern (an RE2, or a pattern text implicitly converted to one). |
| [in] | rewrite | The replacement template. |
- Returns
- The number of replacements made (
0 if none, or if rewrite was malformed).
◆ init()
| void real::compat::re2::RE2::init |
( |
std::string_view |
pattern, |
|
|
const Options & |
options |
|
) |
| |
|
inlineprivate |
Compiles pattern_ under options, or records why it could not be compiled.
- Parameters
-
| [in] | pattern | The pattern text (pre-literal()-expansion). |
| [in] | options | The construction options. |
◆ log_error()
| static void real::compat::re2::RE2::log_error |
( |
const Options & |
options, |
|
|
std::string_view |
what, |
|
|
std::string_view |
pattern, |
|
|
std::string_view |
detail |
|
) |
| |
|
inlinestaticprivate |
Writes the message RE2 writes to stderr on a failed compile, once an explicit set_log_errors(true) armed the logging: "Error parsing" with the pattern and the error for a pattern it cannot parse, "Error compiling" with the pattern for one too large.
- Parameters
-
| [in] | options | The options the compile ran under. |
| [in] | what | "Error parsing" or "Error compiling". |
| [in] | pattern | The pattern, as RE2 prints it. |
| [in] | detail | The error, or empty for none. |
◆ NumberOfCapturingGroups()
| int real::compat::re2::RE2::NumberOfCapturingGroups |
( |
| ) |
const |
|
inlinenoexcept |
The number of capturing groups (excluding group 0), or 0 if !ok().
- Returns
- The capturing-group count.
◆ ok()
| bool real::compat::re2::RE2::ok |
( |
| ) |
const |
|
inlinenoexcept |
◆ options()
| const Options & real::compat::re2::RE2::options |
( |
| ) |
const |
|
inlinenoexcept |
The construction options this RE2 was built with.
- Returns
- The options, valid as long as this object is alive.
◆ options_to_flags()
Translates the Options fields this layer honors into real::flags.
- Parameters
-
| [in] | options | The options to translate. |
- Returns
- The equivalent real::flags set.
◆ PartialMatch()
template<typename ... Args>
| static bool real::compat::re2::RE2::PartialMatch |
( |
std::string_view |
text, |
|
|
const RE2 & |
re, |
|
|
Args &&... |
args |
|
) |
| |
|
inlinestatic |
Unanchored match: re must match some substring of text.
- Template Parameters
-
| Args | Destination pointer types (deduced), one per submatch extracted. |
- Parameters
-
| [in] | text | The subject text. |
| [in] | re | The pattern (an RE2, or a pattern text implicitly converted to one). |
| [out] | args | Destinations for the first sizeof...(Args) capturing groups (see Arg); pass none to just test for a match. |
- Returns
true on a match with every args extraction succeeding.
◆ pattern()
| const std::string & real::compat::re2::RE2::pattern |
( |
| ) |
const |
|
inlinenoexcept |
The pattern text this RE2 was built from.
- Returns
- The pattern, valid as long as this object is alive.
◆ program_bytes()
| static std::size_t real::compat::re2::RE2::program_bytes |
( |
const real::regex & |
re | ) |
|
|
inlinestaticprivatenoexcept |
The bytes of re's compiled program: every table it holds, as RE2 counts its program (the pattern's own copy and the tables' spare capacity are not counted).
- Parameters
-
| [in] | re | A compiled pattern. |
- Returns
- Its size in bytes.
◆ QuoteMeta()
| static std::string real::compat::re2::RE2::QuoteMeta |
( |
std::string_view |
unquoted | ) |
|
|
inlinestatic |
Escapes every regex metacharacter in unquoted so the result matches it literally.
- Parameters
-
| [in] | unquoted | The raw text. |
- Returns
- The escaped pattern text.
◆ Replace()
| static bool real::compat::re2::RE2::Replace |
( |
std::string * |
str, |
|
|
const RE2 & |
re, |
|
|
std::string_view |
rewrite |
|
) |
| |
|
inlinestatic |
Replaces the first match of re in *str with rewrite.
rewrite may reference groups RE2-style: \0 (whole match), \1…\9, \\ for a literal backslash.
- Parameters
-
| [in,out] | str | The subject; rewritten in place only on success. |
| [in] | re | The pattern (an RE2, or a pattern text implicitly converted to one). |
| [in] | rewrite | The replacement template. |
- Returns
true if a match was found and rewrite was well-formed.
◆ trunc()
| static std::string real::compat::re2::RE2::trunc |
( |
std::string_view |
pattern | ) |
|
|
inlinestaticprivate |
The pattern as RE2 prints it in a compile error: its first 100 bytes and ... past that.
- Parameters
-
- Returns
- The printed form.
◆ unsupported_option()
| static std::string_view real::compat::re2::RE2::unsupported_option |
( |
const Options & |
options | ) |
|
|
inlinestaticprivate |
The first unsupported-option rejection reason for options, if any.
- Parameters
-
| [in] | options | The options to check. |
- Returns
- A human-readable reason, or an empty view if every option is supported.
◆ within_budget()
| static constexpr bool real::compat::re2::RE2::within_budget |
( |
std::size_t |
bytes, |
|
|
std::size_t |
instructions, |
|
|
std::int64_t |
max_mem |
|
) |
| |
|
inlinestaticconstexprprivatenoexcept |
Whether a program of bytes and instructions fits max_mem as RE2 bounds it: two thirds of the budget for the program, and 100 000 instructions when those two thirds are not positive.
- Parameters
-
- Returns
- True when it fits.
The documentation for this class was generated from the following file: