Commit 26c7367a
26c7367ad43aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 3 files · +1 added · ~2 modified
Description
fix(codegen): StrArray slicing via a[range] and a[start, len]
Same fix as the IntArray slice support, applied to StrArray. Detect RangeNode and 2-arg subscript at codegen, emit sp_StrArray_slice (a new helper that uses sp_StrArray_push so it inherits the inline-buffer small-array optimization). Type inference returns str_array for those forms; bare a[i] still returns string.
Refs #37.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 26c7367ad43a
- Parent
- 431b2d1f2bb0
lib/sp_runtime.hmodifiedView file
@@ -242,6 +242,10 @@
242242
static inline mrb_int sp_StrArray_length(sp_StrArray*a){return a->len;}243243
static inline mrb_bool sp_StrArray_empty(sp_StrArray*a){return a->len==0;}244244
static inline const char*sp_StrArray_get(sp_StrArray*a,mrb_int i){if(i<0)i+=a->len;return a->data[i];}245+
/* a[start, len] / a[start..end] for StrArray. Same negative-start and246+
* length-clamping semantics as sp_IntArray_slice. Out-of-bounds start247+
* returns an empty StrArray (we don't have a nullable form). */248+
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;}245249
static inline void sp_StrArray_set(sp_StrArray*a,mrb_int i,const char*v){if(i<0)i+=a->len;while(i>=a->len)sp_StrArray_push(a,"");a->data[i]=v;}246250
static const char*sp_StrArray_join(sp_StrArray*a,const char*sep){size_t sl=strlen(sep),cap=256;char*buf=(char*)malloc(cap);size_t len=0;for(mrb_int i=0;i<a->len;i++){if(i>0){if(len+sl>=cap){cap*=2;buf=(char*)realloc(buf,cap);}memcpy(buf+len,sep,sl);len+=sl;}size_t el=strlen(a->data[i]);if(len+el>=cap){cap=(len+el)*2+1;buf=(char*)realloc(buf,cap);}memcpy(buf+len,a->data[i],el);len+=el;}buf[len]=0;char*r=sp_str_alloc(len);memcpy(r,buf,len);free(buf);return r;}247251
static mrb_bool sp_StrArray_include(sp_StrArray*a,const char*v){for(mrb_int i=0;i<a->len;i++)if(strcmp(a->data[i],v)==0)return TRUE;return FALSE;}spinel_codegen.rbmodifiedView file
Loading diff…
test/bm_str_array_range.rbaddedView file
Loading diff…