mirror of
https://github.com/Ed94/Odin.git
synced 2026-07-24 16:37:54 +00:00
Merge pull request #5288 from Feoramund/strconv-append-to-write
Clarify `strconv.append_*` to `strconv.write_*`
This commit is contained in:
@@ -385,17 +385,17 @@ to_diagnostic_format_writer :: proc(w: io.Writer, val: Value, padding := 0) -> i
|
||||
// which we want for the diagnostic format.
|
||||
case f16:
|
||||
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:] }
|
||||
io.write_string(w, str) or_return
|
||||
case f32:
|
||||
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:] }
|
||||
io.write_string(w, str) or_return
|
||||
case f64:
|
||||
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:] }
|
||||
io.write_string(w, str) or_return
|
||||
|
||||
|
||||
@@ -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) {
|
||||
switch i in a {
|
||||
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:
|
||||
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 {
|
||||
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
|
||||
@@ -286,7 +286,7 @@ marshal_to_writer :: proc(w: io.Writer, v: any, opt: ^Marshal_Options) -> (err:
|
||||
case runtime.Type_Info_Integer:
|
||||
buf: [40]byte
|
||||
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
|
||||
case: return .Unsupported_Type
|
||||
|
||||
+4
-4
@@ -1122,7 +1122,7 @@ _fmt_int :: proc(fi: ^Info, u: u64, base: int, is_signed: bool, bit_size: int, d
|
||||
flags: strconv.Int_Flags
|
||||
if fi.hash && !fi.zero && start == 0 { flags += {.Prefix} }
|
||||
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
|
||||
defer fi.zero = prev_zero
|
||||
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
|
||||
if fi.hash && !fi.zero && start == 0 { flags += {.Prefix} }
|
||||
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 {
|
||||
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
|
||||
str := strconv.append_float(buf[:], amt, 'f', prec, 64)
|
||||
str := strconv.write_float(buf[:], amt, 'f', prec, 64)
|
||||
|
||||
// Add the unit at the end.
|
||||
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
|
||||
|
||||
// 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 {
|
||||
// Strip sign from "+<value>" but not "+Inf".
|
||||
|
||||
+8
-8
@@ -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) {
|
||||
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)
|
||||
}
|
||||
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)
|
||||
s := strconv.write_bits(buf[:], u64(i), base, true, 64, strconv.digits, nil)
|
||||
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) {
|
||||
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)
|
||||
}
|
||||
write_i128 :: proc(w: Writer, i: i128, base: int = 10, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
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)
|
||||
}
|
||||
write_f16 :: proc(w: Writer, val: f16, n_written: ^int = nil) -> (n: int, err: Error) {
|
||||
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]
|
||||
if s[1] == '+' || 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) {
|
||||
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]
|
||||
if s[1] == '+' || 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) {
|
||||
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]
|
||||
if s[1] == '+' || 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
|
||||
|
||||
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) {
|
||||
case 0:
|
||||
write_string(w, "00", &n) or_return
|
||||
|
||||
@@ -103,7 +103,7 @@ round :: proc(x: $T/Fixed($Backing, $Fraction_Width)) -> Backing {
|
||||
}
|
||||
|
||||
@(require_results)
|
||||
append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
|
||||
write :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
|
||||
Integer_Width :: 8*size_of(Backing) - Fraction_Width
|
||||
|
||||
x := x
|
||||
@@ -124,16 +124,16 @@ append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
|
||||
|
||||
when size_of(Backing) < 16 {
|
||||
T :: u64
|
||||
append_uint :: strconv.append_uint
|
||||
write_uint :: strconv.write_uint
|
||||
} else {
|
||||
T :: u128
|
||||
append_uint :: strconv.append_u128
|
||||
write_uint :: strconv.write_u128
|
||||
}
|
||||
|
||||
integer := T(x.i) >> Fraction_Width
|
||||
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)
|
||||
if fraction != 0 {
|
||||
buf[i] = '.'
|
||||
@@ -155,7 +155,7 @@ append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
|
||||
@(require_results)
|
||||
to_string :: proc(x: $T/Fixed($Backing, $Fraction_Width), allocator := context.allocator) -> string {
|
||||
buf: [48]byte
|
||||
s := append(buf[:], x)
|
||||
s := write(buf[:], x)
|
||||
str := make([]byte, len(s), allocator)
|
||||
copy(str, s)
|
||||
return string(str)
|
||||
@@ -294,3 +294,8 @@ _power_of_two_table := [129]string{
|
||||
"85070591730234615865843651857942052864",
|
||||
"170141183460469231731687303715884105728",
|
||||
}
|
||||
|
||||
@(deprecated="Use write instead")
|
||||
append :: proc(dst: []byte, x: $T/Fixed($Backing, $Fraction_Width)) -> string {
|
||||
return write(dst, x)
|
||||
}
|
||||
|
||||
+1
-1
@@ -125,7 +125,7 @@ percent_encode :: proc(s: string, allocator := context.allocator) -> string {
|
||||
bytes, n := utf8.encode_rune(ch)
|
||||
for byte in bytes[:n] {
|
||||
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_string(&b, t)
|
||||
}
|
||||
|
||||
+1
-1
@@ -57,7 +57,7 @@ write_encoded_rune :: proc(f: Handle, r: rune) -> (n: int, err: Error) {
|
||||
if r < 32 {
|
||||
if wrap(write_string(f, "\\x"), &n, &err) { return }
|
||||
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) {
|
||||
case 0: if wrap(write_string(f, "00"), &n, &err) { return }
|
||||
case 1: if wrap(write_rune(f, '0'), &n, &err) { return }
|
||||
|
||||
@@ -59,7 +59,7 @@ write_encoded_rune :: proc(f: ^File, r: rune) -> (n: int, err: Error) {
|
||||
if r < 32 {
|
||||
if wrap(write_string(f, "\\x"), &n, &err) { return }
|
||||
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) {
|
||||
case 0: if wrap(write_string(f, "00"), &n, &err) { return }
|
||||
case 1: if wrap(write_rune(f, '0'), &n, &err) { return }
|
||||
|
||||
@@ -12,11 +12,11 @@ Decimal :: struct {
|
||||
Sets a Decimal from a given string `s`. The string is expected to represent a float. Stores parsed number in the given Decimal structure.
|
||||
If parsing fails, the Decimal will be left in an undefined state.
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- d: Pointer to a Decimal struct where the parsed result will be stored
|
||||
- s: The input string representing the floating-point number
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- ok: A boolean indicating whether the parsing was successful
|
||||
*/
|
||||
set :: proc(d: ^Decimal, s: string) -> (ok: bool) {
|
||||
@@ -104,11 +104,11 @@ set :: proc(d: ^Decimal, s: string) -> (ok: bool) {
|
||||
/*
|
||||
Converts a Decimal to a string representation, using the provided buffer as storage.
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- buf: A byte slice buffer to hold the resulting string
|
||||
- a: The struct to be converted to a string
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- A string representation of the Decimal
|
||||
*/
|
||||
decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
|
||||
@@ -150,7 +150,7 @@ decimal_to_string :: proc(buf: []byte, a: ^Decimal) -> string {
|
||||
/*
|
||||
Trims trailing zeros in the given Decimal, updating the count and decimal_point values as needed.
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: Pointer to the Decimal struct to be trimmed
|
||||
*/
|
||||
trim :: proc(a: ^Decimal) {
|
||||
@@ -166,7 +166,7 @@ Converts a given u64 integer `idx` to its Decimal representation in the provided
|
||||
|
||||
**Used for internal Decimal Operations.**
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: Where the result will be stored
|
||||
- idx: The value to be assigned to the Decimal
|
||||
*/
|
||||
@@ -190,11 +190,11 @@ assign :: proc(a: ^Decimal, idx: u64) {
|
||||
trim(a)
|
||||
}
|
||||
/*
|
||||
Shifts the Decimal value to the right by k positions.
|
||||
Shifts the Decimal value to the right by k positions.
|
||||
|
||||
**Used for internal Decimal Operations.**
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal struct to be shifted
|
||||
- k: The number of positions to shift right
|
||||
*/
|
||||
@@ -344,7 +344,7 @@ Shifts the decimal of the input value to the left by `k` places
|
||||
|
||||
WARNING: asserts `k < 61`
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal to be modified
|
||||
- k: The number of places to shift the decimal to the left
|
||||
*/
|
||||
@@ -405,7 +405,7 @@ shift_left :: proc(a: ^Decimal, k: uint) #no_bounds_check {
|
||||
/*
|
||||
Shifts the decimal of the input value by the specified number of places
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal to be modified
|
||||
- i: The number of places to shift the decimal (positive for left shift, negative for right shift)
|
||||
*/
|
||||
@@ -435,7 +435,7 @@ shift :: proc(a: ^Decimal, i: int) {
|
||||
/*
|
||||
Determines if the Decimal can be rounded up at the given digit index
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal to check
|
||||
- nd: The digit index to consider for rounding up
|
||||
|
||||
@@ -455,7 +455,7 @@ can_round_up :: proc(a: ^Decimal, nd: int) -> bool {
|
||||
/*
|
||||
Rounds the Decimal at the given digit index
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal to be modified
|
||||
- nd: The digit index to round
|
||||
*/
|
||||
@@ -470,7 +470,7 @@ round :: proc(a: ^Decimal, nd: int) {
|
||||
/*
|
||||
Rounds the Decimal up at the given digit index
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal to be modified
|
||||
- nd: The digit index to round up
|
||||
*/
|
||||
@@ -493,7 +493,7 @@ round_up :: proc(a: ^Decimal, nd: int) {
|
||||
/*
|
||||
Rounds down the decimal value to the specified number of decimal places
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: The Decimal value to be rounded down
|
||||
- nd: The number of decimal places to round down to
|
||||
|
||||
@@ -522,7 +522,7 @@ round_down :: proc(a: ^Decimal, nd: int) {
|
||||
/*
|
||||
Extracts the rounded integer part of a decimal value
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- a: A pointer to the Decimal value to extract the rounded integer part from
|
||||
|
||||
WARNING: There are no guarantees about overflow.
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package strconv
|
||||
|
||||
// (2025-06-05) These procedures are to be removed at a later release.
|
||||
|
||||
@(deprecated="Use write_bits instead")
|
||||
append_bits :: proc(buf: []byte, x: u64, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string {
|
||||
return write_bits(buf, x, base, is_signed, bit_size, digits, flags)
|
||||
}
|
||||
|
||||
@(deprecated="Use write_bits_128 instead")
|
||||
append_bits_128 :: proc(buf: []byte, x: u128, base: int, is_signed: bool, bit_size: int, digits: string, flags: Int_Flags) -> string {
|
||||
return write_bits_128(buf, x, base, is_signed, bit_size, digits, flags)
|
||||
}
|
||||
|
||||
@(deprecated="Use write_bool instead")
|
||||
append_bool :: proc(buf: []byte, b: bool) -> string {
|
||||
return write_bool(buf, b)
|
||||
}
|
||||
|
||||
@(deprecated="Use write_uint instead")
|
||||
append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
|
||||
return write_uint(buf, u, base)
|
||||
}
|
||||
|
||||
@(deprecated="Use write_int instead")
|
||||
append_int :: proc(buf: []byte, i: i64, base: int) -> string {
|
||||
return write_int(buf, i, base)
|
||||
}
|
||||
|
||||
@(deprecated="Use write_u128 instead")
|
||||
append_u128 :: proc(buf: []byte, u: u128, base: int) -> string {
|
||||
return write_u128(buf, u, base)
|
||||
}
|
||||
|
||||
@(deprecated="Use write_float instead")
|
||||
append_float :: proc(buf: []byte, f: f64, fmt: byte, prec, bit_size: int) -> string {
|
||||
return write_float(buf, f, fmt, prec, bit_size)
|
||||
}
|
||||
@@ -23,7 +23,7 @@ _f64_info := Float_Info{52, 11, -1023}
|
||||
/*
|
||||
Converts a floating-point number to a string with the specified format and precision.
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
|
||||
buf: A byte slice to store the resulting string
|
||||
val: The floating-point value to be converted
|
||||
@@ -40,7 +40,7 @@ Example:
|
||||
bit_size := 64
|
||||
result := strconv.generic_ftoa(buf[:], val, fmt, precision, bit_size) -> "3.14"
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- A byte slice containing the formatted string
|
||||
*/
|
||||
generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, precision, bit_size: int) -> []byte {
|
||||
@@ -122,7 +122,7 @@ generic_ftoa :: proc(buf: []byte, val: f64, fmt: byte, precision, bit_size: int)
|
||||
/*
|
||||
Converts a decimal floating-point number into a byte buffer with the given format
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- buf: The byte buffer to store the formatted number
|
||||
- shortest: If true, generates the shortest representation of the number
|
||||
- neg: If true, the number is negative
|
||||
@@ -130,7 +130,7 @@ Converts a decimal floating-point number into a byte buffer with the given forma
|
||||
- precision: The number of digits after the decimal point
|
||||
- fmt: The format specifier (accepted values: 'f', 'F', 'e', 'E', 'g', 'G')
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- A byte slice containing the formatted decimal floating-point number
|
||||
*/
|
||||
format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slice, precision: int, fmt: byte) -> []byte {
|
||||
@@ -256,7 +256,7 @@ format_digits :: proc(buf: []byte, shortest: bool, neg: bool, digs: Decimal_Slic
|
||||
/*
|
||||
Rounds the given decimal number to its shortest representation, considering the provided floating-point format
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- d: The decimal number to round
|
||||
- mant: The mantissa of the floating-point number
|
||||
- exp: The exponent of the floating-point number
|
||||
@@ -331,11 +331,11 @@ round_shortest :: proc(d: ^decimal.Decimal, mant: u64, exp: int, flt: ^Float_Inf
|
||||
/*
|
||||
Converts a decimal number to its floating-point representation with the given format and returns the resulting bits
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- d: Pointer to the decimal number to convert
|
||||
- info: Pointer to the Float_Info structure containing information about the floating-point format
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- b: The bits representing the floating-point number
|
||||
- overflow: A boolean indicating whether an overflow occurred during conversion
|
||||
*/
|
||||
|
||||
+15
-15
@@ -12,12 +12,12 @@ digits := "0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
/*
|
||||
Determines whether the given unsigned 64-bit integer is a negative value by interpreting it as a signed integer with the specified bit size.
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- x: The unsigned 64-bit integer to check for negativity
|
||||
- is_signed: A boolean indicating if the input should be treated as a signed integer
|
||||
- bit_size: The bit size of the signed integer representation (8, 16, 32, or 64)
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- u: The absolute value of the input integer
|
||||
- neg: A boolean indicating whether the input integer is negative
|
||||
*/
|
||||
@@ -48,9 +48,9 @@ is_integer_negative :: proc(x: u64, is_signed: bool, bit_size: int) -> (u: u64,
|
||||
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
|
||||
- x: The integer value to convert
|
||||
- base: The base for the integer representation (2 <= base <= MAX_BASE)
|
||||
@@ -59,12 +59,12 @@ Appends the string representation of an integer to a buffer with specified base,
|
||||
- digits: The digit set used for the integer representation
|
||||
- flags: The Int_Flags bit set to control integer formatting
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- 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 {
|
||||
panic("strconv: illegal base passed to append_bits")
|
||||
panic("strconv: illegal base passed to write_bits")
|
||||
}
|
||||
|
||||
a: [129]byte
|
||||
@@ -106,12 +106,12 @@ append_bits :: proc(buf: []byte, x: u64, base: int, is_signed: bool, bit_size: i
|
||||
/*
|
||||
Determines whether the given unsigned 128-bit integer is a negative value by interpreting it as a signed integer with the specified bit size.
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- x: The unsigned 128-bit integer to check for negativity
|
||||
- is_signed: A boolean indicating if the input should be treated as a signed integer
|
||||
- bit_size: The bit size of the signed integer representation (8, 16, 32, 64, or 128)
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- u: The absolute value of the input integer
|
||||
- neg: A boolean indicating whether the input integer is negative
|
||||
*/
|
||||
@@ -146,9 +146,9 @@ is_integer_negative_128 :: proc(x: u128, is_signed: bool, bit_size: int) -> (u:
|
||||
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
|
||||
- x: The 128-bit integer value to convert
|
||||
- base: The base for the integer representation (2 <= base <= MAX_BASE)
|
||||
@@ -157,12 +157,12 @@ Appends the string representation of a 128-bit integer to a buffer with specifie
|
||||
- digits: The digit set used for the integer representation
|
||||
- flags: The Int_Flags bit set to control integer formatting
|
||||
|
||||
**Returns**
|
||||
- The string containing the integer representation appended to the buffer
|
||||
**Returns**
|
||||
- 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 {
|
||||
panic("strconv: illegal base passed to append_bits")
|
||||
panic("strconv: illegal base passed to write_bits")
|
||||
}
|
||||
|
||||
a: [140]byte
|
||||
|
||||
+138
-138
@@ -5,8 +5,8 @@ import "decimal"
|
||||
/*
|
||||
Parses a boolean value from the input string
|
||||
|
||||
**Inputs**
|
||||
- s: The input string
|
||||
**Inputs**
|
||||
- s: The input string
|
||||
- true: "1", "t", "T", "true", "TRUE", "True"
|
||||
- false: "0", "f", "F", "false", "FALSE", "False"
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil)
|
||||
@@ -386,7 +386,7 @@ Parses an unsigned integer value from the input string, using the specified base
|
||||
- If base is not 0, it will be used for parsing regardless of any prefix in the input string
|
||||
|
||||
Example:
|
||||
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
parse_uint_example :: proc() {
|
||||
@@ -399,14 +399,14 @@ Example:
|
||||
n, ok = strconv.parse_uint("0xffff") // with prefix and inferred base
|
||||
fmt.println(n,ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1234 true
|
||||
65535 true
|
||||
65535 true
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
|
||||
value: The parsed uint value
|
||||
ok: `false` if no appropriate value could be found; the value was negative; he input string contained more than just the number
|
||||
@@ -423,7 +423,7 @@ parse_uint :: proc(s: string, base := 0, n: ^int = nil) -> (value: uint, ok: boo
|
||||
/*
|
||||
Parses an integer value from a string in the given base, without any prefix
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing the integer value
|
||||
- base: The base (radix) to use for parsing the integer (1-16)
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil)
|
||||
@@ -436,12 +436,12 @@ Example:
|
||||
n, ok := strconv.parse_i128_of_base("-1234eeee", 10)
|
||||
fmt.println(n,ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
-1234 false
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- value: The parsed i128 value
|
||||
- ok: false if no numeric value of the appropriate base could be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -491,7 +491,7 @@ parse_i128_of_base :: proc(str: string, base: int, n: ^int = nil) -> (value: i12
|
||||
/*
|
||||
Parses an integer value from a string in base 10, unless there's a prefix
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing the integer value
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil)
|
||||
|
||||
@@ -506,13 +506,13 @@ Example:
|
||||
n, ok = strconv.parse_i128_maybe_prefixed("0xeeee")
|
||||
fmt.println(n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1234 true
|
||||
61166 true
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed i128 value
|
||||
- ok: `false` if a valid integer could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -574,7 +574,7 @@ parse_i128 :: proc{parse_i128_maybe_prefixed, parse_i128_of_base}
|
||||
/*
|
||||
Parses an unsigned integer value from a string in the given base, without any prefix
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing the integer value
|
||||
- base: The base (radix) to use for parsing the integer (1-16)
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil)
|
||||
@@ -590,13 +590,13 @@ Example:
|
||||
n, ok = strconv.parse_u128_of_base("5678eeee", 16)
|
||||
fmt.println(n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1234 false
|
||||
1450766062 true
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed u128 value
|
||||
- ok: `false` if no numeric value of the appropriate base could be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -634,7 +634,7 @@ parse_u128_of_base :: proc(str: string, base: int, n: ^int = nil) -> (value: u12
|
||||
/*
|
||||
Parses an unsigned integer value from a string in base 10, unless there's a prefix
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing the integer value
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil)
|
||||
|
||||
@@ -649,13 +649,13 @@ Example:
|
||||
n, ok = strconv.parse_u128_maybe_prefixed("5678eeee")
|
||||
fmt.println(n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1234 true
|
||||
5678 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed u128 value
|
||||
- ok: false if a valid integer could not be found, if the value was negative, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -706,10 +706,10 @@ parse_u128 :: proc{parse_u128_maybe_prefixed, parse_u128_of_base}
|
||||
/*
|
||||
Converts a byte to lowercase
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- ch: A byte character to be converted to lowercase.
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- A lowercase byte character.
|
||||
*/
|
||||
@(private)
|
||||
@@ -717,7 +717,7 @@ lower :: #force_inline proc "contextless" (ch: byte) -> byte { return ('a' - 'A'
|
||||
/*
|
||||
Parses a 32-bit floating point number from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- s: The input string containing a 32-bit floating point number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -732,13 +732,13 @@ Example:
|
||||
n, ok = strconv.parse_f32("5678e2")
|
||||
fmt.printfln("%.3f %v", n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
0.000 false
|
||||
567800.000 true
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 32-bit floating point number.
|
||||
- ok: `false` if a base 10 float could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -750,7 +750,7 @@ parse_f32 :: proc(s: string, n: ^int = nil) -> (value: f32, ok: bool) {
|
||||
/*
|
||||
Parses a 64-bit floating point number from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 64-bit floating point number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -765,13 +765,13 @@ Example:
|
||||
n, ok = strconv.parse_f64("5678e2")
|
||||
fmt.printfln("%.3f %v", n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
0.000 false
|
||||
567800.000 true
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 64-bit floating point number.
|
||||
- ok: `false` if a base 10 float could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -787,7 +787,7 @@ parse_f64 :: proc(str: string, n: ^int = nil) -> (value: f64, ok: bool) {
|
||||
/*
|
||||
Parses a 32-bit floating point number from a string and returns the parsed number, the length of the parsed substring, and a boolean indicating whether the parsing was successful
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 32-bit floating point number.
|
||||
|
||||
Example:
|
||||
@@ -801,14 +801,14 @@ Example:
|
||||
n, _, ok = strconv.parse_f32_prefix("5678e2")
|
||||
fmt.printfln("%.3f %v", n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
0.000 false
|
||||
567800.000 true
|
||||
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 32-bit floating point number.
|
||||
- nr: The length of the parsed substring.
|
||||
- ok: A boolean indicating whether the parsing was successful.
|
||||
@@ -822,7 +822,7 @@ parse_f32_prefix :: proc(str: string) -> (value: f32, nr: int, ok: bool) {
|
||||
/*
|
||||
Parses a 64-bit floating point number from a string and returns the parsed number, the length of the parsed substring, and a boolean indicating whether the parsing was successful
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 64-bit floating point number.
|
||||
|
||||
Example:
|
||||
@@ -846,7 +846,7 @@ Output:
|
||||
1234.000 true
|
||||
13.370 true
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- value: The parsed 64-bit floating point number.
|
||||
- nr: The length of the parsed substring.
|
||||
- ok: `false` if a base 10 float could not be found
|
||||
@@ -1184,7 +1184,7 @@ parse_f64_prefix :: proc(str: string) -> (value: f64, nr: int, ok: bool) {
|
||||
/*
|
||||
Parses a 128-bit complex number from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 128-bit complex number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -1200,13 +1200,13 @@ Example:
|
||||
c, ok = strconv.parse_complex128("5+7i hellope", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
3+1i 4 true
|
||||
5+7i 4 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 128-bit complex number.
|
||||
- ok: `false` if a complex number could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -1232,12 +1232,12 @@ parse_complex128 :: proc(str: string, n: ^int = nil) -> (value: complex128, ok:
|
||||
}
|
||||
|
||||
value = complex(real_value, imag_value)
|
||||
return
|
||||
return
|
||||
}
|
||||
/*
|
||||
Parses a 64-bit complex number from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 64-bit complex number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -1253,13 +1253,13 @@ Example:
|
||||
c, ok = strconv.parse_complex64("5+7i hellope", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
3+1i 4 true
|
||||
5+7i 4 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 64-bit complex number.
|
||||
- ok: `false` if a complex number could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -1271,7 +1271,7 @@ parse_complex64 :: proc(str: string, n: ^int = nil) -> (value: complex64, ok: bo
|
||||
/*
|
||||
Parses a 32-bit complex number from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 32-bit complex number.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -1287,13 +1287,13 @@ Example:
|
||||
c, ok = strconv.parse_complex32("5+7i hellope", &n)
|
||||
fmt.printfln("%v %i %t", c, n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
3+1i 4 true
|
||||
5+7i 4 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 32-bit complex number.
|
||||
- ok: `false` if a complex number could not be found, or if the input string contained more than just the number.
|
||||
*/
|
||||
@@ -1305,7 +1305,7 @@ parse_complex32 :: proc(str: string, n: ^int = nil) -> (value: complex32, ok: bo
|
||||
/*
|
||||
Parses a 256-bit quaternion from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 256-bit quaternion.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -1321,13 +1321,13 @@ Example:
|
||||
q, ok = strconv.parse_quaternion256("1+2i+3j+4k hellope", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1+2i+3j+4k 10 true
|
||||
1+2i+3j+4k 10 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 256-bit quaternion.
|
||||
- ok: `false` if a quaternion could not be found, or if the input string contained more than just the quaternion.
|
||||
*/
|
||||
@@ -1385,7 +1385,7 @@ parse_quaternion256 :: proc(str: string, n: ^int = nil) -> (value: quaternion256
|
||||
/*
|
||||
Parses a 128-bit quaternion from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 128-bit quaternion.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -1401,13 +1401,13 @@ Example:
|
||||
q, ok = strconv.parse_quaternion128("1+2i+3j+4k hellope", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1+2i+3j+4k 10 true
|
||||
1+2i+3j+4k 10 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 128-bit quaternion.
|
||||
- ok: `false` if a quaternion could not be found, or if the input string contained more than just the quaternion.
|
||||
*/
|
||||
@@ -1419,7 +1419,7 @@ parse_quaternion128 :: proc(str: string, n: ^int = nil) -> (value: quaternion128
|
||||
/*
|
||||
Parses a 64-bit quaternion from a string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing a 64-bit quaternion.
|
||||
- n: An optional pointer to an int to store the length of the parsed substring (default: nil).
|
||||
|
||||
@@ -1435,13 +1435,13 @@ Example:
|
||||
q, ok = strconv.parse_quaternion64("1+2i+3j+4k hellope", &n)
|
||||
fmt.printfln("%v %i %t", q, n, ok)
|
||||
}
|
||||
|
||||
|
||||
Output:
|
||||
|
||||
1+2i+3j+4k 10 true
|
||||
1+2i+3j+4k 10 false
|
||||
|
||||
**Returns**
|
||||
|
||||
**Returns**
|
||||
- value: The parsed 64-bit quaternion.
|
||||
- ok: `false` if a quaternion could not be found, or if the input string contained more than just the quaternion.
|
||||
*/
|
||||
@@ -1450,20 +1450,20 @@ parse_quaternion64 :: proc(str: string, n: ^int = nil) -> (value: quaternion64,
|
||||
v, ok = parse_quaternion256(str, n)
|
||||
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**
|
||||
- buf: The buffer to append the boolean value to
|
||||
- b: The boolean value to be appended
|
||||
**Inputs**
|
||||
- buf: The buffer to write the boolean value to
|
||||
- b: The boolean value to be written
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
append_bool_example :: proc() {
|
||||
write_bool_example :: proc() {
|
||||
buf: [6]byte
|
||||
result := strconv.append_bool(buf[:], true)
|
||||
result := strconv.write_bool(buf[:], true)
|
||||
fmt.println(result, buf)
|
||||
}
|
||||
|
||||
@@ -1471,10 +1471,10 @@ Output:
|
||||
|
||||
true [116, 114, 117, 101, 0, 0]
|
||||
|
||||
**Returns**
|
||||
- The resulting string after appending the boolean value
|
||||
**Returns**
|
||||
- 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
|
||||
if b {
|
||||
n = copy(buf, "true")
|
||||
@@ -1483,21 +1483,21 @@ append_bool :: proc(buf: []byte, b: bool) -> string {
|
||||
}
|
||||
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**
|
||||
- buf: The buffer to append the unsigned integer value to
|
||||
- u: The unsigned integer value to be appended
|
||||
**Inputs**
|
||||
- buf: The buffer to write the unsigned integer value to
|
||||
- u: The unsigned integer value to be written
|
||||
- base: The base to use for converting the integer value
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
append_uint_example :: proc() {
|
||||
write_uint_example :: proc() {
|
||||
buf: [4]byte
|
||||
result := strconv.append_uint(buf[:], 42, 16)
|
||||
result := strconv.write_uint(buf[:], 42, 16)
|
||||
fmt.println(result, buf)
|
||||
}
|
||||
|
||||
@@ -1505,27 +1505,27 @@ Output:
|
||||
|
||||
2a [50, 97, 0, 0]
|
||||
|
||||
**Returns**
|
||||
- The resulting string after appending the unsigned integer value
|
||||
**Returns**
|
||||
- The resulting string after writing the unsigned integer value
|
||||
*/
|
||||
append_uint :: proc(buf: []byte, u: u64, base: int) -> string {
|
||||
return append_bits(buf, u, base, false, 8*size_of(uint), digits, nil)
|
||||
write_uint :: proc(buf: []byte, u: u64, base: int) -> string {
|
||||
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**
|
||||
- buf: The buffer to append the signed integer value to
|
||||
- i: The signed integer value to be appended
|
||||
**Inputs**
|
||||
- buf: The buffer to write the signed integer value to
|
||||
- i: The signed integer value to be written
|
||||
- base: The base to use for converting the integer value
|
||||
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
append_int_example :: proc() {
|
||||
write_int_example :: proc() {
|
||||
buf: [4]byte
|
||||
result := strconv.append_int(buf[:], -42, 10)
|
||||
result := strconv.write_int(buf[:], -42, 10)
|
||||
fmt.println(result, buf)
|
||||
}
|
||||
|
||||
@@ -1533,23 +1533,23 @@ Output:
|
||||
|
||||
-42 [45, 52, 50, 0]
|
||||
|
||||
**Returns**
|
||||
- The resulting string after appending the signed integer value
|
||||
**Returns**
|
||||
- The resulting string after writing the signed integer value
|
||||
*/
|
||||
append_int :: proc(buf: []byte, i: i64, base: int) -> string {
|
||||
return append_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil)
|
||||
write_int :: proc(buf: []byte, i: i64, base: int) -> string {
|
||||
return write_bits(buf, u64(i), base, true, 8*size_of(int), digits, nil)
|
||||
}
|
||||
|
||||
|
||||
|
||||
append_u128 :: proc(buf: []byte, u: u128, base: int) -> string {
|
||||
return append_bits_128(buf, u, base, false, 8*size_of(uint), digits, nil)
|
||||
write_u128 :: proc(buf: []byte, u: u128, base: int) -> string {
|
||||
return write_bits_128(buf, u, base, false, 8*size_of(uint), digits, nil)
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
Converts an integer value to a string and stores it in the given buffer
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- buf: The buffer to store the resulting string
|
||||
- i: The integer value to be converted
|
||||
|
||||
@@ -1567,16 +1567,16 @@ Output:
|
||||
|
||||
42 [52, 50, 0, 0]
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- The resulting string after converting the integer value
|
||||
*/
|
||||
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
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- s: The string to be converted
|
||||
|
||||
Example:
|
||||
@@ -1591,17 +1591,17 @@ Output:
|
||||
|
||||
42
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- The resulting integer value
|
||||
*/
|
||||
atoi :: proc(s: string) -> int {
|
||||
v, _ := parse_int(s)
|
||||
return v
|
||||
}
|
||||
/*
|
||||
/*
|
||||
Converts a string to a float64 value
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- s: The string to be converted
|
||||
|
||||
Example:
|
||||
@@ -1616,21 +1616,21 @@ Output:
|
||||
|
||||
3.140
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- The resulting float64 value after converting the string
|
||||
*/
|
||||
atof :: proc(s: string) -> f64 {
|
||||
v, _ := parse_f64(s)
|
||||
return v
|
||||
}
|
||||
// Alias to `append_float`
|
||||
ftoa :: append_float
|
||||
/*
|
||||
Appends a float64 value as a string to the given buffer with the specified format and precision
|
||||
// Alias to `write_float`
|
||||
ftoa :: write_float
|
||||
/*
|
||||
Writes a float64 value as a string to the given buffer with the specified format and precision
|
||||
|
||||
**Inputs**
|
||||
- buf: The buffer to append the float64 value to
|
||||
- f: The float64 value to be appended
|
||||
**Inputs**
|
||||
- buf: The buffer to write the float64 value to
|
||||
- f: The float64 value to be written
|
||||
- fmt: The byte specifying the format 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)
|
||||
@@ -1639,9 +1639,9 @@ Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
append_float_example :: proc() {
|
||||
write_float_example :: proc() {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -1649,20 +1649,20 @@ Output:
|
||||
|
||||
+3.14 [43, 51, 46, 49, 52, 0, 0, 0]
|
||||
|
||||
**Returns**
|
||||
- The resulting string after appending the float
|
||||
**Returns**
|
||||
- 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))
|
||||
}
|
||||
/*
|
||||
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**
|
||||
- buf: The byte slice to which the quoted string will be appended
|
||||
**Inputs**
|
||||
- buf: The byte slice to which the quoted string will be written
|
||||
- str: The input string to be quoted
|
||||
|
||||
!! ISSUE !! NOT EXPECTED -- "\"hello\"" was expected
|
||||
!! ISSUE !! NOT EXPECTED -- "\"hello\"" was expected
|
||||
|
||||
Example:
|
||||
|
||||
@@ -1678,8 +1678,8 @@ 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]
|
||||
|
||||
**Returns**
|
||||
- The resulting string after appending the quoted string representation
|
||||
**Returns**
|
||||
- The resulting string after writing the quoted string representation
|
||||
*/
|
||||
quote :: proc(buf: []byte, str: string) -> string {
|
||||
write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
|
||||
@@ -1719,10 +1719,10 @@ quote :: proc(buf: []byte, str: string) -> string {
|
||||
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**
|
||||
- buf: The byte slice to which the quoted rune will be appended
|
||||
**Inputs**
|
||||
- buf: The byte slice to which the quoted rune will be written
|
||||
- r: The input rune to be quoted
|
||||
|
||||
Example:
|
||||
@@ -1739,8 +1739,8 @@ Output:
|
||||
|
||||
'A' [39, 65, 39, 0]
|
||||
|
||||
**Returns**
|
||||
- The resulting string after appending the quoted rune representation
|
||||
**Returns**
|
||||
- The resulting string after writing the quoted rune representation
|
||||
*/
|
||||
quote_rune :: proc(buf: []byte, r: rune) -> string {
|
||||
write_byte :: proc(buf: []byte, i: ^int, bytes: ..byte) {
|
||||
@@ -1783,7 +1783,7 @@ quote_rune :: proc(buf: []byte, r: rune) -> string {
|
||||
if r < 32 {
|
||||
write_string(buf, &i, "\\x")
|
||||
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) {
|
||||
case 0: write_string(buf, &i, "00")
|
||||
case 1: write_rune(buf, &i, '0')
|
||||
@@ -1800,11 +1800,11 @@ quote_rune :: proc(buf: []byte, r: rune) -> string {
|
||||
/*
|
||||
Unquotes a single character from the input string, considering the given quote character
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- str: The input string containing the character to unquote
|
||||
- quote: The quote character to consider (e.g., '"')
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
@@ -1815,12 +1815,12 @@ Example:
|
||||
fmt.printf("r: <%v>, multiple_bytes:%v, tail_string:<%s>, success:%v\n",r, multiple_bytes, tail_string, success)
|
||||
}
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
Source: 'The' raven
|
||||
r: <'>, multiple_bytes:false, tail_string:<The' raven>, success:true
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- r: The unquoted rune
|
||||
- multiple_bytes: A boolean indicating if the rune has multiple bytes
|
||||
- tail_string: The remaining portion of the input string after unquoting the character
|
||||
@@ -1923,13 +1923,13 @@ unquote_char :: proc(str: string, quote: byte) -> (r: rune, multiple_bytes: bool
|
||||
/*
|
||||
Unquotes the input string considering any type of quote character and returns the unquoted string
|
||||
|
||||
**Inputs**
|
||||
**Inputs**
|
||||
- lit: The input string to unquote
|
||||
- allocator: (default: context.allocator)
|
||||
|
||||
WARNING: This procedure gives unexpected results if the quotes are not the first and last characters.
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strconv"
|
||||
@@ -1947,10 +1947,10 @@ Example:
|
||||
src="The raven \'Huginn\' is black."
|
||||
s, allocated, ok = strconv.unquote_string(src) // Will produce undesireable results
|
||||
fmt.println(src)
|
||||
fmt.printf("Unquoted: <%s>, alloc:%v, ok:%v\n", s, allocated, ok)
|
||||
fmt.printf("Unquoted: <%s>, alloc:%v, ok:%v\n", s, allocated, ok)
|
||||
}
|
||||
|
||||
Output:
|
||||
Output:
|
||||
|
||||
"The raven Huginn is black."
|
||||
Unquoted: <The raven Huginn is black.>, alloc:false, ok:true
|
||||
@@ -1961,7 +1961,7 @@ Output:
|
||||
The raven 'Huginn' is black.
|
||||
Unquoted: <he raven 'Huginn' is black>, alloc:false, ok:true
|
||||
|
||||
**Returns**
|
||||
**Returns**
|
||||
- res: The resulting unquoted string
|
||||
- allocated: A boolean indicating if the resulting string was allocated using the provided allocator
|
||||
- success: A boolean indicating whether the unquoting was successful
|
||||
@@ -2002,7 +2002,7 @@ unquote_string :: proc(lit: string, allocator := context.allocator) -> (res: str
|
||||
return s, false, true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
context.allocator = allocator
|
||||
|
||||
buf_len := 3*len(s) / 2
|
||||
|
||||
@@ -675,7 +675,7 @@ Returns:
|
||||
*/
|
||||
write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int, always_signed := false) -> (n: int) {
|
||||
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,
|
||||
// we skip it unless it's followed by an `I` (because of +Inf).
|
||||
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) {
|
||||
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') {
|
||||
s = s[1:]
|
||||
}
|
||||
@@ -739,7 +739,7 @@ Output:
|
||||
*/
|
||||
write_f32 :: proc(b: ^Builder, f: f32, fmt: byte, always_signed := false) -> (n: int) {
|
||||
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') {
|
||||
s = s[1:]
|
||||
}
|
||||
@@ -761,7 +761,7 @@ Returns:
|
||||
*/
|
||||
write_f64 :: proc(b: ^Builder, f: f64, fmt: byte, always_signed := false) -> (n: int) {
|
||||
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') {
|
||||
s = s[1:]
|
||||
}
|
||||
@@ -782,7 +782,7 @@ Returns:
|
||||
*/
|
||||
write_u64 :: proc(b: ^Builder, i: u64, base: int = 10) -> (n: int) {
|
||||
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)
|
||||
}
|
||||
/*
|
||||
@@ -800,7 +800,7 @@ Returns:
|
||||
*/
|
||||
write_i64 :: proc(b: ^Builder, i: i64, base: int = 10) -> (n: int) {
|
||||
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)
|
||||
}
|
||||
/*
|
||||
|
||||
@@ -1238,7 +1238,7 @@ test_count_digits :: proc(t: ^testing.T) {
|
||||
buf: [64]u8
|
||||
for n in 0..<i64(base*base*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,
|
||||
len(str) == count,
|
||||
"decimal %i in base-%i digit count is %i, does not match length %i of %q",
|
||||
|
||||
Reference in New Issue
Block a user