aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2024-05-20 14:42:13 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2024-05-20 14:42:13 +0200
commite0d3d68ce5d413356bf3fb47fe00c2b6bfb2ad6c (patch)
treeddd9f05fed5f8120c2f2e4782518ce43958ee94a /core
parentf64139987006a40c0548cdc507c4aca72e1435ea (diff)
Fix the way '%32b' and other prefixed numbers are written.
Diffstat (limited to 'core')
-rw-r--r--core/fmt/fmt.odin69
1 files changed, 58 insertions, 11 deletions
diff --git a/core/fmt/fmt.odin b/core/fmt/fmt.odin
index 01d4ec26d..61060e194 100644
--- a/core/fmt/fmt.odin
+++ b/core/fmt/fmt.odin
@@ -994,6 +994,33 @@ _fmt_int :: proc(fi: ^Info, u: u64, base: int, is_signed: bool, bit_size: int, d
}
}
+ buf: [256]byte
+ start := 0
+
+ if fi.hash && !is_signed {
+ switch base {
+ case 2:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'b', &fi.n)
+ start = 2
+
+ case 8:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'o', &fi.n)
+ start = 2
+
+ case 12:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'o', &fi.n)
+ start = 2
+
+ case 16:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'x', &fi.n)
+ start = 2
+ }
+ }
+
prec := 0
if fi.prec_set {
prec = fi.prec
@@ -1019,14 +1046,10 @@ _fmt_int :: proc(fi: ^Info, u: u64, base: int, is_signed: bool, bit_size: int, d
panic("_fmt_int: unknown base, whoops")
}
- buf: [256]byte
- start := 0
-
flags: strconv.Int_Flags
- if fi.hash { flags |= {.Prefix} }
- if fi.plus { flags |= {.Plus} }
+ 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)
-
prev_zero := fi.zero
defer fi.zero = prev_zero
fi.zero = false
@@ -1056,6 +1079,33 @@ _fmt_int_128 :: proc(fi: ^Info, u: u128, base: int, is_signed: bool, bit_size: i
}
}
+ buf: [256]byte
+ start := 0
+
+ if fi.hash && !is_signed {
+ switch base {
+ case 2:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'b', &fi.n)
+ start = 2
+
+ case 8:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'o', &fi.n)
+ start = 2
+
+ case 12:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'o', &fi.n)
+ start = 2
+
+ case 16:
+ io.write_byte(fi.writer, '0', &fi.n)
+ io.write_byte(fi.writer, 'x', &fi.n)
+ start = 2
+ }
+ }
+
prec := 0
if fi.prec_set {
prec = fi.prec
@@ -1081,12 +1131,9 @@ _fmt_int_128 :: proc(fi: ^Info, u: u128, base: int, is_signed: bool, bit_size: i
panic("_fmt_int: unknown base, whoops")
}
- buf: [256]byte
- start := 0
-
flags: strconv.Int_Flags
- if fi.hash && !fi.zero { flags |= {.Prefix} }
- if fi.plus { flags |= {.Plus} }
+ 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)
if fi.hash && fi.zero && fi.indent == 0 {