Commit da4c7a7f
da4c7a7f0b76Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 5 files · +2 added · ~3 modified
Description
Implement String#byteslice
"hello".byteslice(0, 3) was unresolved. Add a byte-indexed substring
(distinct from the char-indexed #slice) via a new sp_str_byteslice
runtime helper: negative start counts from the byte length, and the
single-argument form takes one byte. Out-of-range yields "" to match
#slice rather than CRuby's nil.
Refs #1219
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- da4c7a7f0b76
- Parent
- d1156d779150
lib/sp_runtime.hmodifiedView file
@@ -1388,6 +1388,10 @@
13881388
range forms keep returning "" on OOB via sp_str_sub_range; only13891389
the bare single-int index aliases here. Issue #619 puzzle 3. */13901390
static const char*sp_str_char_at_or_nil(const char*s,mrb_int i){mrb_int cl=sp_str_length(s);if(i<0)i+=cl;if(i<0||i>=cl)return NULL;return sp_str_sub_range(s,i,1);}1391+
/* String#byteslice(start,len): byte-indexed (unlike the char-indexed1392+
sp_str_sub_range). Negative start counts from the byte length. Out of1393+
range yields the empty string, matching slice's "" rather than CRuby nil. */1394+
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;}13911395
/* Char-indexed variant; the second arg used to be a hoisted byte length, now a13921396
hoisted codepoint count. We don't need it for correctness, but keeping the13931397
ABI lets callers pass it without a wrapper. */spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/string_byteslice.rbaddedView file
Loading diff…
test/string_byteslice.rb.expectedaddedView file
Loading diff…