REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
regex_core.hpp File Reference

std::regex compatibility, part 1/3: the constants, the error type, the backend-routing screens and basic_regex. More...

#include <real/version.hpp>
#include <cstddef>
#include <initializer_list>
#include <atomic>
#include <mutex>
#include <optional>
#include <regex>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>
#include <real/real.hpp>
Include dependency graph for regex_core.hpp:

Classes

class  real::compat::regex_error
 std::regex_error-compatible exception; every regex error this layer reports has this type. More...
 
class  real::compat::detail::lazy_std_engine< StdRegex >
 An engine built on first use and published once: a reader after the build takes no lock, and a copy made while another thread builds sees the finished engine or none (and then builds its own). Copyable, so the regex that holds it stays copyable. More...
 
struct  real::compat::detail::end_variant
 One variant of a pattern for match_not_eol / match_not_eow: REAL's compiled rewrite, or the original's own engine when the rewrite changed nothing, or neither when REAL does not compile the rewrite (a $ or \b inside a lookaround would nest one). More...
 
struct  real::compat::detail::end_variants
 The three variants a pattern may need: not_eol, not_eow, both. More...
 
class  real::compat::basic_regex< CharT, Traits >
 A std::basic_regex-compatible pattern, backed by REAL where it can serve the pattern, else by std. More...
 

Namespaces

namespace  real
 REAL's public API: real::regex, real::static_regex, real::flags and the match/iterator types built on them.
 
namespace  real::compat
 Drop-in replacements for <regex>, backed by REAL's linear-time engine where it can serve the pattern and by std::regex otherwise, never by a silent divergence.
 
namespace  real::compat::regex_constants
 Compatibility constants mirroring std::regex_constants (own values, mapped internally).
 
namespace  real::compat::detail
 Backend routing and format expansion for the compat layer. Not a stable API.
 

Typedefs

using real::compat::regex_constants::error_type = std::regex_constants::error_type
 Error categories, aliased to std's so regex_error::code() is a true drop-in.
 
using real::compat::regex = basic_regex< char >
 The char-path compat regex (real-eligible).
 
using real::compat::wregex = basic_regex< wchar_t >
 The wide compat regex (always the std backend).
 

Enumerations

enum  real::compat::regex_constants::syntax_option_type : unsigned {
  real::compat::regex_constants::ECMAScript = 0 , real::compat::regex_constants::icase = 1U << 0U , real::compat::regex_constants::nosubs = 1U << 1U , real::compat::regex_constants::optimize = 1U << 2U ,
  real::compat::regex_constants::collate = 1U << 3U , real::compat::regex_constants::multiline = 1U << 4U , real::compat::regex_constants::basic = 1U << 5U , real::compat::regex_constants::extended = 1U << 6U ,
  real::compat::regex_constants::awk = 1U << 7U , real::compat::regex_constants::grep = 1U << 8U , real::compat::regex_constants::egrep = 1U << 9U
}
 Grammar / option flags (own bit values; mapped to real::flags or std at construction). More...
 
enum  real::compat::regex_constants::match_flag_type : unsigned {
  real::compat::regex_constants::match_default = 0 , real::compat::regex_constants::match_not_bol = 1U << 0U , real::compat::regex_constants::match_not_eol = 1U << 1U , real::compat::regex_constants::match_not_bow = 1U << 2U ,
  real::compat::regex_constants::match_not_eow = 1U << 3U , real::compat::regex_constants::match_any = 1U << 4U , real::compat::regex_constants::match_not_null = 1U << 5U , real::compat::regex_constants::match_continuous = 1U << 6U ,
  real::compat::regex_constants::match_prev_avail = 1U << 7U , real::compat::regex_constants::format_default = 0 , real::compat::regex_constants::format_sed = 1U << 8U , real::compat::regex_constants::format_no_copy = 1U << 9U ,
  real::compat::regex_constants::format_first_only = 1U << 10U
}
 Match-control flags: the common subset. More...
 
enum class  real::compat::policy : std::uint8_t { real::compat::strict , real::compat::fallback }
 The policy for a pattern the linear engine cannot represent (a backreference, an unbounded lookbehind, a POSIX class, …): strict, the default, rejects it; fallback delegates it to std::regex, forfeiting the linear-time guarantee for that pattern. More...
 

Functions

constexpr syntax_option_type real::compat::regex_constants::operator| (syntax_option_type a, syntax_option_type b) noexcept
 Bitwise OR of two syntax options, so ECMAScript | icase stays typed.
 
constexpr syntax_option_type real::compat::regex_constants::operator& (syntax_option_type a, syntax_option_type b) noexcept
 Bitwise AND of two syntax options, for testing a bit.
 
constexpr match_flag_type real::compat::regex_constants::operator| (match_flag_type a, match_flag_type b) noexcept
 Bitwise OR of two match flags.
 
constexpr match_flag_type real::compat::regex_constants::operator& (match_flag_type a, match_flag_type b) noexcept
 Bitwise AND of two match flags, for testing a bit.
 
constexpr match_flag_type real::compat::regex_constants::operator~ (match_flag_type a) noexcept
 Bitwise complement of a match-flag set, for masking bits off.
 
bool real::compat::detail::grammar_forces_std (regex_constants::syntax_option_type f) noexcept
 Options REAL cannot serve once translate_posix has declined: a POSIX grammar bit, and always collate and nosubs (REAL reports every group, std only group 0 under nosubs).
 
bool real::compat::detail::pattern_forces_std (std::string_view p) noexcept
 Pattern text REAL accepts but reads differently from std, screened out before REAL compiles it so that the divergence is never silent.
 
bool real::compat::detail::std_sed_keeps_final_backslash ()
 Whether the native std keeps a final lone backslash of a format_sed format, as libstdc++ and libc++ do; MS STL drops it. REAL's sed expansion follows the native std there.
 
bool real::compat::detail::std_dollar_zero_is_match ()
 Whether the native std reads $0 in a format as the whole match, as libstdc++ and libc++ do; match_results::format follows it, being the one place REAL expands a $0 itself.
 
template<bool Sed, typename CharT , typename Put , typename Group , typename Prefix , typename Suffix >
void real::compat::detail::expand_replacement (const CharT *first, const CharT *last, Put &&put, Group &&group, Prefix &&prefix, Suffix &&suffix)
 Expands a replacement format: the one rule set behind match_results::format and regex_replace.
 
bool real::compat::detail::format_forces_std (std::string_view fmt) noexcept
 Whether a replacement format holds $0, which std implementations read differently (libstdc++: the whole match; MS STL: a literal), so the replace routes to std; $$ is an escaped dollar.
 
std::string real::compat::detail::posix_class_ranges (std::string_view name)
 A POSIX bracket-class name to its ASCII (C-locale) range content, appended inside a [...] during ERE translation. Empty for an unknown name (the caller then falls back to std).
 
std::optional< std::string > real::compat::detail::translate_ecma_classes (std::string_view p, bool icase)
 Rewrites the POSIX classes an ECMAScript bracket may hold ([[:alpha:]], which [re.grammar] adds to ECMAScript) as their ASCII ranges, which REAL reads; everything else passes through unchanged.
 
bool real::compat::detail::translate_bracket (std::string_view p, std::size_t &i, std::string &out)
 Translates a POSIX bracket expression, the same in BRE and ERE: a POSIX class becomes its ASCII ranges, other members pass through.
 
bool real::compat::detail::append_awk_escape (std::string_view p, std::size_t &i, std::string &out)
 Appends REAL's form of the awk escape at i, as the exact byte \xHH: \b is a backspace, not a word boundary; \a, \n, \t, \r, \f, \v the controls; \/ and \" literals; \ddd an octal byte, up to 0377.
 
bool real::compat::detail::has_empty_alternation_branch (std::string_view p)
 Whether p has an empty alternation branch ((|, |), ||, or a | at either end), which std rejects in the POSIX grammars. Conservative: a false positive costs only linear coverage, a false negative would be a silent over-accept.
 
std::optional< std::string > real::compat::detail::translate_ere (std::string_view p, bool awk=false)
 Translates an ERE pattern (with awk, an awk one: append_awk_escape) to REAL's syntax, or nullopt on a construct the grammars read differently: an ECMAScript shorthand, an ambiguous {, an unknown class, an empty branch (has_empty_alternation_branch).
 
std::optional< std::string > real::compat::detail::translate_bre (std::string_view p)
 Translates a BRE pattern to REAL's syntax, or nullopt: \(, \) and \{n\} group and quantify, a bare ( ) { } | + ? is escaped as the literal it is. Declines on a backreference, an ECMAScript escape, a non-strict \{, an unknown class, a * opening an expression, or a ^ or $ away from the pattern's ends.
 
template<typename LineFn >
std::optional< std::string > real::compat::detail::translate_newline_alt (std::string_view p, LineFn translate_line)
 grep / egrep: each newline-separated line is translated by translate_line and the lines joined with |, the lowest precedence, so each line keeps its own anchors. A line that declines, or an empty one, declines the whole pattern.
 
std::optional< std::string > real::compat::detail::translate_posix (std::string_view p, regex_constants::syntax_option_type f)
 Dispatches a single POSIX grammar to its translator; nullopt for no grammar bit or several, or under collate or nosubs.
 
real::flags real::compat::detail::to_real (regex_constants::syntax_option_type f) noexcept
 Maps compat options to REAL's flags, always with bytes | ecma: one REAL byte per std::regex char.
 
std::regex_constants::syntax_option_type real::compat::detail::to_std (regex_constants::syntax_option_type f) noexcept
 Maps compat options to std::regex syntax flags (the fallback path).
 
template<typename Call >
decltype(auto) real::compat::detail::std_call (Call &&call)
 Runs call on the std backend and reports its errors as regex_error, the type every error of this layer has. A std::basic_regex can fail while it matches (error_complexity, error_stack), long after it was built.
 
std::string real::compat::detail::rewrite_end_context (std::string_view pattern, bool not_eol, bool not_eow, bool multiline)
 pattern with match_not_eol and match_not_eow written into it, so a REAL search honors them without run-time context: under not_eol a $ holds at no end of the sequence (in multiline only before a line terminator), under not_eow a \b holds at no end and a \B does.
 

Variables

constexpr error_type real::compat::regex_constants::error_collate {std::regex_constants::error_collate}
 As std's: an invalid collating element.
 
constexpr error_type real::compat::regex_constants::error_ctype {std::regex_constants::error_ctype}
 As std's: an invalid character class.
 
constexpr error_type real::compat::regex_constants::error_escape {std::regex_constants::error_escape}
 As std's: an invalid escape.
 
constexpr error_type real::compat::regex_constants::error_backref {std::regex_constants::error_backref}
 As std's: an invalid back reference.
 
constexpr error_type real::compat::regex_constants::error_brack {std::regex_constants::error_brack}
 As std's: mismatched brackets.
 
constexpr error_type real::compat::regex_constants::error_paren {std::regex_constants::error_paren}
 As std's: mismatched parentheses.
 
constexpr error_type real::compat::regex_constants::error_brace {std::regex_constants::error_brace}
 As std's: mismatched braces.
 
constexpr error_type real::compat::regex_constants::error_badbrace {std::regex_constants::error_badbrace}
 As std's: an invalid range in braces.
 
constexpr error_type real::compat::regex_constants::error_range {std::regex_constants::error_range}
 As std's: an invalid character range.
 
constexpr error_type real::compat::regex_constants::error_space {std::regex_constants::error_space}
 As std's: out of memory.
 
constexpr error_type real::compat::regex_constants::error_badrepeat {std::regex_constants::error_badrepeat}
 As std's: a repeat with nothing to repeat.
 
constexpr error_type real::compat::regex_constants::error_complexity {std::regex_constants::error_complexity}
 As std's; also a strict-policy rejection.
 
constexpr error_type real::compat::regex_constants::error_stack {std::regex_constants::error_stack}
 As std's: out of stack.
 
template<typename CharT , typename Traits >
constexpr bool real::compat::detail::real_eligible
 Whether REAL can serve this instantiation: char with default traits only, anything else is std. A compile-time gate: REAL's char-only code must be compiled out for other CharT, not skipped.
 

Detailed Description

std::regex compatibility, part 1/3: the constants, the error type, the backend-routing screens and basic_regex.