Commit fcba3098

fcba30981477Merged

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

Description

String#sub/gsub with a hash replacement

sub/gsub with a regex (or string for sub) pattern and a str_str_hash replacement route to the *_str_str_hash runtime helpers, substituting the matched substring's hash value (empty string for a missing key).

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

Details

Commit
fcba30981477
Parent
448eff1fcaa7

src/codegen.cmodifiedView file

@@ -2396,7 +2396,8 @@
23962396 if (rt == TY_STRING) {
23972397 /* string methods taking a regex-literal argument route to the engine */
23982398 if ((!strcmp(name, "gsub") || !strcmp(name, "sub")) && argc == 2 && re_lit_index(c, argv[0]) >= 0) {
2399- buf_printf(b, "sp_re_%s(sp_re_pat_%d, %s, ", name, re_lit_index(c, argv[0]), r);
2399+ const char *suf = comp_ntype(c, argv[1]) == TY_STR_STR_HASH ? "_str_str_hash" : "";
2400+ buf_printf(b, "sp_re_%s%s(sp_re_pat_%d, %s, ", name, suf, re_lit_index(c, argv[0]), r);
24002401 emit_expr(c, argv[1], b); buf_puts(b, ")");
24012402 }
24022403 else if (!strcmp(name, "split") && argc == 1 && re_lit_index(c, argv[0]) >= 0) {
@@ -2515,6 +2516,9 @@
25152516 else if (!strcmp(name, "gsub") && argc == 2) {
25162517 buf_printf(b, "sp_str_gsub(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ", "); emit_expr(c, argv[1], b); buf_puts(b, ")");
25172518 }
2519+ else if (!strcmp(name, "sub") && argc == 2 && comp_ntype(c, argv[1]) == TY_STR_STR_HASH) {
2520+ buf_printf(b, "sp_str_sub_str_str_hash(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ", "); emit_expr(c, argv[1], b); buf_puts(b, ")");
2521+ }
25182522 else if (!strcmp(name, "sub") && argc == 2) {
25192523 buf_printf(b, "sp_str_sub(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ", "); emit_expr(c, argv[1], b); buf_puts(b, ")");
25202524 }