qr_reader-native 0.0.1
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.
- checksums.yaml +7 -0
- data/LICENSE-mruby.txt +22 -0
- data/LICENSE-spinel.txt +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/ext/qr_reader_native/SPINEL_VERSION +1 -0
- data/ext/qr_reader_native/extconf.rb +8 -0
- data/ext/qr_reader_native/mruby/error.h +13 -0
- data/ext/qr_reader_native/mruby/internal.h +145 -0
- data/ext/qr_reader_native/mruby/string.h +22 -0
- data/ext/qr_reader_native/mruby.h +93 -0
- data/ext/qr_reader_native/mruby_shim.h +118 -0
- data/ext/qr_reader_native/qr_reader_native.c +13513 -0
- data/ext/qr_reader_native/qr_reader_native.h +29 -0
- data/ext/qr_reader_native/qr_reader_native_ext.c +598 -0
- data/ext/qr_reader_native/re_cased.h +71 -0
- data/ext/qr_reader_native/re_compile.c +5784 -0
- data/ext/qr_reader_native/re_ctype.h +3540 -0
- data/ext/qr_reader_native/re_exec.c +2190 -0
- data/ext/qr_reader_native/re_internal.h +671 -0
- data/ext/qr_reader_native/re_prop.h +4629 -0
- data/ext/qr_reader_native/re_spinel.c +363 -0
- data/ext/qr_reader_native/re_spinel.h +22 -0
- data/ext/qr_reader_native/re_spinel_utf8.inc +97 -0
- data/ext/qr_reader_native/re_utf8.c +156 -0
- data/ext/qr_reader_native/sp_alloc.c +1447 -0
- data/ext/qr_reader_native/sp_alloc.h +1196 -0
- data/ext/qr_reader_native/sp_argf.h +33 -0
- data/ext/qr_reader_native/sp_array.c +1052 -0
- data/ext/qr_reader_native/sp_array.h +456 -0
- data/ext/qr_reader_native/sp_bigint.c +5826 -0
- data/ext/qr_reader_native/sp_bigint.h +117 -0
- data/ext/qr_reader_native/sp_cold.c +5259 -0
- data/ext/qr_reader_native/sp_compat.h +402 -0
- data/ext/qr_reader_native/sp_core.c +818 -0
- data/ext/qr_reader_native/sp_core.h +56 -0
- data/ext/qr_reader_native/sp_crypto.c +877 -0
- data/ext/qr_reader_native/sp_crypto.h +169 -0
- data/ext/qr_reader_native/sp_dtoa.c +1533 -0
- data/ext/qr_reader_native/sp_dtoa.h +29 -0
- data/ext/qr_reader_native/sp_encoding_names.h +172 -0
- data/ext/qr_reader_native/sp_enum.h +86 -0
- data/ext/qr_reader_native/sp_exc.c +1185 -0
- data/ext/qr_reader_native/sp_exc.h +186 -0
- data/ext/qr_reader_native/sp_exc_ctx.h +56 -0
- data/ext/qr_reader_native/sp_fiber.c +889 -0
- data/ext/qr_reader_native/sp_fiber.h +162 -0
- data/ext/qr_reader_native/sp_fiber_ctx.h +84 -0
- data/ext/qr_reader_native/sp_format.c +695 -0
- data/ext/qr_reader_native/sp_format.h +83 -0
- data/ext/qr_reader_native/sp_gc.c +1793 -0
- data/ext/qr_reader_native/sp_gc.h +762 -0
- data/ext/qr_reader_native/sp_hash.c +131 -0
- data/ext/qr_reader_native/sp_hash.h +131 -0
- data/ext/qr_reader_native/sp_inspect.c +215 -0
- data/ext/qr_reader_native/sp_inspect.h +128 -0
- data/ext/qr_reader_native/sp_io.c +1676 -0
- data/ext/qr_reader_native/sp_io.h +272 -0
- data/ext/qr_reader_native/sp_iobuffer.c +1014 -0
- data/ext/qr_reader_native/sp_iobuffer.h +167 -0
- data/ext/qr_reader_native/sp_marshal.c +487 -0
- data/ext/qr_reader_native/sp_marshal.h +60 -0
- data/ext/qr_reader_native/sp_net.c +1029 -0
- data/ext/qr_reader_native/sp_net.h +205 -0
- data/ext/qr_reader_native/sp_pack.c +1254 -0
- data/ext/qr_reader_native/sp_poly_cold.c +1220 -0
- data/ext/qr_reader_native/sp_proc.c +410 -0
- data/ext/qr_reader_native/sp_proc.h +327 -0
- data/ext/qr_reader_native/sp_process.c +547 -0
- data/ext/qr_reader_native/sp_process_status.c +211 -0
- data/ext/qr_reader_native/sp_process_status.h +66 -0
- data/ext/qr_reader_native/sp_random.c +287 -0
- data/ext/qr_reader_native/sp_random.h +52 -0
- data/ext/qr_reader_native/sp_range.h +144 -0
- data/ext/qr_reader_native/sp_re.c +1379 -0
- data/ext/qr_reader_native/sp_re.h +186 -0
- data/ext/qr_reader_native/sp_re_names.h +59 -0
- data/ext/qr_reader_native/sp_sched.c +4044 -0
- data/ext/qr_reader_native/sp_sched.h +295 -0
- data/ext/qr_reader_native/sp_slab.c +1507 -0
- data/ext/qr_reader_native/sp_str.c +1462 -0
- data/ext/qr_reader_native/sp_str.h +306 -0
- data/ext/qr_reader_native/sp_str_crypt.c +23 -0
- data/ext/qr_reader_native/sp_string.c +92 -0
- data/ext/qr_reader_native/sp_string.h +253 -0
- data/ext/qr_reader_native/sp_system.c +78 -0
- data/ext/qr_reader_native/sp_system.h +18 -0
- data/ext/qr_reader_native/sp_time.c +860 -0
- data/ext/qr_reader_native/sp_time.h +100 -0
- data/ext/qr_reader_native/sp_types.h +292 -0
- data/ext/qr_reader_native/spinel_rt.h +19017 -0
- data/ext/qr_reader_native/unicase.c +399 -0
- data/ext/qr_reader_native/unicase.h +901 -0
- data/lib/qr_reader/native.rb +6 -0
- metadata +153 -0
|
@@ -0,0 +1,671 @@
|
|
|
1
|
+
/*
|
|
2
|
+
** re_internal.h - internal definitions for regexp engine
|
|
3
|
+
**
|
|
4
|
+
** See Copyright Notice in mruby.h
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
#ifndef MRB_RE_INTERNAL_H
|
|
8
|
+
#define MRB_RE_INTERNAL_H
|
|
9
|
+
|
|
10
|
+
#include <mruby.h>
|
|
11
|
+
#include <mruby/internal.h>
|
|
12
|
+
#include <stdint.h>
|
|
13
|
+
|
|
14
|
+
MRB_BEGIN_DECL
|
|
15
|
+
|
|
16
|
+
/* The Unicode foldings /i reads are core's table, which only a build reading
|
|
17
|
+
its strings as characters and classifying them by Unicode carries. /i
|
|
18
|
+
therefore folds the way that build's own case conversion does and no other
|
|
19
|
+
way, a pattern read as bytes having no character to fold in the first
|
|
20
|
+
place. The types a POSIX bracket reads above ASCII are this gem's table,
|
|
21
|
+
carried on the same condition: a build that asked to leave the case table
|
|
22
|
+
behind is counting its bytes, and the type table is nothing it wants
|
|
23
|
+
instead. The two names say which of the tables a site reads. */
|
|
24
|
+
#if defined(MRB_UTF8_STRING) && !defined(MRB_USE_ASCII_CTYPE)
|
|
25
|
+
# define RE_UNICODE_CASE
|
|
26
|
+
# define RE_UNICODE_CTYPE
|
|
27
|
+
#endif
|
|
28
|
+
|
|
29
|
+
/* Bytecode instructions for the NFA engine */
|
|
30
|
+
enum re_opcode {
|
|
31
|
+
RE_CHAR, /* match literal byte: operand = byte value. The bytes of one
|
|
32
|
+
character are a run of these, so a run matches the
|
|
33
|
+
character it spells and stops where the character does. */
|
|
34
|
+
RE_BYTE, /* match one byte that spells no character: operand = the
|
|
35
|
+
byte, always above 127. It matches only where the subject
|
|
36
|
+
byte stands alone, starting no whole character of its own,
|
|
37
|
+
which is the rule a class already reads such a byte by (see
|
|
38
|
+
RE_CLASS_BYTE). A byte inside a character is that
|
|
39
|
+
character's, not this one, so the position a search reaches
|
|
40
|
+
is never inside one and every offset it records is a
|
|
41
|
+
character boundary without a test for it. Against a
|
|
42
|
+
byte-indexed subject every byte stands alone and this is
|
|
43
|
+
RE_CHAR. */
|
|
44
|
+
RE_ANY, /* match any character (. without DOTALL) */
|
|
45
|
+
RE_ANY_NL, /* match any character including newline (. with DOTALL) */
|
|
46
|
+
RE_CLASS, /* match character class: operand = class_id */
|
|
47
|
+
RE_NCLASS, /* match negated character class: operand = class_id */
|
|
48
|
+
RE_MATCH, /* successful match */
|
|
49
|
+
RE_JMP, /* unconditional jump: operand = target offset */
|
|
50
|
+
RE_SPLIT, /* fork: operand = target offset (greedy: try next first) */
|
|
51
|
+
RE_SPLITNG, /* fork: operand = target offset (non-greedy: try jump first) */
|
|
52
|
+
RE_SAVE, /* save capture position: operand = slot number */
|
|
53
|
+
RE_BOL, /* assert beginning of line (^) */
|
|
54
|
+
RE_EOL, /* assert end of line ($) */
|
|
55
|
+
RE_BOT, /* assert beginning of text (\A) */
|
|
56
|
+
RE_EOT, /* assert end of text (\z) */
|
|
57
|
+
RE_EOTNL, /* assert end of text or before final \n (\Z) */
|
|
58
|
+
RE_WBOUND, /* assert word boundary (\b) */
|
|
59
|
+
RE_NWBOUND, /* assert non-word boundary (\B) */
|
|
60
|
+
RE_BACKREF, /* backreference: a = group number, offset = 1 if case-insensitive */
|
|
61
|
+
RE_COND, /* the conditional (?(cond)yes|no): a = the group the
|
|
62
|
+
condition names, offset = where `no` begins. Goes on at
|
|
63
|
+
pc + 1 where the group has matched, its capture pair
|
|
64
|
+
being closed, and at `offset` where it has not; a group
|
|
65
|
+
still open, or one a repetition has just re-entered,
|
|
66
|
+
has not. Nothing is pushed: the choice is the captures'
|
|
67
|
+
and a failure after it backtracks past it as past any
|
|
68
|
+
straight-line instruction. `yes` ends with a jump past
|
|
69
|
+
`no`, and with no `no` the offset is the text after the
|
|
70
|
+
group. */
|
|
71
|
+
RE_LOOKAHEAD, /* positive lookahead: offset = end of sub-pattern */
|
|
72
|
+
RE_NEG_LOOKAHEAD, /* negative lookahead: offset = end of sub-pattern */
|
|
73
|
+
RE_LOOKBEHIND, /* positive lookbehind: offset = end, a = 1 once the
|
|
74
|
+
widths below are in place and 0 while the parse has
|
|
75
|
+
left the measuring to measure_deferred_lookbehinds().
|
|
76
|
+
It does not rewind: each branch of the sub-pattern
|
|
77
|
+
rewinds by its own width, so the opener leaves the
|
|
78
|
+
input where the lookbehind was entered and goes on at
|
|
79
|
+
pc + 1. */
|
|
80
|
+
RE_NEG_LOOKBEHIND, /* negative lookbehind: as above */
|
|
81
|
+
RE_LB_WIDTH, /* rewind by one fixed width, at the head of the branch
|
|
82
|
+
that takes it: offset holds the byte count in its
|
|
83
|
+
high 8 bits and the character count in its low 8
|
|
84
|
+
bits, a fixed-width branch being at most 255 bytes
|
|
85
|
+
wide so that both fit. The executor rewinds by bytes
|
|
86
|
+
against a binary subject and by characters otherwise,
|
|
87
|
+
which is why the two counts travel together; too
|
|
88
|
+
little text before is this branch failing, and what
|
|
89
|
+
the search tries next is the branch after it.
|
|
90
|
+
|
|
91
|
+
A lookbehind whose top-level alternation takes
|
|
92
|
+
different widths per branch, `(?<=ab|c)`, carries one
|
|
93
|
+
of these at each branch's head, so the branches are
|
|
94
|
+
tried in the order the alternation already gives them
|
|
95
|
+
and each rewinds by its own measure; one whose body
|
|
96
|
+
is a single fixed width carries one at the head of
|
|
97
|
+
the body. RE_LOOK_END then asserts that the branch
|
|
98
|
+
landed back where the lookbehind was entered, which
|
|
99
|
+
is what keeps a branch of one width from matching
|
|
100
|
+
from another's rewind. */
|
|
101
|
+
RE_ATOMIC, /* atomic group (?>...): offset = the group's number,
|
|
102
|
+
1 for the first one the pattern opens and no two the
|
|
103
|
+
same. The body follows and ends at the RE_ATOMIC_END
|
|
104
|
+
with the same number; once the body has matched, a
|
|
105
|
+
failure after it fails the whole group rather than
|
|
106
|
+
backtracking into it. */
|
|
107
|
+
RE_ATOMIC_END, /* end of an atomic group's body: offset = the number of
|
|
108
|
+
the RE_ATOMIC it closes */
|
|
109
|
+
RE_LOOK_END, /* end of a lookaround's sub-pattern: offset = the
|
|
110
|
+
lookaround's number, from the same count as RE_ATOMIC;
|
|
111
|
+
a holds RE_LOOK_NEGATED and RE_LOOK_LANDING. The
|
|
112
|
+
instruction after it is
|
|
113
|
+
the text after the lookaround, which is where the
|
|
114
|
+
opener's `offset` points, so the opener finds its end
|
|
115
|
+
at offset - 1. */
|
|
116
|
+
RE_CALL, /* run the body of group `a` again (\g<name>): offset =
|
|
117
|
+
where the body starts. The executor pushes a frame
|
|
118
|
+
holding where to go on when the body completes (pc + 1)
|
|
119
|
+
and where the input stood, clears the group's end slot
|
|
120
|
+
so the group reads as unmatched while the invocation
|
|
121
|
+
is open, and jumps. The frame lives on the choice
|
|
122
|
+
point stack, so backtracking past the call unwinds it
|
|
123
|
+
with everything else and MRB_REGEXP_STACK_LIMIT is
|
|
124
|
+
what bounds the call depth. Every entry into a called
|
|
125
|
+
group's body is one of these, the inline occurrence
|
|
126
|
+
included: resolve_calls() reroutes it through a
|
|
127
|
+
trampoline appended after the pattern, so the body has
|
|
128
|
+
one entry and one exit however it is reached. Until
|
|
129
|
+
that pass runs, `offset` is not a code index but the
|
|
130
|
+
parser's own bookkeeping, which is why this opcode is
|
|
131
|
+
not in op_holds_code_index(). */
|
|
132
|
+
RE_RETURN, /* end of a called group's body: a = the group. Finds
|
|
133
|
+
the topmost frame no return has answered yet, writes
|
|
134
|
+
the group's capture pair from it -- the invocation
|
|
135
|
+
that completes last is the one the pair names, as in
|
|
136
|
+
CRuby -- leaves a marker in the frame's place and
|
|
137
|
+
goes on where the frame says. */
|
|
138
|
+
RE_ABSENT_START, /* the absent repeater (?~e) is entered: the state its
|
|
139
|
+
scan runs on goes on the choice point stack, holding
|
|
140
|
+
where the absent began and how far it may reach. */
|
|
141
|
+
RE_ABSENT, /* the head of one round of that scan: offset = the
|
|
142
|
+
instruction after the group, where the absent goes on
|
|
143
|
+
once the scan has found the end it takes. The body
|
|
144
|
+
follows and ends at the RE_ABSENT_END; the round
|
|
145
|
+
either takes an end and jumps to `offset`, or runs
|
|
146
|
+
the body once at the position it stands at and goes
|
|
147
|
+
on to the next position. */
|
|
148
|
+
RE_ABSENT_END, /* end of the absent's body, reached where the body
|
|
149
|
+
matched: the match is not the absent's, it is what
|
|
150
|
+
says the absent may not reach past it, so the round
|
|
151
|
+
records how far the absent may still reach and the
|
|
152
|
+
scan goes on at the next position. */
|
|
153
|
+
/* spinel: \G, assert the position this search started from, which the
|
|
154
|
+
backtracking engine keeps as bt_state::gpos; the pattern needs that
|
|
155
|
+
engine */
|
|
156
|
+
RE_GPOS,
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/* RE_LOOK_END::a. The polarity says what a matched sub-pattern means. The
|
|
160
|
+
landing bit asks that the sub-pattern have ended where the lookaround was
|
|
161
|
+
entered, and stands only on a lookbehind whose branches take different
|
|
162
|
+
widths, the one shape that can miss: rewound by a branch two characters
|
|
163
|
+
wide, a branch one character wide matches and stops short. A lookahead
|
|
164
|
+
starts where the search stands and a lookbehind of one width is that wide
|
|
165
|
+
down every path, so neither can land anywhere else and neither pays for
|
|
166
|
+
the test. */
|
|
167
|
+
#define RE_LOOK_NEGATED 1
|
|
168
|
+
#define RE_LOOK_LANDING 2
|
|
169
|
+
|
|
170
|
+
/* RE_LB_WIDTH::offset packs the two units one width is counted in. Both are
|
|
171
|
+
at most 255, the widest a lookbehind branch may be. */
|
|
172
|
+
#define RE_LB_PACK(bytes, chars) \
|
|
173
|
+
((uint16_t)((((bytes) & 0xff) << 8) | ((chars) & 0xff)))
|
|
174
|
+
#define RE_LB_BYTES(offset) ((int)((offset) >> 8))
|
|
175
|
+
#define RE_LB_CHARS(offset) ((int)((offset) & 0xff))
|
|
176
|
+
|
|
177
|
+
/* Bytecode instruction (4 bytes each for alignment) */
|
|
178
|
+
typedef struct {
|
|
179
|
+
uint8_t op;
|
|
180
|
+
uint8_t a; /* small operand or class id */
|
|
181
|
+
uint16_t offset; /* jump target or extended operand */
|
|
182
|
+
} re_inst;
|
|
183
|
+
|
|
184
|
+
/* Character class bitmap (ASCII range) */
|
|
185
|
+
#define RE_CLASS_BITMAP_SIZE 16 /* 128 bits = 16 bytes for ASCII */
|
|
186
|
+
|
|
187
|
+
/* A class member that is a byte rather than a codepoint, held in the same
|
|
188
|
+
range list with this bit set. A pattern byte that starts no whole character
|
|
189
|
+
is a byte, and the two spaces collide over U+0080 to U+00FF: the byte 0xB5
|
|
190
|
+
and the character U+00B5 both arrive as the number 0xB5, so the number alone
|
|
191
|
+
cannot say which was written. The tag sits above every codepoint, so a
|
|
192
|
+
tagged range can never overlap an untagged one. */
|
|
193
|
+
#define RE_CLASS_BYTE 0x80000000u
|
|
194
|
+
|
|
195
|
+
typedef struct {
|
|
196
|
+
uint8_t bitmap[RE_CLASS_BITMAP_SIZE]; /* bitmap for 0-127 */
|
|
197
|
+
/* Non-ASCII codepoint ranges, and byte ranges tagged with RE_CLASS_BYTE.
|
|
198
|
+
Stored as flat (lo, hi) pairs: ranges[2k] = lo, ranges[2k+1] = hi
|
|
199
|
+
(inclusive). NULL when the class has no non-ASCII members (the common
|
|
200
|
+
case). */
|
|
201
|
+
uint32_t *ranges;
|
|
202
|
+
uint32_t num_ranges;
|
|
203
|
+
uint32_t range_capa;
|
|
204
|
+
mrb_bool negated;
|
|
205
|
+
mrb_bool utf8_any; /* match any non-ASCII byte if true */
|
|
206
|
+
#ifdef RE_UNICODE_CTYPE
|
|
207
|
+
/* What the POSIX brackets in the class hold above ASCII, as re_ctype bits:
|
|
208
|
+
a character belongs when its type has a bit of ctype_yes, or lacks a bit
|
|
209
|
+
of ctype_no ([:^alpha:] is every character that is not a letter). Neither
|
|
210
|
+
is spelled out as ranges: a class holding [[:alpha:]] would carry the
|
|
211
|
+
letters as hundreds of ranges, and be read through them one by one at
|
|
212
|
+
every character. A byte that is no character has no type: it belongs
|
|
213
|
+
under ctype_no and not under ctype_yes.
|
|
214
|
+
|
|
215
|
+
ctype_all and ctype_none are the same question in the other direction:
|
|
216
|
+
the character's type must have every bit of the one and no bit of the
|
|
217
|
+
other. `&&` is what asks for them, an intersection of brackets being a
|
|
218
|
+
conjunction where a union is the disjunction the pair above spells, and
|
|
219
|
+
a bracket on its own reaches them through the pair, [[:alpha:]] naming
|
|
220
|
+
one bit either way. A class holds a character through its brackets when
|
|
221
|
+
the pair admits it, if the class has one, and the two masks do.
|
|
222
|
+
|
|
223
|
+
ctype_fold is set under /i when the class has any of the four: the type
|
|
224
|
+
read is then that of the character and of every character sharing its
|
|
225
|
+
folding, so that [[:upper:]] under /i holds "ā" through "Ā". A member the
|
|
226
|
+
class holds by bit or by range is closed under folding at compile time
|
|
227
|
+
instead; see compile_charclass(). */
|
|
228
|
+
uint16_t ctype_yes;
|
|
229
|
+
uint16_t ctype_no;
|
|
230
|
+
uint16_t ctype_all;
|
|
231
|
+
uint16_t ctype_none;
|
|
232
|
+
mrb_bool ctype_fold;
|
|
233
|
+
#endif
|
|
234
|
+
} re_charclass;
|
|
235
|
+
|
|
236
|
+
/* Longest capture name that fits in re_named_capture::name_len. The bound
|
|
237
|
+
sits beside the field it describes so a later change of width is one line.
|
|
238
|
+
The widening inside the macro is not cosmetic: on a target whose argument
|
|
239
|
+
type is no wider than the field, the naive comparison is against a constant
|
|
240
|
+
that type cannot exceed, and the compiler reports it. */
|
|
241
|
+
#define RE_MAX_NAME_LEN UINT32_MAX
|
|
242
|
+
#define RE_NAME_LEN_FITS(n) ((uintmax_t)(n) <= RE_MAX_NAME_LEN)
|
|
243
|
+
|
|
244
|
+
/* Named capture entry */
|
|
245
|
+
typedef struct {
|
|
246
|
+
const char *name;
|
|
247
|
+
uint32_t name_len;
|
|
248
|
+
uint16_t group;
|
|
249
|
+
} re_named_capture;
|
|
250
|
+
|
|
251
|
+
/* Compiled regexp pattern */
|
|
252
|
+
typedef struct mrb_regexp_pattern {
|
|
253
|
+
re_inst *code; /* bytecode array */
|
|
254
|
+
uint32_t code_len; /* number of instructions */
|
|
255
|
+
re_charclass *classes; /* character class table */
|
|
256
|
+
uint16_t num_classes;
|
|
257
|
+
uint16_t num_captures; /* number of capture groups (including group 0) */
|
|
258
|
+
uint32_t flags;
|
|
259
|
+
re_named_capture *named_captures;
|
|
260
|
+
char *named_arena; /* owned storage for named_captures[i].name; NULL if num_named == 0 */
|
|
261
|
+
uint16_t num_named;
|
|
262
|
+
mrb_bool has_backref; /* true if pattern uses \1-\9 */
|
|
263
|
+
mrb_bool needs_backtrack; /* true if pattern needs backtracking engine */
|
|
264
|
+
uint8_t *prefix; /* literal prefix bytes for fast skip (or NULL) */
|
|
265
|
+
uint8_t prefix_len; /* length of prefix (0 = no prefix) */
|
|
266
|
+
uint8_t first_bytes[32]; /* bitmap of possible first bytes, all 256 */
|
|
267
|
+
mrb_bool has_first_bytes; /* true if first_bytes is usable for skipping */
|
|
268
|
+
uint8_t first_byte_count; /* how many bytes first_byte[] holds (1..3), or 0
|
|
269
|
+
when the set is wider and only the bitmap
|
|
270
|
+
serves; meaningful only under has_first_bytes */
|
|
271
|
+
uint8_t first_byte[3]; /* the whole first-byte set when it is this small,
|
|
272
|
+
so the skip can memchr instead of walking */
|
|
273
|
+
mrb_bool is_literal; /* true if pattern is pure literal (no metacharacters) */
|
|
274
|
+
uint8_t anchor; /* the anchor every branch asserts before consuming
|
|
275
|
+
input (the weakest across branches), so the
|
|
276
|
+
engines scan only positions it can pass: line
|
|
277
|
+
starts under RE_ANCHOR_BOL, the string start
|
|
278
|
+
alone under RE_ANCHOR_BOT */
|
|
279
|
+
uint8_t loop_depth; /* deepest nesting of repetitions whose body can
|
|
280
|
+
match empty (see RE_MAX_PASS) */
|
|
281
|
+
/* Cached VM state for pike_vm (avoids malloc per mrb_re_exec call) */
|
|
282
|
+
uint32_t *cached_visited; /* generation-based visited array */
|
|
283
|
+
void *cached_threads[2]; /* curr/next thread lists */
|
|
284
|
+
int cached_list_capa; /* capacity of cached thread lists */
|
|
285
|
+
mrb_bool cache_in_use; /* re-entrancy guard */
|
|
286
|
+
} mrb_regexp_pattern;
|
|
287
|
+
|
|
288
|
+
/* mrb_regexp_pattern::anchor. Ordered by how much each restricts where a
|
|
289
|
+
match can start, so the weakest guarantee across branches is their min. */
|
|
290
|
+
#define RE_ANCHOR_NONE 0
|
|
291
|
+
#define RE_ANCHOR_BOL 1 /* every branch passes ^ first: line starts only */
|
|
292
|
+
#define RE_ANCHOR_BOT 2 /* every branch passes \A first: string start only */
|
|
293
|
+
|
|
294
|
+
/* Regexp flags */
|
|
295
|
+
#define RE_FLAG_IGNORECASE 1
|
|
296
|
+
#define RE_FLAG_MULTILINE 2 /* ^ and $ match at \n boundaries */
|
|
297
|
+
#define RE_FLAG_DOTALL 4 /* . matches \n (Ruby's /m for dot behavior) */
|
|
298
|
+
#define RE_FLAG_EXTENDED 8 /* ignore whitespace and #comments in pattern */
|
|
299
|
+
|
|
300
|
+
/* Note: Ruby's /m flag means BOTH multiline anchors AND dotall.
|
|
301
|
+
Ruby's /i flag is ignorecase. Ruby's /x flag is extended. */
|
|
302
|
+
|
|
303
|
+
/* Step limit for ReDoS protection */
|
|
304
|
+
#ifndef MRB_REGEXP_STEP_LIMIT
|
|
305
|
+
#define MRB_REGEXP_STEP_LIMIT 1000000
|
|
306
|
+
#endif
|
|
307
|
+
|
|
308
|
+
/* How tall the backtracking engine's stack may stand in one search: the
|
|
309
|
+
choice points it has not tried yet and the writes it has not taken back,
|
|
310
|
+
counted together (see bt_room() in re_exec.c). That stack is on the heap,
|
|
311
|
+
so a search spends a constant amount of C stack however long the subject
|
|
312
|
+
is, and what this bounds is what it holds instead. Where
|
|
313
|
+
MRB_REGEXP_STEP_LIMIT bounds the work one search may do, this bounds the
|
|
314
|
+
state it may hold while doing it.
|
|
315
|
+
|
|
316
|
+
What it counts is live entries, not bytes. The two arrays behind them grow
|
|
317
|
+
geometrically and keep their capacity for the rest of the search, so a
|
|
318
|
+
search that fills one, backtracks, and then fills the other holds both
|
|
319
|
+
high-water marks at once. Neither array is grown past this limit, though
|
|
320
|
+
(see bt_push()), so the memory one search may ask for is bounded by it:
|
|
321
|
+
at most this many entries in each array, an entry being 32 and 16 bytes
|
|
322
|
+
on a 64-bit ABI and 24 and 8 on a 32-bit one, so 96 KiB together at the
|
|
323
|
+
default on a 64-bit build and 64 KiB on a 32-bit one. The capture slots
|
|
324
|
+
and the iteration records (see backtrack_exec()) are sized by the pattern
|
|
325
|
+
and lie outside it.
|
|
326
|
+
|
|
327
|
+
The default is where the state moving off the C stack costs no pattern the
|
|
328
|
+
subject it used to match, and no higher: moving it is one change and
|
|
329
|
+
letting a search hold more of it is another, and a default above this one
|
|
330
|
+
would make the second silently. The old limit allowed 1,000 C frames, and
|
|
331
|
+
a frame is not an entry: a fork was one frame and is one choice point,
|
|
332
|
+
while a capture was one frame and is up to three undo records, so what a
|
|
333
|
+
pattern spends per iteration is what it holds. `(a)*?b` crossed 498
|
|
334
|
+
characters on 1,000 frames and crosses 682 on 2,048 entries, the tightest
|
|
335
|
+
of the shapes measured; a chain of atomic groups or of lookarounds, which
|
|
336
|
+
spent two frames a link and now spends none once each has closed, is
|
|
337
|
+
bounded by the pattern rather than by this limit either way. A build that
|
|
338
|
+
wants a longer subject to match, or a smaller ceiling on the memory a
|
|
339
|
+
search may ask for, sets it. */
|
|
340
|
+
#ifdef MRB_REGEXP_RECURSION_LIMIT
|
|
341
|
+
/* The engine no longer recurses per fork, so nothing counts C frames any more
|
|
342
|
+
and this knob is gone. Its replacement counts entries on a heap stack, and
|
|
343
|
+
the two measure different things: a value chosen for the old one does not
|
|
344
|
+
carry over, and a build that means to keep a restriction has to choose a
|
|
345
|
+
new one rather than have this header guess. */
|
|
346
|
+
#error MRB_REGEXP_RECURSION_LIMIT was replaced by MRB_REGEXP_STACK_LIMIT
|
|
347
|
+
#endif
|
|
348
|
+
#ifndef MRB_REGEXP_STACK_LIMIT
|
|
349
|
+
#define MRB_REGEXP_STACK_LIMIT 2048
|
|
350
|
+
#endif
|
|
351
|
+
|
|
352
|
+
/* What a build may set it to. Outside this it bounds nothing.
|
|
353
|
+
|
|
354
|
+
The floor is what the engine itself asks: at 0 no search could hold a
|
|
355
|
+
single entry and every pattern that reaches this engine would answer
|
|
356
|
+
RegexpError, which is a limit that has stopped being one. Any value from 1
|
|
357
|
+
up is a build's to choose. A low one is not a broken build but a smaller
|
|
358
|
+
ceiling on what one search may ask the allocator for, bought by refusing
|
|
359
|
+
more patterns: an ordinary one holds a handful of entries whatever the
|
|
360
|
+
subject (ten groups and a backreference hold twenty-two, two to a group
|
|
361
|
+
with the whole match's own pair among them, before the first repetition
|
|
362
|
+
adds any), so a build that sets the limit that low is choosing memory
|
|
363
|
+
over the patterns it can match. The gem's own tests ask for 48, which is
|
|
364
|
+
where every pattern they take for granted matches again, and skip below
|
|
365
|
+
it (see test/backtracking_stack.rb).
|
|
366
|
+
|
|
367
|
+
The two limits are set apart from one another as well. Filling the stack
|
|
368
|
+
costs a handful of steps an entry, so a build that turns this one up far
|
|
369
|
+
enough puts it out of MRB_REGEXP_STEP_LIMIT's reach: a search that was to
|
|
370
|
+
stop here stops there instead. Nothing in the engine reads that as an
|
|
371
|
+
error, the two limits being answers to different questions, but the tests
|
|
372
|
+
that pin this one size their subjects from it and are skipped there.
|
|
373
|
+
|
|
374
|
+
Above the ceiling the arithmetic that sizes the arrays stops holding: a
|
|
375
|
+
capacity is doubled in 32 bits and multiplied by an entry's width to ask
|
|
376
|
+
the allocator for bytes, which is 32 bits wide too on a 32-bit ABI, and a
|
|
377
|
+
limit this high has in any case stopped being one, the two arrays at it
|
|
378
|
+
standing at hundreds of megabytes.
|
|
379
|
+
|
|
380
|
+
A negative value is refused here rather than read as no limit at all: the
|
|
381
|
+
count it is compared against is unsigned (see bt_room() in re_exec.c), so
|
|
382
|
+
-1 would stand for the largest ceiling there is. */
|
|
383
|
+
#if MRB_REGEXP_STACK_LIMIT < 1 || MRB_REGEXP_STACK_LIMIT > (1 << 24)
|
|
384
|
+
#error MRB_REGEXP_STACK_LIMIT must stand between 1 and 16777216
|
|
385
|
+
#endif
|
|
386
|
+
|
|
387
|
+
/* How deep a pattern may nest. Every construct that opens a level costs one:
|
|
388
|
+
a group of any kind, a lookaround, an atomic group, an inline option
|
|
389
|
+
toggle, which encloses the rest of the group it stands in and so is a level
|
|
390
|
+
of its own, and a character class nested in another (see open_level() and
|
|
391
|
+
parse_class_levels() in re_compile.c). A pattern past this is `parse depth
|
|
392
|
+
limit over`, which is CRuby's message for the same refusal.
|
|
393
|
+
|
|
394
|
+
The default is Onigmo's ONIG_MAX_PARSE_DEPTH, so a pattern is refused
|
|
395
|
+
exactly where CRuby refuses it, and that agreement is all this limit
|
|
396
|
+
guards: the parser keeps the levels it has open on the heap, 32 bytes
|
|
397
|
+
each, so a pattern nested past what the machine holds is refused by the
|
|
398
|
+
allocator, at whatever depth that is, and no build has a stack to size
|
|
399
|
+
this from. One value holds for every build. (A nested character class is a
|
|
400
|
+
level on a stack of its own, and the class table bounds those at 256 as
|
|
401
|
+
well, so a build at the default limit meets the table there and one that
|
|
402
|
+
sets this at or below 256 meets this.)
|
|
403
|
+
|
|
404
|
+
The floor is 1, a build that takes no nesting at all; the ceiling is where
|
|
405
|
+
the limit stops being one, a million levels being 32 MiB of them. */
|
|
406
|
+
#ifndef MRB_REGEXP_PARSE_DEPTH_LIMIT
|
|
407
|
+
#define MRB_REGEXP_PARSE_DEPTH_LIMIT 4096
|
|
408
|
+
#endif
|
|
409
|
+
|
|
410
|
+
#if MRB_REGEXP_PARSE_DEPTH_LIMIT < 1 || MRB_REGEXP_PARSE_DEPTH_LIMIT > (1 << 20)
|
|
411
|
+
#error MRB_REGEXP_PARSE_DEPTH_LIMIT must stand between 1 and 1048576
|
|
412
|
+
#endif
|
|
413
|
+
|
|
414
|
+
/* What a search answers when the backtracking engine stopped before it had
|
|
415
|
+
an answer (see mrb_re_exec()). The caller raises on it: what the search had
|
|
416
|
+
found by then is not a shorter or a later match, and reading it as one was
|
|
417
|
+
the defect. Which one it was names what to do about it: a limit names the
|
|
418
|
+
knob to turn, where RE_NOMEM says the allocator refused and no knob will
|
|
419
|
+
help. Turning MRB_REGEXP_STACK_LIMIT up in answer to a refused allocation
|
|
420
|
+
would make the memory it failed to find scarcer still, so the two are kept
|
|
421
|
+
apart all the way out of the engine. */
|
|
422
|
+
#define RE_OVER_STACK_LIMIT (-1)
|
|
423
|
+
#define RE_OVER_STEP_LIMIT (-2)
|
|
424
|
+
#define RE_NOMEM (-3)
|
|
425
|
+
|
|
426
|
+
/* Maximum captures */
|
|
427
|
+
#define RE_MAX_CAPTURES 32
|
|
428
|
+
|
|
429
|
+
/* Thread struct for Pike VM (also used for cache sizing). `sp` is the
|
|
430
|
+
input position the thread is waiting for; the outer loop only dispatches
|
|
431
|
+
a thread when its sp matches the loop's current sp, otherwise the thread
|
|
432
|
+
is deferred to the next iteration. This keeps multi-byte consumers
|
|
433
|
+
(RE_CLASS over a UTF-8 char, advancing 3 bytes) in sync with single-byte
|
|
434
|
+
consumers (RE_CHAR, advancing 1 byte) without requiring a uniform
|
|
435
|
+
char-step outer loop -- both varieties just enqueue at their own sp+N. */
|
|
436
|
+
typedef struct {
|
|
437
|
+
uint32_t pc;
|
|
438
|
+
int cap_slot;
|
|
439
|
+
const char *sp;
|
|
440
|
+
} re_thread_cache;
|
|
441
|
+
|
|
442
|
+
/* A pike_vm step walks a repetition's body once per nesting level, so that a
|
|
443
|
+
loop's final empty iteration can finish even when the closure resumed
|
|
444
|
+
inside the body and already marked that iteration's tail (see add_thread).
|
|
445
|
+
The cap keeps a pathologically nested pattern from growing the thread lists
|
|
446
|
+
with the square of the program; past it, such a pattern keeps the older,
|
|
447
|
+
stale-capture behaviour rather than costing memory. */
|
|
448
|
+
#define RE_MAX_PASS 4
|
|
449
|
+
#define RE_PASS_SPAN(depth) \
|
|
450
|
+
((uint32_t)((depth) < RE_MAX_PASS ? (depth) : RE_MAX_PASS) + 1)
|
|
451
|
+
|
|
452
|
+
/* Capacity of one pike_vm thread list, shared by the VM and by the cache the
|
|
453
|
+
compiler pre-allocates for it so the two cannot drift. An instruction
|
|
454
|
+
enqueues at most one thread per pass, and threads waiting on a later sp are
|
|
455
|
+
carried over from the previous step on top of that. */
|
|
456
|
+
#define RE_LIST_CAPA(code_len, depth) \
|
|
457
|
+
((int)(code_len) * (int)(RE_PASS_SPAN(depth) + 1) + 16)
|
|
458
|
+
|
|
459
|
+
/* Compile a pattern string into `pat`, which the caller passes zero filled
|
|
460
|
+
and keeps reachable from a GC object for the whole call. Every buffer the
|
|
461
|
+
compile allocates hangs off `pat` from the moment it is allocated, so a
|
|
462
|
+
compile that raises, over a bad pattern or a refused allocation, leaves
|
|
463
|
+
them to mrb_re_free() rather than to the frame the longjmp abandons: the
|
|
464
|
+
zero fill is what lets mrb_re_free() read a pattern that got that far.
|
|
465
|
+
`pat->code_len` is written last and stays zero until the pattern is
|
|
466
|
+
complete. */
|
|
467
|
+
/* `binary` says the pattern string is byte-indexed, which is the one thing
|
|
468
|
+
mruby has where CRuby reads an encoding: a byte of an ASCII-8BIT pattern is
|
|
469
|
+
a byte, and bytes that would spell a character in a UTF-8 one do not spell
|
|
470
|
+
it here. It decides what a quantifier after them repeats. */
|
|
471
|
+
void mrb_re_compile(mrb_state *mrb, mrb_regexp_pattern *pat, const char *pattern, mrb_int len, uint32_t flags, mrb_bool binary);
|
|
472
|
+
|
|
473
|
+
/* Free a compiled pattern */
|
|
474
|
+
void mrb_re_free(mrb_state *mrb, mrb_regexp_pattern *pat);
|
|
475
|
+
|
|
476
|
+
/* Append the set flags among RE_FLAG_MULTILINE/IGNORECASE/EXTENDED to `str`
|
|
477
|
+
as letters, in the order Regexp#to_s and Regexp#inspect write them
|
|
478
|
+
(m, i, x). Shared so a compile error can quote a pattern the way
|
|
479
|
+
Regexp#inspect would, `/pattern/flags`, without duplicating the table
|
|
480
|
+
`regexp_to_s()` walks in regexp.c. */
|
|
481
|
+
void mrb_re_flags_cat(mrb_state *mrb, mrb_value str, uint32_t flags);
|
|
482
|
+
|
|
483
|
+
/* Word character (\w) test */
|
|
484
|
+
mrb_bool mrb_re_is_word_char(uint32_t c);
|
|
485
|
+
|
|
486
|
+
/* The two foldings whose result is an ASCII letter. Every build carries them,
|
|
487
|
+
whether or not it has the Unicode table, so that folding "ASCII only" covers
|
|
488
|
+
the whole of the equivalence class an ASCII letter belongs to rather than
|
|
489
|
+
the part of it that is ASCII: without them /k/i would miss U+212A and, the
|
|
490
|
+
sign flipped, [^k] under /i would accept it. */
|
|
491
|
+
#define RE_FOLD_LONG_S 0x017F /* to 's' */
|
|
492
|
+
#define RE_FOLD_KELVIN 0x212A /* to 'k' */
|
|
493
|
+
|
|
494
|
+
/* The types a POSIX bracket can name, as bits: [[:alpha:]] holds a character
|
|
495
|
+
whose type has RE_CTYPE_ALPHA. Every build reads them for ASCII off the
|
|
496
|
+
list in re_compile.c; above ASCII only a RE_UNICODE_CTYPE build has an
|
|
497
|
+
answer, which mrb_re_ctype() reads off re_ctype.h. [:xdigit:] and [:ascii:]
|
|
498
|
+
are sets ASCII defines and have no bit. */
|
|
499
|
+
enum re_ctype {
|
|
500
|
+
RE_CTYPE_ALPHA = 1 << 0,
|
|
501
|
+
RE_CTYPE_UPPER = 1 << 1,
|
|
502
|
+
RE_CTYPE_LOWER = 1 << 2,
|
|
503
|
+
RE_CTYPE_DIGIT = 1 << 3,
|
|
504
|
+
RE_CTYPE_ALNUM = 1 << 4,
|
|
505
|
+
RE_CTYPE_WORD = 1 << 5,
|
|
506
|
+
RE_CTYPE_PUNCT = 1 << 6,
|
|
507
|
+
RE_CTYPE_SPACE = 1 << 7,
|
|
508
|
+
RE_CTYPE_BLANK = 1 << 8,
|
|
509
|
+
RE_CTYPE_GRAPH = 1 << 9,
|
|
510
|
+
RE_CTYPE_PRINT = 1 << 10,
|
|
511
|
+
RE_CTYPE_CNTRL = 1 << 11
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
#ifdef RE_UNICODE_CTYPE
|
|
515
|
+
/* The types of a codepoint above ASCII, as the bits above. */
|
|
516
|
+
uint16_t mrb_re_ctype(uint32_t cp);
|
|
517
|
+
|
|
518
|
+
/* Whether a class holds a codepoint above ASCII, or a byte tagged
|
|
519
|
+
RE_CLASS_BYTE, through the brackets in it and the utf8_any catch-all. The
|
|
520
|
+
class matcher calls this for a class holding any bracket, once the ranges
|
|
521
|
+
have said nothing. */
|
|
522
|
+
mrb_bool mrb_re_class_ctype_match(const re_charclass *cc, uint32_t cp);
|
|
523
|
+
|
|
524
|
+
/* Whether the class says anything about a character's type at all. */
|
|
525
|
+
#define RE_CLASS_HAS_CTYPE(cc) \
|
|
526
|
+
((cc)->ctype_yes | (cc)->ctype_no | (cc)->ctype_all | (cc)->ctype_none)
|
|
527
|
+
|
|
528
|
+
/* The longest run of [lo, hi] the brackets in a class answer alike, as its
|
|
529
|
+
last codepoint, with *in the answer they give it. The table holds the types
|
|
530
|
+
as runs of codepoints sharing a set, so a span is read off it a run at a
|
|
531
|
+
time where asking codepoint by codepoint would be a search each. Both
|
|
532
|
+
bounds are above ASCII. The compiler calls this to write out the part of a
|
|
533
|
+
range list a bracket holds, which is what an intersection needs and a class
|
|
534
|
+
that only holds its brackets does not. */
|
|
535
|
+
uint32_t mrb_re_ctype_span(const re_charclass *cc, uint32_t lo, uint32_t hi, mrb_bool *in);
|
|
536
|
+
#endif
|
|
537
|
+
|
|
538
|
+
/* Simple case folding: the folded codepoint, or cp itself when it folds to
|
|
539
|
+
nothing else. With RE_UNICODE_CASE that is ASCII plus every 1:1 Unicode
|
|
540
|
+
folding, read off core's table; without it, ASCII plus the two above.
|
|
541
|
+
Neither build folds a codepoint that has no single counterpart to fold to
|
|
542
|
+
(U+FB00 to "ff"). */
|
|
543
|
+
uint32_t mrb_re_case_fold(uint32_t cp);
|
|
544
|
+
|
|
545
|
+
/* True when [lo, hi] holds a codepoint that carries case folding data this
|
|
546
|
+
build does not have. A pattern reaching one of those under /i is refused at
|
|
547
|
+
compile time, since folding ASCII and carrying on would answer wrongly: the
|
|
548
|
+
missing fold shows up as a missed match in `[X]` and, with the sign flipped,
|
|
549
|
+
as a false accept in `[^X]`. The test is having the data rather than being
|
|
550
|
+
foldable, so two kinds fall inside it that no build folds: a source whose
|
|
551
|
+
fold expands into several codepoints (U+FB00 to "ff"), and the uncased
|
|
552
|
+
neighbours the coarse ranges close over. A build with the table compiles
|
|
553
|
+
both and matches them literally, so what the two builds differ in there is
|
|
554
|
+
what they refuse rather than what they answer. A build with the data has
|
|
555
|
+
nothing to refuse, so the test compiles away there. The arguments are
|
|
556
|
+
evaluated at most once, but only by the definition that uses them, so pass
|
|
557
|
+
plain values. */
|
|
558
|
+
#ifdef RE_UNICODE_CASE
|
|
559
|
+
#define mrb_re_needs_case_data(lo, hi) FALSE
|
|
560
|
+
#else
|
|
561
|
+
mrb_bool mrb_re_needs_case_data(uint32_t lo, uint32_t hi);
|
|
562
|
+
#endif
|
|
563
|
+
|
|
564
|
+
/* Walking a table takes data only the option build has, and there the table is
|
|
565
|
+
core's: mrb_uni_case_unfold() and the two range walks beside it in
|
|
566
|
+
mruby/internal.h are what the compiler reaches for. Without the option the
|
|
567
|
+
compiler reaches the same two foldings directly, since there are only two. */
|
|
568
|
+
|
|
569
|
+
static inline int
|
|
570
|
+
mrb_re_charlen(const char *s, const char *end, mrb_bool binary)
|
|
571
|
+
{
|
|
572
|
+
/* ASCII is answered here: this is asked at every step of a search, and
|
|
573
|
+
the scan behind mrb_enc_charlen() is a call away. */
|
|
574
|
+
if (binary || (s < end && (uint8_t)*s < 0x80)) return 1;
|
|
575
|
+
return (int)mrb_enc_charlen(s, end);
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
static inline uint32_t
|
|
579
|
+
mrb_re_decode_char(const char *s, const char *end, int *len, mrb_bool binary)
|
|
580
|
+
{
|
|
581
|
+
if (binary) {
|
|
582
|
+
if (len) *len = 1;
|
|
583
|
+
return (uint8_t)*s;
|
|
584
|
+
}
|
|
585
|
+
mrb_int n;
|
|
586
|
+
uint32_t cp = mrb_enc_decode(s, end, &n);
|
|
587
|
+
if (len) *len = (int)n;
|
|
588
|
+
return cp;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
/* TRUE when s points into the middle of a character that starts earlier in
|
|
592
|
+
the string, so it is not a place a match may start at. A byte that looks
|
|
593
|
+
like a continuation byte but follows no lead byte that reaches it belongs
|
|
594
|
+
to no character and stands on its own, and there the head of the character
|
|
595
|
+
covering s is s itself. The matcher asks this at every position it tries,
|
|
596
|
+
so keep the answer for a byte that starts a character here rather than in
|
|
597
|
+
the call. */
|
|
598
|
+
static inline mrb_bool
|
|
599
|
+
mrb_re_char_interior_p(const char *str, const char *s, const char *end)
|
|
600
|
+
{
|
|
601
|
+
if (s >= end || ((uint8_t)*s & 0xC0) != 0x80) return FALSE;
|
|
602
|
+
return mrb_enc_char_head(str, s, end) != s;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/* Whether a codepoint is one of the word characters a boundary sits beside.
|
|
606
|
+
This is what `[[:word:]]` holds rather than what `\w` does: a boundary is
|
|
607
|
+
the one thing a pattern cannot spell another way, since asking for one
|
|
608
|
+
around any script takes a lookaround either side of the position, where a
|
|
609
|
+
class only takes the bracket written out. So the shorthand keeps the ASCII
|
|
610
|
+
set CRuby gives it, and the boundary reads every script, as CRuby's does.
|
|
611
|
+
A build with no table has no answer above ASCII, and there the boundary
|
|
612
|
+
reads as `[[:word:]]` does on it: the ASCII word characters and no more. */
|
|
613
|
+
static inline mrb_bool
|
|
614
|
+
mrb_re_word_cp(uint32_t cp)
|
|
615
|
+
{
|
|
616
|
+
if (cp < 128) return mrb_re_is_word_char(cp);
|
|
617
|
+
#ifdef RE_UNICODE_CTYPE
|
|
618
|
+
return (mrb_re_ctype(cp) & RE_CTYPE_WORD) != 0;
|
|
619
|
+
#else
|
|
620
|
+
return FALSE;
|
|
621
|
+
#endif
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/* Whether the character starting at `s` is a word character.
|
|
625
|
+
|
|
626
|
+
A byte below 0x80 is its own character whatever the subject is, and it is
|
|
627
|
+
what almost every boundary in almost every subject sits beside, so it is
|
|
628
|
+
answered without decoding. A binary subject holds bytes rather than
|
|
629
|
+
characters, and a byte at or above 0x80 stands for no character there: the
|
|
630
|
+
table must not be asked about it, or the lone byte 0xB5 would be the word
|
|
631
|
+
character µ rather than the byte it is. */
|
|
632
|
+
static inline mrb_bool
|
|
633
|
+
mrb_re_word_at(const char *s, const char *end, mrb_bool binary)
|
|
634
|
+
{
|
|
635
|
+
uint8_t b = (uint8_t)*s;
|
|
636
|
+
if (b < 0x80) return mrb_re_is_word_char(b);
|
|
637
|
+
if (binary) return FALSE;
|
|
638
|
+
return mrb_re_word_cp(mrb_re_decode_char(s, end, NULL, FALSE));
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/* Whether the character ending at `s` is one. Reading it takes the head of
|
|
642
|
+
the character the byte before belongs to, which is the one step the engine
|
|
643
|
+
ever takes backward. */
|
|
644
|
+
static inline mrb_bool
|
|
645
|
+
mrb_re_word_before(const char *str, const char *s, const char *end, mrb_bool binary)
|
|
646
|
+
{
|
|
647
|
+
uint8_t b = (uint8_t)s[-1];
|
|
648
|
+
if (b < 0x80) return mrb_re_is_word_char(b);
|
|
649
|
+
if (binary) return FALSE;
|
|
650
|
+
const char *head = mrb_enc_char_head(str, s - 1, end);
|
|
651
|
+
return mrb_re_word_cp(mrb_re_decode_char(head, end, NULL, FALSE));
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/* Execute a match.
|
|
655
|
+
Returns number of captures filled (0 = no match), or RE_OVER_*_LIMIT with
|
|
656
|
+
nothing in `captures` to read.
|
|
657
|
+
captures[2*n] = start, captures[2*n+1] = end for group n. */
|
|
658
|
+
int mrb_re_exec(mrb_state *mrb, const mrb_regexp_pattern *pat,
|
|
659
|
+
const char *str, mrb_int len, mrb_int start,
|
|
660
|
+
int *captures, int captures_size, mrb_bool binary);
|
|
661
|
+
|
|
662
|
+
/* Execute a match backward: the last match that starts at or before `limit`.
|
|
663
|
+
Answers as mrb_re_exec() does, and clears the capture buffer itself before
|
|
664
|
+
each of the searches it makes, having to make more than one. */
|
|
665
|
+
int mrb_re_rexec(mrb_state *mrb, const mrb_regexp_pattern *pat,
|
|
666
|
+
const char *str, mrb_int len, mrb_int limit,
|
|
667
|
+
int *captures, int captures_size, mrb_bool binary);
|
|
668
|
+
|
|
669
|
+
MRB_END_DECL
|
|
670
|
+
|
|
671
|
+
#endif /* MRB_RE_INTERNAL_H */
|