Commit 178c1c60
178c1c606061Merged
NHH <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified
Description
runtime,codegen: fix String#oct prefix auto-detection
strtoll(p, NULL, 2) does not skip the 0b prefix (only base 16 auto-skips 0x), so "0b101".oct returned 0 instead of 5. Skip past the prefix with p+2, matching the existing 0o handling.
Details
- Commit
- 178c1c606061
- Parent
- 2b5da1b0fd56
lib/sp_runtime.hmodifiedView file
@@ -212,6 +212,10 @@
212212
static mrb_int sp_int_ceil(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v>0&&q<INT64_MAX/f)return(q+1)*f;return q*f;}213213
static mrb_int sp_int_floor(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);mrb_int q=v/f,r=v%f;if(r!=0&&v<0&&q>INT64_MIN/f)return(q-1)*f;return q*f;}214214
static mrb_int sp_int_truncate(mrb_int v,mrb_int nd){if(nd>=0)return v;mrb_int p=-nd;if(p>=19)return 0;mrb_int f=sp_ipow10(p);return(v/f)*f;}215+
/* String#oct: parse with prefix auto-detection (0x=hex, 0b=bin,216+
0o=oct, 0=oct, otherwise decimal), then fall back to base-8217+
if no prefix. Matches CRuby behavior. */218+
static mrb_int sp_str_oct(const char*s){if(!s)return 0;const char*p=s;while(*p==' '||*p=='\t')p++;if(p[0]=='0'){if(p[1]=='x'||p[1]=='X')return(mrb_int)strtoll(p,NULL,16);if(p[1]=='b'||p[1]=='B')return(mrb_int)strtoll(p+2,NULL,2);if(p[1]=='o'||p[1]=='O')return(mrb_int)strtoll(p+2,NULL,8);return(mrb_int)strtoll(p,NULL,8);}return(mrb_int)strtoll(p,NULL,8);}215219
216220
/* Forward decls for helpers used across this header (and by the217221
string->number parsers that now live in libspinel_rt.a). */spinel_codegen.rbmodifiedView file
Loading diff…
test/str_oct_prefix.rbaddedView file
Loading diff…
test/str_oct_prefix.rb.expectedaddedView file
Loading diff…