REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::compat::re2::RE2 Class Reference

RE2-compatible drop-in for RE2. Backed by real::regex — linear-time, ReDoS-safe. More...

#include <re2.hpp>

Collaboration diagram for real::compat::re2::RE2:
[legend]

Classes

class  Options
 RE2-compatible construction options. Mirrors real RE2's RE2::Options field-for-field (names, defaults); see the file-level doc comment for which fields this layer honors. More...
 
class  Set
 RE2::Set: Add patterns, Compile once, then Match repeatedly, on real::regex_set::which(). The anchor is applied by wrapping each pattern, real::regex_set being unanchored. More...
 

Public Types

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...
 

Public Member Functions

 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().
 

Static Public Member Functions

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.
 

Static Public Attributes

static constexpr Anchor UNANCHORED {Anchor::UNANCHORED}
 As RE2's RE2::UNANCHORED.
 
static constexpr Anchor ANCHOR_START {Anchor::ANCHOR_START}
 As RE2's RE2::ANCHOR_START.
 
static constexpr Anchor ANCHOR_BOTH {Anchor::ANCHOR_BOTH}
 As RE2's RE2::ANCHOR_BOTH.
 
static constexpr ErrorCode NoError {ErrorCode::NoError}
 As RE2's RE2::NoError.
 
static constexpr ErrorCode ErrorSyntax {ErrorCode::ErrorSyntax}
 This layer's syntax category.
 
static constexpr ErrorCode ErrorUnsupported {ErrorCode::ErrorUnsupported}
 This layer's unsupported category.
 
static constexpr ErrorCode ErrorPatternTooLarge {ErrorCode::ErrorPatternTooLarge}
 As RE2's RE2::ErrorPatternTooLarge.
 

Private Member Functions

void init (std::string_view pattern, const Options &options)
 Compiles pattern_ under options, or records why it could not be compiled.
 

Static Private Member Functions

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.
 

Private Attributes

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 Private Attributes

static constexpr std::string_view pattern_too_large {"pattern too large - compile failed"}
 RE2's text for ErrorPatternTooLarge.
 

Detailed Description

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.

Member Enumeration Documentation

◆ Anchor

enum class real::compat::re2::RE2::Anchor : std::uint8_t
strong

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

enum class real::compat::re2::RE2::ErrorCode : std::uint8_t
strong

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.

Constructor & Destructor Documentation

◆ 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]patternThe 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]patternThe 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]patternThe pattern text.

◆ RE2() [4/4]

real::compat::re2::RE2::RE2 ( std::string_view  pattern,
const Options &  options 
)
inline

Compiles pattern with options.

Parameters
[in]patternThe pattern text.
[in]optionsThe construction options.

Member Function Documentation

◆ 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
ArgsDestination pointer types (deduced), one per submatch extracted.
Parameters
[in,out]inputThe subject text; shrunk from the front on a successful match.
[in]reThe pattern (an RE2, or a pattern text implicitly converted to one).
[out]argsDestinations 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

The coarse rejection category, or ErrorCode::NoError if ok().

Returns
The category, ErrorCode::NoError when construction succeeded.

◆ 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
ResultThe match-result type (real::regex::result_type).
Parameters
[out]outAppended to on success (left unspecified on failure).
[in]rewriteThe rewrite template.
[in]matchThe 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
ResultThe match-result type (real::regex::result_type).
ArgsDestination pointer types (deduced).
Parameters
[in]matchThe match attempt's result.
[out]argsDestinations 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
ArgsDestination pointer types (deduced), one per submatch extracted.
Parameters
[in,out]inputThe subject text; shrunk from the front on a successful match.
[in]reThe pattern (an RE2, or a pattern text implicitly converted to one).
[out]argsDestinations 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
ArgsDestination pointer types (deduced), one per submatch extracted.
Parameters
[in]textThe subject text.
[in]reThe pattern (an RE2, or a pattern text implicitly converted to one).
[out]argsDestinations 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]strThe subject; rewritten in place only if every match's rewrite expansion succeeds.
[in]reThe pattern (an RE2, or a pattern text implicitly converted to one).
[in]rewriteThe 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]patternThe pattern text (pre-literal()-expansion).
[in]optionsThe 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]optionsThe options the compile ran under.
[in]what"Error parsing" or "Error compiling".
[in]patternThe pattern, as RE2 prints it.
[in]detailThe 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

Whether construction succeeded (error_code() == ErrorCode::NoError).

Returns
Whether the pattern compiled.

◆ 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()

static real::flags real::compat::re2::RE2::options_to_flags ( const Options &  options)
inlinestaticprivate

Translates the Options fields this layer honors into real::flags.

Parameters
[in]optionsThe 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
ArgsDestination pointer types (deduced), one per submatch extracted.
Parameters
[in]textThe subject text.
[in]reThe pattern (an RE2, or a pattern text implicitly converted to one).
[out]argsDestinations 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]reA 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]unquotedThe 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]strThe subject; rewritten in place only on success.
[in]reThe pattern (an RE2, or a pattern text implicitly converted to one).
[in]rewriteThe 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
[in]patternThe pattern.
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]optionsThe 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
[in]bytesThe program's size (program_bytes).
[in]instructionsIts instruction count.
[in]max_memThe budget (Options::max_mem).
Returns
True when it fits.

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