REAL
Regular Expression Algorithmic Library — constexpr C++20 regex
Loading...
Searching...
No Matches
real::detail::inner_literal_detail Namespace Reference

Helpers for real::detail::extract_inner_literal; not part of any interface. More...

Classes

struct  walk_state
 Extraction state threaded through the walk: the growing byte run, the best literal so far, and the top-level concat child each run began at (so the winner carries its prefix boundary). More...
 

Functions

constexpr std::uint32_t score_run (std::span< const std::uint8_t > run, const std::vector< bool > &folded)
 Selectivity of a byte run.
 
constexpr std::int32_t fold_pair_letter (const ast &tree, const ast_node &n)
 The lower-case ASCII letter a class node matches in either case and in no other way, else -1.
 
constexpr void flush (walk_state &st)
 Score the current byte run and keep it (with its prefix boundary) if it beats best, then clear it (capped at inner_literal_max).
 
constexpr bool is_pure_byte_run (const ast &tree, std::int32_t idx) noexcept
 Whether idx is a pure fixed byte run: only byte / nested concat / group of the same.
 
constexpr bool byte_run_is_empty (const ast &tree, std::int32_t idx) noexcept
 Whether a byte-run subtree can match the empty string — i.e. holds no byte at all.
 
constexpr bool walk (const ast &tree, std::int32_t idx, walk_state &st, std::int32_t top_child)
 Walk one node, appending guaranteed-present literal bytes to walk_state::run.
 
constexpr bool is_top_wb_anchor (const ast &tree, std::int32_t idx) noexcept
 Whether idx is a top-level \b or \B anchor — a peel candidate.
 
constexpr std::uint8_t wb_hint_from_anchor (anchor_kind k) noexcept
 Encodes a peeled word-boundary anchor as the hint value inner_literal::wb_lead and inner_literal::wb_trail carry.
 
constexpr bool is_word_only_run (const ast &tree, std::int32_t k)
 Whether node k is a repetition, at least once, of a class every member of which is a word character (groups around it looked through).
 
constexpr std::size_t byte_run_width (const ast &tree, std::int32_t idx) noexcept
 Width in bytes of a subtree is_pure_byte_run accepted.
 
constexpr bool is_unit (const ast &tree, std::int32_t idx) noexcept
 Whether a subtree matches exactly one unit: a byte, a class or ., groups looked through.
 
constexpr bool scan_prefix (const ast &tree, std::int32_t idx, std::array< bool, 256 > &bytes)
 Marks in bytes every byte a match of the subtree can contain, and says whether the subtree has a RIGID variable width: an alternation whose branches differ in width, or a repeat of anything wider than one unit whose count is not fixed.
 
constexpr bool can_occur_in_prefix (const inner_literal &lit, const std::array< bool, 256 > &bytes) noexcept
 Whether an occurrence of lit can begin inside a prefix whose matches hold only the bytes flagged in bytes.
 

Detailed Description

Helpers for real::detail::extract_inner_literal; not part of any interface.

Function Documentation

◆ byte_run_is_empty()

constexpr bool real::detail::inner_literal_detail::byte_run_is_empty ( const ast &  tree,
std::int32_t  idx 
)
constexprnoexcept

Whether a byte-run subtree can match the empty string — i.e. holds no byte at all.

Meaningful only on a subtree is_pure_byte_run accepted; other kinds answer true, the safe refusal. is_pure_byte_run accepts an EMPTY run, which an alternation branch must not be: a zero-width branch makes the alternation nullable, and a reverse-prefix must consume what it spans.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is the empty subtree, which is empty.
Returns
Whether the subtree matches the empty string.

◆ byte_run_width()

constexpr std::size_t real::detail::inner_literal_detail::byte_run_width ( const ast &  tree,
std::int32_t  idx 
)
constexprnoexcept

Width in bytes of a subtree is_pure_byte_run accepted.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is the empty subtree, of width 0.
Returns
The number of byte nodes under idx.

◆ can_occur_in_prefix()

constexpr bool real::detail::inner_literal_detail::can_occur_in_prefix ( const inner_literal &  lit,
const std::array< bool, 256 > &  bytes 
)
constexprnoexcept

Whether an occurrence of lit can begin inside a prefix whose matches hold only the bytes flagged in bytes.

Wholly inside takes every byte of the literal. Straddling the prefix's end needs nothing more: the part past the end overlaps the literal itself, so it repeats the literal's head, which lies in the prefix – every byte is then flagged anyway.

Parameters
[in]litThe inner literal.
[in]bytesOne flag per byte value the prefix can match.
Returns
False only when no occurrence can begin inside the prefix's text.

◆ flush()

constexpr void real::detail::inner_literal_detail::flush ( walk_state &  st)
constexpr

Score the current byte run and keep it (with its prefix boundary) if it beats best, then clear it (capped at inner_literal_max).

Prefer a true inner run (run_top >= 1) over a head run (run_top == 0) even with a slightly lower score: the head is already filtered by extract_prefix / find_prefix, and the IL route only fires for prefix_child_count >= 1. Among same-kind candidates, higher score_run still wins.

Parameters
[in,out]stThe walk state whose walk_state::run is scored and then cleared.

◆ fold_pair_letter()

constexpr std::int32_t real::detail::inner_literal_detail::fold_pair_letter ( const ast &  tree,
const ast_node &  n 
)
constexpr

The lower-case ASCII letter a class node matches in either case and in no other way, else -1.

The class must be exactly one letter's two cases: [xX], or a lone letter under icase ((?i)x). Under text-mode icase the letter's Unicode fold orbit must be those two only, read from the fold table the compiler folds with: k (KELVIN SIGN) and s (LONG S) have a third member, a multi-byte one a two-byte scan would miss. In bytes mode or under (?a) the fold is ASCII-only.

Parameters
[in]treeThe AST holding the node.
[in]nThe node.
Returns
The lower-case letter, or -1.

◆ is_pure_byte_run()

constexpr bool real::detail::inner_literal_detail::is_pure_byte_run ( const ast &  tree,
std::int32_t  idx 
)
constexprnoexcept

Whether idx is a pure fixed byte run: only byte / nested concat / group of the same.

Decides whether an alternation may be flushed past: with every branch literal text, the reverse-prefix still represents it as a deterministic byte DFA. Anything else (klass, repeat, nested alt, …) declines.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is the empty subtree and counts as pure.
Returns
true if every node in the subtree is a fixed byte, a concat of such, or a group of such.

◆ is_top_wb_anchor()

constexpr bool real::detail::inner_literal_detail::is_top_wb_anchor ( const ast &  tree,
std::int32_t  idx 
)
constexprnoexcept

Whether idx is a top-level \b or \B anchor — a peel candidate.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is not an anchor.
Returns
true for a word-boundary or not-word-boundary anchor node.

◆ is_unit()

constexpr bool real::detail::inner_literal_detail::is_unit ( const ast &  tree,
std::int32_t  idx 
)
constexprnoexcept

Whether a subtree matches exactly one unit: a byte, a class or ., groups looked through.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is the empty subtree, which is not a unit.
Returns
True for such a single atom.

◆ is_word_only_run()

constexpr bool real::detail::inner_literal_detail::is_word_only_run ( const ast &  tree,
std::int32_t  k 
)
constexpr

Whether node k is a repetition, at least once, of a class every member of which is a word character (groups around it looked through).

What makes a peeled lead \b sound before such a prefix: the reverse scan finds the leftmost start of the run, and every later start is preceded by a member of the class – a word character – so no later start can be a boundary. Only the leftmost can, and that is the one confirm_at checks.

Parameters
[in]treeThe parsed pattern.
[in]kThe node.
Returns
True for such a run.

◆ scan_prefix()

constexpr bool real::detail::inner_literal_detail::scan_prefix ( const ast &  tree,
std::int32_t  idx,
std::array< bool, 256 > &  bytes 
)
constexpr

Marks in bytes every byte a match of the subtree can contain, and says whether the subtree has a RIGID variable width: an alternation whose branches differ in width, or a repeat of anything wider than one unit whose count is not fixed.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is the empty subtree.
[in,out]bytesOne flag per byte value, set for each byte the subtree can match.
Returns
Whether such a construct occurs under idx.

◆ score_run()

constexpr std::uint32_t real::detail::inner_literal_detail::score_run ( std::span< const std::uint8_t >  run,
const std::vector< bool > &  folded 
)
constexpr

Selectivity of a byte run.

The sum of per-byte rarity (2000 - byte_frequency). A sum (rather than the rarest byte alone) approximates the product of per-byte match probabilities, so it rewards both a rare byte and a longer literal — the two things that shrink the candidate count.

Parameters
[in]runThe bytes to score.
[in]foldedOne flag per byte of run: a letter that matches in either case, whose traffic is the sum of its two cases'.
Returns
The run's selectivity; higher is rarer and therefore better.

◆ walk()

constexpr bool real::detail::inner_literal_detail::walk ( const ast &  tree,
std::int32_t  idx,
walk_state &  st,
std::int32_t  top_child 
)
constexpr

Walk one node, appending guaranteed-present literal bytes to walk_state::run.

Every byte appended is present in every match; the confirming scan verifies the context. Pure-literal alternations flush and continue (no branch bytes), so a later run (req=) can still arm. An optional breaks the run, as a class does. Met before any kept run, the walk goes on; after an inner run, the walk ends with that run kept; after a HEAD run, the extraction declines by choice (https?:// keeps its head literal http, a stronger filter than an inner scan for ://). Past an optional the literal must score at least optional_literal_min_score. An optional over a body wider than one unit in the prefix is left to extract_inner_literal's rigid-prefix decline: (ab)?bb is (ab|)bb.

Parameters
[in]treeThe AST holding the node.
[in]idxNode index; a negative index is the empty subtree and succeeds trivially.
[in,out]stWalk state the run accumulates into.
[in]top_childThe top-level concat child index this node belongs to, or -1 when nested in a group/repeat — a run starting there has no clean top-level prefix boundary.
Returns
false to DECLINE the whole extraction: a non-literal alternation, an optional (repeat with min 0) with no rare inner run before it, a lookaround or an anchor would make a required inner literal unsound, since a path could bypass it, or the route a loss.

◆ wb_hint_from_anchor()

constexpr std::uint8_t real::detail::inner_literal_detail::wb_hint_from_anchor ( anchor_kind  k)
constexprnoexcept

Encodes a peeled word-boundary anchor as the hint value inner_literal::wb_lead and inner_literal::wb_trail carry.

Parameters
[in]kThe anchor kind that was peeled.
Returns
1 for \b, 2 for \B, 0 for any other anchor (nothing peeled).