std::regex
This module provides regular expression helpers built on top of:
- the
regexpprimitive (compiled regex bytecode view), and - a boxed/owned
RegExptype for runtime-compiled patterns.
Regex literals are part of the language surface:
/pattern/flagsproduces aregexpvalue, compiled at compile time (see literals regexp).
Exported API#
The initial std::regex surface is intentionally small and focuses on:
- basic matching (
matches,exec,match_first), - searching and iteration (
search,iter), - runtime compilation (
RegExp.compile(...)), - explicit ownership via the
RegExpboxed type.
module std::regex;
export const EXEC_MATCH: int = 1;
export const EXEC_NO_MATCH: int = 0;
export const EXEC_ERR_MEMORY: int = -1;
export const EXEC_ERR_TIMEOUT: int = -2;
export const EXEC_ERR_INVALID_INPUT: int = -3;
export struct ExecResult {
// Use `std::regex::EXEC_*` constants.
code: int,
start: int,
end: int,
}
export fn exec (re: regexp, input: string) -> ExecResult;
export fn matches (re: regexp, input: string) -> bool;
export fn is_match (re: regexp, input: string) -> bool; // compatibility alias
export fn search (re: regexp, input: string, start: int) -> ExecResult;
export fn match_first (re: regexp, input: string) -> string?;
export struct MatchIter {
re: regexp,
input: string,
input_len: i64,
offset: int,
done: bool,
}
export fn iter (re: regexp, input: string) -> MatchIter;
export error CompileFailed {
code: int,
}
export struct RegExp {
// Runtime-owned compiled bytecode when produced by `RegExp.compile(...)`.
value: regexp,
}
impl RegExp {
public fn empty () -> RegExp;
public fn compile (pattern: string, flags: string) -> std::result::Result(RegExp, CompileFailed);
public fn as_regexp (self: &RegExp) -> regexp;
}
impl RegExp as std::interfaces::Drop {
public fn drop (mut self: &RegExp) -> void;
}
Notes:
- The
regexpprimitive is a non-owning{ ptr, len }view; regex literals embed compiled bytecode in rodata. RegExp.compile(...)produces runtime-owned heap bytecode, andRegExp.drop()releases only that tracked runtime-owned form.- Runtime-owned regex bytecode is allocated through the bundled-runtime
silk_rt_malloc_bytes(...)surface, which stores the allocation-time realloc/free hooks with the allocation itself. Laterdrop()/std::runtime::regex::free(...)calls therefore still use the correct free path even if the embedder changessilk_rt_set_allocator. - The bundled runtime allocator override is process-global. The runtime synchronizes allocator-hook updates and current-hook reads internally, but concurrent allocator changes can still affect which hook future regex compilation work observes.
- Wrapping a borrowed/literal/foreign
regexpinRegExpdoes not transfer ownership;RegExp.drop()andstd::runtime::regex::free(...)treat such values as safe no-ops instead of freeing arbitrary pointers. ExecResult.start/endare byte offsets into the inputstring.ExecResultimplementsstd::interfaces::Len(len() -> i64), returning the matched byte length (end - start) whencode == EXEC_MATCHand0otherwise.EXEC_ERR_TIMEOUTis the bounded-runtime signal for pathological matching. The bundled engine executes under a conservative operation budget instead of being allowed to backtrack without a limit.EXEC_ERR_INVALID_INPUTcovers null/invalid pointers, overlarge lengths, and malformed foreignregexpbytecode rejected by the runtime wrapper before the bundled engine is entered.testandmatchare reserved keywords in Silk; this module usesmatchesandmatch_firstinstead.MatchIterprovidesnext() -> ExecResult?and can be consumed withfor m in std::regex::iter(re, input) { ... }.- matches are yielded as
ExecResultvalues withcode == EXEC_MATCH, - a runtime error (
code < 0) is yielded once and then the iterator ends, - empty matches advance by 1 byte to guarantee progress.
RegExp.compile(...)and compile-time regexp literals both run under the same conservative regexp compile stack budget. Overly deep nesting fails as an invalid regexp (CompileFailed{ code: 3 }forRegExp.compile(...),E2104for literals) instead of recursing without a bound.- At ABI boundaries, downstream C code should treat
regexpvalues as opaque bytecode views and should only forward values produced by Silk regex literals orRegExp.compile(...); the runtime rejects malformed foreign buffers, but the bytecode format itself is not a public construction API, and only runtime-compiled values are releasable through the regex free/drop path.
Related Documents#
- literals regexp (regex literals)
- types (
regexp) - unicode (Unicode helpers used by the regex runtime)
- number (numeric parsing/formatting)
Source repository · Edit this page · View Markdown