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

A multi-rule DFA: maximal-munch (dfa_mode::munch) or which-matched unanchored scan (dfa_mode::which_matched). More...

#include <dfa.hpp>

Collaboration diagram for real::dfa:
[legend]

Public Member Functions

 dfa (std::span< const detail::program_view > programs, dfa_mode mode=dfa_mode::munch)
 Builds the DFA from compiled programs (the embedder path).
 
 dfa (std::span< const regex > patterns, dfa_mode mode=dfa_mode::munch)
 Builds the DFA from regexes (a convenience over regex::raw_program).
 
std::optional< dfa_matchmatch (std::string_view rest) const noexcept
 Matches the longest pattern anchored at the start of rest.
 
std::vector< bool > which_matched (std::string_view text, bool first_byte_skip=true) const
 Which patterns match the subject at least once (single-pass).
 
bool has_first_byte_skip () const noexcept
 True if set-level first-byte skip is armed for which_matched.
 
bool is_unanchored () const noexcept
 True if this DFA was built with mid-stream restart (which-matched mode).
 
std::size_t state_count () const noexcept
 The number of states in the minimized automaton (includes the dead state).
 
std::size_t rule_count () const noexcept
 The number of patterns the DFA was built from.
 
std::size_t class_count () const noexcept
 The number of byte-equivalence classes (the reduced alphabet width).
 

Static Private Member Functions

static std::vector< detail::program_viewviews_of (std::span< const regex > patterns)
 Materializes program views from patterns (helper for the regex ctor).
 

Private Attributes

detail::dfa_tables tables_
 The immutable baked tables.
 

Detailed Description

A multi-rule DFA: maximal-munch (dfa_mode::munch) or which-matched unanchored scan (dfa_mode::which_matched).

Built once (heap-allocated tables), then immutable and cheap to copy-share. match is the lexer munch. which_matched is Stage-2 multi-accept (only valid when built with dfa_mode::which_matched).

Constructor & Destructor Documentation

◆ dfa() [1/2]

real::dfa::dfa ( std::span< const detail::program_view programs,
dfa_mode  mode = dfa_mode::munch 
)
inlineexplicit

Builds the DFA from compiled programs (the embedder path).

Parameters
[in]programsThe patterns' programs, in priority order (see regex::raw_program).
[in]modeMunch (default) or which-matched unanchored multi-accept.
Exceptions
real::dfa_errorif any program holds a non-head zero-width assertion, a lookaround, a code-point class, or a possessive/atomic construct.

◆ dfa() [2/2]

real::dfa::dfa ( std::span< const regex patterns,
dfa_mode  mode = dfa_mode::munch 
)
inlineexplicit

Builds the DFA from regexes (a convenience over regex::raw_program).

Parameters
[in]patternsThe patterns, in priority order; they must outlive this call.
[in]modeMunch (default) or which-matched.
Exceptions
real::dfa_errorif any pattern holds a non-head zero-width assertion, a lookaround, a code-point class, or a possessive/atomic construct.

Member Function Documentation

◆ class_count()

std::size_t real::dfa::class_count ( ) const
inlinenoexcept

The number of byte-equivalence classes (the reduced alphabet width).

Returns
The class count.

◆ has_first_byte_skip()

bool real::dfa::has_first_byte_skip ( ) const
inlinenoexcept

True if set-level first-byte skip is armed for which_matched.

Returns
Whether every rule contributed a valid first-byte set at build time.

◆ is_unanchored()

bool real::dfa::is_unanchored ( ) const
inlinenoexcept

True if this DFA was built with mid-stream restart (which-matched mode).

Returns
Whether the tables carry self-restart transitions.

◆ match()

std::optional< dfa_match > real::dfa::match ( std::string_view  rest) const
inlinenoexcept

Matches the longest pattern anchored at the start of rest.

Maximal munch: the longest match wins; on equal length the earliest pattern (lowest index passed to the constructor) wins; an empty match never wins.

Parameters
[in]restThe text to match at its start.
Returns
The winning rule index and byte length, or std::nullopt if nothing non-empty matches.

◆ rule_count()

std::size_t real::dfa::rule_count ( ) const
inlinenoexcept

The number of patterns the DFA was built from.

Returns
The rule count, which is also the width of which_matched's answer.

◆ state_count()

std::size_t real::dfa::state_count ( ) const
inlinenoexcept

The number of states in the minimized automaton (includes the dead state).

Returns
The state count.

◆ views_of()

static std::vector< detail::program_view > real::dfa::views_of ( std::span< const regex patterns)
inlinestaticprivate

Materializes program views from patterns (helper for the regex ctor).

Parameters
[in]patternsThe compiled regexes, which must outlive the views.
Returns
One view per pattern, in the same order.

◆ which_matched()

std::vector< bool > real::dfa::which_matched ( std::string_view  text,
bool  first_byte_skip = true 
) const
inline

Which patterns match the subject at least once (single-pass).

Requires a build with dfa_mode::which_matched. Returns a bitset of length rule_count in construction order. Early-exits when every pattern has hit. Empty matches are excluded (only states that accepted after consuming a byte contribute, via post-move accept masks).

Parameters
[in]textSubject text.
[in]first_byte_skipWhen true (default), fast-forward over bytes that cannot start any rule while the walk is in the start state (a pure optimization). Pass false to disable for equivalence tests.
Returns
One bool per rule, in construction order, true where that pattern matched.

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