From 677e7ff642dc1ebb732ca46b7e39b71521012e3f Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Mon, 14 Nov 2022 16:32:50 +0100 Subject: [PATCH] Don't write leading + unless +Inf or we ask for it. --- core/strings/builder.odin | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/core/strings/builder.odin b/core/strings/builder.odin index 2623ee0e7..02177ba8c 100644 --- a/core/strings/builder.odin +++ b/core/strings/builder.odin @@ -300,30 +300,44 @@ write_escaped_rune :: proc(b: ^Builder, r: rune, quote: byte, html_safe := false } // writes a f64 value into the builder, returns the written amount of characters -write_float :: proc(b: ^Builder, f: f64, fmt: byte, prec, bit_size: int) -> (n: int) { +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) + // 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') { + s = s[1:] + } return write_string(b, s) } // writes a f16 value into the builder, returns the written amount of characters -write_f16 :: proc(b: ^Builder, f: f16, fmt: byte) -> (n: int) { +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)) + if !always_signed && (buf[0] == '+' && buf[1] != 'I') { + s = s[1:] + } return write_string(b, s) } // writes a f32 value into the builder, returns the written amount of characters -write_f32 :: proc(b: ^Builder, f: f32, fmt: byte) -> (n: int) { +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)) + if !always_signed && (buf[0] == '+' && buf[1] != 'I') { + s = s[1:] + } return write_string(b, s) } // writes a f64 value into the builder, returns the written amount of characters -write_f64 :: proc(b: ^Builder, f: f64, fmt: byte) -> (n: int) { +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)) + if !always_signed && (buf[0] == '+' && buf[1] != 'I') { + s = s[1:] + } return write_string(b, s) }