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 end
94379437 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 type
94419443 types.push(recv_type)
@@ -15112,6 +15114,34 @@
1511215114 end
1511315115 end
1511415116
15117+ if mname == "each_byte"
15118+ if @nd_block[nid] >= 0 && recv >= 0
15119+ 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+ end
15126+ declare_var(bp, "int")
15127+ tmp = new_temp
15128+ src = rc
15129+ if rt == "mutable_str"
15130+ src = rc + "->data"
15131+ end
15132+ src_tmp = new_temp
15133+ 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 + 1
15137+ compile_stmts_body(@nd_body[@nd_block[nid]])
15138+ @indent = @indent - 1
15139+ emit(" }")
15140+ return 1
15141+ end
15142+ end
15143+ end
15144+
1511515145 if mname == "each_line"
1511615146 if @nd_block[nid] >= 0 && recv >= 0
1511715147 rt = infer_type(recv)