Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io.string_reader: fix needs_fill_until check #21005

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion vlib/io/string_reader/string_reader.v
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub fn (r StringReader) needs_fill() bool {
// needs_fill_until returns whether the buffer needs refilling in order to read
// `n` bytes
pub fn (r StringReader) needs_fill_until(n int) bool {
return r.offset + n >= r.builder.len
return r.offset + n > r.builder.len
}

// fill_bufer tries to read data into the buffer until either a 0 length read or if read_to_end_of_stream
Expand Down
14 changes: 14 additions & 0 deletions vlib/io/string_reader/string_reader_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ fn test_from_string() {
}
}

fn test_from_string_read_byte_one_by_one() {
mut reader := StringReader.new(source: 'test')
assert reader.read_bytes(1)![0].ascii_str() == 't'
assert reader.read_bytes(1)![0].ascii_str() == 'e'
assert reader.read_bytes(1)![0].ascii_str() == 's'
assert reader.read_bytes(1)![0].ascii_str() == 't'

if _ := reader.read_all(false) {
assert false, 'should return Eof'
} else {
assert err is io.Eof
}
}

fn test_from_string_and_reader() {
buf := Buf{
bytes: 'buffer'.bytes()
Expand Down