Commit a25ecbf6

a25ecbf674c9Merged

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

Description

Array#slice_before / slice_after with an int delimiter

Add sp_IntArray_slice_before / slice_after (split into chunks before/after each element equal to the delimiter) and render them through the inline .to_a.inspect chain, reusing the IntArrayPtrArray inspect helper.

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

Details

Commit
a25ecbf674c9
Parent
1e3634da9cf1

lib/sp_runtime.hmodifiedView file

@@ -1990,6 +1990,10 @@
19901990 type is one of the four built-in T_array shapes, recurse into the
19911991 matching primitive inspect . */
19921992 static const char*sp_IntArrayPtrArray_inspect(sp_PtrArray*a){sp_String*s=sp_String_new("[");for(mrb_int i=0;i<a->len;i++){if(i>0)sp_String_append(s,", ");sp_String_append(s,sp_IntArray_inspect((sp_IntArray*)a->data[i]));}sp_String_append(s,"]");return s->data;}
1993+/* Array#slice_before(delim): start a new chunk before each element == delim. */
1994+static sp_PtrArray*sp_IntArray_slice_before(sp_IntArray*a,mrb_int d){sp_PtrArray*out=sp_PtrArray_new();if(!a)return out;sp_IntArray*cur=sp_IntArray_new();for(mrb_int i=0;i<a->len;i++){mrb_int e=a->data[a->start+i];if(e==d&&cur->len>0){sp_PtrArray_push(out,cur);cur=sp_IntArray_new();}sp_IntArray_push(cur,e);}if(cur->len>0)sp_PtrArray_push(out,cur);return out;}
1995+/* Array#slice_after(delim): end a chunk after each element == delim. */
1996+static sp_PtrArray*sp_IntArray_slice_after(sp_IntArray*a,mrb_int d){sp_PtrArray*out=sp_PtrArray_new();if(!a)return out;sp_IntArray*cur=sp_IntArray_new();for(mrb_int i=0;i<a->len;i++){mrb_int e=a->data[a->start+i];sp_IntArray_push(cur,e);if(e==d){sp_PtrArray_push(out,cur);cur=sp_IntArray_new();}}if(cur->len>0)sp_PtrArray_push(out,cur);return out;}
19931997 /* Issue #742: Array#combination(k) on int_array -- emit all
19941998 k-element ordered combinations as a PtrArray of IntArrays. */
19951999 static void sp_int_combination_recur(sp_IntArray*src,mrb_int start,mrb_int k,sp_IntArray*acc,sp_PtrArray*out){if(k==0){sp_IntArray*cp=sp_IntArray_new();for(mrb_int i=0;i<acc->len;i++)sp_IntArray_push(cp,acc->data[acc->start+i]);sp_PtrArray_push(out,cp);return;}for(mrb_int i=start;i<=src->len-k;i++){sp_IntArray_push(acc,src->data[src->start+i]);sp_int_combination_recur(src,i+1,k-1,acc,out);acc->len--;}}