Commit 2c053fc7

2c053fc7f248Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · ~5 modified

Description

fix(analyze,codegen,runtime): Regexp.escape / Regexp.quote with single-string arg

Implements Ruby's Regexp.escape(s) / Regexp.quote(s) so a caller can build a Regexp.new(...) whose pattern matches the input bytes literally. The classic use site is the assert_select shape: Regexp.new("<a href=\"" + Regexp.escape(href) + "\">").

The runtime helper sp_re_escape prefixes regex metachars (\\ . ? * + ^ $ | ( ) [ ] { } # -) and ASCII whitespace ( \\t \\n \\r \\f \\v) with a single backslash, returning the heap-tracked string. Inputs with no escapable chars pass through unchanged (no alloc). The analyzer arm in infer_constant_recv_type types the call as "string"; the codegen arm in compile_constant_recv_expr routes to sp_re_escape. The 0-arg or multi-arg form falls through to unresolved-call so misuse is loud.

Extends the existing test fixture to exercise:

  • Regexp.escape / .quote roundtripping through Regexp.new
  • =~ returning Integer|nil (the prior commit's CRuby fix)
  • $~ / $& / $\`` / $'` regex match globals

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

Details

Commit
2c053fc7f248
Parent
82d5292a9f99

lib/sp_runtime.hmodifiedView file

@@ -1372,6 +1372,53 @@
13721372 (returning -1) stays available for internal callers needing the
13731373 sentinel form. */
13741374 static sp_RbVal sp_re_match_poly(mrb_regexp_pattern *pat, const char *str) { mrb_int n = sp_re_match(pat, str); return n < 0 ? sp_box_nil() : sp_box_int(n); }
1375+
1376+/* Regexp.escape(s) / Regexp.quote(s) -- prefix every regex metachar
1377+ and whitespace byte with a single backslash, returning a heap
1378+ string that callers can feed into `Regexp.new(...)` to match the
1379+ original bytes literally. Matches CRuby's rb_reg_quote for the
1380+ ASCII range (the only range Spinel's regex engine indexes today,
1381+ so multibyte passes through unchanged).
1382+
1383+ The metachars covered: \\ . ? * + ^ $ | ( ) [ ] { } # -
1384+ The whitespace covered: ' ' \t \n \r \f \v
1385+ Everything else copies byte-for-byte. */
1386+static const char *sp_re_escape(const char *src) {
1387+ size_t i, in_len = strlen(src);
1388+ size_t out_len = 0;
1389+ for (i = 0; i < in_len; i++) {
1390+ unsigned char c = (unsigned char)src[i];
1391+ if (c == '\\' || c == '.' || c == '?' || c == '*' || c == '+' ||
1392+ c == '^' || c == '$' || c == '|' || c == '(' || c == ')' ||
1393+ c == '[' || c == ']' || c == '{' || c == '}' || c == '#' ||
1394+ c == '-' || c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
1395+ c == '\f' || c == '\v') {
1396+ out_len += 2;
1397+ } else {
1398+ out_len += 1;
1399+ }
1400+ }
1401+ if (out_len == in_len) {
1402+ return src;
1403+ }
1404+ char *buf = sp_str_alloc(out_len);
1405+ size_t j = 0;
1406+ for (i = 0; i < in_len; i++) {
1407+ unsigned char c = (unsigned char)src[i];
1408+ if (c == '\\' || c == '.' || c == '?' || c == '*' || c == '+' ||
1409+ c == '^' || c == '$' || c == '|' || c == '(' || c == ')' ||
1410+ c == '[' || c == ']' || c == '{' || c == '}' || c == '#' ||
1411+ c == '-' || c == ' ' || c == '\t' || c == '\n' || c == '\r' ||
1412+ c == '\f' || c == '\v') {
1413+ buf[j++] = '\\';
1414+ buf[j++] = (char)c;
1415+ } else {
1416+ buf[j++] = (char)c;
1417+ }
1418+ }
1419+ buf[j] = 0;
1420+ return buf;
1421+}
13751422 static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }
13761423 /* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative
13771424 cls_id so the dispatch path is uniform. */