|
REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
|
Multi-pattern set: which patterns match the subject at least once. More...
#include <regex_set.hpp>
Classes | |
| struct | fused_state |
| The fused scan's state: the DFA, its subset maps, and what the deferred build counts. More... | |
Public Member Functions | |
| regex_set (std::span< const std::string_view > patterns, flags compile_flags=flags::none) | |
Compiles every pattern in patterns (construction order = bitset order). | |
| regex_set (const std::string_view *patterns, std::size_t n, flags compile_flags=flags::none) | |
| Convenience: compile from a contiguous array of string views. | |
| regex_set (std::initializer_list< std::string_view > patterns, flags compile_flags=flags::none) | |
Brace-init: regex_set{"a", "b", R"(\\d+)"} . | |
| regex_set (std::span< const std::string > patterns, flags compile_flags=flags::none) | |
Compile from owning strings (e.g. std::vector<std::string>). | |
| std::size_t | size () const noexcept |
| Number of patterns in the set (bitset length). | |
| bool | empty () const noexcept |
| True if the set has no patterns. | |
| flags | compile_flags () const noexcept |
| Compilation flags shared by every member. | |
| bool | uses_fused () const noexcept |
| True when a fused single-pass DFA is active: built at construction, or since, once the set walked fused_deferred_bytes. | |
| std::size_t | eligible_count () const noexcept |
| How many members the fused DFA holds, or 0 when uses_fused is false. | |
| bool | is_match (std::string_view text, std::size_t pos=0, std::size_t endpos=npos) const |
| True if any pattern matches the subject at least once. | |
| std::vector< bool > | matches (std::string_view text, std::size_t pos=0, std::size_t endpos=npos) const |
| Which patterns match at least once (construction-order bitset). | |
| std::vector< std::size_t > | which (std::string_view text, std::size_t pos=0, std::size_t endpos=npos) const |
| Indices of patterns that match (construction order, ascending). | |
| const regex & | operator[] (std::size_t i) const |
Access the compiled pattern at construction index i. | |
Static Public Attributes | |
| static constexpr std::size_t | fused_min_eligible {56} |
| Eligible-member count at which the set builds its fused single-pass DFA at construction. | |
| static constexpr std::size_t | fused_deferred_min_eligible {24} |
| Eligible-member count from which a set builds its fused DFA once it has walked fused_deferred_bytes, rather than at construction. | |
| static constexpr std::size_t | fused_deferred_bytes {std::size_t {1} << 20U} |
| Whole-subject bytes a set of fused_deferred_min_eligible to fused_min_eligible members walks before it builds its fused DFA. | |
Private Member Functions | |
| void | build_from_views (std::span< const std::string_view > patterns) |
| Compiles every pattern, then splits them into the DFA-eligible subset (fused when it reaches the threshold) and the ineligible remainder each query searches individually. | |
| void | partition (fused_state &st) const |
| Splits the members into the DFA-eligible subset and the rest (a single-pattern munch DFA decides eligibility). | |
| void | build_fused (fused_state &st) const |
| Builds the fused which-matched DFA over the eligible members. | |
| void | build_deferred (fused_state &st) const |
| The deferred build: partitions if construction did not, then builds the fused DFA when enough members are eligible. A fused DFA past its bounds leaves the set on walks for good, as does too small an eligible subset: matches must not throw what construction did not. | |
| const fused_state * | ready_fused () const noexcept |
| The fused state when its DFA is built, without counting or building anything. | |
| const fused_state * | fused_for (std::string_view text) const |
The fused state for a whole-subject matches over text: counts text toward the deferred build, and runs the build once the count reaches fused_deferred_bytes. | |
| void | arm_byte_filter () |
| Classifies members into a SPARSE partition one scan can serve, and arms the filter. | |
| bool | is_sparse (std::size_t i) const noexcept |
One bit, no search: is member i served by the byte filter? | |
| bool | filter_excludes (std::string_view text, std::size_t pos, std::size_t endpos) const |
| True when the filter PROVES no member it serves can match in the region. | |
Private Attributes | |
| std::vector< regex > | members_ |
| Every compiled pattern, in construction order. | |
| flags | flags_ {flags::none} |
| Flags shared by every member. | |
| std::shared_ptr< fused_state > | fused_state_ |
| Null when the set can never fuse. | |
| std::vector< std::uint64_t > | sparse_bits_ |
| Bit i set when the byte filter serves member i. | |
| std::array< std::uint8_t, 8 > | filter_bytes_ {} |
| The served members' first-byte union, in the mask load's layout. | |
| std::uint8_t | filter_count_ {0} |
| Valid entries in filter_bytes_; 0 = filter off. | |
Static Private Attributes | |
| static constexpr std::uint8_t | filter_union_max {8} |
| Widest first-byte union the byte filter carries: one 16-byte masked block's capacity. | |
Multi-pattern set: which patterns match the subject at least once.
Construction compiles every pattern (same flags as regex). If any pattern is invalid or unsupported, the constructor throws regex_error — there is no silent skip. Capture groups are not reported by the set; re-run the individual regex if groups are needed.
Bitset order is the construction order: index 0 is the first pattern, etc.
|
inlineexplicit |
Compiles every pattern in patterns (construction order = bitset order).
| [in] | patterns | Pattern texts; an empty set is allowed. |
| [in] | compile_flags | Flags applied to every pattern (same as regex). |
| regex_error | if any pattern fails to compile. |
|
inline |
Convenience: compile from a contiguous array of string views.
| [in] | patterns | Pointer to the first pattern. |
| [in] | n | Pattern count. |
| [in] | compile_flags | Flags shared by every member. |
|
inline |
Brace-init: regex_set{"a", "b", R"(\\d+)"} .
| [in] | patterns | The patterns to compile. |
| [in] | compile_flags | Flags shared by every member. |
|
inlineexplicit |
Compile from owning strings (e.g. std::vector<std::string>).
| [in] | patterns | The patterns to compile; only borrowed for the duration of the call. |
| [in] | compile_flags | Flags shared by every member. |
|
inlineprivate |
Classifies members into a SPARSE partition one scan can serve, and arms the filter.
Zero work per call is the constraint: classifying per query charges every call, including an any-match walk whose first member hits at once. Everything happens here, once; the walks test one bit.
The classification is the engine's own: first_bytes_valid plus single_first or a small_set_size of 2..8. The hint builder sets small_set only when all 256 bytes of first_bytes count 2..8, so the array is the COMPLETE leading set, which is what makes skipping a member sound. A nullable pattern has first_bytes_valid false and stays wide.
The union is capped in construction order (narrowest-first reorders which members are served, for no gain), so several sparse members cannot form a dense union. A wide member never joins: one \w-leading pattern costs the others nothing.
|
inlineprivate |
The deferred build: partitions if construction did not, then builds the fused DFA when enough members are eligible. A fused DFA past its bounds leaves the set on walks for good, as does too small an eligible subset: matches must not throw what construction did not.
| [in,out] | st | The state to complete and publish. |
|
inlineprivate |
Compiles every pattern, then splits them into the DFA-eligible subset (fused when it reaches the threshold) and the ineligible remainder each query searches individually.
| [in] | patterns | The patterns to compile, in construction order. |
| regex_error | if any pattern fails to compile. |
|
inlineprivate |
Builds the fused which-matched DFA over the eligible members.
| [in,out] | st | The partitioned state. |
| dfa_error | when the fused automaton exceeds its state or work bound. |
|
inlinenoexcept |
Compilation flags shared by every member.
|
inlinenoexcept |
How many members the fused DFA holds, or 0 when uses_fused is false.
Until the fused DFA is active every member walks individually, so this is 0 even for patterns a DFA would accept.
|
inlinenoexcept |
True if the set has no patterns.
|
inlineprivate |
True when the filter PROVES no member it serves can match in the region.
| [in] | text | Subject. |
| [in] | pos | Start offset, as regex::search takes it. |
| [in] | endpos | Region end, as regex::search takes it (truncates the view). |
|
inlineprivate |
The fused state for a whole-subject matches over text: counts text toward the deferred build, and runs the build once the count reaches fused_deferred_bytes.
| [in] | text | The subject about to be searched. |
|
inline |
True if any pattern matches the subject at least once.
Stops at the first matching pattern (any-match early exit). Region semantics match regex::search — endpos truncates the view; pos is the start offset (not a slice).
| [in] | text | Subject. |
| [in] | pos | Byte offset the search starts at. |
| [in] | endpos | Byte offset the region ends at; defaults to the end of text. |
|
inlineprivatenoexcept |
One bit, no search: is member i served by the byte filter?
| [in] | i | Construction index of the member. |
|
inline |
Which patterns match at least once (construction-order bitset).
Index i is true iff pattern i matched. Order is always construction order, whether the set walks members individually or through a fused scan.
| [in] | text | Subject. |
| [in] | pos | Byte offset the search starts at. |
| [in] | endpos | Byte offset the region ends at; defaults to the end of text. |
|
inline |
Access the compiled pattern at construction index i.
| [in] | i | Construction index. |
| std::out_of_range | when i is past the last member. |
|
inlineprivate |
Splits the members into the DFA-eligible subset and the rest (a single-pattern munch DFA decides eligibility).
| [in,out] | st | The state receiving both lists. |
|
inlineprivatenoexcept |
The fused state when its DFA is built, without counting or building anything.
|
inlinenoexcept |
Number of patterns in the set (bitset length).
|
inlinenoexcept |
True when a fused single-pass DFA is active: built at construction, or since, once the set walked fused_deferred_bytes.
|
inline |
Indices of patterns that match (construction order, ascending).
| [in] | text | Subject. |
| [in] | pos | Byte offset the search starts at. |
| [in] | endpos | Byte offset the region ends at; defaults to the end of text. |
|
staticconstexprprivate |
Widest first-byte union the byte filter carries: one 16-byte masked block's capacity.
Private, unlike fused_min_eligible, as a scan width rather than a contract; its rationale names detail:: symbols a public reference page cannot link.
|
staticconstexpr |
Eligible-member count from which a set builds its fused DFA once it has walked fused_deferred_bytes, rather than at construction.
From about this many members one fused scan costs less than the walks, but the build costs milliseconds, more than a set built for one short subject gets back. So the set walks until its whole-subject matches calls total fused_deferred_bytes, about the build's cost, which bounds the total at about twice the cheaper choice in hindsight. Below this count the walks win outright.
|
staticconstexpr |
Eligible-member count at which the set builds its fused single-pass DFA at construction.
One fused automaton costs a build and a full scan; N walks cost N searches that can each stop early. From this count the fused scan pays on the first subject; between fused_deferred_min_eligible and here it pays only once enough text has been searched.