Commit 3f4dba60

3f4dba60e46cMerged

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

Description

feat: add String#succ / #next and Integer#next

String succ increments the rightmost character, handling carry for digits, lowercase, and uppercase letters. Adds a new char on overflow ("zz" -> "aaa"). Integer#next is an alias for succ. Added sp_str_succ to runtime.

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

Details

Commit
3f4dba60e46c
Parent
ac52488aee0d

lib/sp_runtime.hmodifiedView file

@@ -172,6 +172,7 @@
172172 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;}
173173 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;}
174174 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;}
175+static const char*sp_str_succ(const char*s){size_t l=strlen(s);if(l==0){char*r=(char*)malloc(1);r[0]=0;return r;}char*r=(char*)malloc(l+2);memcpy(r,s,l+1);mrb_int i=(mrb_int)l-1;while(i>=0){unsigned char c=(unsigned char)r[i];if(c>='0'&&c<'9'){r[i]=c+1;return r;}if(c=='9'){r[i]='0';i--;continue;}if(c>='a'&&c<'z'){r[i]=c+1;return r;}if(c=='z'){r[i]='a';i--;continue;}if(c>='A'&&c<'Z'){r[i]=c+1;return r;}if(c=='Z'){r[i]='A';i--;continue;}r[i]=c+1;return r;}memmove(r+1,r,l+1);if(r[1]=='0')r[0]='1';else if(r[1]=='a')r[0]='a';else if(r[1]=='A')r[0]='A';else r[0]=r[1];return r;}
175176 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;}
176177 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=(char*)malloc(l+1);memcpy(r,buf,l+1);sp_StrArray_push(a,r);}return a;}
177178 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;}