Browse Source

Don't write leading + unless +Inf or we ask for it.

Jeroen van Rijn 2 years ago
parent
commit
677e7ff642
1 changed files with 18 additions and 4 deletions
  1. 18 4
      core/strings/builder.odin

+ 18 - 4
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
 // 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
 	buf: [384]byte
 	s := strconv.append_float(buf[:], f, fmt, prec, bit_size)
 	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)
 	return write_string(b, s)
 }
 }
 
 
 // writes a f16 value into the builder, returns the written amount of characters
 // 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
 	buf: [384]byte
 	s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
 	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)
 	return write_string(b, s)
 }
 }
 
 
 // writes a f32 value into the builder, returns the written amount of characters
 // 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
 	buf: [384]byte
 	s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
 	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)
 	return write_string(b, s)
 }
 }
 
 
 // writes a f64 value into the builder, returns the written amount of characters
 // 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
 	buf: [384]byte
 	s := strconv.append_float(buf[:], f64(f), fmt, 2*size_of(f), 8*size_of(f))
 	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)
 	return write_string(b, s)
 }
 }