Commit 41412c72

41412c72b60aMerged

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

Description

String methods: ascii_only?, valid_encoding?, lines(chomp:), scan captures, squeeze/delete multi-arg, string range splat

  • Add ascii_only? and valid_encoding? dispatch (TY_BOOL return)
  • Handle lines(chomp: true) and each_line(chomp: true) with sp_str_lines_chomp
  • Add codepoints non-block form -> sp_str_codepoints
  • Add delete with multiple args -> sp_str_delete_n
  • Add squeeze with multiple args -> sp_str_squeeze_n
  • Add scan with capture groups -> sp_re_scan_poly (TY_POLY_ARRAY)
  • Fix [*"a".."e"] string range splat using sp_StrArray_from_string_range

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

Details

Commit
41412c72b60a
Parent
d9c41d3f3abe

src/analyze.cmodifiedView file

@@ -4,6 +4,15 @@
44 #include <stdlib.h>
55 #include <string.h>
66
7+static int re_has_captures(const char *src) {
8+ if (!src) return 0;
9+ for (const char *p = src; *p; p++) {
10+ if (*p == '\\') { if (p[1]) p++; continue; }
11+ if (*p == '(' && p[1] != '?') return 1;
12+ }
13+ return 0;
14+}
15+
716 /* ---- operator classification ---- */
817
918 static int str_in(const char *s, const char *const *set) {
@@ -982,13 +991,22 @@
982991 if (!strcmp(name, "scrub") || !strcmp(name, "crypt")) return TY_STRING;
983992 if (!strcmp(name, "rindex")) return TY_INT;
984993 if (!strcmp(name, "partition") || !strcmp(name, "rpartition")) return TY_STR_ARRAY;
985- if (!strcmp(name, "casecmp?")) return TY_BOOL;
994+ if (!strcmp(name, "casecmp?") || !strcmp(name, "ascii_only?") || !strcmp(name, "valid_encoding?")) return TY_BOOL;
986995 if (!strcmp(name, "to_f")) return TY_FLOAT;
987996 if (!strcmp(name, "each_char") || !strcmp(name, "each_line") || !strcmp(name, "each_byte")) return TY_STRING;
988997 { int blk = nt_ref(nt, id, "block");
989998 if (blk >= 0 && (!strcmp(name, "chars") || !strcmp(name, "lines"))) return TY_STRING;
990999 if (blk >= 0 && (!strcmp(name, "bytes") || !strcmp(name, "codepoints"))) return TY_STRING; }
991- if (!strcmp(name, "split") || !strcmp(name, "lines") || !strcmp(name, "scan")) return TY_STR_ARRAY;
1000+ if (!strcmp(name, "split") || !strcmp(name, "lines")) return TY_STR_ARRAY;
1001+ if (!strcmp(name, "scan") && argc == 1) {
1002+ /* scan with capture groups returns poly_array (array of arrays or strings) */
1003+ const char *aty = nt_type(nt, argv[0]);
1004+ if (aty && !strcmp(aty, "RegularExpressionNode")) {
1005+ const char *src = nt_str(nt, argv[0], "unescaped");
1006+ if (src && re_has_captures(src)) return TY_POLY_ARRAY;
1007+ }
1008+ return TY_STR_ARRAY;
1009+ }
9921010 if (!strcmp(name, "upto") && argc == 1) return TY_STR_ARRAY; /* blockless: materialized sequence */
9931011 if (!strcmp(name, "bytes") || !strcmp(name, "codepoints")) return TY_INT_ARRAY;
9941012 if (!strcmp(name, "unpack") && argc == 1) return TY_POLY_ARRAY;