Commit c3c367ce
c3c367ce9094Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
test(codegen): pin String#each_byte byte-level iteration
String#each_byte was already wired up in compile_string_method_expr (line ~21598) but had no dedicated regression-guard test. This pins the byte-level iteration semantics so a future refactor of the string iterators preserves the difference between each_char (yields codepoint substrings) and each_byte (yields raw byte values).
Test exercises five shapes:
- two-byte ASCII string yields 97, 98 (a, b)
- empty string yields nothing — no
putslines - mixed alpha + digit (A1z) yields 65, 49, 122
- a newline byte (\n => 10) confirms control characters route through the same path
- multi-byte UTF-8 (
é) yields the raw 0xC3 0xA9 byte pair, NOT the codepoint 233. This is the documented CRuby semantics and is the key behaviour distinguishing each_byte from each_char - accumulator over "hello" sums to 532; covers block-body capture of an outer local
Bootstrap: gen2.c == gen3.c. Tests: 213 pass.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- c3c367ce9094
- Parent
- 02005f26deba
spinel_codegen.rbmodifiedView file
@@ -2251,6 +2251,17 @@
22512251
end22522252
return "int"22532253
end2254+
# String#each_byte returns the receiver per CRuby. The block-bearing2255+
# form is handled in compile_string_method_expr; the inference rule2256+
# here is what makes `ret = "hi".each_byte { ... }` typed as string.2257+
if mname == "each_byte"2258+
if recv >= 0 && @nd_block[nid] >= 02259+
rt = infer_type(recv)2260+
if rt == "string" || rt == "mutable_str"2261+
return rt2262+
end2263+
end2264+
end22542265
if mname == "then" || mname == "yield_self"22552266
# Return type is the block's return type. Bind the block param to22562267
# the receiver's type so infer_type sees the inner shadow, not any@@ -15996,6 +16007,30 @@
1599616007
end1599716008
1599816009
def compile_string_method_expr(nid, mname, rc)16010+
# String#each_byte returns the receiver per CRuby. The statement-level16011+
# handler at compile_block_iteration_stmt emits the loop for `do …16012+
# end` / `{ … }` with no captured value; the expression-level form16013+
# (e.g. `ret = "hi".each_byte { ... }`) needs to produce the16014+
# receiver. Same loop body, just emit-then-return-rc.16015+
if mname == "each_byte" && @nd_block[nid] >= 016016+
bp = get_block_param(nid, 0)16017+
if bp == ""16018+
bp = "_b"16019+
end16020+
itmp = new_temp16021+
src_tmp = new_temp16022+
emit(" const char *" + src_tmp + " = " + rc + ";")16023+
emit(" for (mrb_int " + itmp + " = 0; " + src_tmp + "[" + itmp + "]; " + itmp + "++) {")16024+
emit(" mrb_int lv_" + bp + " = (unsigned char)" + src_tmp + "[" + itmp + "];")16025+
@indent = @indent + 116026+
push_scope16027+
declare_var(bp, "int")16028+
compile_stmts_body(@nd_body[@nd_block[nid]])16029+
pop_scope16030+
@indent = @indent - 116031+
emit(" }")16032+
return rc16033+
end1599916034
if mname == "length"1600016035
# Only use hoisted length if the receiver matches (otherwise we'd1600116036
# return the wrong string's length).test/string_each_byte.rbaddedView file
Loading diff…