Commit a440e996
a440e996c51cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
regex: honour \NNN octal and \xHH hex escapes
/\033/ and /\x1b/ silently fell through to literal-character emit:
\033 was parsed as three RE_CHAR ops for '0', '3', '3', so a regex
intended to match ESC matched only the literal substring "033". Added
octal (1-3 digits, value 0-255) and hex (1-2 digits) handling to
parse_escape. \1-\9 backref dispatch in the top-level path is unchanged
so digit-leading escapes there still work as backrefs.
The mruby vendored copy has the same gap; this change applies only to the spinel-local copy.
Fixes #1009
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- a440e996c51c
- Parent
- 3af683552685
lib/regexp/re_compile.cmodifiedView file
@@ -250,6 +250,43 @@
250250
this arm only fires from read_class_atom -- i.e. always the251251
character-class meaning. Issue #632. */252252
case 'b': return 0x08;253+
/* Octal escape `\NNN` (1-3 digits, value 0-255). The outer dispatcher254+
consumes `\1`-`\9` as backref, so the only octal-leading digit255+
that reaches here from the top level is `\0` -- but parse_escape256+
also fires from read_class_atom inside `[...]`, where backref257+
parsing does not apply, so the full 0-7 range needs handling.258+
Issue #1009. */259+
case '0': case '1': case '2': case '3':260+
case '4': case '5': case '6': case '7': {261+
int val = ch - '0';262+
int n = 1;263+
while (n < 3) {264+
int d = peek(c);265+
if (d < '0' || d > '7') break;266+
val = val * 8 + (d - '0');267+
next_char(c);268+
n++;269+
}270+
return val & 0xff;271+
}272+
/* Hex escape `\xHH` (1-2 hex digits, value 0-255). Spinel does not273+
yet implement the `\x{HHHH}` form for codepoints above 0xff. */274+
case 'x': {275+
int val = 0;276+
int n = 0;277+
while (n < 2) {278+
int d = peek(c);279+
int v;280+
if (d >= '0' && d <= '9') v = d - '0';281+
else if (d >= 'a' && d <= 'f') v = d - 'a' + 10;282+
else if (d >= 'A' && d <= 'F') v = d - 'A' + 10;283+
else break;284+
val = val * 16 + v;285+
next_char(c);286+
n++;287+
}288+
return val & 0xff;289+
}253290
default: return ch; /* literal: \., \\, \/, \(, etc. */254291
}255292
}test/i1009.rbaddedView file
Loading diff…
test/i1009.rb.expectedaddedView file
Loading diff…