From 981a2e1a0097ebd2e3237f5693cf8d929c6a8ca9 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Sun, 18 Aug 2024 23:03:07 -0400 Subject: Add missing `io.Stream_Mode` responses --- core/bytes/buffer.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/bytes/buffer.odin') diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index a7e9b1c64..f46fb826a 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -402,7 +402,7 @@ _buffer_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offse 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, .Size, .Destroy, .Query}) } return 0, .Empty } -- cgit v1.2.3 From 8251f4d7d009ce5f0e7d9293d70458971dd4c553 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 19 Aug 2024 02:05:05 -0400 Subject: Return `.EOF` in `bytes.buffer_read_at` instead This is consistent with the other stream `read` procs --- core/bytes/buffer.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/bytes/buffer.odin') diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index f46fb826a..995068aad 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -249,7 +249,7 @@ buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.E b.last_read = .Invalid if uint(offset) >= len(b.buf) { - err = .Invalid_Offset + err = .EOF return } n = copy(p, b.buf[offset:]) -- cgit v1.2.3 From 7c6cc8104112bd6e260355f4ba3566ffe0de8809 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 19 Aug 2024 03:15:58 -0400 Subject: Add `Seek` behavior to `bytes.Buffer` --- core/bytes/buffer.odin | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'core/bytes/buffer.odin') diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index 995068aad..4208b0d46 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -310,6 +310,26 @@ 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 + } + + if abs < 0 { + return 0, .Invalid_Offset + } + b.last_read = .Invalid + b.off = int(abs) + 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,6 +415,9 @@ _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)) return @@ -402,7 +425,7 @@ _buffer_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offse buffer_destroy(b) return case .Query: - return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Size, .Destroy, .Query}) + return io.query_utility({.Read, .Read_At, .Write, .Write_At, .Seek, .Size, .Destroy, .Query}) } return 0, .Empty } -- cgit v1.2.3 From e83b982afe103544112dea83e1befe1061f9ea93 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Mon, 19 Aug 2024 04:03:40 -0400 Subject: Measure `bytes.Buffer` size by `length` instead of `capacity` --- core/bytes/buffer.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core/bytes/buffer.odin') diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index 4208b0d46..0399beea4 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -419,7 +419,7 @@ _buffer_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, offse 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) -- cgit v1.2.3 From 6798d15ecb96e28f35f1a38d6bbc3ea1de10b423 Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Thu, 22 Aug 2024 18:19:55 -0400 Subject: Check `int(abs)` instead to avoid overflows --- core/bytes/buffer.odin | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'core/bytes/buffer.odin') diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index 0399beea4..a61f8f00d 100644 --- a/core/bytes/buffer.odin +++ b/core/bytes/buffer.odin @@ -323,11 +323,12 @@ buffer_seek :: proc(b: ^Buffer, offset: i64, whence: io.Seek_From) -> (i64, io.E return 0, .Invalid_Whence } - if abs < 0 { + abs_int := int(abs) + if abs_int < 0 { return 0, .Invalid_Offset } b.last_read = .Invalid - b.off = int(abs) + b.off = abs_int return abs, nil } -- cgit v1.2.3 From f453054affd7b85f8c751c4f62b21788ae9d8ceb Mon Sep 17 00:00:00 2001 From: Feoramund <161657516+Feoramund@users.noreply.github.com> Date: Tue, 27 Aug 2024 18:45:13 -0400 Subject: Return `0, nil` in all `io` cases where an empty slice is provided --- core/bytes/buffer.odin | 6 ++++++ core/bytes/reader.odin | 6 ++++++ core/io/util.odin | 9 +++++++++ core/os/os2/file_linux.odin | 6 ++++++ core/os/os2/file_windows.odin | 6 +++++- core/os/stream.odin | 12 ++++++++++++ 6 files changed, 44 insertions(+), 1 deletion(-) (limited to 'core/bytes/buffer.odin') diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin index a61f8f00d..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,6 +249,9 @@ 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) { diff --git a/core/bytes/reader.odin b/core/bytes/reader.odin index d85e4fe13..2e1c5ed42 100644 --- a/core/bytes/reader.odin +++ b/core/bytes/reader.odin @@ -34,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 } @@ -43,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 } diff --git a/core/io/util.odin b/core/io/util.odin index 27c344890..e65a69fb3 100644 --- a/core/io/util.odin +++ b/core/io/util.odin @@ -340,6 +340,9 @@ _limited_reader_proc :: proc(stream_data: rawptr, mode: Stream_Mode, p: []byte, l := (^Limited_Reader)(stream_data) #partial switch mode { case .Read: + if len(p) == 0 { + return 0, nil + } if l.n <= 0 { return 0, .EOF } @@ -394,6 +397,9 @@ _section_reader_proc :: proc(stream_data: rawptr, mode: Stream_Mode, p: []byte, s := (^Section_Reader)(stream_data) #partial switch mode { case .Read: + if len(p) == 0 { + return 0, nil + } if s.off >= s.limit { return 0, .EOF } @@ -405,6 +411,9 @@ _section_reader_proc :: proc(stream_data: rawptr, mode: Stream_Mode, p: []byte, s.off += i64(n) return case .Read_At: + if len(p) == 0 { + return 0, nil + } p, off := p, offset if off < 0 || off >= s.limit - s.base { diff --git a/core/os/os2/file_linux.odin b/core/os/os2/file_linux.odin index 11be64741..ad6ddbf17 100644 --- a/core/os/os2/file_linux.odin +++ b/core/os/os2/file_linux.odin @@ -201,6 +201,9 @@ _read :: proc(f: ^File_Impl, p: []byte) -> (i64, Error) { } _read_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (i64, Error) { + if len(p) == 0 { + return 0, nil + } if offset < 0 { return 0, .Invalid_Offset } @@ -226,6 +229,9 @@ _write :: proc(f: ^File_Impl, p: []byte) -> (i64, Error) { } _write_at :: proc(f: ^File_Impl, p: []byte, offset: i64) -> (i64, Error) { + if len(p) == 0 { + return 0, nil + } if offset < 0 { return 0, .Invalid_Offset } diff --git a/core/os/os2/file_windows.odin b/core/os/os2/file_windows.odin index 56ac7aaae..47e5f0cf2 100644 --- a/core/os/os2/file_windows.odin +++ b/core/os/os2/file_windows.odin @@ -266,6 +266,11 @@ _read :: proc(f: ^File_Impl, p: []byte) -> (n: i64, err: Error) { } _read_internal :: proc(f: ^File_Impl, p: []byte) -> (n: i64, err: Error) { + length := len(p) + if length == 0 { + return + } + read_console :: proc(handle: win32.HANDLE, b: []byte) -> (n: int, err: Error) { if len(b) == 0 { return 0, nil @@ -320,7 +325,6 @@ _read_internal :: proc(f: ^File_Impl, p: []byte) -> (n: i64, err: Error) { single_read_length: win32.DWORD total_read: int - length := len(p) sync.shared_guard(&f.rw_mutex) // multiple readers diff --git a/core/os/stream.odin b/core/os/stream.odin index ad8c2461c..39edc9cd5 100644 --- a/core/os/stream.odin +++ b/core/os/stream.odin @@ -21,6 +21,9 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, case .Flush: os_err = flush(fd) case .Read: + if len(p) == 0 { + return 0, nil + } n_int, os_err = read(fd, p) n = i64(n_int) if n == 0 && os_err == nil { @@ -28,18 +31,27 @@ _file_stream_proc :: proc(stream_data: rawptr, mode: io.Stream_Mode, p: []byte, } case .Read_At: + if len(p) == 0 { + return 0, nil + } n_int, os_err = read_at(fd, p, offset) n = i64(n_int) if n == 0 && os_err == nil { err = .EOF } case .Write: + if len(p) == 0 { + return 0, nil + } n_int, os_err = write(fd, p) n = i64(n_int) if n == 0 && os_err == nil { err = .EOF } case .Write_At: + if len(p) == 0 { + return 0, nil + } n_int, os_err = write_at(fd, p, offset) n = i64(n_int) if n == 0 && os_err == nil { -- cgit v1.2.3