Commit 75829a54
75829a54c241Merged
Islam Gagiev <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(codegen,runtime): add String#chop
Removes the last character from a string, or both \r\n if the string ends with that sequence. Returns empty string for empty input.
- lib/sp_runtime.h: add sp_str_chop() next to sp_str_chomp()
- spinel_codegen.rb: infer_type + codegen + feature detection
- test/string_chop.rb: 6 cases covering basic, crlf, newline, empty, single char
Details
- Commit
- 75829a54c241
- Parent
- 84d241a676e0
lib/sp_runtime.hmodifiedView file
@@ -501,6 +501,7 @@
501501
static sp_StrArray*sp_readlines(void){sp_StrArray*a=sp_StrArray_new();char buf[4096];while(fgets(buf,sizeof(buf),stdin)){size_t l=strlen(buf);char*r=sp_str_alloc_raw(l+1);memcpy(r,buf,l+1);sp_StrArray_push(a,r);}return a;}502502
static const char*sp_str_strip(const char*s){while(*s&&isspace((unsigned char)*s))s++;size_t l=strlen(s);while(l>0&&isspace((unsigned char)s[l-1]))l--;char*r=sp_str_alloc_raw(l+1);memcpy(r,s,l);r[l]=0;return r;}503503
static const char*sp_str_chomp(const char*s){size_t l=strlen(s);while(l>0&&(s[l-1]=='\n'||s[l-1]=='\r'))l--;char*r=sp_str_alloc_raw(l+1);memcpy(r,s,l);r[l]=0;return r;}504+
static const char*sp_str_chop(const char*s){size_t l=strlen(s);if(l>0){if(l>=2&&s[l-2]=='\r'&&s[l-1]=='\n')l-=2;else l--;}char*r=sp_str_alloc_raw(l+1);memcpy(r,s,l);r[l]=0;return r;}504505
static mrb_bool sp_str_include(const char*s,const char*sub){return strstr(s,sub)!=NULL;}505506
static mrb_bool sp_str_start_with(const char*s,const char*p){return strncmp(s,p,strlen(p))==0;}506507
static mrb_bool sp_str_end_with(const char*s,const char*suf){size_t ls=strlen(s),lsuf=strlen(suf);if(lsuf>ls)return FALSE;return strcmp(s+ls-lsuf,suf)==0;}spinel_codegen.rbmodifiedView file
Loading diff…
test/string_chop.rbaddedView file
Loading diff…
test/string_chop.rb.expectedaddedView file
Loading diff…