2020-11-23 16:17:39 +01:00
|
|
|
module io
|
|
|
|
|
|
|
|
struct Buf {
|
|
|
|
pub:
|
|
|
|
bytes []byte
|
|
|
|
mut:
|
2021-03-24 11:39:09 +01:00
|
|
|
i int
|
2020-11-23 16:17:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut b Buf) read(mut buf []byte) ?int {
|
|
|
|
if !(b.i < b.bytes.len) {
|
2020-12-16 18:22:26 +01:00
|
|
|
return none
|
2020-11-23 16:17:39 +01:00
|
|
|
}
|
2022-03-09 19:26:00 +01:00
|
|
|
n := copy(mut buf, b.bytes[b.i..])
|
2020-11-23 16:17:39 +01:00
|
|
|
b.i += n
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_read_all() {
|
|
|
|
buf := Buf{
|
|
|
|
bytes: '123'.repeat(10).bytes()
|
|
|
|
}
|
2020-12-16 18:22:26 +01:00
|
|
|
res := read_all(reader: buf) or {
|
2020-11-23 16:17:39 +01:00
|
|
|
assert false
|
|
|
|
''.bytes()
|
|
|
|
}
|
|
|
|
assert res == '123'.repeat(10).bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_read_all_huge() {
|
2020-12-16 18:22:26 +01:00
|
|
|
buf := Buf{
|
|
|
|
bytes: '123'.repeat(100000).bytes()
|
|
|
|
}
|
|
|
|
res := read_all(reader: buf) or {
|
|
|
|
assert false
|
|
|
|
''.bytes()
|
|
|
|
}
|
|
|
|
assert res == '123'.repeat(100000).bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StringReader {
|
2021-03-24 11:39:09 +01:00
|
|
|
text string
|
2020-12-16 18:22:26 +01:00
|
|
|
mut:
|
|
|
|
place int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (mut s StringReader) read(mut buf []byte) ?int {
|
|
|
|
if s.place >= s.text.len {
|
|
|
|
return none
|
|
|
|
}
|
2022-03-09 19:26:00 +01:00
|
|
|
read := copy(mut buf, s.text[s.place..].bytes())
|
2020-12-16 18:22:26 +01:00
|
|
|
s.place += read
|
|
|
|
return read
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
newline_count = 100000
|
|
|
|
)
|
|
|
|
|
|
|
|
fn test_stringreader() {
|
2021-03-24 11:39:09 +01:00
|
|
|
text := '12345\n'.repeat(io.newline_count)
|
2020-12-16 18:22:26 +01:00
|
|
|
mut s := StringReader{
|
|
|
|
text: text
|
|
|
|
}
|
2021-05-13 09:26:10 +02:00
|
|
|
mut r := new_buffered_reader(reader: s)
|
2020-12-16 18:22:26 +01:00
|
|
|
for i := 0; true; i++ {
|
|
|
|
if _ := r.read_line() {
|
|
|
|
} else {
|
2021-03-24 11:39:09 +01:00
|
|
|
assert i == io.newline_count
|
2020-12-16 18:22:26 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _ := r.read_line() {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
leftover := read_all(reader: r) or {
|
|
|
|
assert false
|
|
|
|
panic('bad')
|
|
|
|
}
|
|
|
|
if leftover.len > 0 {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_stringreader2() {
|
2021-03-24 11:39:09 +01:00
|
|
|
text := '12345\r\n'.repeat(io.newline_count)
|
2020-12-16 18:22:26 +01:00
|
|
|
mut s := StringReader{
|
|
|
|
text: text
|
|
|
|
}
|
2021-05-13 09:26:10 +02:00
|
|
|
mut r := new_buffered_reader(reader: s)
|
2020-12-16 18:22:26 +01:00
|
|
|
for i := 0; true; i++ {
|
|
|
|
if _ := r.read_line() {
|
|
|
|
} else {
|
2021-03-24 11:39:09 +01:00
|
|
|
assert i == io.newline_count
|
2020-12-16 18:22:26 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _ := r.read_line() {
|
|
|
|
assert false
|
|
|
|
}
|
2021-05-13 09:26:10 +02:00
|
|
|
leftover := read_all(reader: r) or {
|
2020-12-16 18:22:26 +01:00
|
|
|
assert false
|
|
|
|
panic('bad')
|
|
|
|
}
|
|
|
|
if leftover.len > 0 {
|
|
|
|
assert false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_leftover() {
|
|
|
|
text := 'This is a test\r\nNice try!'
|
|
|
|
mut s := StringReader{
|
|
|
|
text: text
|
|
|
|
}
|
2021-05-13 09:26:10 +02:00
|
|
|
mut r := new_buffered_reader(reader: s)
|
2020-12-16 18:22:26 +01:00
|
|
|
_ := r.read_line() or {
|
|
|
|
assert false
|
|
|
|
panic('bad')
|
|
|
|
}
|
|
|
|
line2 := r.read_line() or {
|
|
|
|
assert false
|
|
|
|
panic('bad')
|
|
|
|
}
|
|
|
|
assert line2 == 'Nice try!'
|
|
|
|
if _ := r.read_line() {
|
2020-11-23 16:17:39 +01:00
|
|
|
assert false
|
2020-12-16 18:22:26 +01:00
|
|
|
panic('bad')
|
2020-11-23 16:17:39 +01:00
|
|
|
}
|
2020-12-16 18:22:26 +01:00
|
|
|
assert r.end_of_stream()
|
2020-11-23 16:17:39 +01:00
|
|
|
}
|