8#ifndef REAL_FRONTEND_INNER_LITERAL_HPP
9#define REAL_FRONTEND_INNER_LITERAL_HPP
29 std::array<std::uint8_t, 16>
bytes {};
37 [[nodiscard]]
constexpr bool found()
const
46 namespace inner_literal_detail {
52 std::vector<std::uint8_t>
run;
61 constexpr std::uint32_t
score_run(std::span<const std::uint8_t> run)
64 for (
const std::uint8_t b : run) {
74 if (!st.
run.empty()) {
76 const std::uint32_t s {
score_run(std::span<const std::uint8_t>(st.
run.data(), len))};
78 st.
best.
len =
static_cast<std::uint8_t
>(len);
80 for (std::size_t i = 0; i < len; ++i) {
98 std::int32_t top_child)
103 const ast_node& n {tree.
nodes[
static_cast<std::size_t
>(idx)]};
108 if (st.
run.empty()) {
111 st.
run.push_back(n.byte);
116 for (std::int32_t c = n.child; c >= 0; c = tree.
nodes[
static_cast<std::size_t
>(c)].next) {
117 if (!
walk(tree, c, st, -1)) {
125 return walk(tree, n.child, st, -1);
131 if (!
walk(tree, n.child, st, -1)) {
161 for (std::int32_t c = root.child; c >= 0; c = tree.
nodes[
static_cast<std::size_t
>(c)].next, ++i) {
183 std::int32_t c {root.
child};
184 for (std::int32_t i = 0; i + 1 <
count; ++i) {
185 c =
prefix.nodes[
static_cast<std::size_t
>(c)].next;
187 prefix.nodes[
static_cast<std::size_t
>(c)].next = -1;
Pattern text → AST, via a constexpr recursive-descent parser.
constexpr std::uint32_t score_run(std::span< const std::uint8_t > run)
Selectivity of a byte run: the sum of per-byte rarity (2000 - byte_frequency). A sum (rather than the...
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 st.run. top_child is the top-level conca...
constexpr void flush(walk_state &st)
Score the current byte run and keep it (with its prefix boundary) if it beats best,...
constexpr inner_literal extract_inner_literal(const ast &tree)
Extract the best required inner literal from a pattern's AST (a pure function on the node pool)....
constexpr std::uint16_t byte_frequency(std::uint8_t b)
Approximate static frequency of a byte in mixed English + source text (occurrences per 10000; higher ...
constexpr std::size_t inner_literal_max
The most bytes an inner literal keeps (a longer memmem target is diminishing returns and storage).
@ any
One codepoint, except newline (the . metacharacter).
@ repeat
Child repeated [min, max] times (max -1 = unbounded).
@ lookaround
Bounded lookaround: child = sub-pattern, negated = (?!/(?<!), direction = ahead/behind.
@ concat
Children matched in sequence.
@ alternation
Children are branches, leftmost preferred.
@ anchor
Zero-width assertion; kind in real::detail::ast_node::anchor.
@ klass
One codepoint constrained by classes[klass] (a class_def; negated or not).
@ empty
Matches the empty string.
@ group
Child wrapped in a group; group >= 0 when capturing.
@ prefix
Anchored at the start position (Python re.match).
ast build_prefix_ast(const ast &tree, std::int32_t count)
Build the prefix sub-AST: the first count top-level concat children (count >= 1). Copies the tree and...
Search acceleration: pattern analysis and candidate-finding.
One AST node. Active fields depend on kind (noted per field).
std::int32_t child
First child (concat, repeat, alternation, group).
A parsed pattern: the node pool plus side tables.
std::vector< ast_node > nodes
The node pool; root indexes it.
std::int32_t root
Index of the root node.
Extraction state threaded through the walk: the growing byte run, the best literal so far,...
std::int32_t best_top
Top-level child where the winning run began.
std::vector< std::uint8_t > run
std::int32_t run_top
Top-level child where the current run began (-1 = nested).
The best required inner literal of a pattern (the memmem candidate). len == 0 means the pattern decli...
std::uint32_t score
Selectivity: higher = rarer/longer = fewer memmem candidates.
std::int32_t prefix_child_count
constexpr bool found() const
std::array< std::uint8_t, 16 > bytes
REAL's version macros and the C++20 language-standard guard.