Commit af3fc4d4

af3fc4d42699Merged

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

Description

feat: add String#delete_prefix and String#delete_suffix

Removes a matching prefix or suffix, returning a new string. Leaves the original unchanged if the prefix/suffix doesn't match. Added sp_str_delete_prefix and sp_str_delete_suffix to runtime.

Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]

Details

Commit
af3fc4d42699
Parent
6d1683ce7a77

lib/sp_runtime.hmodifiedView file

@@ -169,6 +169,8 @@
169169 static const char*sp_str_upcase(const char*s){size_t l=strlen(s);char*r=(char*)malloc(l+1);for(size_t i=0;i<=l;i++)r[i]=toupper((unsigned char)s[i]);return r;}
170170 static const char*sp_str_downcase(const char*s){size_t l=strlen(s);char*r=(char*)malloc(l+1);for(size_t i=0;i<=l;i++)r[i]=tolower((unsigned char)s[i]);return r;}
171171 static const char*sp_str_swapcase(const char*s){size_t l=strlen(s);char*r=(char*)malloc(l+1);for(size_t i=0;i<=l;i++){unsigned char c=(unsigned char)s[i];if(isupper(c))r[i]=tolower(c);else if(islower(c))r[i]=toupper(c);else r[i]=s[i];}return r;}
172+static const char*sp_str_delete_prefix(const char*s,const char*p){size_t sl=strlen(s),pl=strlen(p);if(pl<=sl&&memcmp(s,p,pl)==0){char*r=(char*)malloc(sl-pl+1);memcpy(r,s+pl,sl-pl+1);return r;}char*r=(char*)malloc(sl+1);memcpy(r,s,sl+1);return r;}
173+static const char*sp_str_delete_suffix(const char*s,const char*p){size_t sl=strlen(s),pl=strlen(p);if(pl<=sl&&memcmp(s+sl-pl,p,pl)==0){char*r=(char*)malloc(sl-pl+1);memcpy(r,s,sl-pl);r[sl-pl]=0;return r;}char*r=(char*)malloc(sl+1);memcpy(r,s,sl+1);return r;}
172174 static const char*sp_gets(void){char buf[4096];if(!fgets(buf,sizeof(buf),stdin))return NULL;size_t l=strlen(buf);char*r=(char*)malloc(l+1);memcpy(r,buf,l+1);return r;}
173175 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=(char*)malloc(l+1);memcpy(r,s,l);r[l]=0;return r;}
174176 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=(char*)malloc(l+1);memcpy(r,s,l);r[l]=0;return r;}