Commit 236f0f17
236f0f17387dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
Add string chomp(sep) and slicing (s[a..b], s[start,len])
- chomp(arg) -> sp_str_chomp_sep (paragraph mode chomp("") and explicit separators); fixes str_chomp_paragraph_mode.
- s[a..b] / s[a...b] -> sp_str_sub_range_r (range flags bit 4 = exclusive); s[start, len] -> sp_str_sub_range; single-int [] stays char-at.
make test: 24 (up from 22).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 236f0f17387d
- Parent
- 7222d5888da6
src/codegen.cmodifiedView file
@@ -727,6 +727,9 @@
727727
else if (!strcmp(name, "strip")) buf_printf(b, "sp_str_strip(%s)", r);728728
else if (!strcmp(name, "lstrip")) buf_printf(b, "sp_str_lstrip(%s)", r);729729
else if (!strcmp(name, "rstrip")) buf_printf(b, "sp_str_rstrip(%s)", r);730+
else if (!strcmp(name, "chomp") && argc == 1) {731+
buf_printf(b, "sp_str_chomp_sep(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")");732+
}730733
else if (!strcmp(name, "chomp")) buf_printf(b, "sp_str_chomp(%s)", r);731734
else if (!strcmp(name, "chop")) buf_printf(b, "sp_str_chop(%s)", r);732735
else if (!strcmp(name, "to_s") || !strcmp(name, "to_str") || !strcmp(name, "dup")) buf_puts(b, r);@@ -744,6 +747,21 @@
744747
else if (!strcmp(name, "index") && argc == 1) {745748
buf_printf(b, "sp_str_index(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")");746749
}750+
else if (!strcmp(name, "[]") && argc == 1 && nt_type(c->nt, argv[0]) &&751+
!strcmp(nt_type(c->nt, argv[0]), "RangeNode")) {752+
/* s[a..b] / s[a...b] */753+
int rn = argv[0];754+
int excl = (int)(nt_int(c->nt, rn, "flags", 0) & 4) ? 1 : 0;755+
buf_printf(b, "sp_str_sub_range_r(%s, ", r);756+
emit_expr(c, nt_ref(c->nt, rn, "left"), b); buf_puts(b, ", ");757+
emit_expr(c, nt_ref(c->nt, rn, "right"), b);758+
buf_printf(b, ", %d)", excl);759+
}760+
else if (!strcmp(name, "[]") && argc == 2) {761+
/* s[start, len] */762+
buf_printf(b, "sp_str_sub_range(%s, ", r);763+
emit_expr(c, argv[0], b); buf_puts(b, ", "); emit_expr(c, argv[1], b); buf_puts(b, ")");764+
}747765
else if (!strcmp(name, "[]") && argc == 1) {748766
buf_printf(b, "sp_str_char_at_or_nil(%s, ", r); emit_expr(c, argv[0], b); buf_puts(b, ")");749767
}