Commit 65250c0a

65250c0ad119Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +1 added · ~3 modified

Description

fix(regexp): support UTF-8 codepoints in [...] character classes

Stage 2 of issue #61. The character class implementation kept only an ASCII bitmap (if (ch < 128) class_set_bit), so non-ASCII chars in [...] were silently dropped at compile time and the class matched nothing.

Changes:

  • re_charclass grows a (ranges, num_ranges, range_capa) tail. Each entry is an inclusive [lo, hi] codepoint pair.
  • compile_charclass reads class atoms via a new read_class_atom that decodes a full UTF-8 codepoint when the byte is >= 0xC0. Single codepoints store as [cp, cp]; literal ranges (α-ω, [a-z₀-₉]) store the spans directly. Mixed ASCII/non-ASCII ranges fall into the codepoint list (the bitmap covers ASCII only).
  • class_match takes a uint32_t codepoint. Sub-128 hits the bitmap;

    = 128 walks the range list, falling back to the existing utf8_any flag (used by negated shorthand like \D).

  • re_exec decodes the input UTF-8 codepoint once per outer step and passes it to class_match for both engines (Pike VM and the backtracker used by lookbehind).
  • Outer loop skips UTF-8 continuation bytes (0x80-0xBF) when seeding a new match attempt; without this, [^…] matches the orphaned continuation byte position inside a multi-byte char.
  • first_set_walk bails out when the class has any non-ASCII range (the prefix-byte optimization can't represent codepoint sets).
  • re_free releases per-class ranges.

test/regex_utf8_charclass.rb covers the issue's exact form ([₀₁₂₃₄₅₆₇₈₉]+), explicit codepoint ranges, negation, mixed ASCII/non-ASCII, and the non-matching cases.

Refs #61

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
65250c0ad119
Parent
fdceb0c5ecd8

lib/regexp/re_compile.cmodifiedView file

@@ -122,7 +122,27 @@
122122 }
123123 }
124124
125+/* Append a non-ASCII codepoint range [lo, hi]. Both bounds must be >= 128. */
125126 static void
127+class_add_range(re_charclass *cc, uint32_t lo, uint32_t hi)
128+{
129+ if (cc->num_ranges >= cc->range_capa) {
130+ cc->range_capa = cc->range_capa ? cc->range_capa * 2 : 4;
131+ cc->ranges = (uint32_t*)realloc(cc->ranges, sizeof(uint32_t) * 2 * cc->range_capa);
132+ }
133+ cc->ranges[2 * cc->num_ranges] = lo;
134+ cc->ranges[2 * cc->num_ranges + 1] = hi;
135+ cc->num_ranges++;
136+}
137+
138+/* Add a single non-ASCII codepoint to the class. */
139+static void
140+class_add_codepoint(re_charclass *cc, uint32_t cp)
141+{
142+ class_add_range(cc, cp, cp);
143+}
144+
145+static void
126146 class_set_range(re_charclass *cc, uint8_t lo, uint8_t hi)
127147 {
128148 for (int i = lo; i <= hi; i++) {
@@ -189,6 +209,29 @@
189209 }
190210 }
191211
212+/* Read one character class atom: either an ASCII byte (0-127), a
213+ `\escape`, or a full multi-byte UTF-8 codepoint. Returns the
214+ codepoint and advances c->p. */
215+static uint32_t
216+read_class_atom(re_compiler *c)
217+{
218+ if (peek(c) == '\\') {
219+ next_char(c);
220+ return (uint32_t)parse_escape(c);
221+ }
222+ uint8_t b = (uint8_t)*c->p;
223+ if (b < 0xC0) {
224+ /* ASCII or stray continuation byte. */
225+ return (uint32_t)next_char(c);
226+ }
227+ /* Multi-byte UTF-8 leader: decode the full codepoint. */
228+ int len = 0;
229+ uint32_t cp = re_utf8_decode(c->p, &len);
230+ if (c->p + len > c->src_end) len = (int)(c->src_end - c->p);
231+ c->p += len;
232+ return cp;
233+}
234+
192235 /* Parse [...] character class */
193236 static void
194237 compile_charclass(re_compiler *c)
@@ -204,42 +247,42 @@
204247
205248 mrb_bool first = TRUE;
206249 while (peek(c) != ']' || first) {
207- int ch;
208-
209250 if (peek(c) < 0) compile_error(c, "unterminated character class");
210251 first = FALSE;
211252
253+ /* Shorthand classes (\d, \D, \w, \W, \s, \S) are handled before
254+ the codepoint-aware path so the single-byte semantics stay
255+ intact. */
212256 if (peek(c) == '\\') {
213- next_char(c);
214- int esc = peek(c);
257+ int esc = (c->p + 1 < c->src_end) ? (uint8_t)c->p[1] : -1;
215258 if (esc == 'd' || esc == 'D' || esc == 'w' || esc == 'W' || esc == 's' || esc == 'S') {
216- next_char(c);
259+ next_char(c); /* '\\' */
260+ next_char(c); /* spec */
217261 class_add_shorthand(cc, esc);
218262 continue;
219263 }
220- ch = parse_escape(c);
221264 }
222- else {
223- ch = next_char(c);
224- }
225265
226- /* check for range a-z */
266+ uint32_t cp = read_class_atom(c);
267+
268+ /* check for range a-z (or U+xxxx-U+yyyy) */
227269 if (peek(c) == '-' && c->p + 1 < c->src_end && c->p[1] != ']') {
228270 next_char(c); /* skip '-' */
229- int hi;
230- if (peek(c) == '\\') {
231- next_char(c);
232- hi = parse_escape(c);
271+ uint32_t hi = read_class_atom(c);
272+ if (cp < 128 && hi < 128) {
273+ class_set_range(cc, (uint8_t)cp, (uint8_t)hi);
233274 }
234275 else {
235- hi = next_char(c);
236- }
237- if (ch < 128 && hi < 128) {
238- class_set_range(cc, (uint8_t)ch, (uint8_t)hi);
276+ /* Range that touches non-ASCII: store as codepoint range.
277+ Mixed ASCII/non-ASCII ranges are rare; stash the whole
278+ span in the codepoint list (the bitmap covers ASCII only,
279+ so a non-ASCII upper bound forces the codepoint path). */
280+ if (cp <= hi) class_add_range(cc, cp, hi);
239281 }
240282 }
241283 else {
242- if (ch < 128) class_set_bit(cc, (uint8_t)ch);
284+ if (cp < 128) class_set_bit(cc, (uint8_t)cp);
285+ else class_add_codepoint(cc, cp);
243286 }
244287 }
245288 next_char(c); /* skip ']' */
@@ -771,6 +814,7 @@
771814 const re_charclass *cc = &classes[code[pc].a];
772815 for (int i = 0; i < 16; i++) bm[i] |= cc->bitmap[i];
773816 if (cc->utf8_any) return FALSE; /* non-ASCII possible */
817+ if (cc->num_ranges > 0) return FALSE; /* non-ASCII codepoints possible */
774818 return TRUE;
775819 }
776820 case RE_NCLASS: {
@@ -919,7 +963,12 @@
919963 {
920964 if (pat) {
921965 free(pat->code);
922- free(pat->classes);
966+ if (pat->classes) {
967+ for (uint16_t i = 0; i < pat->num_classes; i++) {
968+ free(pat->classes[i].ranges);
969+ }
970+ free(pat->classes);
971+ }
923972 free(pat->named_captures);
924973 free(pat->prefix);
925974 free(pat->cached_visited);