Commit 31422a7e

31422a7e70afMerged

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

Description

codegen: add String#index(str,offset) and fix String#rindex to use _opt sentinel

  • Add 2-arg String#index(sub, start) emitting sp_str_index_from_opt, consistent with the 1-arg form's nullable-int return convention.
  • Fix String#rindex(sub) to emit sp_str_rindex_opt (SP_INT_NIL on miss) instead of sp_str_rindex (-1 on miss), so .inspect / .nil? / == nil work correctly on the result.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
31422a7e70af
Parent
3ef414b9109f

src/codegen.cmodifiedView file

@@ -7213,6 +7213,11 @@
72137213 /* nil-on-miss carried as the SP_INT_NIL sentinel (a nullable int) */
72147214 buf_printf(b, "sp_str_index_opt(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")");
72157215 }
7216+ else if (!strcmp(name, "index") && argc == 2) {
7217+ buf_printf(b, "sp_str_index_from_opt(%s, ", r);
7218+ emit_expr(c, argv[0], b); buf_puts(b, ", ");
7219+ emit_expr(c, argv[1], b); buf_puts(b, ")");
7220+ }
72167221 else if ((!strcmp(name, "partition") || !strcmp(name, "rpartition")) && argc == 1 &&
72177222 re_lit_index(c, argv[0]) < 0) {
72187223 buf_printf(b, "sp_str_%s(%s, ", name, r); emit_expr(c, argv[0], b); buf_puts(b, ")");
@@ -7231,7 +7236,7 @@
72317236 else if (!strcmp(name, "rpartition") && argc == 1 && re_lit_index(c, argv[0]) >= 0) {
72327237 buf_printf(b, "sp_re_rpartition(sp_re_pat_%d, %s)", re_lit_index(c, argv[0]), r);
72337238 }
7234- else if (!strcmp(name, "rindex") && argc == 1) { buf_printf(b, "sp_str_rindex(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
7239+ else if (!strcmp(name, "rindex") && argc == 1) { buf_printf(b, "sp_str_rindex_opt(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
72357240 else if (!strcmp(name, "rindex") && argc == 2) { buf_printf(b, "sp_str_rindex_from(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ", "); emit_expr(c, argv[1], b); buf_puts(b, ")"); }
72367241 else if (!strcmp(name, "crypt") && argc == 1) { buf_printf(b, "sp_str_crypt(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")"); }
72377242 else if (!strcmp(name, "scrub") && argc == 0) buf_printf(b, "sp_str_scrub(%s, 0)", r);