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)
 Selectivity of a byte run.
 
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 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.
 

Detailed Description

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

Function Documentation

◆ 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.

◆ 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.

Used to decide whether an alternation may be skipped (flush+continue) without appending any branch bytes — every branch is fixed-width literal text, so the reverse-prefix can still represent the alt as a deterministic byte DFA. Anything else (klass, repeat, nested alt, lookaround, …) keeps the conservative decline.

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.

◆ score_run()

constexpr std::uint32_t real::detail::inner_literal_detail::score_run ( std::span< const std::uint8_t >  run)
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.
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 then verifies the surrounding context. Pure-literal alternations flush and continue (no branch bytes) so a later unconditional run can still arm. An optional declines the whole extraction, which is a choice and not a requirement: flushing past it would be sound, since bytes after an optional are still required, but it would take https?:// off its head literal http, and a required HEAD is a stronger filter than an inner scan for ://.

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), a lookaround or an anchor would make a required inner literal unsound, since a path could bypass it.

◆ 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).