REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::compat::basic_regex< CharT, Traits > Class Template Reference

A std::basic_regex-compatible pattern, backed by real where proven, else std. More...

#include <regex_core.hpp>

Public Types

using value_type = CharT
 Character type.
 
using flag_type = regex_constants::syntax_option_type
 Option type.
 
using string_type = std::basic_string< CharT >
 Pattern string type.
 

Public Member Functions

 basic_regex ()=default
 An empty pattern on the std backend — the variant's first alternative default-constructs.
 
 basic_regex (const CharT *pattern, flag_type f=regex_constants::ECMAScript, policy pol=policy::strict)
 Compiles pattern from a C string.
 
 basic_regex (const string_type &pattern, flag_type f=regex_constants::ECMAScript, policy pol=policy::strict)
 Compiles pattern from an owned string.
 
 basic_regex (const CharT *pattern, std::size_t len, flag_type f=regex_constants::ECMAScript, policy pol=policy::strict)
 Compiles the first len characters of pattern, which need not be NUL-terminated.
 
template<typename It >
 basic_regex (It begin, It end, flag_type f=regex_constants::ECMAScript, policy pol=policy::strict)
 Compiles the pattern in [begin, end).
 
std::size_t mark_count () const noexcept
 Number of marked sub-expressions (excluding group 0), as std::basic_regex.
 
flag_type flags () const noexcept
 The flags this regex was built with.
 
void swap (basic_regex &other) noexcept
 Exchanges engines, flags, policy and cached state with other.
 
bool uses_real () const noexcept
 True if this regex is backed by the real engine (vs the std fallback).
 
bool uses_fallback () const noexcept
 True if this regex fell back to std::regex (a policy::fallback regex on an ineligible pattern) — so this pattern is not linear-time / ReDoS-safe. Always false under strict.
 
compat::policy policy () const noexcept
 The drop-in policy this regex was constructed with.
 
const std::variant< std::basic_regex< CharT, Traits >, real::regex > & engine () const noexcept
 Access the active backend (engine-facing; used by the free functions).
 
bool nullable () const noexcept
 Whether the pattern can match the empty string (real's empty_match_possible hint).
 
bool posix_longest () const noexcept
 Whether this is a POSIX pattern routed to REAL: search/match must use leftmost-**longest** bounds (the POSIX semantics), not the default leftmost-first. Set for any of the five POSIX grammars once detail::translate_posix has translated it onto REAL; a pattern that stays on the std backend leaves it false, since std applies POSIX semantics itself.
 
bool uses_real_traversal () const noexcept
 Whether replace/iterate run on the real traversal (real-backed AND non-nullable AND no nullable captured-repeat group). A nullable pattern delegates replace/iterate to std (the empty-match traversal differs; and iterating a nullable pattern whose per-position match cost is O(n) is O(n²) on any linear engine, so routing it buys correctness but not a linear guarantee — see the nullable note in COMPATIBILITY.md). A pattern with a capturing group that is nullable under a quantifier ((ab|)+a) is itself non-nullable as a whole, but real's last-consuming-iteration capture (RE2/Rust/Go lineage) diverges from an ECMAScript backtracker's extra empty final iteration on that GROUP's span — so it routes too, for the same reason: regex_search/match are unaffected (see the nullable-loop group-capture section of COMPATIBILITY.md — the search residue is intentional, not an oversight).
 
const std::basic_regex< CharT, Traits > & std_engine () const
 The std::regex for the std / lazy-std path (built once on demand for a real-backed pattern reached via a constraining flag / nullable replace-iterate / $0/sed replace).
 

Private Member Functions

void assign (std::basic_string_view< CharT > pattern, flag_type f)
 Compiles pattern into this object, replacing whatever it held.
 
void reject_or_fallback (std::string_view sv, flag_type f, const std::string &reason)
 The policy branch for a pattern the linear engine cannot represent: strict throws regex_error with error_complexity and a REAL-identifiable message; fallback delegates it to std::regex.
 
void emplace_std (std::string_view sv, flag_type f)
 Compiles sv on the standard-library backend and stores it.
 

Private Attributes

std::variant< std::basic_regex< CharT, Traits >, real::regexengine_
 Whichever backend compiled this pattern; see uses_real and uses_fallback.
 
string_type pattern_
 Original pattern (for the lazy std build).
 
flag_type flags_ {regex_constants::ECMAScript}
 Syntax options it was compiled with (flags).
 
std::size_t mark_count_ {}
 Capturing groups excluding the whole match (mark_count).
 
bool nullable_ {}
 empty_match_possible (real-backed).
 
bool nullable_captured_repeat_ {}
 nullable_captured_repeat (real-backed) — a nullable capturing group under a quantifier; see uses_real_traversal.
 
bool posix_longest_ {}
 A POSIX grammar translated onto REAL: search uses leftmost-longest bounds.
 
std::optional< std::basic_regex< CharT, Traits > > lazy_std_
 Lazy std for nullable replace/iterate.
 
compat::policy policy_ {policy::strict}
 strict rejects ineligible, fallback delegates to std.
 

Detailed Description

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
class real::compat::basic_regex< CharT, Traits >

A std::basic_regex-compatible pattern, backed by real where proven, else std.

Template Parameters
CharTCharacter type (char; other types route straight to std).
TraitsRegex traits (std parity).

Constructor & Destructor Documentation

◆ basic_regex() [1/4]

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
real::compat::basic_regex< CharT, Traits >::basic_regex ( const CharT *  pattern,
flag_type  f = regex_constants::ECMAScript,
policy  pol = policy::strict 
)
inlineexplicit

Compiles pattern from a C string.

Parameters
[in]patternNUL-terminated pattern text.
[in]fSyntax options; the grammar they select may route the pattern to std.
[in]polStrict rejects a pattern REAL cannot represent linearly; fallback routes it to std.
Exceptions
real::compat::regex_erroron an invalid pattern, or on a strict-policy rejection.

◆ basic_regex() [2/4]

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
real::compat::basic_regex< CharT, Traits >::basic_regex ( const string_type pattern,
flag_type  f = regex_constants::ECMAScript,
policy  pol = policy::strict 
)
inlineexplicit

Compiles pattern from an owned string.

Parameters
[in]patternThe pattern text.
[in]fSyntax options.
[in]polRejection policy; see the C-string overload.
Exceptions
real::compat::regex_erroron an invalid pattern, or on a strict-policy rejection.

◆ basic_regex() [3/4]

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
real::compat::basic_regex< CharT, Traits >::basic_regex ( const CharT *  pattern,
std::size_t  len,
flag_type  f = regex_constants::ECMAScript,
policy  pol = policy::strict 
)
inline

Compiles the first len characters of pattern, which need not be NUL-terminated.

Parameters
[in]patternPattern text.
[in]lenIts length in characters.
[in]fSyntax options.
[in]polRejection policy.
Exceptions
real::compat::regex_erroron an invalid pattern, or on a strict-policy rejection.

◆ basic_regex() [4/4]

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
template<typename It >
real::compat::basic_regex< CharT, Traits >::basic_regex ( It  begin,
It  end,
flag_type  f = regex_constants::ECMAScript,
policy  pol = policy::strict 
)
inline

Compiles the pattern in [begin, end).

Parameters
[in]beginStart of the pattern text.
[in]endOne past its end.
[in]fSyntax options.
[in]polRejection policy.
Exceptions
real::compat::regex_erroron an invalid pattern, or on a strict-policy rejection.

Member Function Documentation

◆ assign()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
void real::compat::basic_regex< CharT, Traits >::assign ( std::basic_string_view< CharT >  pattern,
flag_type  f 
)
inlineprivate

Compiles pattern into this object, replacing whatever it held.

Parameters
[in]patternThe pattern text.
[in]fSyntax options.
Exceptions
real::compat::regex_erroron an invalid pattern, or on a strict-policy rejection.

◆ emplace_std()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
void real::compat::basic_regex< CharT, Traits >::emplace_std ( std::string_view  sv,
flag_type  f 
)
inlineprivate

Compiles sv on the standard-library backend and stores it.

Parameters
[in]svThe pattern text.
[in]fSyntax options, translated by detail::to_std.

◆ engine()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
const std::variant< std::basic_regex< CharT, Traits >, real::regex > & real::compat::basic_regex< CharT, Traits >::engine ( ) const
inlinenoexcept

Access the active backend (engine-facing; used by the free functions).

Returns
The variant holding whichever backend compiled this pattern.

◆ flags()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
flag_type real::compat::basic_regex< CharT, Traits >::flags ( ) const
inlinenoexcept

The flags this regex was built with.

Returns
Those flags.

◆ mark_count()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
std::size_t real::compat::basic_regex< CharT, Traits >::mark_count ( ) const
inlinenoexcept

Number of marked sub-expressions (excluding group 0), as std::basic_regex.

Returns
The group count.

◆ nullable()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
bool real::compat::basic_regex< CharT, Traits >::nullable ( ) const
inlinenoexcept

Whether the pattern can match the empty string (real's empty_match_possible hint).

Empty-match traversal (replace / iterate) follows Python's advance rules in real, which differ from ECMAScript. So a nullable real-backed pattern routes those operations to a lazily built std::regex (std_engine) — per operation, not at construction, so search/match keep real's linear-time guarantee even on nullable-ReDoS patterns like (a*)*.

Returns
true if it is nullable; regex_replace then routes to std, whose empty-match traversal differs from REAL's Python-lineage one.

◆ policy()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
compat::policy real::compat::basic_regex< CharT, Traits >::policy ( ) const
inlinenoexcept

The drop-in policy this regex was constructed with.

Returns
Strict or fallback.

◆ posix_longest()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
bool real::compat::basic_regex< CharT, Traits >::posix_longest ( ) const
inlinenoexcept

Whether this is a POSIX pattern routed to REAL: search/match must use leftmost-**longest** bounds (the POSIX semantics), not the default leftmost-first. Set for any of the five POSIX grammars once detail::translate_posix has translated it onto REAL; a pattern that stays on the std backend leaves it false, since std applies POSIX semantics itself.

Returns
true under a POSIX grammar that REAL is running.

◆ reject_or_fallback()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
void real::compat::basic_regex< CharT, Traits >::reject_or_fallback ( std::string_view  sv,
flag_type  f,
const std::string &  reason 
)
inlineprivate

The policy branch for a pattern the linear engine cannot represent: strict throws regex_error with error_complexity and a REAL-identifiable message; fallback delegates it to std::regex.

Parameters
[in]svThe pattern text.
[in]fSyntax options.
[in]reasonMessage carried by the thrown regex_error under strict policy.
Exceptions
real::compat::regex_errorunder policy::strict; compiles on std under policy::fallback.

◆ std_engine()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
const std::basic_regex< CharT, Traits > & real::compat::basic_regex< CharT, Traits >::std_engine ( ) const
inline

The std::regex for the std / lazy-std path (built once on demand for a real-backed pattern reached via a constraining flag / nullable replace-iterate / $0/sed replace).

Thread-safe: std::regex guarantees concurrent const operations on one object are safe, but this builds lazy_std_ (a mutable member) on demand. A function-local static build mutex serialises the build (and the read is taken under the same lock), so the guarantee holds for nullable AND non-nullable real-backed patterns. std::once_flag would be lighter but is non-copyable, and basic_regex must stay copyable (std::regex is); a static mutex keeps the value semantics defaulted. The build is per operation, cold relative to matching.

Returns
The wrapped std::basic_regex; compiling one on demand if this pattern is real-backed.

◆ swap()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
void real::compat::basic_regex< CharT, Traits >::swap ( basic_regex< CharT, Traits > &  other)
inlinenoexcept

Exchanges engines, flags, policy and cached state with other.

Parameters
[in,out]otherThe regex to swap with.

◆ uses_fallback()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
bool real::compat::basic_regex< CharT, Traits >::uses_fallback ( ) const
inlinenoexcept

True if this regex fell back to std::regex (a policy::fallback regex on an ineligible pattern) — so this pattern is not linear-time / ReDoS-safe. Always false under strict.

Returns
true if std::basic_regex holds it — in which case the linear-time guarantee does not apply.

◆ uses_real()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
bool real::compat::basic_regex< CharT, Traits >::uses_real ( ) const
inlinenoexcept

True if this regex is backed by the real engine (vs the std fallback).

Returns
true if REAL's linear engine holds it.

◆ uses_real_traversal()

template<typename CharT = char, typename Traits = std::regex_traits<CharT>>
bool real::compat::basic_regex< CharT, Traits >::uses_real_traversal ( ) const
inlinenoexcept

Whether replace/iterate run on the real traversal (real-backed AND non-nullable AND no nullable captured-repeat group). A nullable pattern delegates replace/iterate to std (the empty-match traversal differs; and iterating a nullable pattern whose per-position match cost is O(n) is O(n²) on any linear engine, so routing it buys correctness but not a linear guarantee — see the nullable note in COMPATIBILITY.md). A pattern with a capturing group that is nullable under a quantifier ((ab|)+a) is itself non-nullable as a whole, but real's last-consuming-iteration capture (RE2/Rust/Go lineage) diverges from an ECMAScript backtracker's extra empty final iteration on that GROUP's span — so it routes too, for the same reason: regex_search/match are unaffected (see the nullable-loop group-capture section of COMPATIBILITY.md — the search residue is intentional, not an oversight).

Returns
true for a real-backed, non-nullable pattern.

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