cue/format: pad floats with leading or trailing periods
This is just a readability thing and does what your typical
spreadsheet does.
Also if we every want to allow paths like `a.1` and paths
with a leading `.`, the presence of `.1` is, albeit not
necessarily ambiguous, at least confusing.
Change-Id: I66490c5f7d9746b20d23c0eb69ee3dd0b89c25c3
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7742
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
diff --git a/cue/format/printer.go b/cue/format/printer.go
index 0d5ed74..79edc1a 100644
--- a/cue/format/printer.go
+++ b/cue/format/printer.go
@@ -133,7 +133,22 @@
data[1] >= '0' && data[1] <= '9' {
data = "0o" + data[1:]
}
+ // Pad trailing dot before multiplier.
+ if p := strings.IndexByte(data, '.'); p >= 0 && data[p+1] > '9' {
+ data = data[:p+1] + "0" + data[p+1:]
+ }
+
case token.FLOAT:
+ // Pad leading or trailing dots.
+ switch p := strings.IndexByte(data, '.'); {
+ case p < 0:
+ case p == 0:
+ data = "0" + data
+ case p == len(data)-1:
+ data += "0"
+ case data[p+1] > '9':
+ data = data[:p+1] + "0" + data[p+1:]
+ }
if strings.IndexByte(data, 'E') != -1 {
data = strings.ToLower(data)
}
diff --git a/cue/format/testdata/values.golden b/cue/format/testdata/values.golden
index cd048f6..c8968aa 100644
--- a/cue/format/testdata/values.golden
+++ b/cue/format/testdata/values.golden
@@ -2,5 +2,13 @@
a: 0e1
a: 0e+1
a: 0e1
-a: .3e+1
-a: .3e+1
+a: 0.3e+1
+a: 0.3e+1
+a: 0.3
+a: 3.0
+a: 3.0T
+a: 3.0e100
+
+s: """
+ x\"\"\"
+ """
diff --git a/cue/format/testdata/values.input b/cue/format/testdata/values.input
index 77a34a4..6203408 100644
--- a/cue/format/testdata/values.input
+++ b/cue/format/testdata/values.input
@@ -4,3 +4,12 @@
a: 0E1
a: .3e+1
a: .3E+1
+a: .3
+a: 3.
+a: 3.T
+a: 3.e100
+
+
+s: """
+ x\"\"\"
+ """
\ No newline at end of file
diff --git a/internal/third_party/yaml/decode_test.go b/internal/third_party/yaml/decode_test.go
index 7b70071..66fb314 100644
--- a/internal/third_party/yaml/decode_test.go
+++ b/internal/third_party/yaml/decode_test.go
@@ -56,7 +56,7 @@
"v: 0.1",
}, {
"v: .1",
- "v: .1",
+ "v: 0.1",
}, {
"v: .Inf",
"v: +Inf",
@@ -68,7 +68,7 @@
"v: -10",
}, {
"v: -.1",
- "v: -.1",
+ "v: -0.1",
},
// Simple values.