Clarify strconv.append_* to strconv.write_*

This commit is contained in:
Feoramund
2025-06-05 16:56:00 -04:00
parent 90a1b0ec4b
commit b7de15caa3
12 changed files with 81 additions and 81 deletions
+3 -3
View File
@@ -385,17 +385,17 @@ to_diagnostic_format_writer :: proc(w: io.Writer, val: Value, padding := 0) -> i
// which we want for the diagnostic format. // which we want for the diagnostic format.
case f16: case f16:
buf: [64]byte buf: [64]byte
str := strconv.append_float(buf[:], f64(v), 'f', 2*size_of(f16), 8*size_of(f16)) str := strconv.write_float(buf[:], f64(v), 'f', 2*size_of(f16), 8*size_of(f16))
if str[0] == '+' && str != "+Inf" { str = str[1:] } if str[0] == '+' && str != "+Inf" { str = str[1:] }
io.write_string(w, str) or_return io.write_string(w, str) or_return
case f32: case f32:
buf: [128]byte buf: [128]byte
str := strconv.append_float(buf[:], f64(v), 'f', 2*size_of(f32), 8*size_of(f32)) str := strconv.write_float(buf[:], f64(v), 'f', 2*size_of(f32), 8*size_of(f32))
if str[0] == '+' && str != "+Inf" { str = str[1:] } if str[0] == '+' && str != "+Inf" { str = str[1:] }
io.write_string(w, str) or_return io.write_string(w, str) or_return
case f64: case f64:
buf: [256]byte buf: [256]byte
str := strconv.append_float(buf[:], f64(v), 'f', 2*size_of(f64), 8*size_of(f64)) str := strconv.write_float(buf[:], f64(v), 'f', 2*size_of(f64), 8*size_of(f64))
if str[0] == '+' && str != "+Inf" { str = str[1:] } if str[0] == '+' && str != "+Inf" { str = str[1:] }
io.write_string(w, str) or_return io.write_string(w, str) or_return
+4 -4
View File
@@ -108,13 +108,13 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
if opt.write_uint_as_hex && (opt.spec == .JSON5 || opt.spec == .MJSON) { if opt.write_uint_as_hex && (opt.spec == .JSON5 || opt.spec == .MJSON) {
switch i in a { switch i in a {
case u8, u16, u32, u64, u128: case u8, u16, u32, u64, u128:
s = strconv.append_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix }) s = strconv.write_bits_128(buf[:], u, 16, info.signed, 8*ti.size, "0123456789abcdef", { .Prefix })
case: case:
s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil) s = strconv.write_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
} }
} else { } else {
s = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil) s = strconv.write_bits_128(buf[:], u, 10, info.signed, 8*ti.size, "0123456789", nil)
} }
io.write_string(w, s) or_return io.write_string(w, s) or_return
@@ -286,7 +286,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
case runtime.Type_Info_Integer: case runtime.Type_Info_Integer:
buf: [40]byte buf: [40]byte
u := cast_any_int_to_u128(ka) u := cast_any_int_to_u128(ka)
name = strconv.append_bits_128(buf[:], u, 10, info.signed, 8*kti.size, "0123456789", nil) name = strconv.write_bits_128(buf[:], u, 10, info.signed, 8*kti.size, "0123456789", nil)
opt_write_key(w, opt, name) or_return opt_write_key(w, opt, name) or_return
case: return .Unsupported_Type case: return .Unsupported_Type
+4 -4
View File
@@ -1122,7 +1122,7 @@ _fmt_int :: proc(fi: ^Info, u: u64, base: int, is_signed: bool, bit_size: int, d
flags: strconv.Int_Flags flags: strconv.Int_Flags
if fi.hash && !fi.zero && start == 0 { flags += {.Prefix} } if fi.hash && !fi.zero && start == 0 { flags += {.Prefix} }
if fi.plus { flags += {.Plus} } if fi.plus { flags += {.Plus} }
s := strconv.append_bits(buf[start:], u, base, is_signed, bit_size, digits, flags) s := strconv.write_bits(buf[start:], u, base, is_signed, bit_size, digits, flags)
prev_zero := fi.zero prev_zero := fi.zero
defer fi.zero = prev_zero defer fi.zero = prev_zero
fi.zero = false fi.zero = false
@@ -1207,7 +1207,7 @@ _fmt_int_128 :: proc(fi: ^Info, u: u128, base: int, is_signed: bool, bit_size: i
flags: strconv.Int_Flags flags: strconv.Int_Flags
if fi.hash && !fi.zero && start == 0 { flags += {.Prefix} } if fi.hash && !fi.zero && start == 0 { flags += {.Prefix} }
if fi.plus { flags += {.Plus} } if fi.plus { flags += {.Plus} }
s := strconv.append_bits_128(buf[start:], u, base, is_signed, bit_size, digits, flags) s := strconv.write_bits_128(buf[start:], u, base, is_signed, bit_size, digits, flags)
if fi.hash && fi.zero && fi.indent == 0 { if fi.hash && fi.zero && fi.indent == 0 {
c: byte = 0 c: byte = 0
@@ -1272,7 +1272,7 @@ _fmt_memory :: proc(fi: ^Info, u: u64, is_signed: bool, bit_size: int, units: st
} }
buf: [256]byte buf: [256]byte
str := strconv.append_float(buf[:], amt, 'f', prec, 64) str := strconv.write_float(buf[:], amt, 'f', prec, 64)
// Add the unit at the end. // Add the unit at the end.
copy(buf[len(str):], units[off:off+unit_len]) copy(buf[len(str):], units[off:off+unit_len])
@@ -1424,7 +1424,7 @@ _fmt_float_as :: proc(fi: ^Info, v: f64, bit_size: int, verb: rune, float_fmt: b
buf: [386]byte buf: [386]byte
// Can return "NaN", "+Inf", "-Inf", "+<value>", "-<value>". // Can return "NaN", "+Inf", "-Inf", "+<value>", "-<value>".
str := strconv.append_float(buf[:], v, float_fmt, prec, bit_size) str := strconv.write_float(buf[:], v, float_fmt, prec, bit_size)
if !fi.plus { if !fi.plus {
// Strip sign from "+<value>" but not "+Inf". // Strip sign from "+<value>" but not "+Inf".
+8 -8
View File
@@ -22,12 +22,12 @@ write_ptr_at :: proc(w: Writer_At, p: rawptr, byte_size: int, offset: i64, n_wri
write_u64 :: proc(w: Writer, i: u64, base: int = 10, n_written: ^int = nil) -> (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 buf: [32]byte
s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil) s := strconv.write_bits(buf[:], i, base, false, 64, strconv.digits, nil)
return write_string(w, s, n_written) return write_string(w, s, n_written)
} }
write_i64 :: proc(w: Writer, i: i64, base: int = 10, n_written: ^int = nil) -> (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 buf: [32]byte
s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil) s := strconv.write_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
return write_string(w, s, n_written) return write_string(w, s, n_written)
} }
@@ -40,18 +40,18 @@ write_int :: proc(w: Writer, i: int, base: int = 10, n_written: ^int = nil) -> (
write_u128 :: proc(w: Writer, i: u128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) { write_u128 :: proc(w: Writer, i: u128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
buf: [39]byte buf: [39]byte
s := strconv.append_bits_128(buf[:], i, base, false, 128, strconv.digits, nil) s := strconv.write_bits_128(buf[:], i, base, false, 128, strconv.digits, nil)
return write_string(w, s, n_written) return write_string(w, s, n_written)
} }
write_i128 :: proc(w: Writer, i: i128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) { write_i128 :: proc(w: Writer, i: i128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
buf: [40]byte buf: [40]byte
s := strconv.append_bits_128(buf[:], u128(i), base, true, 128, strconv.digits, nil) s := strconv.write_bits_128(buf[:], u128(i), base, true, 128, strconv.digits, nil)
return write_string(w, s, n_written) return write_string(w, s, n_written)
} }
write_f16 :: proc(w: Writer, val: f16, n_written: ^int = nil) -> (n: int, err: Error) { write_f16 :: proc(w: Writer, val: f16, n_written: ^int = nil) -> (n: int, err: Error) {
buf: [386]byte buf: [386]byte
str := strconv.append_float(buf[1:], f64(val), 'f', 2*size_of(val), 8*size_of(val)) str := strconv.write_float(buf[1:], f64(val), 'f', 2*size_of(val), 8*size_of(val))
s := buf[:len(str)+1] s := buf[:len(str)+1]
if s[1] == '+' || s[1] == '-' { if s[1] == '+' || s[1] == '-' {
s = s[1:] s = s[1:]
@@ -67,7 +67,7 @@ write_f16 :: proc(w: Writer, val: f16, n_written: ^int = nil) -> (n: int, err: E
write_f32 :: proc(w: Writer, val: f32, n_written: ^int = nil) -> (n: int, err: Error) { write_f32 :: proc(w: Writer, val: f32, n_written: ^int = nil) -> (n: int, err: Error) {
buf: [386]byte buf: [386]byte
str := strconv.append_float(buf[1:], f64(val), 'f', 2*size_of(val), 8*size_of(val)) str := strconv.write_float(buf[1:], f64(val), 'f', 2*size_of(val), 8*size_of(val))
s := buf[:len(str)+1] s := buf[:len(str)+1]
if s[1] == '+' || s[1] == '-' { if s[1] == '+' || s[1] == '-' {
s = s[1:] s = s[1:]
@@ -83,7 +83,7 @@ write_f32 :: proc(w: Writer, val: f32, n_written: ^int = nil) -> (n: int, err: E
write_f64 :: proc(w: Writer, val: f64, n_written: ^int = nil) -> (n: int, err: Error) { write_f64 :: proc(w: Writer, val: f64, n_written: ^int = nil) -> (n: int, err: Error) {
buf: [386]byte buf: [386]byte
str := strconv.append_float(buf[1:], val, 'f', 2*size_of(val), 8*size_of(val)) str := strconv.write_float(buf[1:], val, 'f', 2*size_of(val), 8*size_of(val))
s := buf[:len(str)+1] s := buf[:len(str)+1]
if s[1] == '+' || s[1] == '-' { if s[1] == '+' || s[1] == '-' {
s = s[1:] s = s[1:]
@@ -130,7 +130,7 @@ write_encoded_rune :: proc(w: Writer, r: rune, write_quote := true, n_written: ^
write_string(w, `\x`, &n) or_return write_string(w, `\x`, &n) or_return
buf: [2]byte buf: [2]byte
s := strconv.append_bits(buf[:], u64(r), 16, true, 64, strconv.digits, nil) s := strconv.write_bits(buf[:], u64(r), 16, true, 64, strconv.digits, nil)
switch len(s) { switch len(s) {
case 0: case 0:
write_string(w, "00", &n) or_return write_string(w, "00", &n) or_return
+3 -3
View File
@@ -124,16 +124,16 @@ append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
when size_of(Backing) < 16 { when size_of(Backing) < 16 {
T :: u64 T :: u64
append_uint :: strconv.append_uint write_uint :: strconv.write_uint
} else { } else {
T :: u128 T :: u128
append_uint :: strconv.append_u128 write_uint :: strconv.write_u128
} }
integer := T(x.i) >> Fraction_Width integer := T(x.i) >> Fraction_Width
fraction := T(x.i) & (1<<Fraction_Width - 1) fraction := T(x.i) & (1<<Fraction_Width - 1)
s := append_uint(buf[i:], integer, 10) s := write_uint(buf[i:], integer, 10)
i += len(s) i += len(s)
if fraction != 0 { if fraction != 0 {
buf[i] = '.' buf[i] = '.'
+1 -1
View File
@@ -125,7 +125,7 @@ percent_encode :: proc(s: string, allocator := context.allocator) -> string {
bytes, n := utf8.encode_rune(ch) bytes, n := utf8.encode_rune(ch)
for byte in bytes[:n] { for byte in bytes[:n] {
buf: [2]u8 = --- buf: [2]u8 = ---
t := strconv.append_int(buf[:], i64(byte), 16) t := strconv.write_int(buf[:], i64(byte), 16)
strings.write_rune(&b, '%') strings.write_rune(&b, '%')
strings.write_string(&b, t) strings.write_string(&b, t)
} }
+1 -1
View File
@@ -57,7 +57,7 @@ write_encoded_rune :: proc(f: Handle, r: rune) -> (n: int, err: Error) {
if r < 32 { if r < 32 {
if wrap(write_string(f, "\\x"), &n, &err) { return } if wrap(write_string(f, "\\x"), &n, &err) { return }
b: [2]byte b: [2]byte
s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil) s := strconv.write_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
switch len(s) { switch len(s) {
case 0: if wrap(write_string(f, "00"), &n, &err) { return } case 0: if wrap(write_string(f, "00"), &n, &err) { return }
case 1: if wrap(write_rune(f, '0'), &n, &err) { return } case 1: if wrap(write_rune(f, '0'), &n, &err) { return }
+1 -1
View File
@@ -59,7 +59,7 @@ write_encoded_rune :: proc(f: ^File, r: rune) -> (n: int, err: Error) {
if r < 32 { if r < 32 {
if wrap(write_string(f, "\\x"), &n, &err) { return } if wrap(write_string(f, "\\x"), &n, &err) { return }
b: [2]byte b: [2]byte
s := strconv.append_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil) s := strconv.write_bits(b[:], u64(r), 16, true, 64, strconv.digits, nil)
switch len(s) { switch len(s) {
case 0: if wrap(write_string(f, "00"), &n, &err) { return } case 0: if wrap(write_string(f, "00"), &n, &err) { return }
case 1: if wrap(write_rune(f, '0'), &n, &err) { return } case 1: if wrap(write_rune(f, '0'), &n, &err) { return }
+7 -7
View File
@@ -48,7 +48,7 @@ is_integer_negative :: proc(x: u64, is_signed: bool, bit_size: int) -> (u: u64,
return return
} }
/* /*
Appends the string representation of an integer to a buffer with specified base, flags, and digit set. Writes the string representation of an integer to a buffer with specified base, flags, and digit set.
**Inputs** **Inputs**
- buf: The buffer to append the integer representation to - buf: The buffer to append the integer representation to
@@ -62,9 +62,9 @@ Appends the string representation of an integer to a buffer with specified base,
**Returns** **Returns**
- The string containing the integer representation appended to the buffer - The string containing the integer representation appended to the buffer
*/ */
append_bits :: proc(buf: []byte, x: u64, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string { write_bits :: proc(buf: []byte, x: u64, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string {
if base < 2 || base > MAX_BASE { if base < 2 || base > MAX_BASE {
panic("strconv: illegal base passed to append_bits") panic("strconv: illegal base passed to write_bits")
} }
a: [129]byte a: [129]byte
@@ -146,7 +146,7 @@ is_integer_negative_128 :: proc(x: u128, is_signed: bool, bit_size: int) -> (u:
return return
} }
/* /*
Appends the string representation of a 128-bit integer to a buffer with specified base, flags, and digit set. Writes the string representation of a 128-bit integer to a buffer with specified base, flags, and digit set.
**Inputs** **Inputs**
- buf: The buffer to append the integer representation to - buf: The buffer to append the integer representation to
@@ -158,11 +158,11 @@ Appends the string representation of a 128-bit integer to a buffer with specifie
- flags: The Int_Flags bit set to control integer formatting - flags: The Int_Flags bit set to control integer formatting
**Returns** **Returns**
- The string containing the integer representation appended to the buffer - The string containing the integer representation written to the buffer
*/ */
append_bits_128 :: proc(buf: []byte, x: u128, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string { write_bits_128 :: proc(buf: []byte, x: u128, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string {
if base < 2 || base > MAX_BASE { if base < 2 || base > MAX_BASE {
panic("strconv: illegal base passed to append_bits") panic("strconv: illegal base passed to write_bits")
} }
a: [140]byte a: [140]byte
+42 -42
View File
@@ -1451,19 +1451,19 @@ parse_quaternion64 :: proc(str: string, n: ^int = nil) -> (value: quaternion64,
return cast(quaternion64)v, ok return cast(quaternion64)v, ok
} }
/* /*
Appends a boolean value as a string to the given buffer Writes a boolean value as a string to the given buffer
**Inputs** **Inputs**
- buf: The buffer to append the boolean value to - buf: The buffer to write the boolean value to
- b: The boolean value to be appended - b: The boolean value to be written
Example: Example:
import "core:fmt" import "core:fmt"
import "core:strconv" import "core:strconv"
append_bool_example :: proc() { write_bool_example :: proc() {
buf: [6]byte buf: [6]byte
result := strconv.append_bool(buf[:], true) result := strconv.write_bool(buf[:], true)
fmt.println(result, buf) fmt.println(result, buf)
} }
@@ -1472,9 +1472,9 @@ Output:
true [116, 114, 117, 101, 0, 0] true [116, 114, 117, 101, 0, 0]
**Returns** **Returns**
- The resulting string after appending the boolean value - The resulting string after writing the boolean value
*/ */
append_bool :: proc(buf: []byte, b: bool) -> string { write_bool :: proc(buf: []byte, b: bool) -> string {
n := 0 n := 0
if b { if b {
n = copy(buf, "true") n = copy(buf, "true")
@@ -1484,20 +1484,20 @@ append_bool :: proc(buf: []byte, b: bool) -> string {
return string(buf[:n]) return string(buf[:n])
} }
/* /*
Appends an unsigned integer value as a string to the given buffer with the specified base Writes an unsigned integer value as a string to the given buffer with the specified base
**Inputs** **Inputs**
- buf: The buffer to append the unsigned integer value to - buf: The buffer to write the unsigned integer value to
- u: The unsigned integer value to be appended - u: The unsigned integer value to be written
- base: The base to use for converting the integer value - base: The base to use for converting the integer value
Example: Example:
import "core:fmt" import "core:fmt"
import "core:strconv" import "core:strconv"
append_uint_example :: proc() { write_uint_example :: proc() {
buf: [4]byte buf: [4]byte
result := strconv.append_uint(buf[:], 42, 16) result := strconv.write_uint(buf[:], 42, 16)
fmt.println(result, buf) fmt.println(result, buf)
} }
@@ -1506,26 +1506,26 @@ Output:
2a [50, 97, 0, 0] 2a [50, 97, 0, 0]
**Returns** **Returns**
- The resulting string after appending the unsigned integer value - The resulting string after writing the unsigned integer value
*/ */
append_uint :: proc(buf: []byte, u: u64, base: int) -> string { write_uint :: proc(buf: []byte, u: u64, base: int) -> string {
return append_bits(buf, u, base, false, 8*size_of(uint), digits, nil) return write_bits(buf, u, base, false, 8*size_of(uint), digits, nil)
} }
/* /*
Appends a signed integer value as a string to the given buffer with the specified base Writes a signed integer value as a string to the given buffer with the specified base
**Inputs** **Inputs**
- buf: The buffer to append the signed integer value to - buf: The buffer to write the signed integer value to
- i: The signed integer value to be appended - i: The signed integer value to be written
- base: The base to use for converting the integer value - base: The base to use for converting the integer value
Example: Example:
import "core:fmt" import "core:fmt"
import "core:strconv" import "core:strconv"
append_int_example :: proc() { write_int_example :: proc() {
buf: [4]byte buf: [4]byte
result := strconv.append_int(buf[:], -42, 10) result := strconv.write_int(buf[:], -42, 10)
fmt.println(result, buf) fmt.println(result, buf)
} }
@@ -1534,16 +1534,16 @@ Output:
-42 [45, 52, 50, 0] -42 [45, 52, 50, 0]
**Returns** **Returns**
- The resulting string after appending the signed integer value - The resulting string after writing the signed integer value
*/ */
append_int :: proc(buf: []byte, i: i64, base: int) -> string { write_int :: proc(buf: []byte, i: i64, base: int) -> string {
return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil) return write_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil)
} }
append_u128 :: proc(buf: []byte, u: u128, base: int) -> string { write_u128 :: proc(buf: []byte, u: u128, base: int) -> string {
return append_bits_128(buf, u, base, false, 8*size_of(uint), digits, nil) return write_bits_128(buf, u, base, false, 8*size_of(uint), digits, nil)
} }
/* /*
@@ -1571,7 +1571,7 @@ Output:
- The resulting string after converting the integer value - The resulting string after converting the integer value
*/ */
itoa :: proc(buf: []byte, i: int) -> string { itoa :: proc(buf: []byte, i: int) -> string {
return append_int(buf, i64(i), 10) return write_int(buf, i64(i), 10)
} }
/* /*
Converts a string to an integer value Converts a string to an integer value
@@ -1623,14 +1623,14 @@ atof :: proc(s: string) -> f64 {
v, _ := parse_f64(s) v, _ := parse_f64(s)
return v return v
} }
// Alias to `append_float` // Alias to `write_float`
ftoa :: append_float ftoa :: write_float
/* /*
Appends a float64 value as a string to the given buffer with the specified format and precision Writes a float64 value as a string to the given buffer with the specified format and precision
**Inputs** **Inputs**
- buf: The buffer to append the float64 value to - buf: The buffer to write the float64 value to
- f: The float64 value to be appended - f: The float64 value to be written
- fmt: The byte specifying the format to use for the conversion - fmt: The byte specifying the format to use for the conversion
- prec: The precision to use for the conversion - prec: The precision to use for the conversion
- bit_size: The size of the float in bits (32 or 64) - bit_size: The size of the float in bits (32 or 64)
@@ -1639,9 +1639,9 @@ Example:
import "core:fmt" import "core:fmt"
import "core:strconv" import "core:strconv"
append_float_example :: proc() { write_float_example :: proc() {
buf: [8]byte buf: [8]byte
result := strconv.append_float(buf[:], 3.14159, 'f', 2, 64) result := strconv.write_float(buf[:], 3.14159, 'f', 2, 64)
fmt.println(result, buf) fmt.println(result, buf)
} }
@@ -1650,16 +1650,16 @@ Output:
+3.14 [43, 51, 46, 49, 52, 0, 0, 0] +3.14 [43, 51, 46, 49, 52, 0, 0, 0]
**Returns** **Returns**
- The resulting string after appending the float - The resulting string after writing the float
*/ */
append_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string { write_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string {
return string(generic_ftoa(buf, f, fmt, prec, bit_size)) return string(generic_ftoa(buf, f, fmt, prec, bit_size))
} }
/* /*
Appends a quoted string representation of the input string to a given byte slice and returns the result as a string Writes a quoted string representation of the input string to a given byte slice and returns the result as a string
**Inputs** **Inputs**
- buf: The byte slice to which the quoted string will be appended - buf: The byte slice to which the quoted string will be written
- str: The input string to be quoted - str: The input string to be quoted
!! ISSUE !! NOT EXPECTED -- "\"hello\"" was expected !! ISSUE !! NOT EXPECTED -- "\"hello\"" was expected
@@ -1679,7 +1679,7 @@ Output:
"'h''e''l''l''o'" [34, 39, 104, 39, 39, 101, 39, 39, 108, 39, 39, 108, 39, 39, 111, 39, 34, 0, 0, 0] "'h''e''l''l''o'" [34, 39, 104, 39, 39, 101, 39, 39, 108, 39, 39, 108, 39, 39, 111, 39, 34, 0, 0, 0]
**Returns** **Returns**
- The resulting string after appending the quoted string representation - The resulting string after writing the quoted string representation
*/ */
quote :: proc(buf: []byte, str: string) -> string { quote :: proc(buf: []byte, str: string) -> string {
write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) { write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
@@ -1719,10 +1719,10 @@ quote :: proc(buf: []byte, str: string) -> string {
return string(buf[:i]) return string(buf[:i])
} }
/* /*
Appends a quoted rune representation of the input rune to a given byte slice and returns the result as a string Writes a quoted rune representation of the input rune to a given byte slice and returns the result as a string
**Inputs** **Inputs**
- buf: The byte slice to which the quoted rune will be appended - buf: The byte slice to which the quoted rune will be written
- r: The input rune to be quoted - r: The input rune to be quoted
Example: Example:
@@ -1740,7 +1740,7 @@ Output:
'A' [39, 65, 39, 0] 'A' [39, 65, 39, 0]
**Returns** **Returns**
- The resulting string after appending the quoted rune representation - The resulting string after writing the quoted rune representation
*/ */
quote_rune :: proc(buf: []byte, r: rune) -> string { quote_rune :: proc(buf: []byte, r: rune) -> string {
write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) { write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
@@ -1783,7 +1783,7 @@ quote_rune :: proc(buf: []byte, r: rune) -> string {
if r < 32 { if r < 32 {
write_string(buf, &i, "\\x") write_string(buf, &i, "\\x")
b: [2]byte b: [2]byte
s := append_bits(b[:], u64(r), 16, true, 64, digits, nil) s := write_bits(b[:], u64(r), 16, true, 64, digits, nil)
switch len(s) { switch len(s) {
case 0: write_string(buf, &i, "00") case 0: write_string(buf, &i, "00")
case 1: write_rune(buf, &i, '0') case 1: write_rune(buf, &i, '0')
+6 -6
View File
@@ -675,7 +675,7 @@ Returns:
*/ */
write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) { write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
buf: [384]byte buf: [384]byte
s := strconv.append_float(buf[:], f, fmt, prec, bit_size) s := strconv.write_float(buf[:], f, fmt, prec, bit_size)
// If the result starts with a `+` then unless we always want signed results, // If the result starts with a `+` then unless we always want signed results,
// we skip it unless it's followed by an `I` (because of +Inf). // we skip it unless it's followed by an `I` (because of +Inf).
if !always_signed && (buf[0] == '+' && buf[1] != 'I') { if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
@@ -699,7 +699,7 @@ Returns:
*/ */
write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) { write_f16 :: proc(b: ^Builder, f: f16, fmt: byte, always_signed := false) -> (n: int) {
buf: [384]byte buf: [384]byte
s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f)) s := strconv.write_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
if !always_signed && (buf[0] == '+' && buf[1] != 'I') { if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
s = s[1:] s = s[1:]
} }
@@ -739,7 +739,7 @@ Output:
*/ */
write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) { write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
buf: [384]byte buf: [384]byte
s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f)) s := strconv.write_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
if !always_signed && (buf[0] == '+' && buf[1] != 'I') { if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
s = s[1:] s = s[1:]
} }
@@ -761,7 +761,7 @@ Returns:
*/ */
write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) { write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
buf: [384]byte buf: [384]byte
s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f)) s := strconv.write_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
if !always_signed && (buf[0] == '+' && buf[1] != 'I') { if !always_signed && (buf[0] == '+' && buf[1] != 'I') {
s = s[1:] s = s[1:]
} }
@@ -782,7 +782,7 @@ Returns:
*/ */
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) { write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
buf: [32]byte buf: [32]byte
s := strconv.append_bits(buf[:], i, base, false, 64, strconv.digits, nil) s := strconv.write_bits(buf[:], i, base, false, 64, strconv.digits, nil)
return write_string(b, s) return write_string(b, s)
} }
/* /*
@@ -800,7 +800,7 @@ Returns:
*/ */
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) { write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
buf: [32]byte buf: [32]byte
s := strconv.append_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil) s := strconv.write_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
return write_string(b, s) return write_string(b, s)
} }
/* /*
+1 -1
View File
@@ -1238,7 +1238,7 @@ test_count_digits :: proc(t: ^testing.T) {
buf: [64]u8 buf: [64]u8
for n in 0..<i64(base*base*base) { for n in 0..<i64(base*base*base) {
count := math.count_digits_of_base(n, base) count := math.count_digits_of_base(n, base)
str := strconv.append_int(buf[:], n, base) str := strconv.write_int(buf[:], n, base)
if !testing.expectf(t, if !testing.expectf(t,
len(str) == count, len(str) == count,
"decimal %i in base-%i digit count is %i, does not match length %i of %q", "decimal %i in base-%i digit count is %i, does not match length %i of %q",