Commit 1eaa5d48

1eaa5d48ad81Merged

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

Description

Merge PR #649: regex back-reference $+ returns last paren match

feat(codegen,runtime): regex back-reference $+ returns last paren match

Details

Commit
1eaa5d48ad81
Parent
c360ff731f28
Merge parent
478826f8b70a

lib/sp_runtime.hmodifiedView file

@@ -1393,6 +1393,19 @@
13931393 sp_mark_fiber_root_storage();
13941394 }
13951395
1396+/* `$+` / `$LAST_PAREN_MATCH` — contents of the highest-indexed group
1397+ that participated in the match. Walks sp_re_captures[] from 9 down
1398+ and returns the first non-NULL entry. NULL when no group matched
1399+ (codegen ternary falls back to ""). Matches CRuby's behaviour:
1400+ for /(a)(b)?/ matching "a", $+ is "a"; for /(a)(b)/ matching "ab",
1401+ $+ is "b". */
1402+static const char *sp_re_last_paren_match(void) {
1403+ for (int i = 9; i >= 1; i--) {
1404+ if (sp_re_captures[i]) return sp_re_captures[i];
1405+ }
1406+ return NULL;
1407+}
1408+
13961409 static void sp_re_set_captures(const char *str, int *caps, int ncaps) {
13971410 sp_re_last_str = str;
13981411 for (int i = 0; i < 10; i++) sp_re_captures[i] = NULL;