Commit 7caf9b6d
7caf9b6db62eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): endless/beginless range String#[] returns substring not ""
Sam Ruby's #543: ":id"[1..] returned "" instead of "id".
The Ruby 2.6+ endless-range shorthand is the idiomatic way to
"take from index N to end of string"; spinel lowered the
missing right endpoint of the RangeNode to the literal 0,
producing a zero-length slice silently.
Roundhouse's router uses pp[1..] to strip the leading :
from :id pattern segments. Pre-fix every /articles/N
request 404'd because the params capture key was always ""
(falling back to the params.fetch("id", "0") default).
Fix in the string-[] RangeNode arm: when the RangeNode's
@nd_left is -1 (no value, beginless), emit 0 as the
lower bound; when @nd_right is -1 (endless), emit -1 as
the upper bound and force the exclusive flag to 0. CRuby
treats s[1..] and s[1...] as the same s[1..-1] -- the
exclusive flag on the missing endpoint is meaningless.
Variants verified (test/string_endless_range.rb):
- s[1..] -> "ello"
- s[1...] -> "ello"
- s[-2..] -> "lo"
- s[..2] -> "hel"
- s[...2] -> "he"
- ":id"[1..] -> "id"
Note: s[..] (both ends nil) doesn't parse under prism's
current grammar; if it did, the same arm would handle it as
s[0..-1].
Closes #543
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 7caf9b6db62e
- Parent
- 7243161f669d
spinel_codegen.rbmodifiedView file
# length first. Issue #496: the prior `right - left + 1` # formula silently produced a negative length for `s[1..-2]` # and returned "". left = compile_expr(@nd_left[a[0]]) right = compile_expr(@nd_right[a[0]]) return (use_len ? "sp_str_sub_range_len_r" : "sp_str_sub_range_r") + "(" + lprefix + ", " + left + ", " + right + ", " + (range_excl_end(a[0]) == 1 ? "1" : "0") + ")" # # Endless / beginless range support (Ruby 2.6+). `s[1..]` # parses as RangeNode { left: 1, right: -1 (nil) }; CRuby # treats it as `s[1..-1]` (inclusive to end). `s[..5]` # mirrors with `left: -1, right: 5` and is equivalent to # `s[0..5]`. The exclusive flag on the missing endpoint is # ignored in CRuby (`s[1..]` == `s[1...]` == `s[1..-1]`). # Issue #543. Without these arms, `compile_expr(-1)` lowered # the missing endpoint to literal `0`, so `s[1..]` became # `sp_str_sub_range_r(s, 1, 0, 0)` -- length-zero slice, # silent empty string. range_node_543 = a[0] left_nid_543 = @nd_left[range_node_543] right_nid_543 = @nd_right[range_node_543] if left_nid_543 < 0 left = "0" else left = compile_expr(left_nid_543) end if right_nid_543 < 0 right = "-1" excl_543 = "0" else right = compile_expr(right_nid_543) excl_543 = (range_excl_end(range_node_543) == 1 ? "1" : "0") end return (use_len ? "sp_str_sub_range_len_r" : "sp_str_sub_range_r") + "(" + lprefix + ", " + left + ", " + right + ", " + excl_543 + ")" end if a.length >= 2 # s[0, 2]test/string_endless_range.rbaddedView file
Loading diff…
test/string_endless_range.rb.expectedaddedView file
Loading diff…