Skip to content

Commit

Permalink
Fix Char::Reader#each bounds check after block (#13817)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Sep 23, 2023
1 parent b597473 commit 6473d3d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
37 changes: 24 additions & 13 deletions spec/std/char/reader_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,31 @@ describe "Char::Reader" do
reader.current_char.ord.should eq(225)
end

it "is an Enumerable(Char)" do
reader = Char::Reader.new("abc")
sum = 0
reader.each do |char|
sum += char.ord
end.should be_nil
sum.should eq(294)
end
describe "#each" do
it "yields chars" do
reader = Char::Reader.new("abc")
chars = [] of Char
reader.each do |char|
chars << char
end.should be_nil
chars.should eq ['a', 'b', 'c']
end

it "is an Enumerable(Char) but doesn't yield if empty" do
reader = Char::Reader.new("")
reader.each do |char|
fail "reader each shouldn't yield on empty string"
end.should be_nil
it "does not yield if empty" do
reader = Char::Reader.new("")
reader.each do |char|
fail "reader each shouldn't yield on empty string"
end.should be_nil
end

it "checks bounds after block" do
string = "f"
reader = Char::Reader.new(string)
reader.each do |c|
c.should eq 'f'
reader.next_char
end
end
end

it "starts at end" do
Expand Down
3 changes: 2 additions & 1 deletion src/char/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@ struct Char
# C
# ```
def each(&) : Nil
while has_next?
while @pos < @string.bytesize
yield current_char

@pos += @current_char_width
decode_current_char
end
Expand Down

0 comments on commit 6473d3d

Please sign in to comment.