Skip to content

Commit 110d0bf

Browse files
committed
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 <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]> Reviewed-by: CUE cueckoo <[email protected]>
1 parent fecdd83 commit 110d0bf

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

cue/format/printer.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,22 @@ func (p *printer) Print(v interface{}) {
133133
data[1] >= '0' && data[1] <= '9' {
134134
data = "0o" + data[1:]
135135
}
136+
// Pad trailing dot before multiplier.
137+
if p := strings.IndexByte(data, '.'); p >= 0 && data[p+1] > '9' {
138+
data = data[:p+1] + "0" + data[p+1:]
139+
}
140+
136141
case token.FLOAT:
142+
// Pad leading or trailing dots.
143+
switch p := strings.IndexByte(data, '.'); {
144+
case p < 0:
145+
case p == 0:
146+
data = "0" + data
147+
case p == len(data)-1:
148+
data += "0"
149+
case data[p+1] > '9':
150+
data = data[:p+1] + "0" + data[p+1:]
151+
}
137152
if strings.IndexByte(data, 'E') != -1 {
138153
data = strings.ToLower(data)
139154
}

cue/format/testdata/values.golden

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,13 @@ a: 0e+1
22
a: 0e1
33
a: 0e+1
44
a: 0e1
5-
a: .3e+1
6-
a: .3e+1
5+
a: 0.3e+1
6+
a: 0.3e+1
7+
a: 0.3
8+
a: 3.0
9+
a: 3.0T
10+
a: 3.0e100
11+
12+
s: """
13+
x\"\"\"
14+
"""

cue/format/testdata/values.input

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,12 @@ a: 0E+1
44
a: 0E1
55
a: .3e+1
66
a: .3E+1
7+
a: .3
8+
a: 3.
9+
a: 3.T
10+
a: 3.e100
11+
12+
13+
s: """
14+
x\"\"\"
15+
"""

internal/third_party/yaml/decode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var unmarshalTests = []struct {
5656
"v: 0.1",
5757
}, {
5858
"v: .1",
59-
"v: .1",
59+
"v: 0.1",
6060
}, {
6161
"v: .Inf",
6262
"v: +Inf",
@@ -68,7 +68,7 @@ var unmarshalTests = []struct {
6868
"v: -10",
6969
}, {
7070
"v: -.1",
71-
"v: -.1",
71+
"v: -0.1",
7272
},
7373

7474
// Simple values.

0 commit comments

Comments
 (0)