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

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