Commit 85fb3dce

85fb3dce7143Merged

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

Description

analyze,codegen,runtime: String range to_a — single-char ASCII

`("a".."c").to_a` previously emitted `sp_IntArray_from_range` with string-pointer args, failing C compilation. Add a str_array materialiser for single-char ASCII string ranges (the common subset shape). Multi-char string ranges need full String#succ with cross-position carry and fall through to the int path that fails to compile, surfacing the gap.

  • runtime: sp_StrArray_from_string_range walks lo..hi as ASCII chars
  • codegen: compile_range_method_expr's to_a arm + compile_to_a_range_expr detect StringNode endpoints and route to the new helper
  • analyze: range.to_a typed as str_array when the literal endpoints are StringNodes (walks through ParenthesesNode)

Refs #981 (single-char ASCII only; multi-char succ deferred)

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

Details

Commit
85fb3dce7143
Parent
9b245eb2db6e

lib/sp_runtime.hmodifiedView file

@@ -1207,6 +1207,10 @@
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;}
12101214 /* See sp_IntArray_slice_range -- same shape, issue #496. */
12111215 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);}
12121216 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;}