Commit 1761f25e
1761f25e911dMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 7 months ago · 1 file · ~1 modified
Description
feat: add String#each_byte
Iterates each byte of a string as an integer, yielding each to the block. Supports both string and mutable_str receivers.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 1761f25e911d
- Parent
- 7b4d8127849d
spinel_codegen.rbmodifiedView file
@@ -9436,6 +9436,8 @@
94369436
end94379437
elsif mname == "each_char" || mname == "each_line"94389438
types.push("string")9439+
elsif mname == "each_byte"9440+
types.push("int")94399441
elsif mname == "tap" || mname == "then" || mname == "yield_self"94409442
# Block param gets receiver type94419443
types.push(recv_type)@@ -15112,6 +15114,34 @@
1511215114
end1511315115
end1511415116
15117+
if mname == "each_byte"15118+
if @nd_block[nid] >= 0 && recv >= 015119+
rt = infer_type(recv)15120+
if rt == "string" || rt == "mutable_str"15121+
rc = compile_expr(recv)15122+
bp = get_block_param(nid, 0)15123+
if bp == ""15124+
bp = "_b"15125+
end15126+
declare_var(bp, "int")15127+
tmp = new_temp15128+
src = rc15129+
if rt == "mutable_str"15130+
src = rc + "->data"15131+
end15132+
src_tmp = new_temp15133+
emit(" const char *" + src_tmp + " = " + src + ";")15134+
emit(" for (mrb_int " + tmp + " = 0; " + src_tmp + "[" + tmp + "]; " + tmp + "++) {")15135+
emit(" lv_" + bp + " = (unsigned char)" + src_tmp + "[" + tmp + "];")15136+
@indent = @indent + 115137+
compile_stmts_body(@nd_body[@nd_block[nid]])15138+
@indent = @indent - 115139+
emit(" }")15140+
return 115141+
end15142+
end15143+
end15144+
1511515145
if mname == "each_line"1511615146
if @nd_block[nid] >= 0 && recv >= 01511715147
rt = infer_type(recv)