Commit 25ecab10
25ecab1089d0Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
Honor the chomp: keyword in String#each_line
each_line(chomp: true) ignored the keyword and yielded each line with
its trailing newline. Detect the chomp: true pair at compile time and
trim the line ending (\n, \r\n) from the copied span before yielding,
while still advancing past it so the next line starts correctly. Mirrors
the lines(chomp: true) handling.
Fixes #1080
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 25ecab1089d0
- Parent
- 99df0ee0cd18
spinel_codegen.rbmodifiedView file
@@ -37213,6 +37213,26 @@
3721337213
if bp == ""3721437214
bp = "_l"3721537215
end37216+
# `each_line(chomp: true)` strips the trailing line ending (\n, \r\n)37217+
# from each yielded line. Detect the keyword pair at compile time.37218+
chomp_el = 037219+
if @nd_arguments[nid] >= 037220+
aargs_el = get_args(@nd_arguments[nid])37221+
if aargs_el.length >= 1 && @nd_type[aargs_el[0]] == "KeywordHashNode"37222+
kelems_el = parse_id_list(@nd_elements[aargs_el[0]])37223+
kk_el = 037224+
while kk_el < kelems_el.length37225+
if @nd_type[kelems_el[kk_el]] == "AssocNode"37226+
key_el = @nd_key[kelems_el[kk_el]]37227+
val_el = @nd_expression[kelems_el[kk_el]]37228+
if key_el >= 0 && @nd_type[key_el] == "SymbolNode" && @nd_content[key_el] == "chomp" && val_el >= 0 && @nd_type[val_el] == "TrueNode"37229+
chomp_el = 137230+
end37231+
end37232+
kk_el = kk_el + 137233+
end37234+
end37235+
end3721637236
src_el = rc3721737237
if rt == "mutable_str"3721837238
src_el = rc + "->data"@@ -37231,6 +37251,11 @@
3723137251
emit(" while (" + pos_el + " < " + slen_el + " && " + base_el + "[" + pos_el + "] != '\\n') " + pos_el + "++;")3723237252
emit(" if (" + pos_el + " < " + slen_el + ") " + pos_el + "++;")3723337253
emit(" mrb_int " + llen_el + " = " + pos_el + " - " + start_el + ";")37254+
if chomp_el == 137255+
# Trim the trailing line ending from the copied span (pos_el still37256+
# advances past it, so the next iteration starts correctly).37257+
emit(" if (" + llen_el + " > 0 && " + base_el + "[" + start_el + " + " + llen_el + " - 1] == '\\n') { " + llen_el + "--; if (" + llen_el + " > 0 && " + base_el + "[" + start_el + " + " + llen_el + " - 1] == '\\r') " + llen_el + "--; }")37258+
end3723437259
emit(" char *" + buf_el + " = sp_str_alloc(" + llen_el + ");")3723537260
emit(" memcpy(" + buf_el + ", " + base_el + " + " + start_el + ", " + llen_el + ");")3723637261
emit(" " + buf_el + "[" + llen_el + "] = 0;")test/str_each_line_chomp.rbaddedView file
Loading diff…
test/str_each_line_chomp.rb.expectedaddedView file
Loading diff…