aboutsummaryrefslogtreecommitdiff
path: root/core/strings
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-11-14 16:32:50 +0100
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2022-11-14 16:32:50 +0100
commit677e7ff642dc1ebb732ca46b7e39b71521012e3f (patch)
tree0ee8903584c9d498c917f0114dc0c130a4e5ab8b /core/strings
parent682b5fa0d36ca9074142d3f69f4178a19acb1608 (diff)
Don't write leading + unless +Inf or we ask for it.
Diffstat (limited to 'core/strings')
-rw-r--r--core/strings/builder.odin22
1 files 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)
}