Components / Developer tools
Regex tester
A pattern, its flags and a test text, with every match and group highlighted.
Functions: cui_regex_search, cui_regex_tester

Usage
static char pattern[128] = "(?<user>[\\w.]+)@(?<host>[\\w-]+)\\.com";
static char text[4096] = "Write to maya@example.com or help@example.com";
cui_regex_tester_result r = cui_regex_tester(ui, "rx", pattern, sizeof pattern, text, sizeof text);
A pattern field with the flags i (ignore case), m (multi-line ^ and $) and s (dot matches newline) above a test text. Every match is highlighted in the text, and each group in its own color. Under the text, the count is shown, then each match with its range and its groups, named or numbered. An invalid pattern shows the reason under the field. A pattern that would backtrack for too long stops and says so instead of freezing the frame. The matches are worked out again only when the pattern, the flags or the text change. .flags keeps the flags in your variable.
The engine is public. cui_regex_search(pattern, flags, text, len, from, &match, &error) finds the first match at or after a byte offset, with up to nine groups and their names. It supports the common syntax: classes and ranges, \d \w \s and their negations, \b, anchors, groups ( ), (?: ) and (?<name> ), alternation, and greedy or lazy * + ? {n,m}. It does not support lookaround or backreferences. It returns 1 for a match, 0 for none, -1 for an invalid pattern and -2 when the step budget runs out.
The text field is the code editor, which gains marks: ranges of bytes to highlight, in the accent or a series color. The highlighter gains "text", a language that highlights nothing.
Keyboard
- Tab moves from the pattern to the flags and the text. Left and Right move between the flags, Enter or Space flips the focused one, and the text edits like the code editor.
Options of cui_regex_tester
| Option | Type | Default | Description |
|---|---|---|---|
.width | cui_size | Width: CUI_FILL, CUI_FIT, CUI_PX(n) or CUI_PCT(n); unset uses the component's own. | |
.height | float | 180 | of the test text, default 180 |
.flags | int * | CUI_REGEX_*; NULL: kept for you |
cui_regex_match
| Field | Type | Description |
|---|---|---|
start | int | bytes |
end | int | bytes |
groups | int | capture groups in the pattern |
group_start | int[CUI_REGEX_GROUPS] | 1 based; -1 when a group took no part |
group_end | int[CUI_REGEX_GROUPS] | 1 based; -1 when a group took no part |
group_name | char[CUI_REGEX_GROUPS][24] | the name of each (?<name> ) group, indexed like group_start; "" when unnamed |
cui_regex_tester_result
| Field | Type | Description |
|---|---|---|
valid | bool | the pattern is valid and matching finished in time |
matches | int | matches found in the test text |
error | const char * | why, when valid is false; otherwise NULL |
Related
- Text field: A single-line field that edits text in a buffer you own, with a label, an icon, a hint and an error. The search field is a variant.
- Code editor: An editable code field with syntax highlighting, for shader editors, script consoles and config files.
- Test explorer: Test results as a tree of suites and tests, each with its status and time.
- Trace viewer: A tracing timeline in the manner of a performance panel.