io: fix not reading the last byte of a stream with `.read(mut buf)`

pull/9107/head
Delyan Angelov 2021-03-04 08:57:30 +02:00
parent 76c8dea782
commit 2bfa6dfe2f
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 67 additions and 2 deletions

View File

@ -22,7 +22,9 @@ pub struct BufferedReaderConfig {
// new_buffered_reader creates new BufferedReader // new_buffered_reader creates new BufferedReader
pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader { pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
assert o.cap >= 2 if o.cap <= 0 {
panic('new_buffered_reader should be called with a positive `cap`')
}
// create // create
r := &BufferedReader{ r := &BufferedReader{
reader: o.reader reader: o.reader
@ -35,6 +37,9 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader {
// read fufills the Reader interface // read fufills the Reader interface
pub fn (mut r BufferedReader) read(mut buf []byte) ?int { pub fn (mut r BufferedReader) read(mut buf []byte) ?int {
if r.end_of_stream {
return none
}
// read data out of the buffer if we dont have any // read data out of the buffer if we dont have any
if r.needs_fill() { if r.needs_fill() {
if !r.fill_buffer() { if !r.fill_buffer() {
@ -43,6 +48,9 @@ pub fn (mut r BufferedReader) read(mut buf []byte) ?int {
} }
} }
read := copy(buf, r.buf[r.offset..r.len]) read := copy(buf, r.buf[r.offset..r.len])
if read == 0 {
return none
}
r.offset += read r.offset += read
return read return read
} }
@ -79,7 +87,7 @@ fn (mut r BufferedReader) fill_buffer() bool {
// needs_fill returns whether the buffer needs refilling // needs_fill returns whether the buffer needs refilling
fn (r BufferedReader) needs_fill() bool { fn (r BufferedReader) needs_fill() bool {
return r.offset >= r.len - 1 return r.offset >= r.len
} }
// end_of_stream returns whether the end of the stream was reached // end_of_stream returns whether the end of the stream was reached

View File

@ -0,0 +1,57 @@
import io
struct StringReader {
text string
mut:
place int
}
fn imin(a int, b int) int {
return if a < b { a } else { b }
}
fn (mut s StringReader) read(mut buf []byte) ?int {
$if debug {
eprintln('>>>> StringReader.read output buf.len: $buf.len')
}
if s.place > s.text.len + 1 {
return none
}
mut howmany := imin(buf.len, s.text.len - s.place)
xxx := s.text[s.place..s.place + howmany].bytes()
read := copy(buf, xxx)
s.place += read
return read
}
fn read_from_string(text string, capacity int) []byte {
mut str := StringReader{
text: text
}
mut stream := io.new_buffered_reader(reader: io.make_reader(str), cap: capacity)
//
mut buf := []byte{len: 1}
mut res := []byte{}
mut i := 0
for {
z := stream.read(mut buf) or { break }
res << buf
$if debug {
println('capacity: $capacity, i: $i, buf: $buf | z: $z')
}
i++
}
return res
}
pub fn test_reading_from_a_string() {
for capacity in 1 .. 1000 {
assert read_from_string('a', capacity) == [byte(`a`)]
assert read_from_string('ab', capacity) == [byte(`a`), `b`]
assert read_from_string('abc', capacity) == [byte(`a`), `b`, `c`]
assert read_from_string('abcde', capacity) == [byte(`a`), `b`, `c`, `d`, `e`]
large_string_bytes := []byte{len: 1000, init: `x`}
large_string := large_string_bytes.bytestr()
assert read_from_string(large_string, capacity) == large_string_bytes
}
}