Commit edb362fb
edb362fb5792Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
Implement String#ascii_only?
"hello".ascii_only? was unresolved. Add an sp_str_ascii_only runtime
helper that returns true iff every byte is in the 7-bit ASCII range,
and type the predicate as bool.
Fixes #1219
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- edb362fb5792
- Parent
- da4c7a7f0b76
lib/sp_runtime.hmodifiedView file
@@ -1392,6 +1392,8 @@
13921392
sp_str_sub_range). Negative start counts from the byte length. Out of13931393
range yields the empty string, matching slice's "" rather than CRuby nil. */13941394
static const char*sp_str_byteslice(const char*s,mrb_int start,mrb_int len){mrb_int bl=(mrb_int)sp_str_byte_len(s);if(start<0)start+=bl;if(start<0||start>bl||len<0){return &("\xff" "")[1];}if(start+len>bl)len=bl-start;if(len<=0){return &("\xff" "")[1];}char*r=sp_str_alloc_raw(len+1);memcpy(r,s+start,len);r[len]=0;return r;}1395+
/* String#ascii_only?: 1 iff every byte is in the 7-bit ASCII range. */1396+
static int sp_str_ascii_only(const char*s){mrb_int bl=(mrb_int)sp_str_byte_len(s);for(mrb_int i=0;i<bl;i++){if((unsigned char)s[i]>=0x80)return 0;}return 1;}13951397
/* Char-indexed variant; the second arg used to be a hoisted byte length, now a13961398
hoisted codepoint count. We don't need it for correctness, but keeping the13971399
ABI lets callers pass it without a wrapper. */spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_ascii_only.rbaddedView file
Loading diff…
test/string_ascii_only.rb.expectedaddedView file
Loading diff…