Commit e1cf50f1

e1cf50f1cc90Merged

NHH <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Fix String#lines(chomp: true) ignoring the chomp keyword

The codegen unconditionally emitted sp_str_lines(rc) regardless of keyword arguments. Added sp_str_lines_chomp runtime helper that strips \n (and \r\n) line endings, and detect the chomp: true KeywordHashNode pair in the codegen to route accordingly.

Details

Commit
e1cf50f1cc90
Parent
155ec59a2269

lib/sp_runtime.hmodifiedView file

@@ -1302,6 +1302,7 @@
13021302 `end` is computed once at entry so a string with no newlines avoids
13031303 a redundant strlen call on the trailing piece. */
13041304 static sp_StrArray*sp_str_lines(const char*s){sp_StrArray*a=sp_StrArray_new();if(*s==0)return a;const char*end=s+strlen(s);const char*p=s;while(p<end){const char*nl=strchr(p,'\n');size_t n=nl?(size_t)(nl-p+1):(size_t)(end-p);char*r=sp_str_alloc_raw(n+1);memcpy(r,p,n);r[n]=0;sp_StrArray_push(a,r);if(!nl)break;p=nl+1;}return a;}
1305+static sp_StrArray*sp_str_lines_chomp(const char*s){sp_StrArray*a=sp_StrArray_new();if(*s==0)return a;const char*end=s+strlen(s);const char*p=s;while(p<end){const char*nl=strchr(p,'\n');size_t n=nl?(size_t)(nl-p):(size_t)(end-p);if(nl&&nl>s&&nl[-1]=='\r')n--;char*r=sp_str_alloc_raw(n+1);memcpy(r,p,n);r[n]=0;sp_StrArray_push(a,r);if(!nl)break;p=nl+1;}return a;}
13051306 /* Issue #827: gsub previously returned a raw malloc buffer. The GC's
13061307 sp_mark_string writes byte[-1] = 0xfc, which on a raw malloc buffer
13071308 clobbers malloc metadata. Build into a scratch buffer, then copy