diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2024-05-20 14:29:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-20 14:29:50 +0100 |
| commit | 5ed93563a1d70c6d536a84c2e71707755c1744d2 (patch) | |
| tree | f8c819fdad91123b80934a5249ef5617671867f1 | |
| parent | c49a291347f4be332532551d19029c52cb348ef0 (diff) | |
| parent | e0d3d68ce5d413356bf3fb47fe00c2b6bfb2ad6c (diff) | |
Merge pull request #3606 from Kelimion/fmtfix
Fix the way '%32b' and other prefixed numbers are written.
| -rw-r--r-- | core/fmt/fmt.odin | 69 |
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 { |