diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2024-08-30 09:57:02 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-30 09:57:02 +0100 |
| commit | b020b91df2aa86ea3a1500ce0ce459035d26c140 (patch) | |
| tree | 751a689c2ab7686c9c2ab9ac384e1f2f11fea42a /core/bytes/buffer.odin | |
| parent | 291048cb3bdb7f1dc7d57383a1efd6fe77677768 (diff) | |
| parent | cca385209b7b303455ad17f832add12a0afcf16d (diff) | |
Merge pull request #4112 from Feoramund/fix-test-io-issues
Add `core:io` test suite
Diffstat (limited to 'core/bytes/buffer.odin')
| -rw-r--r-- | core/bytes/buffer.odin | 36 |
1 files changed, 33 insertions, 3 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 } |