@oseo/runtime-c 0.0.0-stage → 0.1.0-canary.370

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +674 -0
  2. package/MIT-LICENSE.txt +21 -0
  3. package/README.md +28 -2
  4. package/UNICODE-LICENSE.txt +39 -0
  5. package/dist/index.d.ts +7 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +269 -0
  8. package/dist/index.js.map +1 -0
  9. package/native/oseo_runtime.h +2618 -0
  10. package/native/runtime_agent.c +1717 -0
  11. package/native/runtime_arguments.c +379 -0
  12. package/native/runtime_array.c +5766 -0
  13. package/native/runtime_array_buffer.c +1748 -0
  14. package/native/runtime_async_generator.c +972 -0
  15. package/native/runtime_atomics.c +1283 -0
  16. package/native/runtime_bigint.c +1387 -0
  17. package/native/runtime_bigint_object.c +626 -0
  18. package/native/runtime_binding.c +1029 -0
  19. package/native/runtime_boolean.c +320 -0
  20. package/native/runtime_clock.c +181 -0
  21. package/native/runtime_clock_posix.c +599 -0
  22. package/native/runtime_core.c +488 -0
  23. package/native/runtime_data_view.c +1135 -0
  24. package/native/runtime_date.c +1997 -0
  25. package/native/runtime_descriptor.c +1104 -0
  26. package/native/runtime_disposal.c +1667 -0
  27. package/native/runtime_enumeration.c +636 -0
  28. package/native/runtime_error.c +997 -0
  29. package/native/runtime_event_loop.c +653 -0
  30. package/native/runtime_function.c +3667 -0
  31. package/native/runtime_generator.c +1120 -0
  32. package/native/runtime_internal.h +5578 -0
  33. package/native/runtime_iterator.c +5379 -0
  34. package/native/runtime_json.c +1992 -0
  35. package/native/runtime_map.c +1563 -0
  36. package/native/runtime_math.c +918 -0
  37. package/native/runtime_memory.c +1365 -0
  38. package/native/runtime_module.c +884 -0
  39. package/native/runtime_number.c +2077 -0
  40. package/native/runtime_object.c +423 -0
  41. package/native/runtime_object_builtin.c +3901 -0
  42. package/native/runtime_primitive.c +2268 -0
  43. package/native/runtime_process.c +744 -0
  44. package/native/runtime_promise.c +3378 -0
  45. package/native/runtime_property.c +815 -0
  46. package/native/runtime_proxy.c +1557 -0
  47. package/native/runtime_realm.c +435 -0
  48. package/native/runtime_reflect.c +648 -0
  49. package/native/runtime_regexp.c +3519 -0
  50. package/native/runtime_regexp_matcher.c +3559 -0
  51. package/native/runtime_regexp_symbol.c +1495 -0
  52. package/native/runtime_set.c +1775 -0
  53. package/native/runtime_string.c +2886 -0
  54. package/native/runtime_string_match.c +1185 -0
  55. package/native/runtime_symbol.c +1010 -0
  56. package/native/runtime_typed_array.c +5137 -0
  57. package/native/runtime_unicode_tables.h +14670 -0
  58. package/native/runtime_uri.c +700 -0
  59. package/native/runtime_weak_collection.c +1299 -0
  60. package/package.json +38 -3
@@ -0,0 +1,3559 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /*
3
+ * Oseo C runtime library.
4
+ *
5
+ * MIT License
6
+ *
7
+ * Copyright (c) 2026 Hong Minhee <https://hongminhee.org/>
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
10
+ * of this software and associated documentation files (the "Software"), to deal
11
+ * in the Software without restriction, including without limitation the rights
12
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
+ * copies of the Software, and to permit persons to whom the Software is
14
+ * furnished to do so, subject to the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be included in
17
+ * all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ * SOFTWARE.
26
+ */
27
+
28
+ #include "runtime_internal.h"
29
+
30
+ #include <stdlib.h>
31
+ #include <string.h>
32
+
33
+ /*
34
+ * The generic regular expression matcher: the pattern compiler that turns
35
+ * one already validated pattern into an owned instruction program, and the
36
+ * executor that runs one program over UTF-16 input.
37
+ *
38
+ * This component is the runtime's semantic authority for choice order,
39
+ * capture visibility, assertion and lookaround behavior, backreference
40
+ * resolution, quantifier priority, empty-progress failure, UTF-16 and
41
+ * code-point traversal, and canonicalization. It reproduces the artifact
42
+ * and executor `@oseo/compiler` owns, so a later ahead-of-time lowering
43
+ * has one behavior to match rather than two.
44
+ *
45
+ * Nothing here allocates managed memory or calls user code, so a compile
46
+ * and a match reach no safepoint and observe no managed state. The
47
+ * program and the work area are unmanaged memory taken through the
48
+ * runtime's work-area allocator, which shares the deterministic
49
+ * allocation-attempt counter without collecting, so a sweep can fail any
50
+ * of them and observe the cleanup. Both stacks the executor needs are
51
+ * explicit and checked, so a deeply backtracking pattern reports an owned
52
+ * boundary rather than consuming a native call stack.
53
+ */
54
+
55
+ #define OSEO_REGEXP_INSTRUCTION_LIMIT ((size_t)0x100000u)
56
+ #define OSEO_REGEXP_REGISTER_LIMIT ((size_t)0x40000u)
57
+ #define OSEO_REGEXP_STEP_LIMIT ((uint64_t)0x1000000u)
58
+ #define OSEO_REGEXP_BACKTRACK_LIMIT ((size_t)0x400000u)
59
+ #define OSEO_REGEXP_TRAIL_LIMIT ((size_t)0x400000u)
60
+
61
+ #define OSEO_REGEXP_CODE_POINT_LIMIT ((uint32_t)0x110000u)
62
+ #define OSEO_REGEXP_UNSET ((int64_t)-1)
63
+
64
+ /*
65
+ * One growable array of code-point boundaries.
66
+ *
67
+ * Every character set is an inversion list: a strictly increasing,
68
+ * even-length list of boundaries where membership toggles, so
69
+ * `{0x41, 0x5b}` is U+0041 through U+005A. A closing boundary may equal
70
+ * one past the largest code point, which is the only value the list may
71
+ * hold that is not itself a code point.
72
+ */
73
+ typedef struct {
74
+ uint32_t *values;
75
+ size_t count;
76
+ size_t capacity;
77
+ } OseoRegExpBuffer;
78
+
79
+ static void buffer_initialize(OseoRegExpBuffer *buffer) {
80
+ buffer->values = NULL;
81
+ buffer->count = 0u;
82
+ buffer->capacity = 0u;
83
+ }
84
+
85
+ static void buffer_release(OseoRegExpBuffer *buffer) {
86
+ free(buffer->values);
87
+ buffer_initialize(buffer);
88
+ }
89
+
90
+ static bool buffer_reserve(
91
+ OseoContext *context,
92
+ OseoRegExpBuffer *buffer,
93
+ size_t additional
94
+ ) {
95
+ if (buffer->capacity - buffer->count >= additional) return true;
96
+ size_t needed = buffer->count + additional;
97
+ if (needed < additional) return false;
98
+ size_t capacity = buffer->capacity == 0u ? 8u : buffer->capacity;
99
+ while (capacity < needed) {
100
+ if (capacity > SIZE_MAX / 2u) return false;
101
+ capacity *= 2u;
102
+ }
103
+ if (capacity > SIZE_MAX / sizeof(uint32_t)) return false;
104
+ uint32_t *values = oseo_internal_reallocate_work_bytes(
105
+ context,
106
+ buffer->values,
107
+ capacity * sizeof(uint32_t)
108
+ );
109
+ if (values == NULL) return false;
110
+ buffer->values = values;
111
+ buffer->capacity = capacity;
112
+ return true;
113
+ }
114
+
115
+ static bool buffer_push(
116
+ OseoContext *context,
117
+ OseoRegExpBuffer *buffer,
118
+ uint32_t value
119
+ ) {
120
+ if (!buffer_reserve(context, buffer, 1u)) return false;
121
+ buffer->values[buffer->count] = value;
122
+ buffer->count += 1u;
123
+ return true;
124
+ }
125
+
126
+ /* Whether one inversion list holds one code point. */
127
+ static bool set_has(
128
+ const uint32_t *values,
129
+ size_t count,
130
+ uint32_t code_point
131
+ ) {
132
+ size_t low = 0u;
133
+ size_t high = count;
134
+ while (low < high) {
135
+ size_t middle = low + (high - low) / 2u;
136
+ if (values[middle] <= code_point) low = middle + 1u;
137
+ else high = middle;
138
+ }
139
+ return (low & 1u) == 1u;
140
+ }
141
+
142
+ /* The union of two inversion lists, appended to an empty output. */
143
+ static bool set_union(
144
+ OseoContext *context,
145
+ const uint32_t *left,
146
+ size_t left_count,
147
+ const uint32_t *right,
148
+ size_t right_count,
149
+ OseoRegExpBuffer *output
150
+ ) {
151
+ output->count = 0u;
152
+ size_t left_index = 0u;
153
+ size_t right_index = 0u;
154
+ int depth = 0;
155
+ while (left_index < left_count || right_index < right_count) {
156
+ uint32_t left_value = left_index < left_count
157
+ ? left[left_index]
158
+ : UINT32_MAX;
159
+ uint32_t right_value = right_index < right_count
160
+ ? right[right_index]
161
+ : UINT32_MAX;
162
+ uint32_t value = left_value < right_value ? left_value : right_value;
163
+ int opening = 0;
164
+ if (left_index < left_count && left_value == value) {
165
+ opening += (left_index & 1u) == 0u ? 1 : -1;
166
+ left_index += 1u;
167
+ }
168
+ if (right_index < right_count && right_value == value) {
169
+ opening += (right_index & 1u) == 0u ? 1 : -1;
170
+ right_index += 1u;
171
+ }
172
+ int previous = depth;
173
+ depth += opening;
174
+ if ((previous == 0 && depth > 0) || (previous > 0 && depth == 0)) {
175
+ if (!buffer_push(context, output, value)) return false;
176
+ }
177
+ }
178
+ return true;
179
+ }
180
+
181
+ /* The complement of one inversion list over the whole code-point range. */
182
+ static bool set_complement(
183
+ OseoContext *context,
184
+ const uint32_t *values,
185
+ size_t count,
186
+ OseoRegExpBuffer *output
187
+ ) {
188
+ output->count = 0u;
189
+ uint32_t position = 0u;
190
+ for (size_t index = 0u; index + 1u < count; index += 2u) {
191
+ uint32_t start = values[index];
192
+ if (start > position) {
193
+ if (!buffer_push(context, output, position)) return false;
194
+ if (!buffer_push(context, output, start)) return false;
195
+ }
196
+ position = values[index + 1u];
197
+ }
198
+ if (position < OSEO_REGEXP_CODE_POINT_LIMIT) {
199
+ if (!buffer_push(context, output, position)) return false;
200
+ if (!buffer_push(context, output, OSEO_REGEXP_CODE_POINT_LIMIT)) {
201
+ return false;
202
+ }
203
+ }
204
+ return true;
205
+ }
206
+
207
+ /*
208
+ * Whether every member of one inversion list is a member of another.
209
+ *
210
+ * Both lists are normalized, so adjacent ranges never touch and one
211
+ * inner range is covered only when a single outer range contains it.
212
+ */
213
+ static bool set_contains_set(
214
+ const uint32_t *outer,
215
+ size_t outer_count,
216
+ const uint32_t *inner,
217
+ size_t inner_count
218
+ ) {
219
+ size_t position = 0u;
220
+ for (size_t index = 0u; index + 1u < inner_count; index += 2u) {
221
+ uint32_t start = inner[index];
222
+ uint32_t end = inner[index + 1u];
223
+ while (position + 1u < outer_count && outer[position + 1u] <= start) {
224
+ position += 2u;
225
+ }
226
+ if (position + 1u >= outer_count) return false;
227
+ if (outer[position] > start || outer[position + 1u] < end) {
228
+ return false;
229
+ }
230
+ }
231
+ return true;
232
+ }
233
+
234
+ /* Whether two inversion lists share at least one code point. */
235
+ static bool set_intersects(
236
+ const uint32_t *left,
237
+ size_t left_count,
238
+ const uint32_t *right,
239
+ size_t right_count
240
+ ) {
241
+ size_t left_index = 0u;
242
+ size_t right_index = 0u;
243
+ while (left_index + 1u < left_count && right_index + 1u < right_count) {
244
+ uint32_t left_start = left[left_index];
245
+ uint32_t left_end = left[left_index + 1u];
246
+ uint32_t right_start = right[right_index];
247
+ uint32_t right_end = right[right_index + 1u];
248
+ if (left_start < right_end && right_start < left_end) return true;
249
+ if (left_end <= right_end) left_index += 2u;
250
+ else right_index += 2u;
251
+ }
252
+ return false;
253
+ }
254
+
255
+ static int compare_ranges(const void *first, const void *second) {
256
+ const uint32_t *left = first;
257
+ const uint32_t *right = second;
258
+ if (left[0] != right[0]) return left[0] < right[0] ? -1 : 1;
259
+ if (left[1] != right[1]) return left[1] < right[1] ? -1 : 1;
260
+ return 0;
261
+ }
262
+
263
+ /*
264
+ * Normalize a list of half-open ranges into one inversion list.
265
+ *
266
+ * `ranges` is rewritten in place while sorting, so a caller that still
267
+ * needs its ranges must copy them first.
268
+ */
269
+ static bool set_of_ranges(
270
+ OseoContext *context,
271
+ uint32_t *ranges,
272
+ size_t range_count,
273
+ OseoRegExpBuffer *output
274
+ ) {
275
+ output->count = 0u;
276
+ if (range_count == 0u) return true;
277
+ qsort(ranges, range_count, 2u * sizeof(uint32_t), compare_ranges);
278
+ uint32_t start = ranges[0];
279
+ uint32_t end = ranges[1];
280
+ for (size_t index = 1u; index < range_count; index += 1u) {
281
+ uint32_t next_start = ranges[2u * index];
282
+ uint32_t next_end = ranges[2u * index + 1u];
283
+ if (next_start <= end) {
284
+ if (next_end > end) end = next_end;
285
+ continue;
286
+ }
287
+ if (!buffer_push(context, output, start)) return false;
288
+ if (!buffer_push(context, output, end)) return false;
289
+ start = next_start;
290
+ end = next_end;
291
+ }
292
+ if (!buffer_push(context, output, start)) return false;
293
+ return buffer_push(context, output, end);
294
+ }
295
+
296
+ /* The four code points the LineTerminator production names. */
297
+ static const uint32_t regexp_line_terminators[] = {
298
+ 0x0au, 0x0bu, 0x0du, 0x0eu, 0x2028u, 0x202au,
299
+ };
300
+
301
+ /* The sixty-three characters `WordCharacters` calls basic. */
302
+ static const uint32_t regexp_basic_word[] = {
303
+ 0x30u, 0x3au, 0x41u, 0x5bu, 0x5fu, 0x60u, 0x61u, 0x7bu,
304
+ };
305
+
306
+ /* The decimal digits `\d` names. */
307
+ static const uint32_t regexp_decimal_digits[] = {0x30u, 0x3au};
308
+
309
+ /*
310
+ * The `WhiteSpace`, `LineTerminator`, and Unicode 17.0.0
311
+ * `General_Category=Space_Separator` code points `\s` names, already
312
+ * merged. The differential fixture compares every boundary of this list
313
+ * with the reference hosts, which is what keeps it pinned to the same
314
+ * Unicode release the compiler-side matcher resolves from `@oseo/unicode`.
315
+ */
316
+ static const uint32_t regexp_white_space[] = {
317
+ 0x09u, 0x0eu, 0x20u, 0x21u, 0xa0u, 0xa1u, 0x1680u, 0x1681u,
318
+ 0x2000u, 0x200bu, 0x2028u, 0x202au, 0x202fu, 0x2030u, 0x205fu,
319
+ 0x2060u, 0x3000u, 0x3001u, 0xfeffu, 0xff00u,
320
+ };
321
+
322
+ /*
323
+ * The non-ASCII code points every `Canonicalize` rule leaves alone that
324
+ * the sets above can name.
325
+ *
326
+ * Only `\s`, `\S`, `.`, and a negated class can put a code point outside
327
+ * ASCII into a set this component builds without naming it, and all of
328
+ * them are the caseless separators and format characters listed here.
329
+ * Recording them lets the ignore-case closure prove that a set meets no
330
+ * case-equivalence class it has no data for.
331
+ */
332
+ static const uint32_t regexp_caseless_non_ascii[] = {
333
+ 0xa0u, 0xa1u, 0x1680u, 0x1681u, 0x2000u, 0x200bu, 0x2028u, 0x202au,
334
+ 0x202fu, 0x2030u, 0x205fu, 0x2060u, 0x3000u, 0x3001u, 0xfeffu,
335
+ 0xff00u,
336
+ };
337
+
338
+ /*
339
+ * The two case-equivalence classes that mix ASCII with non-ASCII.
340
+ *
341
+ * Simple case folding maps U+017F to `s` and U+212A to `k` and nothing
342
+ * else outside ASCII to an ASCII code point, and the code-unit
343
+ * uppercase rule maps nothing outside ASCII into ASCII at all. Every
344
+ * remaining class of two or more characters therefore lies entirely
345
+ * inside ASCII or entirely outside it.
346
+ */
347
+ static const uint32_t regexp_folded_k[] = {0x4bu, 0x6bu, 0x212au};
348
+ static const uint32_t regexp_folded_s[] = {0x53u, 0x73u, 0x17fu};
349
+
350
+ #define OSEO_REGEXP_SET_COUNT(array) (sizeof(array) / sizeof(uint32_t))
351
+
352
+ /* One decoded escape, whatever production it came from. */
353
+ typedef enum {
354
+ OSEO_REGEXP_ESCAPE_CHARACTER = 0,
355
+ OSEO_REGEXP_ESCAPE_CLASS = 1,
356
+ OSEO_REGEXP_ESCAPE_BOUNDARY = 2,
357
+ OSEO_REGEXP_ESCAPE_BACKREFERENCE = 3,
358
+ } OseoRegExpEscapeKind;
359
+
360
+ typedef enum {
361
+ OSEO_REGEXP_CLASS_DIGIT = 0,
362
+ OSEO_REGEXP_CLASS_SPACE = 1,
363
+ OSEO_REGEXP_CLASS_WORD = 2,
364
+ } OseoRegExpClassEscape;
365
+
366
+ typedef struct {
367
+ OseoRegExpEscapeKind kind;
368
+ OseoRegExpClassEscape class_escape;
369
+ uint32_t code_point;
370
+ size_t capture;
371
+ bool named;
372
+ bool negated;
373
+ } OseoRegExpEscapeResult;
374
+
375
+ /* One growable array of UTF-16 code units, used for group names. */
376
+ typedef struct {
377
+ uint16_t *values;
378
+ size_t count;
379
+ size_t capacity;
380
+ } OseoRegExpUnitBuffer;
381
+
382
+ static bool unit_buffer_push(
383
+ OseoContext *context,
384
+ OseoRegExpUnitBuffer *buffer,
385
+ uint16_t value
386
+ ) {
387
+ if (buffer->count == buffer->capacity) {
388
+ size_t capacity = buffer->capacity == 0u ? 16u : buffer->capacity * 2u;
389
+ if (capacity > SIZE_MAX / sizeof(uint16_t)) return false;
390
+ uint16_t *values = oseo_internal_reallocate_work_bytes(
391
+ context,
392
+ buffer->values,
393
+ capacity * sizeof(uint16_t)
394
+ );
395
+ if (values == NULL) return false;
396
+ buffer->values = values;
397
+ buffer->capacity = capacity;
398
+ }
399
+ buffer->values[buffer->count] = value;
400
+ buffer->count += 1u;
401
+ return true;
402
+ }
403
+
404
+ /*
405
+ * The emitter and parser state of one compilation.
406
+ *
407
+ * A target operand holds a label while instructions are emitted and the
408
+ * address that label was placed at afterwards, so a forward branch never
409
+ * needs a mutable instruction and moving a block only moves labels.
410
+ */
411
+ typedef struct {
412
+ OseoContext *context;
413
+ const OseoString *source;
414
+ size_t index;
415
+ uint16_t flag_mask;
416
+ bool unicode;
417
+ /*
418
+ * Whether a `\k` reads a group name, which Annex B decides once for
419
+ * the whole pattern: only a pattern that declares a group name does.
420
+ */
421
+ bool named_groups;
422
+ bool ignore_case;
423
+ bool multiline;
424
+ bool dot_all;
425
+
426
+ size_t capture_count;
427
+ size_t next_capture;
428
+ size_t registers;
429
+ size_t fail_label;
430
+ bool has_ignore_case_backreference;
431
+ bool sets_reach_unknown_case;
432
+ OseoRegExpValidation status;
433
+
434
+ OseoRegExpInstruction *instructions;
435
+ size_t instruction_count;
436
+ size_t instruction_capacity;
437
+
438
+ uint32_t *addresses;
439
+ size_t label_count;
440
+ size_t label_capacity;
441
+
442
+ OseoRegExpBuffer set_boundaries;
443
+ uint32_t *set_offsets;
444
+ uint32_t *set_hashes;
445
+ size_t set_count;
446
+ size_t set_capacity;
447
+ size_t word_sets[2];
448
+ bool word_sets_ready[2];
449
+
450
+ OseoRegExpRepeat *repeats;
451
+ size_t repeat_count;
452
+ size_t repeat_capacity;
453
+
454
+ OseoRegExpCapture *captures;
455
+ OseoRegExpUnitBuffer names;
456
+
457
+ OseoRegExpBuffer unknown_region;
458
+ /*
459
+ * Working sets. Every production names the buffers it writes, so a
460
+ * nested build never overwrites a set its caller still holds.
461
+ * `close_additions` and `close_ranges` belong to the ignore-case
462
+ * closure alone, and `blocks` is a stack of term-block starts.
463
+ */
464
+ OseoRegExpBuffer work[5];
465
+ OseoRegExpBuffer close_additions;
466
+ OseoRegExpBuffer close_ranges;
467
+ OseoRegExpBuffer class_ranges;
468
+ OseoRegExpBuffer blocks;
469
+ } OseoRegExpCompiler;
470
+
471
+ static void compiler_fail(
472
+ OseoRegExpCompiler *compiler,
473
+ OseoRegExpValidation status
474
+ ) {
475
+ if (compiler->status == OSEO_REGEXP_VALID) compiler->status = status;
476
+ }
477
+
478
+ static bool compiler_ok(const OseoRegExpCompiler *compiler) {
479
+ return compiler->status == OSEO_REGEXP_VALID;
480
+ }
481
+
482
+ static bool compiler_allocate(OseoRegExpCompiler *compiler, bool succeeded) {
483
+ if (succeeded) return true;
484
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
485
+ return false;
486
+ }
487
+
488
+ static size_t new_label(OseoRegExpCompiler *compiler) {
489
+ if (compiler->label_count == compiler->label_capacity) {
490
+ size_t capacity = compiler->label_capacity == 0u
491
+ ? 16u
492
+ : compiler->label_capacity * 2u;
493
+ if (capacity > SIZE_MAX / sizeof(uint32_t)) {
494
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
495
+ return 0u;
496
+ }
497
+ uint32_t *addresses = oseo_internal_reallocate_work_bytes(
498
+ compiler->context,
499
+ compiler->addresses,
500
+ capacity * sizeof(uint32_t)
501
+ );
502
+ if (addresses == NULL) {
503
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
504
+ return 0u;
505
+ }
506
+ compiler->addresses = addresses;
507
+ compiler->label_capacity = capacity;
508
+ }
509
+ compiler->addresses[compiler->label_count] = UINT32_MAX;
510
+ compiler->label_count += 1u;
511
+ return compiler->label_count - 1u;
512
+ }
513
+
514
+ static void place_label_at(
515
+ OseoRegExpCompiler *compiler,
516
+ size_t label,
517
+ size_t address
518
+ ) {
519
+ if (label < compiler->label_count) {
520
+ compiler->addresses[label] = (uint32_t)address;
521
+ }
522
+ }
523
+
524
+ static void place_label(OseoRegExpCompiler *compiler, size_t label) {
525
+ place_label_at(compiler, label, compiler->instruction_count);
526
+ }
527
+
528
+ static bool reserve_instructions(
529
+ OseoRegExpCompiler *compiler,
530
+ size_t additional
531
+ ) {
532
+ if (compiler->instruction_capacity - compiler->instruction_count >=
533
+ additional) {
534
+ return true;
535
+ }
536
+ size_t needed = compiler->instruction_count + additional;
537
+ size_t capacity = compiler->instruction_capacity == 0u
538
+ ? 16u
539
+ : compiler->instruction_capacity;
540
+ while (capacity < needed) {
541
+ if (capacity > SIZE_MAX / 2u) return false;
542
+ capacity *= 2u;
543
+ }
544
+ if (capacity > SIZE_MAX / sizeof(OseoRegExpInstruction)) return false;
545
+ OseoRegExpInstruction *instructions = oseo_internal_reallocate_work_bytes(
546
+ compiler->context,
547
+ compiler->instructions,
548
+ capacity * sizeof(OseoRegExpInstruction)
549
+ );
550
+ if (instructions == NULL) return false;
551
+ compiler->instructions = instructions;
552
+ compiler->instruction_capacity = capacity;
553
+ return true;
554
+ }
555
+
556
+ static void emit(
557
+ OseoRegExpCompiler *compiler,
558
+ uint8_t opcode,
559
+ uint8_t modifiers,
560
+ uint32_t first,
561
+ uint32_t second,
562
+ uint32_t third,
563
+ uint32_t fourth
564
+ ) {
565
+ if (!compiler_ok(compiler)) return;
566
+ if (compiler->instruction_count >= OSEO_REGEXP_INSTRUCTION_LIMIT) {
567
+ compiler_fail(compiler, OSEO_REGEXP_LIMIT);
568
+ return;
569
+ }
570
+ if (!compiler_allocate(compiler, reserve_instructions(compiler, 1u))) {
571
+ return;
572
+ }
573
+ OseoRegExpInstruction *instruction =
574
+ &compiler->instructions[compiler->instruction_count];
575
+ instruction->opcode = opcode;
576
+ instruction->modifiers = modifiers;
577
+ instruction->operands[0] = first;
578
+ instruction->operands[1] = second;
579
+ instruction->operands[2] = third;
580
+ instruction->operands[3] = fourth;
581
+ compiler->instruction_count += 1u;
582
+ }
583
+
584
+ /*
585
+ * Open a gap of `count` instructions at `at`.
586
+ *
587
+ * A quantifier and an alternation are both discovered only after the
588
+ * code they wrap has been emitted, so their prologue is written into a
589
+ * gap rather than by re-parsing. A label after the gap moves with the
590
+ * block it named. A label exactly at the gap stays, because every label
591
+ * placed at an address names the position execution reaches *before*
592
+ * whatever is emitted there next: the preceding term's continuation and
593
+ * a lookaround body entry both belong in front of the new prologue.
594
+ */
595
+ static bool emit_insert(
596
+ OseoRegExpCompiler *compiler,
597
+ size_t at,
598
+ size_t count
599
+ ) {
600
+ if (!compiler_ok(compiler)) return false;
601
+ if (compiler->instruction_count + count > OSEO_REGEXP_INSTRUCTION_LIMIT) {
602
+ compiler_fail(compiler, OSEO_REGEXP_LIMIT);
603
+ return false;
604
+ }
605
+ if (!compiler_allocate(compiler, reserve_instructions(compiler, count))) {
606
+ return false;
607
+ }
608
+ memmove(
609
+ &compiler->instructions[at + count],
610
+ &compiler->instructions[at],
611
+ (compiler->instruction_count - at) * sizeof(OseoRegExpInstruction)
612
+ );
613
+ memset(
614
+ &compiler->instructions[at],
615
+ 0,
616
+ count * sizeof(OseoRegExpInstruction)
617
+ );
618
+ compiler->instruction_count += count;
619
+ for (size_t label = 0u; label < compiler->label_count; label += 1u) {
620
+ uint32_t address = compiler->addresses[label];
621
+ if (address != UINT32_MAX && address > at) {
622
+ compiler->addresses[label] = address + (uint32_t)count;
623
+ }
624
+ }
625
+ return true;
626
+ }
627
+
628
+ static size_t allocate_registers(OseoRegExpCompiler *compiler, size_t count) {
629
+ if (compiler->registers + count > OSEO_REGEXP_REGISTER_LIMIT) {
630
+ compiler_fail(compiler, OSEO_REGEXP_LIMIT);
631
+ return 0u;
632
+ }
633
+ size_t base = compiler->registers;
634
+ compiler->registers += count;
635
+ return base;
636
+ }
637
+
638
+ static size_t allocate_repeat(
639
+ OseoRegExpCompiler *compiler,
640
+ uint64_t minimum,
641
+ uint64_t maximum,
642
+ uint32_t clear_from,
643
+ uint32_t clear_to
644
+ ) {
645
+ if (compiler->repeat_count == compiler->repeat_capacity) {
646
+ size_t capacity = compiler->repeat_capacity == 0u
647
+ ? 8u
648
+ : compiler->repeat_capacity * 2u;
649
+ if (capacity > SIZE_MAX / sizeof(OseoRegExpRepeat)) {
650
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
651
+ return 0u;
652
+ }
653
+ OseoRegExpRepeat *repeats = oseo_internal_reallocate_work_bytes(
654
+ compiler->context,
655
+ compiler->repeats,
656
+ capacity * sizeof(OseoRegExpRepeat)
657
+ );
658
+ if (repeats == NULL) {
659
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
660
+ return 0u;
661
+ }
662
+ compiler->repeats = repeats;
663
+ compiler->repeat_capacity = capacity;
664
+ }
665
+ compiler->repeats[compiler->repeat_count].minimum = minimum;
666
+ compiler->repeats[compiler->repeat_count].maximum = maximum;
667
+ compiler->repeats[compiler->repeat_count].clear_from = clear_from;
668
+ compiler->repeats[compiler->repeat_count].clear_to = clear_to;
669
+ compiler->repeat_count += 1u;
670
+ return compiler->repeat_count - 1u;
671
+ }
672
+
673
+ /*
674
+ * Intern one set, so equal sets share one program entry.
675
+ *
676
+ * A hash narrows the comparison to sets that could be equal, which keeps
677
+ * a pattern with many distinct classes linear in the number of sets
678
+ * rather than quadratic.
679
+ */
680
+ static size_t intern_set(
681
+ OseoRegExpCompiler *compiler,
682
+ const uint32_t *values,
683
+ size_t count
684
+ ) {
685
+ uint32_t hash = 0x811c9dc5u;
686
+ for (size_t index = 0u; index < count; index += 1u) {
687
+ hash = (hash ^ values[index]) * 0x01000193u;
688
+ }
689
+ for (size_t index = 0u; index < compiler->set_count; index += 1u) {
690
+ if (compiler->set_hashes[index] != hash) continue;
691
+ uint32_t start = compiler->set_offsets[index];
692
+ uint32_t end = compiler->set_offsets[index + 1u];
693
+ if ((size_t)(end - start) != count) continue;
694
+ if (count == 0u ||
695
+ memcmp(
696
+ &compiler->set_boundaries.values[start],
697
+ values,
698
+ count * sizeof(uint32_t)
699
+ ) == 0) {
700
+ return index;
701
+ }
702
+ }
703
+ if (compiler->set_count == compiler->set_capacity) {
704
+ size_t capacity = compiler->set_capacity == 0u
705
+ ? 8u
706
+ : compiler->set_capacity * 2u;
707
+ if (capacity > SIZE_MAX / sizeof(uint32_t) - 1u) {
708
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
709
+ return 0u;
710
+ }
711
+ uint32_t *offsets = oseo_internal_reallocate_work_bytes(
712
+ compiler->context,
713
+ compiler->set_offsets,
714
+ (capacity + 1u) * sizeof(uint32_t)
715
+ );
716
+ if (offsets == NULL) {
717
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
718
+ return 0u;
719
+ }
720
+ compiler->set_offsets = offsets;
721
+ uint32_t *hashes = oseo_internal_reallocate_work_bytes(
722
+ compiler->context,
723
+ compiler->set_hashes,
724
+ capacity * sizeof(uint32_t)
725
+ );
726
+ if (hashes == NULL) {
727
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
728
+ return 0u;
729
+ }
730
+ compiler->set_hashes = hashes;
731
+ compiler->set_capacity = capacity;
732
+ if (compiler->set_count == 0u) compiler->set_offsets[0] = 0u;
733
+ }
734
+ if (compiler->set_boundaries.count + count > UINT32_MAX) {
735
+ compiler_fail(compiler, OSEO_REGEXP_LIMIT);
736
+ return 0u;
737
+ }
738
+ if (!compiler_allocate(
739
+ compiler,
740
+ buffer_reserve(
741
+ compiler->context,
742
+ &compiler->set_boundaries,
743
+ count
744
+ )
745
+ )) {
746
+ return 0u;
747
+ }
748
+ if (count > 0u) {
749
+ memcpy(
750
+ &compiler->set_boundaries.values[compiler->set_boundaries.count],
751
+ values,
752
+ count * sizeof(uint32_t)
753
+ );
754
+ compiler->set_boundaries.count += count;
755
+ }
756
+ compiler->set_hashes[compiler->set_count] = hash;
757
+ compiler->set_count += 1u;
758
+ compiler->set_offsets[compiler->set_count] =
759
+ (uint32_t)compiler->set_boundaries.count;
760
+ return compiler->set_count - 1u;
761
+ }
762
+
763
+ static bool buffer_copy(
764
+ OseoContext *context,
765
+ OseoRegExpBuffer *output,
766
+ const uint32_t *values,
767
+ size_t count
768
+ ) {
769
+ output->count = 0u;
770
+ if (!buffer_reserve(context, output, count)) return false;
771
+ if (count > 0u) memcpy(output->values, values, count * sizeof(uint32_t));
772
+ output->count = count;
773
+ return true;
774
+ }
775
+
776
+ /*
777
+ * A valid modifier group can enable ignore-case matching even when the
778
+ * enclosing RegExp has no `i` flag. This deliberately conservative scan may
779
+ * recognize escaped or class-contained text, but cannot miss an admitted
780
+ * modifier group and keeps the ordinary case-sensitive path allocation-free.
781
+ */
782
+ static bool pattern_may_enable_ignore_case(const OseoString *source) {
783
+ for (size_t index = 0u; index + 2u < source->length; index += 1u) {
784
+ if (source->units[index] != '(' ||
785
+ source->units[index + 1u] != '?') {
786
+ continue;
787
+ }
788
+ for (size_t cursor = index + 2u;
789
+ cursor < source->length;
790
+ cursor += 1u) {
791
+ uint16_t unit = source->units[cursor];
792
+ if (unit == ':' || unit == '-' || unit == ')') break;
793
+ if (unit == 'i') return true;
794
+ }
795
+ }
796
+ return false;
797
+ }
798
+
799
+ /*
800
+ * Build the region of code points whose case-equivalence class this
801
+ * component has no data for.
802
+ *
803
+ * Everything below U+0080 belongs to a class the closure knows exactly,
804
+ * and so do the caseless separators and the two mixed classes. A set
805
+ * that meets this region partially could be closed only by folding data
806
+ * that is not linked, which is a located boundary rather than a guess.
807
+ */
808
+ static bool build_unknown_region(OseoRegExpCompiler *compiler) {
809
+ OseoRegExpBuffer *exempt = &compiler->work[0];
810
+ OseoRegExpBuffer *combined = &compiler->work[1];
811
+ exempt->count = 0u;
812
+ OseoContext *context = compiler->context;
813
+ if (!buffer_push(context, exempt, 0u)) return false;
814
+ if (!buffer_push(context, exempt, 0x80u)) return false;
815
+ if (!set_union(
816
+ context,
817
+ exempt->values,
818
+ exempt->count,
819
+ regexp_caseless_non_ascii,
820
+ OSEO_REGEXP_SET_COUNT(regexp_caseless_non_ascii),
821
+ combined
822
+ )) {
823
+ return false;
824
+ }
825
+ if (compiler->unicode) {
826
+ static const uint32_t mixed[] = {
827
+ 0x17fu, 0x180u, 0x212au, 0x212bu,
828
+ };
829
+ if (!set_union(
830
+ context,
831
+ combined->values,
832
+ combined->count,
833
+ mixed,
834
+ OSEO_REGEXP_SET_COUNT(mixed),
835
+ exempt
836
+ )) {
837
+ return false;
838
+ }
839
+ return set_complement(
840
+ context,
841
+ exempt->values,
842
+ exempt->count,
843
+ &compiler->unknown_region
844
+ );
845
+ }
846
+ return set_complement(
847
+ context,
848
+ combined->values,
849
+ combined->count,
850
+ &compiler->unknown_region
851
+ );
852
+ }
853
+
854
+ /*
855
+ * Close one set under the pattern's canonicalization.
856
+ *
857
+ * `CharacterSetMatcher` compares the canonical form of the input
858
+ * character with the canonical form of every set member, so a set
859
+ * matches exactly the characters whose equivalence class meets it. Only
860
+ * a character with a case sibling can join a set it was not already in,
861
+ * so closing walks the known equivalence classes rather than the whole
862
+ * code-point range, and refuses a set that meets an unknown class.
863
+ */
864
+ static bool close_set(
865
+ OseoRegExpCompiler *compiler,
866
+ const uint32_t *values,
867
+ size_t count,
868
+ OseoRegExpBuffer *output
869
+ ) {
870
+ OseoContext *context = compiler->context;
871
+ if (!compiler->ignore_case) {
872
+ return compiler_allocate(
873
+ compiler,
874
+ buffer_copy(context, output, values, count)
875
+ );
876
+ }
877
+ const uint32_t *region = compiler->unknown_region.values;
878
+ size_t region_count = compiler->unknown_region.count;
879
+ if (set_intersects(values, count, region, region_count) &&
880
+ !set_contains_set(values, count, region, region_count)) {
881
+ compiler_fail(compiler, OSEO_REGEXP_UNSUPPORTED);
882
+ return false;
883
+ }
884
+ OseoRegExpBuffer *ranges = &compiler->close_ranges;
885
+ ranges->count = 0u;
886
+ for (uint32_t offset = 0u; offset < 26u; offset += 1u) {
887
+ uint32_t upper = 0x41u + offset;
888
+ uint32_t lower = 0x61u + offset;
889
+ if (!set_has(values, count, upper) &&
890
+ !set_has(values, count, lower)) {
891
+ continue;
892
+ }
893
+ if (!buffer_push(context, ranges, upper) ||
894
+ !buffer_push(context, ranges, upper + 1u) ||
895
+ !buffer_push(context, ranges, lower) ||
896
+ !buffer_push(context, ranges, lower + 1u)) {
897
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
898
+ return false;
899
+ }
900
+ }
901
+ if (compiler->unicode) {
902
+ const uint32_t *classes[2] = {regexp_folded_k, regexp_folded_s};
903
+ for (size_t index = 0u; index < 2u; index += 1u) {
904
+ bool meets = false;
905
+ for (size_t member = 0u; member < 3u; member += 1u) {
906
+ if (set_has(values, count, classes[index][member])) {
907
+ meets = true;
908
+ }
909
+ }
910
+ if (!meets) continue;
911
+ for (size_t member = 0u; member < 3u; member += 1u) {
912
+ uint32_t code_point = classes[index][member];
913
+ if (!buffer_push(context, ranges, code_point) ||
914
+ !buffer_push(context, ranges, code_point + 1u)) {
915
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
916
+ return false;
917
+ }
918
+ }
919
+ }
920
+ }
921
+ if (ranges->count == 0u) {
922
+ return compiler_allocate(
923
+ compiler,
924
+ buffer_copy(context, output, values, count)
925
+ );
926
+ }
927
+ OseoRegExpBuffer *additions = &compiler->close_additions;
928
+ if (!set_of_ranges(
929
+ context,
930
+ ranges->values,
931
+ ranges->count / 2u,
932
+ additions
933
+ )) {
934
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
935
+ return false;
936
+ }
937
+ return compiler_allocate(
938
+ compiler,
939
+ set_union(
940
+ context,
941
+ values,
942
+ count,
943
+ additions->values,
944
+ additions->count,
945
+ output
946
+ )
947
+ );
948
+ }
949
+
950
+ /* The raw set one `\d`, `\s`, or `\w` escape names, before closing. */
951
+ static bool class_escape_set(
952
+ OseoRegExpCompiler *compiler,
953
+ OseoRegExpClassEscape kind,
954
+ OseoRegExpBuffer *output
955
+ ) {
956
+ if (kind == OSEO_REGEXP_CLASS_DIGIT) {
957
+ return compiler_allocate(
958
+ compiler,
959
+ buffer_copy(
960
+ compiler->context,
961
+ output,
962
+ regexp_decimal_digits,
963
+ OSEO_REGEXP_SET_COUNT(regexp_decimal_digits)
964
+ )
965
+ );
966
+ }
967
+ if (kind == OSEO_REGEXP_CLASS_SPACE) {
968
+ return compiler_allocate(
969
+ compiler,
970
+ buffer_copy(
971
+ compiler->context,
972
+ output,
973
+ regexp_white_space,
974
+ OSEO_REGEXP_SET_COUNT(regexp_white_space)
975
+ )
976
+ );
977
+ }
978
+ /*
979
+ * The edition adds a character outside the basic sixty-three only
980
+ * when both `i` and a unicode-mode flag are set, and closing the
981
+ * basic set under the code-unit rule adds nothing, so one expression
982
+ * covers every flag combination.
983
+ */
984
+ return close_set(
985
+ compiler,
986
+ regexp_basic_word,
987
+ OSEO_REGEXP_SET_COUNT(regexp_basic_word),
988
+ output
989
+ );
990
+ }
991
+
992
+ /*
993
+ * Record one consuming set and note whether it can match a character
994
+ * whose case-equivalence class is unknown, which is what decides
995
+ * afterwards whether a backreference under `i` can be lowered.
996
+ */
997
+ static uint32_t intern_consuming_set(
998
+ OseoRegExpCompiler *compiler,
999
+ const OseoRegExpBuffer *set
1000
+ ) {
1001
+ if (compiler->ignore_case &&
1002
+ set_intersects(
1003
+ set->values,
1004
+ set->count,
1005
+ compiler->unknown_region.values,
1006
+ compiler->unknown_region.count
1007
+ )) {
1008
+ compiler->sets_reach_unknown_case = true;
1009
+ }
1010
+ return (uint32_t)intern_set(compiler, set->values, set->count);
1011
+ }
1012
+
1013
+ static bool ascii_alpha(uint16_t unit) {
1014
+ return (unit >= 'A' && unit <= 'Z') || (unit >= 'a' && unit <= 'z');
1015
+ }
1016
+
1017
+ static bool ascii_digit(uint16_t unit) {
1018
+ return unit >= '0' && unit <= '9';
1019
+ }
1020
+
1021
+ static bool hex_digit(uint16_t unit, uint32_t *value) {
1022
+ if (ascii_digit(unit)) {
1023
+ *value = (uint32_t)(unit - '0');
1024
+ return true;
1025
+ }
1026
+ if (unit >= 'A' && unit <= 'F') {
1027
+ *value = (uint32_t)(unit - 'A') + 10u;
1028
+ return true;
1029
+ }
1030
+ if (unit >= 'a' && unit <= 'f') {
1031
+ *value = (uint32_t)(unit - 'a') + 10u;
1032
+ return true;
1033
+ }
1034
+ return false;
1035
+ }
1036
+
1037
+ static uint16_t unit_at(const OseoRegExpCompiler *compiler, size_t index) {
1038
+ return index < compiler->source->length
1039
+ ? compiler->source->units[index]
1040
+ : (uint16_t)0u;
1041
+ }
1042
+
1043
+ static bool at_end(const OseoRegExpCompiler *compiler) {
1044
+ return compiler->index >= compiler->source->length;
1045
+ }
1046
+
1047
+ /* Read one pattern code point, pairing surrogates in unicode mode. */
1048
+ static uint32_t read_code_point(OseoRegExpCompiler *compiler) {
1049
+ uint16_t lead = unit_at(compiler, compiler->index);
1050
+ compiler->index += 1u;
1051
+ if (!compiler->unicode || lead < 0xd800u || lead > 0xdbffu) {
1052
+ return lead;
1053
+ }
1054
+ uint16_t trail = unit_at(compiler, compiler->index);
1055
+ if (compiler->index >= compiler->source->length || trail < 0xdc00u ||
1056
+ trail > 0xdfffu) {
1057
+ return lead;
1058
+ }
1059
+ compiler->index += 1u;
1060
+ return 0x10000u + (((uint32_t)lead - 0xd800u) << 10u) +
1061
+ ((uint32_t)trail - 0xdc00u);
1062
+ }
1063
+
1064
+ static bool name_hex_digits(
1065
+ const OseoString *source,
1066
+ size_t start,
1067
+ size_t count,
1068
+ uint32_t *value
1069
+ ) {
1070
+ if (start > source->length || count > source->length - start) {
1071
+ return false;
1072
+ }
1073
+ uint32_t result = 0u;
1074
+ for (size_t offset = 0u; offset < count; offset += 1u) {
1075
+ uint32_t digit = 0u;
1076
+ if (!hex_digit(source->units[start + offset], &digit)) return false;
1077
+ result = result * 16u + digit;
1078
+ }
1079
+ *value = result;
1080
+ return true;
1081
+ }
1082
+
1083
+ /*
1084
+ * Read one RegExpUnicodeEscapeSequence[+UnicodeMode] after its `\u`.
1085
+ *
1086
+ * A group name always uses the unicode form, whatever the flags say, so
1087
+ * a braced code point is admitted and an escaped lead surrogate joins an
1088
+ * escaped trail surrogate that follows it. Any other lead or trail
1089
+ * surrogate stays a lone code point, which no identifier admits.
1090
+ */
1091
+ static bool name_unicode_escape(
1092
+ const OseoString *source,
1093
+ size_t *index,
1094
+ uint32_t *code_point
1095
+ ) {
1096
+ size_t cursor = *index;
1097
+ if (cursor < source->length && source->units[cursor] == '{') {
1098
+ cursor += 1u;
1099
+ uint32_t value = 0u;
1100
+ size_t digits = 0u;
1101
+ while (cursor < source->length && source->units[cursor] != '}') {
1102
+ uint32_t digit = 0u;
1103
+ if (!hex_digit(source->units[cursor], &digit) ||
1104
+ value > (0x10ffffu - digit) / 16u) {
1105
+ return false;
1106
+ }
1107
+ value = value * 16u + digit;
1108
+ digits += 1u;
1109
+ cursor += 1u;
1110
+ }
1111
+ if (digits == 0u || cursor >= source->length) return false;
1112
+ *index = cursor + 1u;
1113
+ *code_point = value;
1114
+ return true;
1115
+ }
1116
+ uint32_t value = 0u;
1117
+ if (!name_hex_digits(source, cursor, 4u, &value)) return false;
1118
+ cursor += 4u;
1119
+ uint32_t trail = 0u;
1120
+ if (value >= 0xd800u && value <= 0xdbffu &&
1121
+ cursor + 1u < source->length &&
1122
+ source->units[cursor] == '\\' &&
1123
+ source->units[cursor + 1u] == 'u' &&
1124
+ name_hex_digits(source, cursor + 2u, 4u, &trail) &&
1125
+ trail >= 0xdc00u && trail <= 0xdfffu) {
1126
+ value = 0x10000u + ((value - 0xd800u) << 10u) + (trail - 0xdc00u);
1127
+ cursor += 6u;
1128
+ }
1129
+ *index = cursor;
1130
+ *code_point = value;
1131
+ return true;
1132
+ }
1133
+
1134
+ bool oseo_internal_regexp_group_name(
1135
+ const OseoString *source,
1136
+ size_t start,
1137
+ size_t *end,
1138
+ uint16_t *units,
1139
+ size_t *unit_count
1140
+ ) {
1141
+ size_t index = start;
1142
+ size_t count = 0u;
1143
+ while (index < source->length && source->units[index] != '>') {
1144
+ uint32_t code_point = source->units[index];
1145
+ if (code_point == '\\') {
1146
+ index += 1u;
1147
+ if (index >= source->length || source->units[index] != 'u') {
1148
+ return false;
1149
+ }
1150
+ index += 1u;
1151
+ if (!name_unicode_escape(source, &index, &code_point)) {
1152
+ return false;
1153
+ }
1154
+ } else {
1155
+ index += 1u;
1156
+ /*
1157
+ * RegExpIdentifierStart and RegExpIdentifierPart pair a
1158
+ * written surrogate pair in every mode, not only under `u`.
1159
+ */
1160
+ if (code_point >= 0xd800u && code_point <= 0xdbffu &&
1161
+ index < source->length &&
1162
+ source->units[index] >= 0xdc00u &&
1163
+ source->units[index] <= 0xdfffu) {
1164
+ code_point = 0x10000u + ((code_point - 0xd800u) << 10u) +
1165
+ ((uint32_t)source->units[index] - 0xdc00u);
1166
+ index += 1u;
1167
+ }
1168
+ }
1169
+ bool admitted = code_point == '$' || code_point == '_' ||
1170
+ (count == 0u
1171
+ ? oseo_internal_unicode_id_start(code_point)
1172
+ : (code_point == 0x200cu || code_point == 0x200du ||
1173
+ oseo_internal_unicode_id_continue(code_point)));
1174
+ if (!admitted) return false;
1175
+ if (code_point > 0xffffu) {
1176
+ if (units != NULL) {
1177
+ units[count] = (uint16_t)(
1178
+ 0xd800u + ((code_point - 0x10000u) >> 10u)
1179
+ );
1180
+ units[count + 1u] = (uint16_t)(
1181
+ 0xdc00u + ((code_point - 0x10000u) & 0x3ffu)
1182
+ );
1183
+ }
1184
+ count += 2u;
1185
+ } else {
1186
+ if (units != NULL) units[count] = (uint16_t)code_point;
1187
+ count += 1u;
1188
+ }
1189
+ }
1190
+ if (count == 0u || index >= source->length) return false;
1191
+ *end = index;
1192
+ if (unit_count != NULL) *unit_count = count;
1193
+ return true;
1194
+ }
1195
+
1196
+ /*
1197
+ * Append the decoded name that starts at `start` to `names`.
1198
+ *
1199
+ * Validation already accepted the name, so a failure here is only an
1200
+ * allocation failure. The decoded name is what `groups` reports and what
1201
+ * a named reference compares, so an escaped spelling and a written one
1202
+ * name the same group.
1203
+ */
1204
+ static bool push_group_name(
1205
+ OseoRegExpCompiler *compiler,
1206
+ size_t start,
1207
+ size_t *end,
1208
+ size_t *length
1209
+ ) {
1210
+ size_t count = 0u;
1211
+ if (!oseo_internal_regexp_group_name(
1212
+ compiler->source,
1213
+ start,
1214
+ end,
1215
+ NULL,
1216
+ &count
1217
+ )) {
1218
+ return false;
1219
+ }
1220
+ size_t offset = compiler->names.count;
1221
+ for (size_t index = 0u; index < count; index += 1u) {
1222
+ if (!unit_buffer_push(compiler->context, &compiler->names, 0u)) {
1223
+ return false;
1224
+ }
1225
+ }
1226
+ oseo_internal_regexp_group_name(
1227
+ compiler->source,
1228
+ start,
1229
+ end,
1230
+ &compiler->names.values[offset],
1231
+ &count
1232
+ );
1233
+ *length = count;
1234
+ return true;
1235
+ }
1236
+
1237
+ /*
1238
+ * The first capture index one group name names.
1239
+ *
1240
+ * Names are collected before emission because a reference may precede
1241
+ * its declaration. A duplicate name has more matching captures after the
1242
+ * first, and emission preserves all of them.
1243
+ */
1244
+ static size_t lookup_group(
1245
+ const OseoRegExpCompiler *compiler,
1246
+ const uint16_t *units,
1247
+ size_t length
1248
+ ) {
1249
+ for (size_t index = 0u; index < compiler->capture_count; index += 1u) {
1250
+ const OseoRegExpCapture *capture = &compiler->captures[index];
1251
+ if (!capture->named || capture->name_length != length) continue;
1252
+ if (memcmp(
1253
+ &compiler->names.values[capture->name_offset],
1254
+ units,
1255
+ length * sizeof(uint16_t)
1256
+ ) == 0) {
1257
+ return index + 1u;
1258
+ }
1259
+ }
1260
+ return SIZE_MAX;
1261
+ }
1262
+
1263
+ /*
1264
+ * Assign capture indices and record group names in source order.
1265
+ *
1266
+ * The walk mirrors the validator's group recognition, which has already
1267
+ * accepted the pattern, so it needs no error path of its own.
1268
+ */
1269
+ static bool collect_captures(OseoRegExpCompiler *compiler) {
1270
+ const OseoString *source = compiler->source;
1271
+ bool character_class = false;
1272
+ size_t assigned = 0u;
1273
+ for (size_t index = 0u; index < source->length; index += 1u) {
1274
+ uint16_t unit = source->units[index];
1275
+ if (unit == '\\') {
1276
+ if (index + 1u < source->length) index += 1u;
1277
+ continue;
1278
+ }
1279
+ if (character_class) {
1280
+ if (unit == ']') character_class = false;
1281
+ continue;
1282
+ }
1283
+ if (unit == '[') {
1284
+ character_class = true;
1285
+ continue;
1286
+ }
1287
+ if (unit != '(') continue;
1288
+ bool named = index + 3u < source->length &&
1289
+ source->units[index + 1u] == '?' &&
1290
+ source->units[index + 2u] == '<' &&
1291
+ source->units[index + 3u] != '=' &&
1292
+ source->units[index + 3u] != '!';
1293
+ bool capture = index + 1u >= source->length ||
1294
+ source->units[index + 1u] != '?' || named;
1295
+ if (!capture) continue;
1296
+ if (assigned >= compiler->capture_count) return false;
1297
+ OseoRegExpCapture *record = &compiler->captures[assigned];
1298
+ record->named = named;
1299
+ if (named) compiler->named_groups = true;
1300
+ record->name_offset = 0u;
1301
+ record->name_length = 0u;
1302
+ if (named) {
1303
+ size_t end = 0u;
1304
+ size_t length = 0u;
1305
+ record->name_offset = (uint32_t)compiler->names.count;
1306
+ if (!push_group_name(compiler, index + 3u, &end, &length)) {
1307
+ return false;
1308
+ }
1309
+ record->name_length = (uint32_t)length;
1310
+ index = end;
1311
+ }
1312
+ assigned += 1u;
1313
+ }
1314
+ return assigned == compiler->capture_count;
1315
+ }
1316
+
1317
+ /* Decode one escape sequence, leaving the cursor past it. */
1318
+ static bool parse_escape(
1319
+ OseoRegExpCompiler *compiler,
1320
+ bool in_class,
1321
+ OseoRegExpEscapeResult *result
1322
+ ) {
1323
+ result->kind = OSEO_REGEXP_ESCAPE_CHARACTER;
1324
+ result->class_escape = OSEO_REGEXP_CLASS_DIGIT;
1325
+ result->code_point = 0u;
1326
+ result->capture = 0u;
1327
+ result->named = false;
1328
+ result->negated = false;
1329
+ compiler->index += 1u;
1330
+ if (at_end(compiler)) {
1331
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1332
+ return false;
1333
+ }
1334
+ uint16_t unit = unit_at(compiler, compiler->index);
1335
+ switch (unit) {
1336
+ case 'd':
1337
+ case 'D':
1338
+ result->kind = OSEO_REGEXP_ESCAPE_CLASS;
1339
+ result->class_escape = OSEO_REGEXP_CLASS_DIGIT;
1340
+ result->negated = unit == 'D';
1341
+ compiler->index += 1u;
1342
+ return true;
1343
+ case 's':
1344
+ case 'S':
1345
+ result->kind = OSEO_REGEXP_ESCAPE_CLASS;
1346
+ result->class_escape = OSEO_REGEXP_CLASS_SPACE;
1347
+ result->negated = unit == 'S';
1348
+ compiler->index += 1u;
1349
+ return true;
1350
+ case 'w':
1351
+ case 'W':
1352
+ result->kind = OSEO_REGEXP_ESCAPE_CLASS;
1353
+ result->class_escape = OSEO_REGEXP_CLASS_WORD;
1354
+ result->negated = unit == 'W';
1355
+ compiler->index += 1u;
1356
+ return true;
1357
+ case 'b':
1358
+ compiler->index += 1u;
1359
+ if (in_class) {
1360
+ result->code_point = 0x08u;
1361
+ return true;
1362
+ }
1363
+ result->kind = OSEO_REGEXP_ESCAPE_BOUNDARY;
1364
+ return true;
1365
+ case 'B':
1366
+ compiler->index += 1u;
1367
+ if (in_class) {
1368
+ // Annex B's identity escape; validation refused it in
1369
+ // a unicode-mode class.
1370
+ result->code_point = 'B';
1371
+ return true;
1372
+ }
1373
+ result->kind = OSEO_REGEXP_ESCAPE_BOUNDARY;
1374
+ result->negated = true;
1375
+ return true;
1376
+ case 'f':
1377
+ compiler->index += 1u;
1378
+ result->code_point = 0x0cu;
1379
+ return true;
1380
+ case 'n':
1381
+ compiler->index += 1u;
1382
+ result->code_point = 0x0au;
1383
+ return true;
1384
+ case 'r':
1385
+ compiler->index += 1u;
1386
+ result->code_point = 0x0du;
1387
+ return true;
1388
+ case 't':
1389
+ compiler->index += 1u;
1390
+ result->code_point = 0x09u;
1391
+ return true;
1392
+ case 'v':
1393
+ compiler->index += 1u;
1394
+ result->code_point = 0x0bu;
1395
+ return true;
1396
+ case 'p':
1397
+ case 'P':
1398
+ case 'q':
1399
+ if (!compiler->unicode) break;
1400
+ compiler_fail(compiler, OSEO_REGEXP_UNSUPPORTED);
1401
+ return false;
1402
+ default:
1403
+ break;
1404
+ }
1405
+ if (unit == 'c') {
1406
+ uint16_t letter = unit_at(compiler, compiler->index + 1u);
1407
+ if (ascii_alpha(letter) ||
1408
+ (in_class && !compiler->unicode &&
1409
+ (ascii_digit(letter) || letter == '_'))) {
1410
+ result->code_point = (uint32_t)(letter & 0x1fu);
1411
+ compiler->index += 2u;
1412
+ return true;
1413
+ }
1414
+ // Annex B: the backslash alone, with the `c` left for the next
1415
+ // atom. Validation refused this text under `u` and `v`.
1416
+ result->code_point = '\\';
1417
+ return true;
1418
+ }
1419
+ if (unit == 'x') {
1420
+ uint32_t high = 0u;
1421
+ uint32_t low = 0u;
1422
+ if (!hex_digit(unit_at(compiler, compiler->index + 1u), &high) ||
1423
+ !hex_digit(unit_at(compiler, compiler->index + 2u), &low)) {
1424
+ // Annex B's identity escape over the incomplete `x`.
1425
+ compiler->index += 1u;
1426
+ result->code_point = 'x';
1427
+ return true;
1428
+ }
1429
+ result->code_point = high * 16u + low;
1430
+ compiler->index += 3u;
1431
+ return true;
1432
+ }
1433
+ if (unit == 'u' && !compiler->unicode) {
1434
+ uint32_t value = 0u;
1435
+ for (size_t offset = 1u; offset <= 4u; offset += 1u) {
1436
+ uint32_t digit = 0u;
1437
+ if (!hex_digit(
1438
+ unit_at(compiler, compiler->index + offset),
1439
+ &digit
1440
+ )) {
1441
+ // Annex B's identity escape over the incomplete `u`,
1442
+ // including the braced form without `u` or `v`.
1443
+ compiler->index += 1u;
1444
+ result->code_point = 'u';
1445
+ return true;
1446
+ }
1447
+ value = value * 16u + digit;
1448
+ }
1449
+ compiler->index += 5u;
1450
+ result->code_point = value;
1451
+ return true;
1452
+ }
1453
+ if (unit == 'u') {
1454
+ compiler->index += 1u;
1455
+ if (unit_at(compiler, compiler->index) == '{') {
1456
+ compiler->index += 1u;
1457
+ uint32_t value = 0u;
1458
+ while (!at_end(compiler) &&
1459
+ unit_at(compiler, compiler->index) != '}') {
1460
+ uint32_t digit = 0u;
1461
+ if (!hex_digit(unit_at(compiler, compiler->index), &digit)) {
1462
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1463
+ return false;
1464
+ }
1465
+ value = value * 16u + digit;
1466
+ compiler->index += 1u;
1467
+ }
1468
+ compiler->index += 1u;
1469
+ result->code_point = value;
1470
+ return true;
1471
+ }
1472
+ uint32_t value = 0u;
1473
+ for (size_t offset = 0u; offset < 4u; offset += 1u) {
1474
+ uint32_t digit = 0u;
1475
+ if (!hex_digit(
1476
+ unit_at(compiler, compiler->index + offset),
1477
+ &digit
1478
+ )) {
1479
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1480
+ return false;
1481
+ }
1482
+ value = value * 16u + digit;
1483
+ }
1484
+ compiler->index += 4u;
1485
+ if (compiler->unicode && value >= 0xd800u && value <= 0xdbffu &&
1486
+ unit_at(compiler, compiler->index) == '\\' &&
1487
+ unit_at(compiler, compiler->index + 1u) == 'u') {
1488
+ uint32_t trail = 0u;
1489
+ bool paired = true;
1490
+ for (size_t offset = 0u; offset < 4u; offset += 1u) {
1491
+ uint32_t digit = 0u;
1492
+ if (!hex_digit(
1493
+ unit_at(compiler, compiler->index + 2u + offset),
1494
+ &digit
1495
+ )) {
1496
+ paired = false;
1497
+ break;
1498
+ }
1499
+ trail = trail * 16u + digit;
1500
+ }
1501
+ if (paired && trail >= 0xdc00u && trail <= 0xdfffu) {
1502
+ value = 0x10000u + ((value - 0xd800u) << 10u) +
1503
+ (trail - 0xdc00u);
1504
+ compiler->index += 6u;
1505
+ }
1506
+ }
1507
+ result->code_point = value;
1508
+ return true;
1509
+ }
1510
+ if (unit == 'k' && !in_class && !compiler->unicode &&
1511
+ !compiler->named_groups) {
1512
+ compiler->index += 1u;
1513
+ result->code_point = 'k';
1514
+ return true;
1515
+ }
1516
+ if (unit == 'k' && !in_class) {
1517
+ size_t end = 0u;
1518
+ size_t length = 0u;
1519
+ size_t mark = compiler->names.count;
1520
+ if (!push_group_name(compiler, compiler->index + 2u, &end, &length)) {
1521
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
1522
+ return false;
1523
+ }
1524
+ size_t capture = lookup_group(
1525
+ compiler,
1526
+ &compiler->names.values[mark],
1527
+ length
1528
+ );
1529
+ compiler->names.count = mark;
1530
+ compiler->index = end + 1u;
1531
+ if (capture == SIZE_MAX) {
1532
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1533
+ return false;
1534
+ }
1535
+ result->kind = OSEO_REGEXP_ESCAPE_BACKREFERENCE;
1536
+ result->capture = capture;
1537
+ result->named = true;
1538
+ return true;
1539
+ }
1540
+ if (ascii_digit(unit) && !compiler->unicode) {
1541
+ /*
1542
+ * Annex B: a DecimalEscape names a group only up to the capture
1543
+ * count. Otherwise it is an identity escape over 8 or 9, or a
1544
+ * legacy octal escape, and the digits it leaves are characters.
1545
+ */
1546
+ size_t start = compiler->index;
1547
+ if (!in_class && unit != '0') {
1548
+ size_t value = 0u;
1549
+ size_t end = start;
1550
+ while (end < compiler->source->length &&
1551
+ ascii_digit(unit_at(compiler, end))) {
1552
+ if (value <= compiler->capture_count) {
1553
+ value = value * 10u +
1554
+ (size_t)(unit_at(compiler, end) - '0');
1555
+ }
1556
+ end += 1u;
1557
+ }
1558
+ if (value <= compiler->capture_count) {
1559
+ compiler->index = end;
1560
+ result->kind = OSEO_REGEXP_ESCAPE_BACKREFERENCE;
1561
+ result->capture = value;
1562
+ return true;
1563
+ }
1564
+ }
1565
+ compiler->index += 1u;
1566
+ if (unit >= '8') {
1567
+ result->code_point = unit;
1568
+ return true;
1569
+ }
1570
+ uint32_t value = (uint32_t)(unit - '0');
1571
+ uint16_t next = unit_at(compiler, compiler->index);
1572
+ if (next >= '0' && next <= '7') {
1573
+ value = value * 8u + (uint32_t)(next - '0');
1574
+ compiler->index += 1u;
1575
+ next = unit_at(compiler, compiler->index);
1576
+ if (unit <= '3' && next >= '0' && next <= '7') {
1577
+ value = value * 8u + (uint32_t)(next - '0');
1578
+ compiler->index += 1u;
1579
+ }
1580
+ }
1581
+ result->code_point = value;
1582
+ return true;
1583
+ }
1584
+ if (ascii_digit(unit)) {
1585
+ if (unit == '0' || in_class) {
1586
+ compiler->index += 1u;
1587
+ result->code_point = unit == '0' ? 0u : (uint32_t)unit;
1588
+ return true;
1589
+ }
1590
+ size_t value = 0u;
1591
+ while (!at_end(compiler) &&
1592
+ ascii_digit(unit_at(compiler, compiler->index))) {
1593
+ value = value * 10u +
1594
+ (size_t)(unit_at(compiler, compiler->index) - '0');
1595
+ compiler->index += 1u;
1596
+ if (value > compiler->capture_count) break;
1597
+ }
1598
+ if (value == 0u || value > compiler->capture_count) {
1599
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1600
+ return false;
1601
+ }
1602
+ result->kind = OSEO_REGEXP_ESCAPE_BACKREFERENCE;
1603
+ result->capture = value;
1604
+ return true;
1605
+ }
1606
+ result->code_point = read_code_point(compiler);
1607
+ return true;
1608
+ }
1609
+
1610
+ static void buffer_swap(OseoRegExpBuffer *first, OseoRegExpBuffer *second) {
1611
+ OseoRegExpBuffer temporary = *first;
1612
+ *first = *second;
1613
+ *second = temporary;
1614
+ }
1615
+
1616
+ typedef struct {
1617
+ bool is_set;
1618
+ bool negated;
1619
+ uint32_t code_point;
1620
+ OseoRegExpClassEscape class_escape;
1621
+ } OseoRegExpClassAtom;
1622
+
1623
+ static bool parse_class_atom(
1624
+ OseoRegExpCompiler *compiler,
1625
+ OseoRegExpClassAtom *atom
1626
+ ) {
1627
+ atom->is_set = false;
1628
+ atom->negated = false;
1629
+ atom->code_point = 0u;
1630
+ atom->class_escape = OSEO_REGEXP_CLASS_DIGIT;
1631
+ if (unit_at(compiler, compiler->index) != '\\') {
1632
+ atom->code_point = read_code_point(compiler);
1633
+ return true;
1634
+ }
1635
+ OseoRegExpEscapeResult escape;
1636
+ if (!parse_escape(compiler, true, &escape)) return false;
1637
+ if (escape.kind == OSEO_REGEXP_ESCAPE_CLASS) {
1638
+ atom->is_set = true;
1639
+ atom->negated = escape.negated;
1640
+ atom->class_escape = escape.class_escape;
1641
+ return true;
1642
+ }
1643
+ if (escape.kind != OSEO_REGEXP_ESCAPE_CHARACTER) {
1644
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1645
+ return false;
1646
+ }
1647
+ atom->code_point = escape.code_point;
1648
+ return true;
1649
+ }
1650
+
1651
+ /*
1652
+ * Add one class escape's set to the class being built in `work[0]`,
1653
+ * using `work[1]` and `work[2]` as scratch.
1654
+ */
1655
+ static bool union_class_escape(
1656
+ OseoRegExpCompiler *compiler,
1657
+ const OseoRegExpClassAtom *atom
1658
+ ) {
1659
+ OseoRegExpBuffer *accumulator = &compiler->work[0];
1660
+ if (!class_escape_set(compiler, atom->class_escape, &compiler->work[1])) {
1661
+ return false;
1662
+ }
1663
+ if (atom->negated) {
1664
+ if (!compiler_allocate(
1665
+ compiler,
1666
+ set_complement(
1667
+ compiler->context,
1668
+ compiler->work[1].values,
1669
+ compiler->work[1].count,
1670
+ &compiler->work[2]
1671
+ )
1672
+ )) {
1673
+ return false;
1674
+ }
1675
+ buffer_swap(&compiler->work[1], &compiler->work[2]);
1676
+ }
1677
+ if (!compiler_allocate(
1678
+ compiler,
1679
+ set_union(
1680
+ compiler->context,
1681
+ accumulator->values,
1682
+ accumulator->count,
1683
+ compiler->work[1].values,
1684
+ compiler->work[1].count,
1685
+ &compiler->work[2]
1686
+ )
1687
+ )) {
1688
+ return false;
1689
+ }
1690
+ buffer_swap(accumulator, &compiler->work[2]);
1691
+ return true;
1692
+ }
1693
+
1694
+ /*
1695
+ * Compile one character class into an interned set.
1696
+ *
1697
+ * Closing comes before negation because `CharacterSetMatcher` inverts a
1698
+ * class after canonicalizing its members: `[^a]` under `i` rejects `A`,
1699
+ * while closing the complement instead would accept it.
1700
+ */
1701
+ static uint32_t parse_character_class(OseoRegExpCompiler *compiler) {
1702
+ OseoContext *context = compiler->context;
1703
+ compiler->index += 1u;
1704
+ bool negated = unit_at(compiler, compiler->index) == '^';
1705
+ if (negated) compiler->index += 1u;
1706
+ OseoRegExpBuffer *accumulator = &compiler->work[0];
1707
+ accumulator->count = 0u;
1708
+ compiler->class_ranges.count = 0u;
1709
+ while (!at_end(compiler) && unit_at(compiler, compiler->index) != ']') {
1710
+ OseoRegExpClassAtom first;
1711
+ if (!parse_class_atom(compiler, &first)) return 0u;
1712
+ if (first.is_set) {
1713
+ if (!union_class_escape(compiler, &first)) return 0u;
1714
+ /*
1715
+ * Annex B's CharacterRangeOrUnion with a class escape first:
1716
+ * the dash and the atom after it belong to this union, so
1717
+ * neither may start another range. Validation refused this
1718
+ * text under `u` and `v`.
1719
+ */
1720
+ if (unit_at(compiler, compiler->index) == '-' &&
1721
+ compiler->index + 1u < compiler->source->length &&
1722
+ unit_at(compiler, compiler->index + 1u) != ']') {
1723
+ compiler->index += 1u;
1724
+ OseoRegExpClassAtom second;
1725
+ if (!parse_class_atom(compiler, &second)) return 0u;
1726
+ if (second.is_set) {
1727
+ if (!union_class_escape(compiler, &second)) return 0u;
1728
+ second.code_point = '-';
1729
+ }
1730
+ if (!buffer_push(
1731
+ compiler->context,
1732
+ &compiler->class_ranges,
1733
+ '-'
1734
+ ) ||
1735
+ !buffer_push(
1736
+ compiler->context,
1737
+ &compiler->class_ranges,
1738
+ '-' + 1u
1739
+ ) ||
1740
+ !buffer_push(
1741
+ compiler->context,
1742
+ &compiler->class_ranges,
1743
+ second.code_point
1744
+ ) ||
1745
+ !buffer_push(
1746
+ compiler->context,
1747
+ &compiler->class_ranges,
1748
+ second.code_point + 1u
1749
+ )) {
1750
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
1751
+ return 0u;
1752
+ }
1753
+ }
1754
+ continue;
1755
+ }
1756
+ uint32_t start = first.code_point;
1757
+ uint32_t end = start;
1758
+ if (unit_at(compiler, compiler->index) == '-' &&
1759
+ compiler->index + 1u < compiler->source->length &&
1760
+ unit_at(compiler, compiler->index + 1u) != ']') {
1761
+ compiler->index += 1u;
1762
+ OseoRegExpClassAtom second;
1763
+ if (!parse_class_atom(compiler, &second)) return 0u;
1764
+ if (second.is_set && !compiler->unicode) {
1765
+ /*
1766
+ * Annex B's CharacterRangeOrUnion: the first atom, the
1767
+ * dash, and the class escape. A class escape first is
1768
+ * handled where it is parsed, with its own dash.
1769
+ */
1770
+ if (!buffer_push(
1771
+ compiler->context,
1772
+ &compiler->class_ranges,
1773
+ start
1774
+ ) ||
1775
+ !buffer_push(
1776
+ compiler->context,
1777
+ &compiler->class_ranges,
1778
+ start + 1u
1779
+ ) ||
1780
+ !buffer_push(
1781
+ compiler->context,
1782
+ &compiler->class_ranges,
1783
+ '-'
1784
+ ) ||
1785
+ !buffer_push(
1786
+ compiler->context,
1787
+ &compiler->class_ranges,
1788
+ '-' + 1u
1789
+ )) {
1790
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
1791
+ return 0u;
1792
+ }
1793
+ if (!union_class_escape(compiler, &second)) return 0u;
1794
+ continue;
1795
+ }
1796
+ if (second.is_set || second.code_point < start) {
1797
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1798
+ return 0u;
1799
+ }
1800
+ end = second.code_point;
1801
+ }
1802
+ if (!buffer_push(compiler->context, &compiler->class_ranges, start) ||
1803
+ !buffer_push(
1804
+ compiler->context,
1805
+ &compiler->class_ranges,
1806
+ end + 1u
1807
+ )) {
1808
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
1809
+ return 0u;
1810
+ }
1811
+ }
1812
+ if (at_end(compiler)) {
1813
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
1814
+ return 0u;
1815
+ }
1816
+ compiler->index += 1u;
1817
+ if (!compiler_allocate(
1818
+ compiler,
1819
+ set_of_ranges(
1820
+ context,
1821
+ compiler->class_ranges.values,
1822
+ compiler->class_ranges.count / 2u,
1823
+ &compiler->work[1]
1824
+ )
1825
+ )) {
1826
+ return 0u;
1827
+ }
1828
+ if (!compiler_allocate(
1829
+ compiler,
1830
+ set_union(
1831
+ context,
1832
+ accumulator->values,
1833
+ accumulator->count,
1834
+ compiler->work[1].values,
1835
+ compiler->work[1].count,
1836
+ &compiler->work[2]
1837
+ )
1838
+ )) {
1839
+ return 0u;
1840
+ }
1841
+ if (!close_set(
1842
+ compiler,
1843
+ compiler->work[2].values,
1844
+ compiler->work[2].count,
1845
+ &compiler->work[3]
1846
+ )) {
1847
+ return 0u;
1848
+ }
1849
+ if (!negated) {
1850
+ return intern_consuming_set(compiler, &compiler->work[3]);
1851
+ }
1852
+ if (!compiler_allocate(
1853
+ compiler,
1854
+ set_complement(
1855
+ context,
1856
+ compiler->work[3].values,
1857
+ compiler->work[3].count,
1858
+ &compiler->work[4]
1859
+ )
1860
+ )) {
1861
+ return 0u;
1862
+ }
1863
+ return intern_consuming_set(compiler, &compiler->work[4]);
1864
+ }
1865
+
1866
+ /* The set one single-character atom matches, already closed. */
1867
+ static uint32_t single_character_set(
1868
+ OseoRegExpCompiler *compiler,
1869
+ uint32_t code_point
1870
+ ) {
1871
+ compiler->work[0].count = 0u;
1872
+ if (!compiler_allocate(
1873
+ compiler,
1874
+ buffer_push(compiler->context, &compiler->work[0], code_point) &&
1875
+ buffer_push(
1876
+ compiler->context,
1877
+ &compiler->work[0],
1878
+ code_point + 1u
1879
+ )
1880
+ )) {
1881
+ return 0u;
1882
+ }
1883
+ if (!close_set(
1884
+ compiler,
1885
+ compiler->work[0].values,
1886
+ compiler->work[0].count,
1887
+ &compiler->work[1]
1888
+ )) {
1889
+ return 0u;
1890
+ }
1891
+ return intern_consuming_set(compiler, &compiler->work[1]);
1892
+ }
1893
+
1894
+ static uint32_t dot_set(OseoRegExpCompiler *compiler) {
1895
+ compiler->work[0].count = 0u;
1896
+ if (compiler->dot_all) {
1897
+ if (!compiler_allocate(
1898
+ compiler,
1899
+ buffer_push(compiler->context, &compiler->work[0], 0u) &&
1900
+ buffer_push(
1901
+ compiler->context,
1902
+ &compiler->work[0],
1903
+ OSEO_REGEXP_CODE_POINT_LIMIT
1904
+ )
1905
+ )) {
1906
+ return 0u;
1907
+ }
1908
+ } else if (!compiler_allocate(
1909
+ compiler,
1910
+ set_complement(
1911
+ compiler->context,
1912
+ regexp_line_terminators,
1913
+ OSEO_REGEXP_SET_COUNT(regexp_line_terminators),
1914
+ &compiler->work[0]
1915
+ )
1916
+ )) {
1917
+ return 0u;
1918
+ }
1919
+ if (!close_set(
1920
+ compiler,
1921
+ compiler->work[0].values,
1922
+ compiler->work[0].count,
1923
+ &compiler->work[1]
1924
+ )) {
1925
+ return 0u;
1926
+ }
1927
+ return intern_consuming_set(compiler, &compiler->work[1]);
1928
+ }
1929
+
1930
+ static uint32_t class_escape_atom_set(
1931
+ OseoRegExpCompiler *compiler,
1932
+ OseoRegExpClassEscape kind,
1933
+ bool negated
1934
+ ) {
1935
+ if (!class_escape_set(compiler, kind, &compiler->work[0])) return 0u;
1936
+ if (negated) {
1937
+ if (!compiler_allocate(
1938
+ compiler,
1939
+ set_complement(
1940
+ compiler->context,
1941
+ compiler->work[0].values,
1942
+ compiler->work[0].count,
1943
+ &compiler->work[1]
1944
+ )
1945
+ )) {
1946
+ return 0u;
1947
+ }
1948
+ buffer_swap(&compiler->work[0], &compiler->work[1]);
1949
+ }
1950
+ if (!close_set(
1951
+ compiler,
1952
+ compiler->work[0].values,
1953
+ compiler->work[0].count,
1954
+ &compiler->work[1]
1955
+ )) {
1956
+ return 0u;
1957
+ }
1958
+ return intern_consuming_set(compiler, &compiler->work[1]);
1959
+ }
1960
+
1961
+ static uint32_t word_boundary_set(OseoRegExpCompiler *compiler) {
1962
+ size_t mode = compiler->ignore_case ? 1u : 0u;
1963
+ if (compiler->word_sets_ready[mode]) {
1964
+ return (uint32_t)compiler->word_sets[mode];
1965
+ }
1966
+ if (!class_escape_set(
1967
+ compiler,
1968
+ OSEO_REGEXP_CLASS_WORD,
1969
+ &compiler->work[0]
1970
+ )) {
1971
+ return 0u;
1972
+ }
1973
+ compiler->word_sets[mode] = intern_set(
1974
+ compiler,
1975
+ compiler->work[0].values,
1976
+ compiler->work[0].count
1977
+ );
1978
+ compiler->word_sets_ready[mode] = compiler_ok(compiler);
1979
+ return (uint32_t)compiler->word_sets[mode];
1980
+ }
1981
+
1982
+ static void parse_disjunction(OseoRegExpCompiler *compiler, bool backward);
1983
+
1984
+ static void parse_group(OseoRegExpCompiler *compiler, bool backward) {
1985
+ compiler->index += 1u;
1986
+ bool capturing = true;
1987
+ bool modifier_group = false;
1988
+ bool previous_ignore_case = compiler->ignore_case;
1989
+ bool previous_multiline = compiler->multiline;
1990
+ bool previous_dot_all = compiler->dot_all;
1991
+ if (unit_at(compiler, compiler->index) == '?') {
1992
+ uint16_t kind = unit_at(compiler, compiler->index + 1u);
1993
+ if (kind == ':') {
1994
+ compiler->index += 2u;
1995
+ capturing = false;
1996
+ } else if (kind == '<') {
1997
+ compiler->index += 2u;
1998
+ while (!at_end(compiler) &&
1999
+ unit_at(compiler, compiler->index) != '>') {
2000
+ compiler->index += 1u;
2001
+ }
2002
+ compiler->index += 1u;
2003
+ } else if (kind == 'i' || kind == 'm' || kind == 's' || kind == '-') {
2004
+ uint16_t add = 0u;
2005
+ uint16_t remove = 0u;
2006
+ bool removing = false;
2007
+ compiler->index += 1u;
2008
+ while (!at_end(compiler) &&
2009
+ unit_at(compiler, compiler->index) != ':') {
2010
+ uint16_t flag = unit_at(compiler, compiler->index);
2011
+ if (flag == '-') {
2012
+ removing = true;
2013
+ compiler->index += 1u;
2014
+ continue;
2015
+ }
2016
+ uint16_t bit = flag == 'i'
2017
+ ? OSEO_REGEXP_FLAG_I
2018
+ : flag == 'm'
2019
+ ? OSEO_REGEXP_FLAG_M
2020
+ : OSEO_REGEXP_FLAG_S;
2021
+ if (removing) remove = (uint16_t)(remove | bit);
2022
+ else add = (uint16_t)(add | bit);
2023
+ compiler->index += 1u;
2024
+ }
2025
+ compiler->index += 1u;
2026
+ compiler->ignore_case = (add & OSEO_REGEXP_FLAG_I) != 0u ||
2027
+ (compiler->ignore_case &&
2028
+ (remove & OSEO_REGEXP_FLAG_I) == 0u);
2029
+ compiler->multiline = (add & OSEO_REGEXP_FLAG_M) != 0u ||
2030
+ (compiler->multiline &&
2031
+ (remove & OSEO_REGEXP_FLAG_M) == 0u);
2032
+ compiler->dot_all = (add & OSEO_REGEXP_FLAG_S) != 0u ||
2033
+ (compiler->dot_all &&
2034
+ (remove & OSEO_REGEXP_FLAG_S) == 0u);
2035
+ capturing = false;
2036
+ modifier_group = true;
2037
+ } else {
2038
+ compiler_fail(compiler, OSEO_REGEXP_UNSUPPORTED);
2039
+ return;
2040
+ }
2041
+ }
2042
+ size_t capture = 0u;
2043
+ if (capturing) {
2044
+ capture = compiler->next_capture;
2045
+ compiler->next_capture += 1u;
2046
+ if (capture > compiler->capture_count) {
2047
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
2048
+ return;
2049
+ }
2050
+ emit(
2051
+ compiler,
2052
+ OSEO_REGEXP_OP_SAVE,
2053
+ 0u,
2054
+ (uint32_t)(backward ? 2u * capture + 1u : 2u * capture),
2055
+ 0u,
2056
+ 0u,
2057
+ 0u
2058
+ );
2059
+ }
2060
+ parse_disjunction(compiler, backward);
2061
+ if (modifier_group) {
2062
+ compiler->ignore_case = previous_ignore_case;
2063
+ compiler->multiline = previous_multiline;
2064
+ compiler->dot_all = previous_dot_all;
2065
+ }
2066
+ if (!compiler_ok(compiler)) return;
2067
+ if (unit_at(compiler, compiler->index) != ')') {
2068
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
2069
+ return;
2070
+ }
2071
+ compiler->index += 1u;
2072
+ if (capturing) {
2073
+ emit(
2074
+ compiler,
2075
+ OSEO_REGEXP_OP_SAVE,
2076
+ 0u,
2077
+ (uint32_t)(backward ? 2u * capture : 2u * capture + 1u),
2078
+ 0u,
2079
+ 0u,
2080
+ 0u
2081
+ );
2082
+ }
2083
+ }
2084
+
2085
+ static void parse_atom(OseoRegExpCompiler *compiler, bool backward) {
2086
+ uint8_t direction = backward
2087
+ ? OSEO_REGEXP_INSTRUCTION_BACKWARD
2088
+ : (uint8_t)0u;
2089
+ uint16_t unit = unit_at(compiler, compiler->index);
2090
+ if (unit == '(') {
2091
+ parse_group(compiler, backward);
2092
+ return;
2093
+ }
2094
+ uint32_t set = 0u;
2095
+ if (unit == '.') {
2096
+ compiler->index += 1u;
2097
+ set = dot_set(compiler);
2098
+ } else if (unit == '[') {
2099
+ set = parse_character_class(compiler);
2100
+ } else if (unit == '\\') {
2101
+ OseoRegExpEscapeResult escape;
2102
+ if (!parse_escape(compiler, false, &escape)) return;
2103
+ if (escape.kind == OSEO_REGEXP_ESCAPE_BACKREFERENCE) {
2104
+ uint8_t modifiers = direction;
2105
+ if (compiler->ignore_case) {
2106
+ modifiers = (uint8_t)(
2107
+ modifiers | OSEO_REGEXP_INSTRUCTION_IGNORE_CASE
2108
+ );
2109
+ compiler->has_ignore_case_backreference = true;
2110
+ }
2111
+ for (size_t capture = escape.capture;
2112
+ capture <= compiler->capture_count;
2113
+ capture += 1u) {
2114
+ const OseoRegExpCapture *record =
2115
+ &compiler->captures[capture - 1u];
2116
+ const OseoRegExpCapture *first =
2117
+ &compiler->captures[escape.capture - 1u];
2118
+ if (escape.named &&
2119
+ (!record->named ||
2120
+ record->name_length != first->name_length ||
2121
+ memcmp(
2122
+ &compiler->names.values[record->name_offset],
2123
+ &compiler->names.values[first->name_offset],
2124
+ first->name_length * sizeof(uint16_t)
2125
+ ) != 0)) {
2126
+ continue;
2127
+ }
2128
+ emit(
2129
+ compiler,
2130
+ OSEO_REGEXP_OP_BACKREFERENCE,
2131
+ modifiers,
2132
+ (uint32_t)(2u * capture),
2133
+ 0u,
2134
+ 0u,
2135
+ 0u
2136
+ );
2137
+ if (!escape.named) break;
2138
+ }
2139
+ return;
2140
+ }
2141
+ if (escape.kind == OSEO_REGEXP_ESCAPE_CLASS) {
2142
+ set = class_escape_atom_set(
2143
+ compiler,
2144
+ escape.class_escape,
2145
+ escape.negated
2146
+ );
2147
+ } else if (escape.kind == OSEO_REGEXP_ESCAPE_CHARACTER) {
2148
+ set = single_character_set(compiler, escape.code_point);
2149
+ } else {
2150
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
2151
+ return;
2152
+ }
2153
+ } else {
2154
+ set = single_character_set(compiler, read_code_point(compiler));
2155
+ }
2156
+ if (!compiler_ok(compiler)) return;
2157
+ emit(compiler, OSEO_REGEXP_OP_CONSUME, direction, set, 0u, 0u, 0u);
2158
+ }
2159
+
2160
+ static void parse_lookaround(
2161
+ OseoRegExpCompiler *compiler,
2162
+ bool behind,
2163
+ bool negated
2164
+ ) {
2165
+ compiler->index += behind ? 4u : 3u;
2166
+ size_t frame = allocate_registers(compiler, 1u);
2167
+ size_t body = new_label(compiler);
2168
+ size_t exit_label = new_label(compiler);
2169
+ if (!compiler_ok(compiler)) return;
2170
+ emit(
2171
+ compiler,
2172
+ OSEO_REGEXP_OP_LOOK_START,
2173
+ negated ? OSEO_REGEXP_INSTRUCTION_NEGATED : (uint8_t)0u,
2174
+ (uint32_t)frame,
2175
+ (uint32_t)body,
2176
+ (uint32_t)(negated ? exit_label : compiler->fail_label),
2177
+ 0u
2178
+ );
2179
+ place_label(compiler, body);
2180
+ parse_disjunction(compiler, behind);
2181
+ if (!compiler_ok(compiler)) return;
2182
+ if (unit_at(compiler, compiler->index) != ')') {
2183
+ compiler_fail(compiler, OSEO_REGEXP_INVALID);
2184
+ return;
2185
+ }
2186
+ compiler->index += 1u;
2187
+ emit(
2188
+ compiler,
2189
+ OSEO_REGEXP_OP_LOOK_END,
2190
+ negated ? OSEO_REGEXP_INSTRUCTION_NEGATED : (uint8_t)0u,
2191
+ (uint32_t)frame,
2192
+ (uint32_t)exit_label,
2193
+ 0u,
2194
+ 0u
2195
+ );
2196
+ place_label(compiler, exit_label);
2197
+ }
2198
+
2199
+ /*
2200
+ * Read the quantifier that follows an atom, if any.
2201
+ *
2202
+ * Validation has already checked the bounds of every `{` that spells a
2203
+ * quantifier. Any other `{` is Annex B's pattern character, which this
2204
+ * leaves for the next term, so a brace without a lower bound or without
2205
+ * its closing brace is no quantifier here.
2206
+ */
2207
+ static bool parse_quantifier(
2208
+ OseoRegExpCompiler *compiler,
2209
+ uint64_t *minimum,
2210
+ uint64_t *maximum,
2211
+ bool *greedy
2212
+ ) {
2213
+ uint16_t unit = unit_at(compiler, compiler->index);
2214
+ if (at_end(compiler)) return false;
2215
+ if (unit == '*') {
2216
+ *minimum = 0u;
2217
+ *maximum = UINT64_MAX;
2218
+ } else if (unit == '+') {
2219
+ *minimum = 1u;
2220
+ *maximum = UINT64_MAX;
2221
+ } else if (unit == '?') {
2222
+ *minimum = 0u;
2223
+ *maximum = 1u;
2224
+ } else if (unit == '{') {
2225
+ size_t cursor = compiler->index + 1u;
2226
+ uint64_t low = 0u;
2227
+ while (cursor < compiler->source->length &&
2228
+ ascii_digit(compiler->source->units[cursor])) {
2229
+ low = low * 10u +
2230
+ (uint64_t)(compiler->source->units[cursor] - '0');
2231
+ cursor += 1u;
2232
+ }
2233
+ if (cursor == compiler->index + 1u) return false;
2234
+ uint64_t high = low;
2235
+ if (cursor < compiler->source->length &&
2236
+ compiler->source->units[cursor] == ',') {
2237
+ cursor += 1u;
2238
+ if (cursor < compiler->source->length &&
2239
+ compiler->source->units[cursor] == '}') {
2240
+ high = UINT64_MAX;
2241
+ } else {
2242
+ high = 0u;
2243
+ while (cursor < compiler->source->length &&
2244
+ ascii_digit(compiler->source->units[cursor])) {
2245
+ high = high * 10u +
2246
+ (uint64_t)(compiler->source->units[cursor] - '0');
2247
+ cursor += 1u;
2248
+ }
2249
+ }
2250
+ }
2251
+ if (cursor >= compiler->source->length ||
2252
+ compiler->source->units[cursor] != '}') {
2253
+ return false;
2254
+ }
2255
+ *minimum = low;
2256
+ *maximum = high;
2257
+ compiler->index = cursor;
2258
+ } else {
2259
+ return false;
2260
+ }
2261
+ compiler->index += 1u;
2262
+ *greedy = true;
2263
+ if (!at_end(compiler) && unit_at(compiler, compiler->index) == '?') {
2264
+ *greedy = false;
2265
+ compiler->index += 1u;
2266
+ }
2267
+ return true;
2268
+ }
2269
+
2270
+ static void build_quantified(
2271
+ OseoRegExpCompiler *compiler,
2272
+ size_t atom_start,
2273
+ size_t capture_before,
2274
+ size_t capture_after,
2275
+ uint64_t minimum,
2276
+ uint64_t maximum,
2277
+ bool greedy
2278
+ ) {
2279
+ size_t counter = allocate_registers(compiler, 2u);
2280
+ size_t position = counter + 1u;
2281
+ uint32_t clear_from = 0u;
2282
+ uint32_t clear_to = 0u;
2283
+ if (capture_after > capture_before) {
2284
+ clear_from = (uint32_t)(2u * capture_before);
2285
+ clear_to = (uint32_t)(2u * (capture_after - 1u) + 2u);
2286
+ }
2287
+ size_t repeat = allocate_repeat(
2288
+ compiler,
2289
+ minimum,
2290
+ maximum,
2291
+ clear_from,
2292
+ clear_to
2293
+ );
2294
+ if (!compiler_ok(compiler)) return;
2295
+ if (!emit_insert(compiler, atom_start, 3u)) return;
2296
+ size_t head = new_label(compiler);
2297
+ size_t enter = new_label(compiler);
2298
+ size_t body = new_label(compiler);
2299
+ size_t exit_label = new_label(compiler);
2300
+ if (!compiler_ok(compiler)) return;
2301
+ place_label_at(compiler, head, atom_start + 1u);
2302
+ place_label_at(compiler, enter, atom_start + 2u);
2303
+ place_label_at(compiler, body, atom_start + 3u);
2304
+ OseoRegExpInstruction *prologue = &compiler->instructions[atom_start];
2305
+ prologue[0].opcode = OSEO_REGEXP_OP_REPEAT_INIT;
2306
+ prologue[0].modifiers = 0u;
2307
+ prologue[0].operands[0] = (uint32_t)counter;
2308
+ prologue[1].opcode = OSEO_REGEXP_OP_REPEAT;
2309
+ prologue[1].modifiers = greedy
2310
+ ? OSEO_REGEXP_INSTRUCTION_GREEDY
2311
+ : (uint8_t)0u;
2312
+ prologue[1].operands[0] = (uint32_t)counter;
2313
+ prologue[1].operands[1] = (uint32_t)enter;
2314
+ prologue[1].operands[2] = (uint32_t)exit_label;
2315
+ prologue[1].operands[3] = (uint32_t)repeat;
2316
+ prologue[2].opcode = OSEO_REGEXP_OP_REPEAT_ENTER;
2317
+ prologue[2].modifiers = 0u;
2318
+ prologue[2].operands[0] = (uint32_t)counter;
2319
+ prologue[2].operands[1] = (uint32_t)position;
2320
+ prologue[2].operands[2] = (uint32_t)body;
2321
+ prologue[2].operands[3] = (uint32_t)repeat;
2322
+ emit(
2323
+ compiler,
2324
+ OSEO_REGEXP_OP_REPEAT_END,
2325
+ 0u,
2326
+ (uint32_t)counter,
2327
+ (uint32_t)head,
2328
+ (uint32_t)position,
2329
+ (uint32_t)repeat
2330
+ );
2331
+ place_label(compiler, exit_label);
2332
+ }
2333
+
2334
+ static void parse_term(OseoRegExpCompiler *compiler, bool backward) {
2335
+ uint16_t unit = unit_at(compiler, compiler->index);
2336
+ if (unit == '^' || unit == '$') {
2337
+ compiler->index += 1u;
2338
+ uint8_t modifiers = unit == '$'
2339
+ ? OSEO_REGEXP_INSTRUCTION_END_EDGE
2340
+ : (uint8_t)0u;
2341
+ if (compiler->multiline) {
2342
+ modifiers = (uint8_t)(
2343
+ modifiers | OSEO_REGEXP_INSTRUCTION_MULTILINE
2344
+ );
2345
+ }
2346
+ emit(compiler, OSEO_REGEXP_OP_EDGE, modifiers, 0u, 0u, 0u, 0u);
2347
+ return;
2348
+ }
2349
+ if (unit == '\\') {
2350
+ uint16_t next = unit_at(compiler, compiler->index + 1u);
2351
+ if (next == 'b' || next == 'B') {
2352
+ compiler->index += 2u;
2353
+ uint32_t set = word_boundary_set(compiler);
2354
+ if (!compiler_ok(compiler)) return;
2355
+ emit(
2356
+ compiler,
2357
+ OSEO_REGEXP_OP_BOUNDARY,
2358
+ next == 'B' ? OSEO_REGEXP_INSTRUCTION_NEGATED : (uint8_t)0u,
2359
+ set,
2360
+ 0u,
2361
+ 0u,
2362
+ 0u
2363
+ );
2364
+ return;
2365
+ }
2366
+ }
2367
+ if (unit == '(' && unit_at(compiler, compiler->index + 1u) == '?') {
2368
+ uint16_t kind = unit_at(compiler, compiler->index + 2u);
2369
+ if ((kind == '=' || kind == '!') && compiler->unicode) {
2370
+ parse_lookaround(compiler, false, kind == '!');
2371
+ return;
2372
+ }
2373
+ if (kind == '=' || kind == '!') {
2374
+ /*
2375
+ * Annex B's QuantifiableAssertion repeats like an atom whose
2376
+ * matcher is the lookahead, captures inside it included.
2377
+ */
2378
+ size_t atom_start = compiler->instruction_count;
2379
+ size_t capture_before = compiler->next_capture;
2380
+ parse_lookaround(compiler, false, kind == '!');
2381
+ if (!compiler_ok(compiler)) return;
2382
+ uint64_t minimum = 0u;
2383
+ uint64_t maximum = 0u;
2384
+ bool greedy = true;
2385
+ if (!parse_quantifier(compiler, &minimum, &maximum, &greedy)) {
2386
+ return;
2387
+ }
2388
+ build_quantified(
2389
+ compiler,
2390
+ atom_start,
2391
+ capture_before,
2392
+ compiler->next_capture,
2393
+ minimum,
2394
+ maximum,
2395
+ greedy
2396
+ );
2397
+ return;
2398
+ }
2399
+ if (kind == '<') {
2400
+ uint16_t behind = unit_at(compiler, compiler->index + 3u);
2401
+ if (behind == '=' || behind == '!') {
2402
+ parse_lookaround(compiler, true, behind == '!');
2403
+ return;
2404
+ }
2405
+ }
2406
+ }
2407
+ size_t atom_start = compiler->instruction_count;
2408
+ size_t capture_before = compiler->next_capture;
2409
+ parse_atom(compiler, backward);
2410
+ if (!compiler_ok(compiler)) return;
2411
+ uint64_t minimum = 0u;
2412
+ uint64_t maximum = 0u;
2413
+ bool greedy = true;
2414
+ if (!parse_quantifier(compiler, &minimum, &maximum, &greedy)) return;
2415
+ build_quantified(
2416
+ compiler,
2417
+ atom_start,
2418
+ capture_before,
2419
+ compiler->next_capture,
2420
+ minimum,
2421
+ maximum,
2422
+ greedy
2423
+ );
2424
+ }
2425
+
2426
+ /*
2427
+ * Lay one backward alternative's terms out in reverse.
2428
+ *
2429
+ * A lookbehind body evaluates its rightmost term first, so the emitted
2430
+ * blocks are permuted once the alternative is complete. Every label
2431
+ * placed inside a block moves with it, and a label at a block boundary
2432
+ * belongs to the block that ends there, which is the position execution
2433
+ * continues from once that block has run.
2434
+ */
2435
+ static bool reverse_alternative(OseoRegExpCompiler *compiler, size_t base) {
2436
+ size_t block_count = compiler->blocks.count - base;
2437
+ if (block_count < 2u) return true;
2438
+ size_t alt_start = compiler->blocks.values[base];
2439
+ size_t alt_end = compiler->instruction_count;
2440
+ size_t span = alt_end - alt_start;
2441
+ if (span == 0u) return true;
2442
+ OseoRegExpInstruction *scratch = oseo_internal_allocate_work_bytes(
2443
+ compiler->context,
2444
+ span * sizeof(OseoRegExpInstruction)
2445
+ );
2446
+ if (scratch == NULL) return false;
2447
+ uint32_t *new_starts = oseo_internal_allocate_work_bytes(
2448
+ compiler->context,
2449
+ block_count * sizeof(uint32_t)
2450
+ );
2451
+ if (new_starts == NULL) {
2452
+ free(scratch);
2453
+ return false;
2454
+ }
2455
+ size_t cursor = alt_start;
2456
+ for (size_t offset = block_count; offset > 0u; offset -= 1u) {
2457
+ size_t block = offset - 1u;
2458
+ size_t start = compiler->blocks.values[base + block];
2459
+ size_t end = block + 1u < block_count
2460
+ ? compiler->blocks.values[base + block + 1u]
2461
+ : alt_end;
2462
+ new_starts[block] = (uint32_t)cursor;
2463
+ if (end > start) {
2464
+ memcpy(
2465
+ &scratch[cursor - alt_start],
2466
+ &compiler->instructions[start],
2467
+ (end - start) * sizeof(OseoRegExpInstruction)
2468
+ );
2469
+ }
2470
+ cursor += end - start;
2471
+ }
2472
+ memcpy(
2473
+ &compiler->instructions[alt_start],
2474
+ scratch,
2475
+ span * sizeof(OseoRegExpInstruction)
2476
+ );
2477
+ free(scratch);
2478
+ for (size_t label = 0u; label < compiler->label_count; label += 1u) {
2479
+ uint32_t address = compiler->addresses[label];
2480
+ /*
2481
+ * A label at the alternative's own start names its entry rather
2482
+ * than the first term, which the reversal leaves where it is:
2483
+ * no label is ever placed at the start of a term block.
2484
+ */
2485
+ if (address == UINT32_MAX || address <= alt_start ||
2486
+ address > alt_end) {
2487
+ continue;
2488
+ }
2489
+ size_t low = 0u;
2490
+ size_t high = block_count;
2491
+ while (low < high) {
2492
+ size_t middle = low + (high - low) / 2u;
2493
+ if (compiler->blocks.values[base + middle] < address) {
2494
+ low = middle + 1u;
2495
+ } else {
2496
+ high = middle;
2497
+ }
2498
+ }
2499
+ size_t block = low - 1u;
2500
+ uint32_t start = compiler->blocks.values[base + block];
2501
+ compiler->addresses[label] = new_starts[block] + (address - start);
2502
+ }
2503
+ free(new_starts);
2504
+ return true;
2505
+ }
2506
+
2507
+ static void parse_alternative(OseoRegExpCompiler *compiler, bool backward) {
2508
+ size_t base = compiler->blocks.count;
2509
+ while (compiler_ok(compiler) && !at_end(compiler)) {
2510
+ uint16_t unit = unit_at(compiler, compiler->index);
2511
+ if (unit == '|' || unit == ')') break;
2512
+ if (!compiler_allocate(
2513
+ compiler,
2514
+ buffer_push(
2515
+ compiler->context,
2516
+ &compiler->blocks,
2517
+ (uint32_t)compiler->instruction_count
2518
+ )
2519
+ )) {
2520
+ break;
2521
+ }
2522
+ parse_term(compiler, backward);
2523
+ }
2524
+ if (compiler_ok(compiler) && backward &&
2525
+ !reverse_alternative(compiler, base)) {
2526
+ compiler_fail(compiler, OSEO_REGEXP_ALLOCATION_FAILURE);
2527
+ }
2528
+ compiler->blocks.count = base;
2529
+ }
2530
+
2531
+ /*
2532
+ * Emit one disjunction.
2533
+ *
2534
+ * The label that names an alternative's entry is placed only once that
2535
+ * alternative's own prologue exists, because the fork an alternation
2536
+ * needs is itself written into a gap at that address. Placing it earlier
2537
+ * would leave it in front of the fork it is supposed to reach.
2538
+ */
2539
+ static void parse_disjunction(OseoRegExpCompiler *compiler, bool backward) {
2540
+ size_t end_label = new_label(compiler);
2541
+ if (!compiler_ok(compiler)) return;
2542
+ size_t pending = SIZE_MAX;
2543
+ for (;;) {
2544
+ size_t alternative_start = compiler->instruction_count;
2545
+ parse_alternative(compiler, backward);
2546
+ if (!compiler_ok(compiler)) return;
2547
+ bool more = !at_end(compiler) &&
2548
+ unit_at(compiler, compiler->index) == '|';
2549
+ if (more) {
2550
+ if (!emit_insert(compiler, alternative_start, 1u)) return;
2551
+ size_t preferred = new_label(compiler);
2552
+ size_t next = new_label(compiler);
2553
+ if (!compiler_ok(compiler)) return;
2554
+ place_label_at(compiler, preferred, alternative_start + 1u);
2555
+ OseoRegExpInstruction *fork =
2556
+ &compiler->instructions[alternative_start];
2557
+ fork->opcode = OSEO_REGEXP_OP_FORK;
2558
+ fork->modifiers = 0u;
2559
+ fork->operands[0] = (uint32_t)preferred;
2560
+ fork->operands[1] = (uint32_t)next;
2561
+ emit(
2562
+ compiler,
2563
+ OSEO_REGEXP_OP_JUMP,
2564
+ 0u,
2565
+ (uint32_t)end_label,
2566
+ 0u,
2567
+ 0u,
2568
+ 0u
2569
+ );
2570
+ if (!compiler_ok(compiler)) return;
2571
+ compiler->index += 1u;
2572
+ if (pending != SIZE_MAX) {
2573
+ place_label_at(compiler, pending, alternative_start);
2574
+ }
2575
+ pending = next;
2576
+ continue;
2577
+ }
2578
+ if (pending != SIZE_MAX) {
2579
+ place_label_at(compiler, pending, alternative_start);
2580
+ }
2581
+ break;
2582
+ }
2583
+ place_label(compiler, end_label);
2584
+ }
2585
+
2586
+ /* Rewrite every label reference into the address it was placed at. */
2587
+ static bool resolve_targets(OseoRegExpCompiler *compiler) {
2588
+ for (size_t index = 0u; index < compiler->instruction_count; index += 1u) {
2589
+ OseoRegExpInstruction *instruction = &compiler->instructions[index];
2590
+ size_t first = SIZE_MAX;
2591
+ size_t second = SIZE_MAX;
2592
+ switch (instruction->opcode) {
2593
+ case OSEO_REGEXP_OP_FORK:
2594
+ first = 0u;
2595
+ second = 1u;
2596
+ break;
2597
+ case OSEO_REGEXP_OP_JUMP:
2598
+ first = 0u;
2599
+ break;
2600
+ case OSEO_REGEXP_OP_REPEAT:
2601
+ first = 1u;
2602
+ second = 2u;
2603
+ break;
2604
+ case OSEO_REGEXP_OP_REPEAT_ENTER:
2605
+ first = 2u;
2606
+ break;
2607
+ case OSEO_REGEXP_OP_REPEAT_END:
2608
+ first = 1u;
2609
+ break;
2610
+ case OSEO_REGEXP_OP_LOOK_START:
2611
+ first = 1u;
2612
+ second = 2u;
2613
+ break;
2614
+ case OSEO_REGEXP_OP_LOOK_END:
2615
+ first = 1u;
2616
+ break;
2617
+ default:
2618
+ continue;
2619
+ }
2620
+ const size_t operands[2] = {first, second};
2621
+ for (size_t slot = 0u; slot < 2u; slot += 1u) {
2622
+ if (operands[slot] == SIZE_MAX) continue;
2623
+ uint32_t label = instruction->operands[operands[slot]];
2624
+ if (label >= compiler->label_count) return false;
2625
+ uint32_t address = compiler->addresses[label];
2626
+ if (address == UINT32_MAX) return false;
2627
+ instruction->operands[operands[slot]] = address;
2628
+ }
2629
+ }
2630
+ return true;
2631
+ }
2632
+
2633
+ void oseo_internal_regexp_program_release(OseoRegExpProgram *program) {
2634
+ if (program == NULL) return;
2635
+ free(program->instructions);
2636
+ free(program->set_boundaries);
2637
+ free(program->set_offsets);
2638
+ free(program->repeats);
2639
+ free(program->name_units);
2640
+ free(program->captures);
2641
+ free(program);
2642
+ }
2643
+
2644
+ static void compiler_release(OseoRegExpCompiler *compiler) {
2645
+ free(compiler->instructions);
2646
+ free(compiler->addresses);
2647
+ free(compiler->set_offsets);
2648
+ free(compiler->set_hashes);
2649
+ free(compiler->repeats);
2650
+ free(compiler->names.values);
2651
+ free(compiler->captures);
2652
+ buffer_release(&compiler->set_boundaries);
2653
+ buffer_release(&compiler->unknown_region);
2654
+ for (size_t index = 0u; index < 5u; index += 1u) {
2655
+ buffer_release(&compiler->work[index]);
2656
+ }
2657
+ buffer_release(&compiler->close_additions);
2658
+ buffer_release(&compiler->close_ranges);
2659
+ buffer_release(&compiler->class_ranges);
2660
+ buffer_release(&compiler->blocks);
2661
+ }
2662
+
2663
+ OseoRegExpProgram *oseo_internal_regexp_program_build(
2664
+ OseoContext *context,
2665
+ const OseoString *source,
2666
+ uint16_t flag_mask,
2667
+ size_t capture_count,
2668
+ OseoRegExpValidation *status
2669
+ ) {
2670
+ OseoRegExpCompiler compiler;
2671
+ memset(&compiler, 0, sizeof(compiler));
2672
+ compiler.context = context;
2673
+ compiler.source = source;
2674
+ compiler.flag_mask = flag_mask;
2675
+ compiler.unicode = (flag_mask &
2676
+ (OSEO_REGEXP_FLAG_U | OSEO_REGEXP_FLAG_V)) != 0u;
2677
+ compiler.ignore_case = (flag_mask & OSEO_REGEXP_FLAG_I) != 0u;
2678
+ compiler.multiline = (flag_mask & OSEO_REGEXP_FLAG_M) != 0u;
2679
+ compiler.dot_all = (flag_mask & OSEO_REGEXP_FLAG_S) != 0u;
2680
+ compiler.capture_count = capture_count;
2681
+ compiler.next_capture = 1u;
2682
+ compiler.status = OSEO_REGEXP_VALID;
2683
+ buffer_initialize(&compiler.set_boundaries);
2684
+ buffer_initialize(&compiler.unknown_region);
2685
+ for (size_t index = 0u; index < 5u; index += 1u) {
2686
+ buffer_initialize(&compiler.work[index]);
2687
+ }
2688
+ buffer_initialize(&compiler.close_additions);
2689
+ buffer_initialize(&compiler.close_ranges);
2690
+ buffer_initialize(&compiler.class_ranges);
2691
+ buffer_initialize(&compiler.blocks);
2692
+ if (capture_count > 0u) {
2693
+ if (capture_count > SIZE_MAX / sizeof(OseoRegExpCapture)) {
2694
+ compiler_release(&compiler);
2695
+ *status = OSEO_REGEXP_LIMIT;
2696
+ return NULL;
2697
+ }
2698
+ compiler.captures = oseo_internal_allocate_work_bytes(
2699
+ context,
2700
+ capture_count * sizeof(OseoRegExpCapture)
2701
+ );
2702
+ if (compiler.captures != NULL) {
2703
+ memset(
2704
+ compiler.captures,
2705
+ 0,
2706
+ capture_count * sizeof(OseoRegExpCapture)
2707
+ );
2708
+ }
2709
+ if (compiler.captures == NULL) {
2710
+ compiler_release(&compiler);
2711
+ *status = OSEO_REGEXP_ALLOCATION_FAILURE;
2712
+ return NULL;
2713
+ }
2714
+ }
2715
+ if (!collect_captures(&compiler)) {
2716
+ compiler_release(&compiler);
2717
+ *status = OSEO_REGEXP_ALLOCATION_FAILURE;
2718
+ return NULL;
2719
+ }
2720
+ if ((compiler.ignore_case || pattern_may_enable_ignore_case(source)) &&
2721
+ !build_unknown_region(&compiler)) {
2722
+ compiler_release(&compiler);
2723
+ *status = OSEO_REGEXP_ALLOCATION_FAILURE;
2724
+ return NULL;
2725
+ }
2726
+ compiler.registers = 2u * (capture_count + 1u);
2727
+ if (compiler.registers > OSEO_REGEXP_REGISTER_LIMIT) {
2728
+ compiler_release(&compiler);
2729
+ *status = OSEO_REGEXP_LIMIT;
2730
+ return NULL;
2731
+ }
2732
+ compiler.fail_label = new_label(&compiler);
2733
+ emit(&compiler, OSEO_REGEXP_OP_SAVE, 0u, 0u, 0u, 0u, 0u);
2734
+ parse_disjunction(&compiler, false);
2735
+ if (compiler_ok(&compiler) && !at_end(&compiler)) {
2736
+ compiler_fail(&compiler, OSEO_REGEXP_INVALID);
2737
+ }
2738
+ emit(&compiler, OSEO_REGEXP_OP_SAVE, 0u, 1u, 0u, 0u, 0u);
2739
+ emit(&compiler, OSEO_REGEXP_OP_ACCEPT, 0u, 0u, 0u, 0u, 0u);
2740
+ place_label(&compiler, compiler.fail_label);
2741
+ emit(&compiler, OSEO_REGEXP_OP_FAIL, 0u, 0u, 0u, 0u, 0u);
2742
+ /*
2743
+ * Only a backreference compares two input characters at run time.
2744
+ * Every other ignore-case decision was folded into a set while the
2745
+ * program was built, so this is the one place that needs case data
2746
+ * for a character the pattern never names, and a program whose sets
2747
+ * can reach a class this component has no data for keeps the
2748
+ * boundary rather than answering from an incomplete table.
2749
+ */
2750
+ if (compiler_ok(&compiler) && compiler.has_ignore_case_backreference &&
2751
+ compiler.sets_reach_unknown_case) {
2752
+ compiler_fail(&compiler, OSEO_REGEXP_UNSUPPORTED);
2753
+ }
2754
+ if (compiler_ok(&compiler) && !resolve_targets(&compiler)) {
2755
+ compiler_fail(&compiler, OSEO_REGEXP_INVALID);
2756
+ }
2757
+ if (!compiler_ok(&compiler)) {
2758
+ OseoRegExpValidation failure = compiler.status;
2759
+ compiler_release(&compiler);
2760
+ *status = failure;
2761
+ return NULL;
2762
+ }
2763
+ OseoRegExpProgram *program = oseo_internal_allocate_work_bytes(
2764
+ context,
2765
+ sizeof(OseoRegExpProgram)
2766
+ );
2767
+ if (program != NULL) memset(program, 0, sizeof(OseoRegExpProgram));
2768
+ if (program == NULL) {
2769
+ compiler_release(&compiler);
2770
+ *status = OSEO_REGEXP_ALLOCATION_FAILURE;
2771
+ return NULL;
2772
+ }
2773
+ program->instructions = compiler.instructions;
2774
+ program->instruction_count = compiler.instruction_count;
2775
+ program->set_boundaries = compiler.set_boundaries.values;
2776
+ program->set_offsets = compiler.set_offsets;
2777
+ program->set_count = compiler.set_count;
2778
+ program->repeats = compiler.repeats;
2779
+ program->repeat_count = compiler.repeat_count;
2780
+ program->name_units = compiler.names.values;
2781
+ program->name_unit_count = compiler.names.count;
2782
+ program->captures = compiler.captures;
2783
+ program->capture_count = capture_count;
2784
+ program->registers = compiler.registers;
2785
+ program->unicode_mode = compiler.unicode;
2786
+ program->ignore_case = compiler.ignore_case;
2787
+ program->has_group_names = false;
2788
+ for (size_t index = 0u; index < capture_count; index += 1u) {
2789
+ if (compiler.captures[index].named) program->has_group_names = true;
2790
+ }
2791
+ compiler.instructions = NULL;
2792
+ compiler.set_boundaries.values = NULL;
2793
+ compiler.set_boundaries.count = 0u;
2794
+ compiler.set_boundaries.capacity = 0u;
2795
+ compiler.set_offsets = NULL;
2796
+ compiler.repeats = NULL;
2797
+ compiler.names.values = NULL;
2798
+ compiler.captures = NULL;
2799
+ compiler_release(&compiler);
2800
+ *status = OSEO_REGEXP_VALID;
2801
+ return program;
2802
+ }
2803
+
2804
+ /*
2805
+ * The mutable state of one attempt.
2806
+ *
2807
+ * Every register write is recorded on a trail, so one backtrack entry
2808
+ * restores the whole state by truncating the trail to a recorded height
2809
+ * rather than by copying the registers.
2810
+ */
2811
+ typedef struct {
2812
+ OseoContext *context;
2813
+ const OseoRegExpProgram *program;
2814
+ const OseoString *subject;
2815
+ int64_t *registers;
2816
+ uint32_t *stack_program;
2817
+ int64_t *stack_index;
2818
+ uint32_t *stack_trail;
2819
+ size_t stack_count;
2820
+ size_t stack_capacity;
2821
+ uint32_t *trail_register;
2822
+ int64_t *trail_value;
2823
+ size_t trail_count;
2824
+ size_t trail_capacity;
2825
+ int64_t index;
2826
+ uint64_t steps;
2827
+ uint64_t step_limit;
2828
+ OseoRegExpExecution outcome;
2829
+ } OseoRegExpMachine;
2830
+
2831
+ static bool program_set_has(
2832
+ const OseoRegExpProgram *program,
2833
+ uint32_t set,
2834
+ uint32_t code_point
2835
+ ) {
2836
+ uint32_t start = program->set_offsets[set];
2837
+ uint32_t end = program->set_offsets[set + 1u];
2838
+ return set_has(
2839
+ &program->set_boundaries[start],
2840
+ (size_t)(end - start),
2841
+ code_point
2842
+ );
2843
+ }
2844
+
2845
+ /*
2846
+ * One character of the input read forward from `index`.
2847
+ *
2848
+ * A pattern with `u` or `v` matches a list of code points and every
2849
+ * other pattern matches a list of UTF-16 code units, which is the only
2850
+ * place the flag set changes what one position advances by. A lone
2851
+ * surrogate is one character in both readings.
2852
+ */
2853
+ static uint32_t character_at(
2854
+ const OseoString *subject,
2855
+ size_t index,
2856
+ bool unicode_mode
2857
+ ) {
2858
+ uint16_t unit = subject->units[index];
2859
+ if (!unicode_mode || unit < 0xd800u || unit > 0xdbffu) return unit;
2860
+ if (index + 1u >= subject->length) return unit;
2861
+ uint16_t trail = subject->units[index + 1u];
2862
+ if (trail < 0xdc00u || trail > 0xdfffu) return unit;
2863
+ return 0x10000u + (((uint32_t)unit - 0xd800u) << 10u) +
2864
+ ((uint32_t)trail - 0xdc00u);
2865
+ }
2866
+
2867
+ static size_t width_at(
2868
+ const OseoString *subject,
2869
+ size_t index,
2870
+ bool unicode_mode
2871
+ ) {
2872
+ if (index >= subject->length) return 1u;
2873
+ return character_at(subject, index, unicode_mode) > 0xffffu ? 2u : 1u;
2874
+ }
2875
+
2876
+ static size_t width_before(
2877
+ const OseoString *subject,
2878
+ size_t index,
2879
+ bool unicode_mode
2880
+ ) {
2881
+ if (!unicode_mode || index < 2u) return 1u;
2882
+ uint16_t trail = subject->units[index - 1u];
2883
+ uint16_t lead = subject->units[index - 2u];
2884
+ bool paired = trail >= 0xdc00u && trail <= 0xdfffu &&
2885
+ lead >= 0xd800u && lead <= 0xdbffu;
2886
+ return paired ? 2u : 1u;
2887
+ }
2888
+
2889
+ /* The character ending at `index`, or -1 at the start of the input. */
2890
+ static int64_t character_before(
2891
+ const OseoString *subject,
2892
+ size_t index,
2893
+ bool unicode_mode
2894
+ ) {
2895
+ if (index == 0u) return -1;
2896
+ size_t width = width_before(subject, index, unicode_mode);
2897
+ return (int64_t)character_at(subject, index - width, unicode_mode);
2898
+ }
2899
+
2900
+ /*
2901
+ * The position one attempt actually starts at.
2902
+ *
2903
+ * Under `u` or `v` both code units of a surrogate pair belong to the one
2904
+ * character they encode, so a start index that splits a pair names that
2905
+ * character rather than its trailing code unit.
2906
+ */
2907
+ static size_t aligned_start(
2908
+ const OseoString *subject,
2909
+ size_t index,
2910
+ bool unicode_mode
2911
+ ) {
2912
+ if (!unicode_mode || index == 0u || index >= subject->length) {
2913
+ return index;
2914
+ }
2915
+ uint16_t trail = subject->units[index];
2916
+ uint16_t lead = subject->units[index - 1u];
2917
+ bool paired = trail >= 0xdc00u && trail <= 0xdfffu &&
2918
+ lead >= 0xd800u && lead <= 0xdbffu;
2919
+ return paired ? index - 1u : index;
2920
+ }
2921
+
2922
+ static bool is_line_terminator(int64_t character) {
2923
+ return character == 0x0a || character == 0x0d || character == 0x2028 ||
2924
+ character == 0x2029;
2925
+ }
2926
+
2927
+ /*
2928
+ * The canonical form of one input character under `i`.
2929
+ *
2930
+ * An ahead-of-time literal descriptor carries the complete table its
2931
+ * build computed from the pinned Unicode data, so the search decides
2932
+ * every character exactly. Without that table the runtime answers from
2933
+ * the rule it links, and the program build refuses a pattern whose sets
2934
+ * can reach a character whose equivalence class that rule cannot decide,
2935
+ * so every character a capture can hold is covered either way.
2936
+ */
2937
+ static uint32_t canonicalize(
2938
+ const OseoRegExpProgram *program,
2939
+ uint32_t character
2940
+ ) {
2941
+ if (program->canonical_count > 0u) {
2942
+ size_t low = 0u;
2943
+ size_t high = program->canonical_count;
2944
+ while (low < high) {
2945
+ size_t middle = low + (high - low) / 2u;
2946
+ uint32_t candidate = program->canonical_characters[middle];
2947
+ if (candidate == character) {
2948
+ return program->canonical_values[middle];
2949
+ }
2950
+ if (candidate < character) low = middle + 1u;
2951
+ else high = middle;
2952
+ }
2953
+ return character;
2954
+ }
2955
+ if (character >= 0x61u && character <= 0x7au) return character - 32u;
2956
+ if (program->unicode_mode) {
2957
+ if (character == 0x17fu) return 0x53u;
2958
+ if (character == 0x212au) return 0x4bu;
2959
+ }
2960
+ return character;
2961
+ }
2962
+
2963
+ static bool machine_write(
2964
+ OseoRegExpMachine *machine,
2965
+ uint32_t reg,
2966
+ int64_t value
2967
+ ) {
2968
+ if (machine->trail_count >= OSEO_REGEXP_TRAIL_LIMIT) {
2969
+ machine->outcome = OSEO_REGEXP_EXECUTION_LIMIT;
2970
+ return false;
2971
+ }
2972
+ if (machine->trail_count == machine->trail_capacity) {
2973
+ size_t capacity = machine->trail_capacity == 0u
2974
+ ? 64u
2975
+ : machine->trail_capacity * 2u;
2976
+ uint32_t *registers = oseo_internal_reallocate_work_bytes(
2977
+ machine->context,
2978
+ machine->trail_register,
2979
+ capacity * sizeof(uint32_t)
2980
+ );
2981
+ if (registers == NULL) {
2982
+ machine->outcome = OSEO_REGEXP_EXECUTION_ALLOCATION;
2983
+ return false;
2984
+ }
2985
+ machine->trail_register = registers;
2986
+ int64_t *values = oseo_internal_reallocate_work_bytes(
2987
+ machine->context,
2988
+ machine->trail_value,
2989
+ capacity * sizeof(int64_t)
2990
+ );
2991
+ if (values == NULL) {
2992
+ machine->outcome = OSEO_REGEXP_EXECUTION_ALLOCATION;
2993
+ return false;
2994
+ }
2995
+ machine->trail_value = values;
2996
+ machine->trail_capacity = capacity;
2997
+ }
2998
+ machine->trail_register[machine->trail_count] = reg;
2999
+ machine->trail_value[machine->trail_count] = machine->registers[reg];
3000
+ machine->trail_count += 1u;
3001
+ machine->registers[reg] = value;
3002
+ return true;
3003
+ }
3004
+
3005
+ static void machine_undo(OseoRegExpMachine *machine, size_t height) {
3006
+ while (machine->trail_count > height) {
3007
+ machine->trail_count -= 1u;
3008
+ machine->registers[machine->trail_register[machine->trail_count]] =
3009
+ machine->trail_value[machine->trail_count];
3010
+ }
3011
+ }
3012
+
3013
+ static bool machine_push(OseoRegExpMachine *machine, uint32_t address) {
3014
+ if (machine->stack_count >= OSEO_REGEXP_BACKTRACK_LIMIT) {
3015
+ machine->outcome = OSEO_REGEXP_EXECUTION_LIMIT;
3016
+ return false;
3017
+ }
3018
+ if (machine->stack_count == machine->stack_capacity) {
3019
+ size_t capacity = machine->stack_capacity == 0u
3020
+ ? 64u
3021
+ : machine->stack_capacity * 2u;
3022
+ uint32_t *addresses = oseo_internal_reallocate_work_bytes(
3023
+ machine->context,
3024
+ machine->stack_program,
3025
+ capacity * sizeof(uint32_t)
3026
+ );
3027
+ if (addresses == NULL) {
3028
+ machine->outcome = OSEO_REGEXP_EXECUTION_ALLOCATION;
3029
+ return false;
3030
+ }
3031
+ machine->stack_program = addresses;
3032
+ int64_t *indexes = oseo_internal_reallocate_work_bytes(
3033
+ machine->context,
3034
+ machine->stack_index,
3035
+ capacity * sizeof(int64_t)
3036
+ );
3037
+ if (indexes == NULL) {
3038
+ machine->outcome = OSEO_REGEXP_EXECUTION_ALLOCATION;
3039
+ return false;
3040
+ }
3041
+ machine->stack_index = indexes;
3042
+ uint32_t *trails = oseo_internal_reallocate_work_bytes(
3043
+ machine->context,
3044
+ machine->stack_trail,
3045
+ capacity * sizeof(uint32_t)
3046
+ );
3047
+ if (trails == NULL) {
3048
+ machine->outcome = OSEO_REGEXP_EXECUTION_ALLOCATION;
3049
+ return false;
3050
+ }
3051
+ machine->stack_trail = trails;
3052
+ machine->stack_capacity = capacity;
3053
+ }
3054
+ machine->stack_program[machine->stack_count] = address;
3055
+ machine->stack_index[machine->stack_count] = machine->index;
3056
+ machine->stack_trail[machine->stack_count] =
3057
+ (uint32_t)machine->trail_count;
3058
+ machine->stack_count += 1u;
3059
+ return true;
3060
+ }
3061
+
3062
+ /* The address one backtrack entry resumes at, or -1 when none is left. */
3063
+ static int64_t machine_pop(OseoRegExpMachine *machine) {
3064
+ if (machine->stack_count == 0u) return -1;
3065
+ machine->stack_count -= 1u;
3066
+ machine->index = machine->stack_index[machine->stack_count];
3067
+ machine_undo(machine, machine->stack_trail[machine->stack_count]);
3068
+ return (int64_t)machine->stack_program[machine->stack_count];
3069
+ }
3070
+
3071
+ /* Match one backreference, or report that the path fails. */
3072
+ static bool match_backreference(
3073
+ OseoRegExpMachine *machine,
3074
+ uint32_t slot,
3075
+ bool backward,
3076
+ bool ignore_case
3077
+ ) {
3078
+ int64_t start = machine->registers[slot];
3079
+ int64_t end = machine->registers[slot + 1u];
3080
+ if (start == OSEO_REGEXP_UNSET || end == OSEO_REGEXP_UNSET) return true;
3081
+ int64_t length = end - start;
3082
+ int64_t target = backward ? machine->index - length : machine->index;
3083
+ if (target < 0 || target + length > (int64_t)machine->subject->length) {
3084
+ return false;
3085
+ }
3086
+ bool unicode_mode = machine->program->unicode_mode;
3087
+ int64_t source = start;
3088
+ int64_t compared = target;
3089
+ while (source < end) {
3090
+ size_t width = width_at(machine->subject, (size_t)source,
3091
+ unicode_mode);
3092
+ uint32_t left = character_at(
3093
+ machine->subject,
3094
+ (size_t)source,
3095
+ unicode_mode
3096
+ );
3097
+ uint32_t right = character_at(
3098
+ machine->subject,
3099
+ (size_t)compared,
3100
+ unicode_mode
3101
+ );
3102
+ if (ignore_case) {
3103
+ left = canonicalize(machine->program, left);
3104
+ right = canonicalize(machine->program, right);
3105
+ }
3106
+ if (left != right) return false;
3107
+ source += (int64_t)width;
3108
+ compared += (int64_t)width;
3109
+ }
3110
+ machine->index = backward ? target : target + length;
3111
+ return true;
3112
+ }
3113
+
3114
+ static bool matches_boundary(OseoRegExpMachine *machine, uint32_t set) {
3115
+ bool unicode_mode = machine->program->unicode_mode;
3116
+ int64_t before = character_before(
3117
+ machine->subject,
3118
+ (size_t)machine->index,
3119
+ unicode_mode
3120
+ );
3121
+ bool word_before = before >= 0 &&
3122
+ program_set_has(machine->program, set, (uint32_t)before);
3123
+ bool word_after = (size_t)machine->index < machine->subject->length &&
3124
+ program_set_has(
3125
+ machine->program,
3126
+ set,
3127
+ character_at(
3128
+ machine->subject,
3129
+ (size_t)machine->index,
3130
+ unicode_mode
3131
+ )
3132
+ );
3133
+ return word_before != word_after;
3134
+ }
3135
+
3136
+ static bool matches_edge(
3137
+ OseoRegExpMachine *machine,
3138
+ bool end_edge,
3139
+ bool multiline
3140
+ ) {
3141
+ bool unicode_mode = machine->program->unicode_mode;
3142
+ if (!end_edge) {
3143
+ if (machine->index == 0) return true;
3144
+ if (!multiline) return false;
3145
+ return is_line_terminator(
3146
+ character_before(
3147
+ machine->subject,
3148
+ (size_t)machine->index,
3149
+ unicode_mode
3150
+ )
3151
+ );
3152
+ }
3153
+ if ((size_t)machine->index == machine->subject->length) return true;
3154
+ if (!multiline) return false;
3155
+ return is_line_terminator(
3156
+ (int64_t)character_at(
3157
+ machine->subject,
3158
+ (size_t)machine->index,
3159
+ unicode_mode
3160
+ )
3161
+ );
3162
+ }
3163
+
3164
+ /* Run one anchored attempt at one already aligned position. */
3165
+ static OseoRegExpExecution match_at(
3166
+ OseoRegExpMachine *machine,
3167
+ size_t start_index
3168
+ ) {
3169
+ const OseoRegExpProgram *program = machine->program;
3170
+ for (size_t index = 0u; index < program->registers; index += 1u) {
3171
+ machine->registers[index] = index < 2u * (program->capture_count + 1u)
3172
+ ? OSEO_REGEXP_UNSET
3173
+ : 0;
3174
+ }
3175
+ machine->stack_count = 0u;
3176
+ machine->trail_count = 0u;
3177
+ machine->index = (int64_t)start_index;
3178
+ machine->outcome = OSEO_REGEXP_EXECUTION_UNMATCHED;
3179
+ bool unicode_mode = program->unicode_mode;
3180
+ size_t address = 0u;
3181
+ for (;;) {
3182
+ if (machine->steps >= machine->step_limit) {
3183
+ return OSEO_REGEXP_EXECUTION_LIMIT;
3184
+ }
3185
+ machine->steps += 1u;
3186
+ if (address >= program->instruction_count) {
3187
+ return OSEO_REGEXP_EXECUTION_UNMATCHED;
3188
+ }
3189
+ const OseoRegExpInstruction *instruction =
3190
+ &program->instructions[address];
3191
+ bool failed = false;
3192
+ switch (instruction->opcode) {
3193
+ case OSEO_REGEXP_OP_ACCEPT:
3194
+ return OSEO_REGEXP_EXECUTION_MATCHED;
3195
+ case OSEO_REGEXP_OP_CONSUME: {
3196
+ bool backward = (instruction->modifiers &
3197
+ OSEO_REGEXP_INSTRUCTION_BACKWARD) != 0u;
3198
+ size_t width = backward
3199
+ ? width_before(
3200
+ machine->subject,
3201
+ (size_t)machine->index,
3202
+ unicode_mode
3203
+ )
3204
+ : width_at(
3205
+ machine->subject,
3206
+ (size_t)machine->index,
3207
+ unicode_mode
3208
+ );
3209
+ int64_t at = backward
3210
+ ? machine->index - (int64_t)width
3211
+ : machine->index;
3212
+ int64_t next = backward
3213
+ ? at
3214
+ : machine->index + (int64_t)width;
3215
+ if (at < 0 || at >= (int64_t)machine->subject->length ||
3216
+ next < 0 ||
3217
+ next > (int64_t)machine->subject->length ||
3218
+ !program_set_has(
3219
+ program,
3220
+ instruction->operands[0],
3221
+ character_at(
3222
+ machine->subject,
3223
+ (size_t)at,
3224
+ unicode_mode
3225
+ )
3226
+ )) {
3227
+ failed = true;
3228
+ } else {
3229
+ machine->index = next;
3230
+ address += 1u;
3231
+ }
3232
+ break;
3233
+ }
3234
+ case OSEO_REGEXP_OP_EDGE:
3235
+ if (matches_edge(
3236
+ machine,
3237
+ (instruction->modifiers &
3238
+ OSEO_REGEXP_INSTRUCTION_END_EDGE) != 0u,
3239
+ (instruction->modifiers &
3240
+ OSEO_REGEXP_INSTRUCTION_MULTILINE) != 0u
3241
+ )) {
3242
+ address += 1u;
3243
+ } else {
3244
+ failed = true;
3245
+ }
3246
+ break;
3247
+ case OSEO_REGEXP_OP_BOUNDARY: {
3248
+ bool negated = (instruction->modifiers &
3249
+ OSEO_REGEXP_INSTRUCTION_NEGATED) != 0u;
3250
+ if (matches_boundary(machine, instruction->operands[0]) !=
3251
+ negated) {
3252
+ address += 1u;
3253
+ } else {
3254
+ failed = true;
3255
+ }
3256
+ break;
3257
+ }
3258
+ case OSEO_REGEXP_OP_SAVE:
3259
+ if (!machine_write(
3260
+ machine,
3261
+ instruction->operands[0],
3262
+ machine->index
3263
+ )) {
3264
+ return machine->outcome;
3265
+ }
3266
+ address += 1u;
3267
+ break;
3268
+ case OSEO_REGEXP_OP_FORK:
3269
+ if (!machine_push(machine, instruction->operands[1])) {
3270
+ return machine->outcome;
3271
+ }
3272
+ address = instruction->operands[0];
3273
+ break;
3274
+ case OSEO_REGEXP_OP_JUMP:
3275
+ address = instruction->operands[0];
3276
+ break;
3277
+ case OSEO_REGEXP_OP_REPEAT: {
3278
+ const OseoRegExpRepeat *repeat =
3279
+ &program->repeats[instruction->operands[3]];
3280
+ uint64_t done =
3281
+ (uint64_t)machine->registers[instruction->operands[0]];
3282
+ if (done < repeat->minimum) {
3283
+ address = instruction->operands[1];
3284
+ } else if (done >= repeat->maximum) {
3285
+ address = instruction->operands[2];
3286
+ } else if ((instruction->modifiers &
3287
+ OSEO_REGEXP_INSTRUCTION_GREEDY) != 0u) {
3288
+ if (!machine_push(machine, instruction->operands[2])) {
3289
+ return machine->outcome;
3290
+ }
3291
+ address = instruction->operands[1];
3292
+ } else {
3293
+ if (!machine_push(machine, instruction->operands[1])) {
3294
+ return machine->outcome;
3295
+ }
3296
+ address = instruction->operands[2];
3297
+ }
3298
+ break;
3299
+ }
3300
+ case OSEO_REGEXP_OP_REPEAT_INIT:
3301
+ if (!machine_write(machine, instruction->operands[0], 0)) {
3302
+ return machine->outcome;
3303
+ }
3304
+ address += 1u;
3305
+ break;
3306
+ case OSEO_REGEXP_OP_REPEAT_ENTER: {
3307
+ const OseoRegExpRepeat *repeat =
3308
+ &program->repeats[instruction->operands[3]];
3309
+ int64_t done = machine->registers[instruction->operands[0]];
3310
+ if (!machine_write(
3311
+ machine,
3312
+ instruction->operands[0],
3313
+ done + 1
3314
+ )) {
3315
+ return machine->outcome;
3316
+ }
3317
+ if (!machine_write(
3318
+ machine,
3319
+ instruction->operands[1],
3320
+ machine->index
3321
+ )) {
3322
+ return machine->outcome;
3323
+ }
3324
+ for (uint32_t slot = repeat->clear_from;
3325
+ slot < repeat->clear_to;
3326
+ slot += 1u) {
3327
+ if (!machine_write(
3328
+ machine,
3329
+ slot,
3330
+ OSEO_REGEXP_UNSET
3331
+ )) {
3332
+ return machine->outcome;
3333
+ }
3334
+ }
3335
+ address = instruction->operands[2];
3336
+ break;
3337
+ }
3338
+ case OSEO_REGEXP_OP_REPEAT_END: {
3339
+ const OseoRegExpRepeat *repeat =
3340
+ &program->repeats[instruction->operands[3]];
3341
+ uint64_t done =
3342
+ (uint64_t)machine->registers[instruction->operands[0]] -
3343
+ 1u;
3344
+ int64_t started =
3345
+ machine->registers[instruction->operands[2]];
3346
+ if (done >= repeat->minimum && started == machine->index) {
3347
+ failed = true;
3348
+ } else {
3349
+ address = instruction->operands[1];
3350
+ }
3351
+ break;
3352
+ }
3353
+ case OSEO_REGEXP_OP_LOOK_START:
3354
+ if (!machine_push(machine, instruction->operands[2])) {
3355
+ return machine->outcome;
3356
+ }
3357
+ if (!machine_write(
3358
+ machine,
3359
+ instruction->operands[0],
3360
+ (int64_t)machine->stack_count - 1
3361
+ )) {
3362
+ return machine->outcome;
3363
+ }
3364
+ address = instruction->operands[1];
3365
+ break;
3366
+ case OSEO_REGEXP_OP_LOOK_END: {
3367
+ size_t frame =
3368
+ (size_t)machine->registers[instruction->operands[0]];
3369
+ if (frame >= machine->stack_count) {
3370
+ failed = true;
3371
+ break;
3372
+ }
3373
+ int64_t entry_index = machine->stack_index[frame];
3374
+ uint32_t entry_trail = machine->stack_trail[frame];
3375
+ machine->stack_count = frame;
3376
+ machine->index = entry_index;
3377
+ if ((instruction->modifiers &
3378
+ OSEO_REGEXP_INSTRUCTION_NEGATED) != 0u) {
3379
+ machine_undo(machine, entry_trail);
3380
+ failed = true;
3381
+ } else {
3382
+ address = instruction->operands[1];
3383
+ }
3384
+ break;
3385
+ }
3386
+ case OSEO_REGEXP_OP_BACKREFERENCE:
3387
+ if (match_backreference(
3388
+ machine,
3389
+ instruction->operands[0],
3390
+ (instruction->modifiers &
3391
+ OSEO_REGEXP_INSTRUCTION_BACKWARD) != 0u,
3392
+ (instruction->modifiers &
3393
+ OSEO_REGEXP_INSTRUCTION_IGNORE_CASE) != 0u
3394
+ )) {
3395
+ address += 1u;
3396
+ } else {
3397
+ failed = true;
3398
+ }
3399
+ break;
3400
+ case OSEO_REGEXP_OP_FAIL:
3401
+ default:
3402
+ failed = true;
3403
+ break;
3404
+ }
3405
+ if (failed) {
3406
+ int64_t resumed = machine_pop(machine);
3407
+ if (resumed < 0) return OSEO_REGEXP_EXECUTION_UNMATCHED;
3408
+ address = (size_t)resumed;
3409
+ }
3410
+ }
3411
+ }
3412
+
3413
+ /*
3414
+ * Mirror anchoredRepeatSet in the compiler executor. Compare the complete
3415
+ * program, including all branch/register links, before exposing any match
3416
+ * state. Unknown shapes keep the ordered matcher and all of its counters.
3417
+ */
3418
+ static bool anchored_repeat_set(
3419
+ const OseoRegExpProgram *program,
3420
+ uint32_t *set
3421
+ ) {
3422
+ if (program->capture_count != 0u || program->registers != 4u ||
3423
+ program->instruction_count != 11u || program->repeat_count != 1u) {
3424
+ return false;
3425
+ }
3426
+ const OseoRegExpRepeat *repeat = &program->repeats[0];
3427
+ if (repeat->minimum != 1u || repeat->maximum != UINT64_MAX ||
3428
+ repeat->clear_from != 0u || repeat->clear_to != 0u) return false;
3429
+ const OseoRegExpInstruction expected[] = {
3430
+ {OSEO_REGEXP_OP_SAVE, 0u, {0u, 0u, 0u, 0u}},
3431
+ {OSEO_REGEXP_OP_EDGE, 0u, {0u, 0u, 0u, 0u}},
3432
+ {OSEO_REGEXP_OP_REPEAT_INIT, 0u, {2u, 0u, 0u, 0u}},
3433
+ {OSEO_REGEXP_OP_REPEAT, OSEO_REGEXP_INSTRUCTION_GREEDY,
3434
+ {2u, 4u, 7u, 0u}},
3435
+ {OSEO_REGEXP_OP_REPEAT_ENTER, 0u, {2u, 3u, 5u, 0u}},
3436
+ {OSEO_REGEXP_OP_CONSUME, 0u, {0u, 0u, 0u, 0u}},
3437
+ {OSEO_REGEXP_OP_REPEAT_END, 0u, {2u, 3u, 3u, 0u}},
3438
+ {OSEO_REGEXP_OP_EDGE, OSEO_REGEXP_INSTRUCTION_END_EDGE,
3439
+ {0u, 0u, 0u, 0u}},
3440
+ {OSEO_REGEXP_OP_SAVE, 0u, {1u, 0u, 0u, 0u}},
3441
+ {OSEO_REGEXP_OP_ACCEPT, 0u, {0u, 0u, 0u, 0u}},
3442
+ {OSEO_REGEXP_OP_FAIL, 0u, {0u, 0u, 0u, 0u}},
3443
+ };
3444
+ for (size_t index = 0u; index < 11u; index += 1u) {
3445
+ const OseoRegExpInstruction *actual = &program->instructions[index];
3446
+ if (actual->opcode != expected[index].opcode ||
3447
+ actual->modifiers != expected[index].modifiers) return false;
3448
+ for (size_t operand = 0u; operand < 4u; operand += 1u) {
3449
+ if (index == 5u && operand == 0u) continue;
3450
+ if (actual->operands[operand] !=
3451
+ expected[index].operands[operand]) return false;
3452
+ }
3453
+ }
3454
+ *set = program->instructions[5].operands[0];
3455
+ return *set < program->set_count;
3456
+ }
3457
+
3458
+ /*
3459
+ * Constant-state ^set+$ scan: one entry step, one per tested character,
3460
+ * and one terminal decision. No stack/trail entries or work allocations
3461
+ * are needed. Non-multiline $ succeeds only at InputLength.
3462
+ */
3463
+ static OseoRegExpExecution scan_anchored_repeat(
3464
+ const OseoRegExpProgram *program,
3465
+ const OseoString *subject,
3466
+ size_t start_index,
3467
+ uint32_t set,
3468
+ int64_t *captures
3469
+ ) {
3470
+ uint64_t steps = 0u;
3471
+ if (steps >= OSEO_REGEXP_STEP_LIMIT) return OSEO_REGEXP_EXECUTION_LIMIT;
3472
+ steps += 1u;
3473
+ if (aligned_start(subject, start_index, program->unicode_mode) != 0u) {
3474
+ return OSEO_REGEXP_EXECUTION_UNMATCHED;
3475
+ }
3476
+ size_t index = 0u;
3477
+ while (index < subject->length) {
3478
+ if (steps >= OSEO_REGEXP_STEP_LIMIT) {
3479
+ return OSEO_REGEXP_EXECUTION_LIMIT;
3480
+ }
3481
+ steps += 1u;
3482
+ uint32_t character = character_at(
3483
+ subject, index, program->unicode_mode
3484
+ );
3485
+ if (!program_set_has(program, set, character)) {
3486
+ return OSEO_REGEXP_EXECUTION_UNMATCHED;
3487
+ }
3488
+ index += character > 0xffffu ? 2u : 1u;
3489
+ }
3490
+ if (steps >= OSEO_REGEXP_STEP_LIMIT) return OSEO_REGEXP_EXECUTION_LIMIT;
3491
+ steps += 1u;
3492
+ if (index == 0u) return OSEO_REGEXP_EXECUTION_UNMATCHED;
3493
+ captures[0] = 0;
3494
+ captures[1] = (int64_t)index;
3495
+ return OSEO_REGEXP_EXECUTION_MATCHED;
3496
+ }
3497
+
3498
+ OseoRegExpExecution oseo_internal_regexp_program_search(
3499
+ OseoContext *context,
3500
+ const OseoRegExpProgram *program,
3501
+ const OseoString *subject,
3502
+ size_t start_index,
3503
+ bool sticky,
3504
+ bool scan_eligible,
3505
+ int64_t *captures
3506
+ ) {
3507
+ size_t total = 2u * (program->capture_count + 1u);
3508
+ for (size_t index = 0u; index < total; index += 1u) {
3509
+ captures[index] = OSEO_REGEXP_UNSET;
3510
+ }
3511
+ if (start_index > subject->length) {
3512
+ return OSEO_REGEXP_EXECUTION_UNMATCHED;
3513
+ }
3514
+ uint32_t scan_set = 0u;
3515
+ if (scan_eligible && anchored_repeat_set(program, &scan_set)) {
3516
+ return scan_anchored_repeat(
3517
+ program, subject, start_index, scan_set, captures
3518
+ );
3519
+ }
3520
+ OseoRegExpMachine machine;
3521
+ memset(&machine, 0, sizeof(machine));
3522
+ machine.program = program;
3523
+ machine.subject = subject;
3524
+ machine.step_limit = OSEO_REGEXP_STEP_LIMIT;
3525
+ machine.context = context;
3526
+ machine.registers = oseo_internal_allocate_work_bytes(
3527
+ context,
3528
+ program->registers * sizeof(int64_t)
3529
+ );
3530
+ OseoRegExpExecution outcome = OSEO_REGEXP_EXECUTION_UNMATCHED;
3531
+ if (machine.registers == NULL) {
3532
+ outcome = OSEO_REGEXP_EXECUTION_ALLOCATION;
3533
+ } else {
3534
+ size_t index = aligned_start(
3535
+ subject,
3536
+ start_index,
3537
+ program->unicode_mode
3538
+ );
3539
+ for (;;) {
3540
+ outcome = match_at(&machine, index);
3541
+ if (outcome == OSEO_REGEXP_EXECUTION_MATCHED) {
3542
+ for (size_t slot = 0u; slot < total; slot += 1u) {
3543
+ captures[slot] = machine.registers[slot];
3544
+ }
3545
+ break;
3546
+ }
3547
+ if (outcome != OSEO_REGEXP_EXECUTION_UNMATCHED) break;
3548
+ if (sticky || index >= subject->length) break;
3549
+ index += width_at(subject, index, program->unicode_mode);
3550
+ }
3551
+ }
3552
+ free(machine.registers);
3553
+ free(machine.stack_program);
3554
+ free(machine.stack_index);
3555
+ free(machine.stack_trail);
3556
+ free(machine.trail_register);
3557
+ free(machine.trail_value);
3558
+ return outcome;
3559
+ }