mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Add an extra optional ^int parameter to most io read/write calls allowing for simplified or_return use with incrementing byte usage
This commit is contained in:
+77
-77
@@ -3,49 +3,49 @@ package io
|
||||
import "core:strconv"
|
||||
import "core:unicode/utf8"
|
||||
|
||||
read_ptr :: proc(r: Reader, p: rawptr, byte_size: int) -> (n: int, err: Error) {
|
||||
return read(r, ([^]byte)(p)[:byte_size])
|
||||
read_ptr :: proc(r: Reader, p: rawptr, byte_size: int, n_read: ^int = nil) -> (n: int, err: Error) {
|
||||
return read(r, ([^]byte)(p)[:byte_size], n_read)
|
||||
}
|
||||
|
||||
write_ptr :: proc(w: Writer, p: rawptr, byte_size: int) -> (n: int, err: Error) {
|
||||
return write(w, ([^]byte)(p)[:byte_size])
|
||||
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)
|
||||
}
|
||||
|
||||
read_ptr_at :: proc(r: Reader_At, p: rawptr, byte_size: int, offset: i64) -> (n: int, err: Error) {
|
||||
return read_at(r, ([^]byte)(p)[:byte_size], offset)
|
||||
read_ptr_at :: proc(r: Reader_At, p: rawptr, byte_size: int, offset: i64, n_read: ^int = nil) -> (n: int, err: Error) {
|
||||
return read_at(r, ([^]byte)(p)[:byte_size], offset, n_read)
|
||||
}
|
||||
|
||||
write_ptr_at :: proc(w: Writer_At, p: rawptr, byte_size: int, offset: i64) -> (n: int, err: Error) {
|
||||
return write_at(w, ([^]byte)(p)[:byte_size], offset)
|
||||
write_ptr_at :: proc(w: Writer_At, p: rawptr, byte_size: int, offset: i64, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
return write_at(w, ([^]byte)(p)[:byte_size], offset, n_written)
|
||||
}
|
||||
|
||||
write_u64 :: proc(w: Writer, i: u64, base: int = 10) -> (n: int, err: Error) {
|
||||
write_u64 :: proc(w: Writer, i: u64, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
buf: [32]byte
|
||||
s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil)
|
||||
return write_string(w, s)
|
||||
return write_string(w, s, n_written)
|
||||
}
|
||||
write_i64 :: proc(w: Writer, i: i64, base: int = 10) -> (n: int, err: Error) {
|
||||
write_i64 :: proc(w: Writer, i: i64, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
buf: [32]byte
|
||||
s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
|
||||
return write_string(w, s)
|
||||
return write_string(w, s, n_written)
|
||||
}
|
||||
|
||||
write_uint :: proc(w: Writer, i: uint, base: int = 10) -> (n: int, err: Error) {
|
||||
return write_u64(w, u64(i), base)
|
||||
write_uint :: proc(w: Writer, i: uint, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
return write_u64(w, u64(i), base, n_written)
|
||||
}
|
||||
write_int :: proc(w: Writer, i: int, base: int = 10) -> (n: int, err: Error) {
|
||||
return write_i64(w, i64(i), base)
|
||||
write_int :: proc(w: Writer, i: int, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
return write_i64(w, i64(i), base, n_written)
|
||||
}
|
||||
|
||||
write_u128 :: proc(w: Writer, i: u128, base: int = 10) -> (n: int, err: Error) {
|
||||
write_u128 :: proc(w: Writer, i: u128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
buf: [32]byte
|
||||
s := strconv.append_bits_128(buf[:], i, base, false, 128, strconv.digits, nil)
|
||||
return write_string(w, s)
|
||||
return write_string(w, s, n_written)
|
||||
}
|
||||
write_i128 :: proc(w: Writer, i: i128, base: int = 10) -> (n: int, err: Error) {
|
||||
write_i128 :: proc(w: Writer, i: i128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
buf: [32]byte
|
||||
s := strconv.append_bits_128(buf[:], u128(i), base, true, 128, strconv.digits, nil)
|
||||
return write_string(w, s)
|
||||
return write_string(w, s, n_written)
|
||||
}
|
||||
|
||||
|
||||
@@ -54,57 +54,51 @@ write_i128 :: proc(w: Writer, i: i128, base: int = 10) -> (n: int, err: Error) {
|
||||
@(private="file")
|
||||
DIGITS_LOWER := "0123456789abcdefx"
|
||||
|
||||
@(private="file")
|
||||
n_write_byte :: proc(w: Writer, b: byte, bytes_processed: ^int) -> Error {
|
||||
err := write_byte(w, b)
|
||||
if err == nil {
|
||||
bytes_processed^ += 1
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
n_wrapper :: proc(n: int, err: Error, bytes_processed: ^int) -> Error {
|
||||
bytes_processed^ += n
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
write_encoded_rune :: proc(w: Writer, r: rune, write_quote := true) -> (n: int, err: Error) {
|
||||
write_encoded_rune :: proc(w: Writer, r: rune, write_quote := true, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
defer if n_written != nil {
|
||||
n_written^ += n
|
||||
}
|
||||
if write_quote {
|
||||
n_write_byte(w, '\'', &n) or_return
|
||||
write_byte(w, '\'', &n) or_return
|
||||
}
|
||||
switch r {
|
||||
case '\a': n_wrapper(write_string(w, `\a"`), &n) or_return
|
||||
case '\b': n_wrapper(write_string(w, `\b"`), &n) or_return
|
||||
case '\e': n_wrapper(write_string(w, `\e"`), &n) or_return
|
||||
case '\f': n_wrapper(write_string(w, `\f"`), &n) or_return
|
||||
case '\n': n_wrapper(write_string(w, `\n"`), &n) or_return
|
||||
case '\r': n_wrapper(write_string(w, `\r"`), &n) or_return
|
||||
case '\t': n_wrapper(write_string(w, `\t"`), &n) or_return
|
||||
case '\v': n_wrapper(write_string(w, `\v"`), &n) or_return
|
||||
case '\a': write_string(w, `\a"`, &n) or_return
|
||||
case '\b': write_string(w, `\b"`, &n) or_return
|
||||
case '\e': write_string(w, `\e"`, &n) or_return
|
||||
case '\f': write_string(w, `\f"`, &n) or_return
|
||||
case '\n': write_string(w, `\n"`, &n) or_return
|
||||
case '\r': write_string(w, `\r"`, &n) or_return
|
||||
case '\t': write_string(w, `\t"`, &n) or_return
|
||||
case '\v': write_string(w, `\v"`, &n) or_return
|
||||
case:
|
||||
if r < 32 {
|
||||
n_wrapper(write_string(w, `\x`), &n) or_return
|
||||
write_string(w, `\x`, &n) or_return
|
||||
|
||||
buf: [2]byte
|
||||
s := strconv.append_bits(buf[:], u64(r), 16, true, 64, strconv.digits, nil)
|
||||
switch len(s) {
|
||||
case 0: n_wrapper(write_string(w, "00"), &n) or_return
|
||||
case 1: n_write_byte(w, '0', &n) or_return
|
||||
case 2: n_wrapper(write_string(w, s), &n) or_return
|
||||
case 0: write_string(w, "00", &n) or_return
|
||||
case 1: write_byte(w, '0', &n) or_return
|
||||
case 2: write_string(w, s, &n) or_return
|
||||
}
|
||||
} else {
|
||||
n_wrapper(write_rune(w, r), &n) or_return
|
||||
write_rune(w, r, &n) or_return
|
||||
}
|
||||
|
||||
}
|
||||
if write_quote {
|
||||
n_write_byte(w, '\'', &n) or_return
|
||||
write_byte(w, '\'', &n) or_return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
write_escaped_rune :: proc(w: Writer, r: rune, quote: byte, html_safe := false) -> (n: int, err: Error) {
|
||||
write_escaped_rune :: proc(w: Writer, r: rune, quote: byte, html_safe := false, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
is_printable :: proc(r: rune) -> bool {
|
||||
if r <= 0xff {
|
||||
switch r {
|
||||
@@ -118,66 +112,72 @@ write_escaped_rune :: proc(w: Writer, r: rune, quote: byte, html_safe := false)
|
||||
// TODO(bill): A proper unicode library will be needed!
|
||||
return false
|
||||
}
|
||||
defer if n_written != nil {
|
||||
n_written^ += n
|
||||
}
|
||||
|
||||
if html_safe {
|
||||
switch r {
|
||||
case '<', '>', '&':
|
||||
n_write_byte(w, '\\', &n) or_return
|
||||
n_write_byte(w, 'u', &n) or_return
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, 'u', &n) or_return
|
||||
for s := 12; s >= 0; s -= 4 {
|
||||
n_write_byte(w, DIGITS_LOWER[r>>uint(s) & 0xf], &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[r>>uint(s) & 0xf], &n) or_return
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if r == rune(quote) || r == '\\' {
|
||||
n_write_byte(w, '\\', &n) or_return
|
||||
n_write_byte(w, byte(r), &n) or_return
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, byte(r), &n) or_return
|
||||
return
|
||||
} else if is_printable(r) {
|
||||
n_wrapper(write_encoded_rune(w, r, false), &n) or_return
|
||||
write_encoded_rune(w, r, false, &n) or_return
|
||||
return
|
||||
}
|
||||
switch r {
|
||||
case '\a': n_wrapper(write_string(w, `\a`), &n) or_return
|
||||
case '\b': n_wrapper(write_string(w, `\b`), &n) or_return
|
||||
case '\e': n_wrapper(write_string(w, `\e`), &n) or_return
|
||||
case '\f': n_wrapper(write_string(w, `\f`), &n) or_return
|
||||
case '\n': n_wrapper(write_string(w, `\n`), &n) or_return
|
||||
case '\r': n_wrapper(write_string(w, `\r`), &n) or_return
|
||||
case '\t': n_wrapper(write_string(w, `\t`), &n) or_return
|
||||
case '\v': n_wrapper(write_string(w, `\v`), &n) or_return
|
||||
case '\a': write_string(w, `\a`, &n) or_return
|
||||
case '\b': write_string(w, `\b`, &n) or_return
|
||||
case '\e': write_string(w, `\e`, &n) or_return
|
||||
case '\f': write_string(w, `\f`, &n) or_return
|
||||
case '\n': write_string(w, `\n`, &n) or_return
|
||||
case '\r': write_string(w, `\r`, &n) or_return
|
||||
case '\t': write_string(w, `\t`, &n) or_return
|
||||
case '\v': write_string(w, `\v`, &n) or_return
|
||||
case:
|
||||
switch c := r; {
|
||||
case c < ' ':
|
||||
n_write_byte(w, '\\', &n) or_return
|
||||
n_write_byte(w, 'x', &n) or_return
|
||||
n_write_byte(w, DIGITS_LOWER[byte(c)>>4], &n) or_return
|
||||
n_write_byte(w, DIGITS_LOWER[byte(c)&0xf], &n) or_return
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, 'x', &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[byte(c)>>4], &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[byte(c)&0xf], &n) or_return
|
||||
|
||||
case c > utf8.MAX_RUNE:
|
||||
c = 0xfffd
|
||||
fallthrough
|
||||
case c < 0x10000:
|
||||
n_write_byte(w, '\\', &n) or_return
|
||||
n_write_byte(w, 'u', &n) or_return
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, 'u', &n) or_return
|
||||
for s := 12; s >= 0; s -= 4 {
|
||||
n_write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf], &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf], &n) or_return
|
||||
}
|
||||
case:
|
||||
n_write_byte(w, '\\', &n) or_return
|
||||
n_write_byte(w, 'U', &n) or_return
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, 'U', &n) or_return
|
||||
for s := 28; s >= 0; s -= 4 {
|
||||
n_write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf], &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[c>>uint(s) & 0xf], &n) or_return
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
write_quoted_string :: proc(w: Writer, str: string, quote: byte = '"') -> (n: int, err: Error) {
|
||||
n_write_byte(w, quote, &n) or_return
|
||||
write_quoted_string :: proc(w: Writer, str: string, quote: byte = '"', n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
defer if n_written != nil {
|
||||
n_written^ += n
|
||||
}
|
||||
write_byte(w, quote, &n) or_return
|
||||
for width, s := 0, str; len(s) > 0; s = s[width:] {
|
||||
r := rune(s[0])
|
||||
width = 1
|
||||
@@ -185,17 +185,17 @@ write_quoted_string :: proc(w: Writer, str: string, quote: byte = '"') -> (n: in
|
||||
r, width = utf8.decode_rune_in_string(s)
|
||||
}
|
||||
if width == 1 && r == utf8.RUNE_ERROR {
|
||||
n_write_byte(w, '\\', &n) or_return
|
||||
n_write_byte(w, 'x', &n) or_return
|
||||
n_write_byte(w, DIGITS_LOWER[s[0]>>4], &n) or_return
|
||||
n_write_byte(w, DIGITS_LOWER[s[0]&0xf], &n) or_return
|
||||
write_byte(w, '\\', &n) or_return
|
||||
write_byte(w, 'x', &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[s[0]>>4], &n) or_return
|
||||
write_byte(w, DIGITS_LOWER[s[0]&0xf], &n) or_return
|
||||
continue
|
||||
}
|
||||
|
||||
n_wrapper(write_escaped_rune(w, r, quote), &n) or_return
|
||||
|
||||
}
|
||||
n_write_byte(w, quote, &n) or_return
|
||||
write_byte(w, quote, &n) or_return
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user