Commit 5599c98e

5599c98e9229Merged

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

Description

runtime: multi-char String range to_a via sp_str_succ

("aa".."ac").to_a returned [] — sp_StrArray_from_string_range only handled single-char ranges and bailed early when either endpoint was longer than one byte. Forward-declare the existing sp_str_succ (it lives further down in the file because of the UTF-8 helpers it uses) and loop with strcmp + succ until we pass the end. 4096-iteration cap guards against pathological prepend cases.

Fixes #981

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
5599c98e9229
Parent
2cf55b9a876a

lib/sp_runtime.hmodifiedView file

@@ -1207,10 +1207,31 @@
12071207 * length-clamping semantics as sp_IntArray_slice. Out-of-bounds start
12081208 * returns an empty StrArray (we don't have a nullable form). */
12091209 static sp_StrArray*sp_StrArray_slice(sp_StrArray*a,mrb_int start,mrb_int len){if(start<0)start+=a->len;if(start<0)start=0;sp_StrArray*b=sp_StrArray_new();if(start>=a->len||len<=0)return b;if(start+len>a->len)len=a->len-start;for(mrb_int i=0;i<len;i++)sp_StrArray_push(b,a->data[start+i]);return b;}
1210-/* String range to_a — single-char ASCII only. CRuby's String#succ
1211- handles multi-char + carry; the subset surfaces multi-char cases
1212- via the C-side int-from-range fallback failing to compile. */
1213-static sp_StrArray*sp_StrArray_from_string_range(const char*s,const char*e,mrb_int excl){sp_StrArray*a=sp_StrArray_new();if(!s||!e||strlen(s)!=1||strlen(e)!=1)return a;char lo=s[0],hi=e[0];if(excl)hi--;for(char c=lo;c<=hi;c++){char*r=sp_str_alloc(1);r[0]=c;sp_StrArray_push(a,r);}return a;}
1210+/* Forward decl — sp_str_succ is defined further down (it uses
1211+ sp_utf8_decode); the StrArray_from_string_range loop below needs
1212+ it visible early. */
1213+static const char *sp_str_succ(const char *s);
1214+/* String range to_a — single-char and multi-char ASCII ranges via
1215+ sp_str_succ. The 4096-iteration cap stops a pathological prepend-
1216+ style infinite loop before it eats memory. */
1217+static sp_StrArray *sp_StrArray_from_string_range(const char *s, const char *e, mrb_int excl) {
1218+ sp_StrArray *a = sp_StrArray_new();
1219+ if (!s || !e) return a;
1220+ const char *cur = s;
1221+ int iters = 0;
1222+ while (iters < 4096) {
1223+ int cmp = strcmp(cur, e);
1224+ if (cmp > 0) break;
1225+ if (cmp == 0 && excl) break;
1226+ char *copy = sp_str_alloc(strlen(cur));
1227+ strcpy(copy, cur);
1228+ sp_StrArray_push(a, copy);
1229+ if (cmp == 0) break;
1230+ cur = sp_str_succ(cur);
1231+ iters++;
1232+ }
1233+ return a;
1234+}
12141235 /* See sp_IntArray_slice_range -- same shape, issue #496. */
12151236 static sp_StrArray*sp_StrArray_slice_range(sp_StrArray*a,mrb_int start,mrb_int end_,mrb_int excl){if(end_<0)end_+=a->len;if(start<0)start+=a->len;mrb_int n=end_-start+(excl?0:1);if(n<0||start<0)n=0;return sp_StrArray_slice(a,start,n);}
12161237 static inline void sp_StrArray_set(sp_StrArray*a,mrb_int i,const char*v){if(!a)return;mrb_int orig=i;if(i<0)i+=a->len;if(i<0)sp_raise_cls("IndexError",sp_sprintf("index %lld too small for array; minimum: %lld",(long long)orig,(long long)-a->len));while(i>=a->len)sp_StrArray_push(a,sp_str_empty);a->data[i]=v;}