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 — a set of patterns tested together. Mirrors real RE2's Set: buffer patterns with Add, Compile once, then Match repeatedly. Maps directly onto REAL's native real::regex_set::which() (index-list semantics match exactly). More...
 

Public Types

enum class  ErrorCode : std::uint8_t { NoError , ErrorSyntax , ErrorUnsupported }
 A coarse error taxonomy — this layer does not reproduce RE2's fine-grained, 14-value ErrorCode (REAL's own parser has a different internal classification); error() always carries the human-readable 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 Optionsoptions () 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.
 

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

Detailed Description

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

Copyable and movable (real RE2 deletes both, for pointer-stability reasons this wrapper does not share — real::regex copies cheaply). A pattern this layer cannot honor does not throw: construction always succeeds syntactically, and ok()/error()/error_code() report the rejection, exactly mirroring how RE2 itself reports a syntax error.

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.

◆ ErrorCode

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

A coarse error taxonomy — this layer does not reproduce RE2's fine-grained, 14-value ErrorCode (REAL's own parser has a different internal classification); error() always carries the human-readable 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()).

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.

A separate overload from the std::string_view one below, not merely a call site of it: std::string's own conversion to std::string_view is itself user-defined, and C++ allows at most one user-defined conversion in an implicit sequence, so a lone string_view constructor would make FullMatch(text, some_std_string, &arg) stop compiling implicitly (real RE2 keeps the same three-constructor split for the same reason).

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.

Empty-match policy matches real RE2's GlobalReplace: a zero-width match whose start equals the end of the previous (accepted) match is skipped — it abuts the prior match and must not produce a second rewrite (e.g. a* on "aa" yields one replacement, not two). Legitimate non-abutting empty matches (e.g. a* on "bbb"#b#b#b#) are still applied.

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.

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

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

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

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