Commit af0646bb
af0646bb4023Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
String#partition with a regex
partition(/re/) returns [before, match, after] from the first regex match (via sp_re_match's pre/match/post globals), or [s, "", ""] on no match.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- af0646bb4023
- Parent
- de54bbb3903d
src/codegen.cmodifiedView file
@@ -2512,6 +2512,17 @@
25122512
re_lit_index(c, argv[0]) < 0) {25132513
buf_printf(b, "sp_str_%s(%s, ", name, r); emit_expr(c, argv[0], b); buf_puts(b, ")");25142514
}2515+
else if (!strcmp(name, "partition") && argc == 1 && re_lit_index(c, argv[0]) >= 0) {2516+
/* [before, match, after] from the first regex match, else [s, "", ""] */2517+
int tr = ++g_tmp;2518+
buf_printf(b, "({ sp_StrArray *_t%d = sp_StrArray_new();"2519+
" if (sp_re_match(sp_re_pat_%d, %s) >= 0) {"2520+
" sp_StrArray_push(_t%d, sp_re_match_pre); sp_StrArray_push(_t%d, sp_re_match_str);"2521+
" sp_StrArray_push(_t%d, sp_re_match_post); } else {"2522+
" sp_StrArray_push(_t%d, %s); sp_StrArray_push(_t%d, SPL(\"\")); sp_StrArray_push(_t%d, SPL(\"\")); }"2523+
" _t%d; })",2524+
tr, re_lit_index(c, argv[0]), r, tr, tr, tr, tr, r, tr, tr, tr);2525+
}25152526
else if (!strcmp(name, "rindex") && argc == 1) { buf_printf(b, "sp_str_rindex(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }25162527
else if (!strcmp(name, "crypt") && argc == 1) { buf_printf(b, "sp_str_crypt(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }25172528
else if (!strcmp(name, "scrub") && argc == 0) buf_printf(b, "sp_str_scrub(%s, 0)", r);