aboutsummaryrefslogtreecommitdiff
path: root/core/bytes
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2024-08-30 09:57:02 +0100
committerGitHub <noreply@github.com>2024-08-30 09:57:02 +0100
commitb020b91df2aa86ea3a1500ce0ce459035d26c140 (patch)
tree751a689c2ab7686c9c2ab9ac384e1f2f11fea42a /core/bytes
parent291048cb3bdb7f1dc7d57383a1efd6fe77677768 (diff)
parentcca385209b7b303455ad17f832add12a0afcf16d (diff)
Merge pull request #4112 from Feoramund/fix-test-io-issues
Add `core:io` test suite
Diffstat (limited to 'core/bytes')
-rw-r--r--core/bytes/buffer.odin36
-rw-r--r--core/bytes/reader.odin11
2 files changed, 42 insertions, 5 deletions
diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin
index a7e9b1c64..f4d883353 100644
--- a/core/bytes/buffer.odin
+++ b/core/bytes/buffer.odin
@@ -144,6 +144,9 @@ buffer_grow :: proc(b: ^Buffer, n: int, loc := #caller_location) {
}
buffer_write_at :: proc(b: ^Buffer, p: []byte, offset: int, loc := #caller_location) -> (n: int, err: io.Error) {
+ if len(p) == 0 {
+ return 0, nil
+ }
b.last_read = .Invalid
if offset < 0 {
err = .Invalid_Offset
@@ -246,10 +249,13 @@ buffer_read_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.
}
buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
+ if len(p) == 0 {
+ return 0, nil
+ }
b.last_read = .Invalid
if uint(offset) >= len(b.buf) {
- err = .Invalid_Offset
+ err = .EOF
return
}
n = copy(p, b.buf[offset:])
@@ -310,6 +316,27 @@ buffer_unread_rune :: proc(b: ^Buffer) -> io.Error {
return nil
}
+buffer_seek :: proc(b: ^Buffer, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
+ abs: i64
+ switch whence {
+ case .Start:
+ abs = offset
+ case .Current:
+ abs = i64(b.off) + offset
+ case .End:
+ abs = i64(len(b.buf)) + offset
+ case:
+ return 0, .Invalid_Whence
+ }
+
+ abs_int := int(abs)
+ if abs_int < 0 {
+ return 0, .Invalid_Offset
+ }
+ b.last_read = .Invalid
+ b.off = abs_int
+ return abs, nil
+}
buffer_read_bytes :: proc(b: ^Buffer, delim: byte) -> (line: []byte, err: io.Error) {
i := index_byte(b.buf[b.off:], delim)
@@ -395,14 +422,17 @@ _buffer_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offse
return io._i64_err(buffer_write(b, p))
case .Write_At:
return io._i64_err(buffer_write_at(b, p, int(offset)))
+ case .Seek:
+ n, err = buffer_seek(b, offset, whence)
+ return
case .Size:
- n = i64(buffer_capacity(b))
+ n = i64(buffer_length(b))
return
case .Destroy:
buffer_destroy(b)
return
case .Query:
- return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Size, .Destroy})
+ return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Destroy, .Query})
}
return 0, .Empty
}
diff --git a/core/bytes/reader.odin b/core/bytes/reader.odin
index 4b18345ba..2e1c5ed42 100644
--- a/core/bytes/reader.odin
+++ b/core/bytes/reader.odin
@@ -9,10 +9,11 @@ Reader :: struct {
prev_rune: int, // previous reading index of rune or < 0
}
-reader_init :: proc(r: ^Reader, s: []byte) {
+reader_init :: proc(r: ^Reader, s: []byte) -> io.Stream {
r.s = s
r.i = 0
r.prev_rune = -1
+ return reader_to_stream(r)
}
reader_to_stream :: proc(r: ^Reader) -> (s: io.Stream) {
@@ -33,6 +34,9 @@ reader_size :: proc(r: ^Reader) -> i64 {
}
reader_read :: proc(r: ^Reader, p: []byte) -> (n: int, err: io.Error) {
+ if len(p) == 0 {
+ return 0, nil
+ }
if r.i >= i64(len(r.s)) {
return 0, .EOF
}
@@ -42,6 +46,9 @@ reader_read :: proc(r: ^Reader, p: []byte) -> (n: int, err: io.Error) {
return
}
reader_read_at :: proc(r: ^Reader, p: []byte, off: i64) -> (n: int, err: io.Error) {
+ if len(p) == 0 {
+ return 0, nil
+ }
if off < 0 {
return 0, .Invalid_Offset
}
@@ -97,7 +104,6 @@ reader_unread_rune :: proc(r: ^Reader) -> io.Error {
return nil
}
reader_seek :: proc(r: ^Reader, offset: i64, whence: io.Seek_From) -> (i64, io.Error) {
- r.prev_rune = -1
abs: i64
switch whence {
case .Start:
@@ -114,6 +120,7 @@ reader_seek :: proc(r: ^Reader, offset: i64, whence: io.Seek_From) -> (i64, io.E
return 0, .Invalid_Offset
}
r.i = abs
+ r.prev_rune = -1
return abs, nil
}
reader_write_to :: proc(r: ^Reader, w: io.Writer) -> (n: i64, err: io.Error) {