Commit 0c21b9c7

0c21b9c76a0aMerged

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

Description

Fix top-level \D / \W / \S double-negation in the regex engine

class_add_shorthand already builds the directly-matching set for every shorthand -- positive for \d \w \s, the explicit complement (plus utf8_any) for \D \W \S -- but the top-level dispatcher routed the uppercase forms through RE_NCLASS, negating the complement a second time. So "5" =~ /\D/ matched (CRuby: nil) and "x" =~ /\D/ failed (CRuby: 0). The [...] path has no NCLASS wrapper and was already correct.

Emit RE_CLASS for all six shorthands at top level (mirroring the \h/\H arm). The compiler sources use no top-level negated shorthand, so this is self-host safe.

Closes #1370

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

Details

Commit
0c21b9c76a0a
Parent
da8e9147f743

lib/regexp/re_compile.cmodifiedView file

@@ -678,7 +678,14 @@
678678 next_char(c);
679679 uint16_t id = add_class(c);
680680 class_add_shorthand(&c->classes[id], ch);
681- emit(c, (ch >= 'A' && ch <= 'Z') ? RE_NCLASS : RE_CLASS, (uint8_t)id, 0);
681+ /* class_add_shorthand already builds the directly-matching set for
682+ every shorthand -- positive for d/w/s, the explicit complement
683+ (plus utf8_any) for D/W/S -- so emit RE_CLASS for all of them.
684+ The old `uppercase -> RE_NCLASS` route negated the complement a
685+ second time, so top-level \D/\W/\S matched exactly the set they
686+ should reject. The `[...]` path was unaffected (no NCLASS wrapper)
687+ and stays correct. Mirrors the \h/\H arm below. */
688+ emit(c, RE_CLASS, (uint8_t)id, 0);
682689 }
683690 else if (ch == 'h' || ch == 'H') {
684691 /* \h / \H both carry their full positive set (hex digits /