diff options
| author | Colin Davidson <colrdavidson@gmail.com> | 2024-09-24 02:32:06 -0700 |
|---|---|---|
| committer | Colin Davidson <colrdavidson@gmail.com> | 2024-09-24 02:32:06 -0700 |
| commit | f3ab14b8ccb45d0fef8a96937635bdf0943ce7d6 (patch) | |
| tree | 1309d7c797117463996a84522ef3d1c9713a286c /core/bytes | |
| parent | 99938c7d4fb26d43a07dd4b8f4f00ab87e67e73f (diff) | |
| parent | f7d74ff3a8596efef67d151ffb758ed085e94be0 (diff) | |
Merge branch 'master' into macharena
Diffstat (limited to 'core/bytes')
| -rw-r--r-- | core/bytes/buffer.odin | 36 | ||||
| -rw-r--r-- | core/bytes/bytes.odin | 12 | ||||
| -rw-r--r-- | core/bytes/reader.odin | 11 |
3 files changed, 47 insertions, 12 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/bytes.odin b/core/bytes/bytes.odin index 5a510951e..c0d25bcce 100644 --- a/core/bytes/bytes.odin +++ b/core/bytes/bytes.odin @@ -334,7 +334,7 @@ Inputs: Returns: - index: The index of the byte `c`, or -1 if it was not found. */ -index_byte :: proc(s: []byte, c: byte) -> (index: int) #no_bounds_check { +index_byte :: proc "contextless" (s: []byte, c: byte) -> (index: int) #no_bounds_check { i, l := 0, len(s) // Guard against small strings. On modern systems, it is ALWAYS @@ -469,18 +469,16 @@ Inputs: Returns: - index: The index of the byte `c`, or -1 if it was not found. */ -last_index_byte :: proc(s: []byte, c: byte) -> int #no_bounds_check { +last_index_byte :: proc "contextless" (s: []byte, c: byte) -> int #no_bounds_check { i := len(s) // Guard against small strings. On modern systems, it is ALWAYS // worth vectorizing assuming there is a hardware vector unit, and // the data size is large enough. if i < SIMD_REG_SIZE_128 { - if i > 0 { // Handle s == nil. - for /**/; i >= 0; i -= 1 { - if s[i] == c { - return i - } + #reverse for ch, j in s { + if ch == c { + return j } } return -1 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) { |