aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2026-02-17 14:34:28 +0000
committergingerBill <gingerBill@users.noreply.github.com>2026-02-17 14:34:28 +0000
commit966b76c4cba2d121ccf65d1a69e4bcbdbc23855c (patch)
tree200984718dde2e1828b86924ae24ddc47d2583d4
parenta7ed7ccd0c392cfbb6cb2e486fc7c75b16791902 (diff)
Add `write_slice`/`read_slice` utility procedures to `core:bytes`, `core:io`, and `core:os`
-rw-r--r--core/bytes/buffer.odin11
-rw-r--r--core/io/util.odin11
-rw-r--r--core/os/file_util.odin19
3 files changed, 41 insertions, 0 deletions
diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin
index dd4b378c0..5fd3145b7 100644
--- a/core/bytes/buffer.odin
+++ b/core/bytes/buffer.odin
@@ -176,6 +176,11 @@ buffer_write_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int, loc := #caller_loca
return buffer_write(b, ([^]byte)(ptr)[:size], loc=loc)
}
+buffer_write_slice :: proc(b: ^Buffer, slice: $S/[]$T, loc := #caller_location) -> (n: int, err: io.Error) {
+ size := len(slice)*size_of(T)
+ return buffer_write(b, ([^]byte)(raw_data(slice))[:size], loc=loc)
+}
+
buffer_write_string :: proc(b: ^Buffer, s: string, loc := #caller_location) -> (n: int, err: io.Error) {
b.last_read = .Invalid
m, ok := _buffer_try_grow(b, len(s), loc=loc)
@@ -248,6 +253,12 @@ buffer_read_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.
return buffer_read(b, ([^]byte)(ptr)[:size])
}
+buffer_read_slice :: proc(b: ^Buffer, slice: $S/[]$T) -> (n: int, err: io.Error) {
+ size := len(slice)*size_of(T)
+ return buffer_read(b, ([^]byte)(raw_data(slice))[:size])
+}
+
+
buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
if len(p) == 0 {
return 0, nil
diff --git a/core/io/util.odin b/core/io/util.odin
index a82a80be0..c1d8d2808 100644
--- a/core/io/util.odin
+++ b/core/io/util.odin
@@ -8,6 +8,11 @@ read_ptr :: proc(r: Reader, p: rawptr, byte_size: int, n_read: ^int = nil) -> (n
return read(r, ([^]byte)(p)[:byte_size], n_read)
}
+read_slice :: proc(r: Reader, p: rawptr, byte_size: int, n_read: ^int = nil) -> (n: int, err: Error) {
+ size := len(slice)*size_of(T)
+ return read_ptr(w, raw_data(slice), size, n_read)
+}
+
write_ptr :: proc(w: Writer, p: rawptr, byte_size: int, n_written: ^int = nil) -> (n: int, err: Error) {
return write(w, ([^]byte)(p)[:byte_size], n_written)
}
@@ -20,6 +25,12 @@ write_ptr_at :: proc(w: Writer_At, p: rawptr, byte_size: int, offset: i64, n_wri
return write_at(w, ([^]byte)(p)[:byte_size], offset, n_written)
}
+write_slice :: proc(w: Writer, slice: $S/[]$T, n_written: ^int = nil) -> (n: int, err: Error) {
+ size := len(slice)*size_of(T)
+ return write_ptr(w, raw_data(slice), size, n_written)
+}
+
+
write_u64 :: proc(w: Writer, i: u64, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
buf: [64]byte
s := strconv.write_bits(buf[:], i, base, false, 64, strconv.digits, nil)
diff --git a/core/os/file_util.odin b/core/os/file_util.odin
index 505432338..235362e04 100644
--- a/core/os/file_util.odin
+++ b/core/os/file_util.odin
@@ -111,6 +111,25 @@ read_ptr :: proc(f: ^File, data: rawptr, len: int) -> (n: int, err: Error) {
}
+/*
+ `write_slice` is a utility procedure that writes the bytes points at `slice`.
+
+ It is equivalent to: `write(f, ([^]byte)(raw_data(slice))[:len(slice)*size_of(slice[0]))])`
+*/
+write_slice :: proc(f: ^File, slice: $S/[]$T) -> (n: int, err: Error) {
+ return write(f, ([^]byte)(raw_data(slice))[:len(slice)*size_of(slice[0]))])
+}
+
+/*
+ `read_slice` is a utility procedure that writes the bytes points at `slice`.
+
+ It is equivalent to: `read(f, ([^]byte)(raw_data(slice))[:len(slice)*size_of(slice[0]))])`
+*/
+read_slice :: proc(f: ^File, slice: $S/[]$T) -> (n: int, err: Error) {
+ return read(f, ([^]byte)(raw_data(slice))[:len(slice)*size_of(slice[0]))])
+}
+
+
/*
`read_at_least` reads from `f` into `buf` until it has read at least `min` bytes.