Skip to content

Commit df6a61f

Browse files
griesemernebulabox
authored andcommitted
cmd/gofmt: normalize number prefixes and exponents
Rewrite non-decimal number prefixes to always use a lower-case base ("0X" -> "0x", etc.), and rewrite exponents to use a lower-case 'e' or 'p'. Leave hexadecimal digits and 0-octals alone. Comparing the best time of 3 runs of `time go test -run All` with the time for a gofmt that doesn't do the rewrite shows no increase in runtime for this bulk gofmt application (in fact on my machine I see a small decline, probably due to cache effects). R=Go1.13 Updates golang#12711. Updates golang#19308. Updates golang#29008. Change-Id: I9c6ebed2ffa0a6a001c59412a73382090955f5a9 Reviewed-on: https://go-review.googlesource.com/c/160184 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 9f62a79 commit df6a61f

File tree

3 files changed

+90
-39
lines changed

3 files changed

+90
-39
lines changed

src/cmd/gofmt/gofmt.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
112112
simplify(file)
113113
}
114114

115+
ast.Inspect(file, normalizeNumbers)
116+
115117
res, err := format(fileSet, file, sourceAdj, indentAdj, src, printer.Config{Mode: printerMode, Tabwidth: tabWidth})
116118
if err != nil {
117119
return err
@@ -326,3 +328,48 @@ func backupFile(filename string, data []byte, perm os.FileMode) (string, error)
326328

327329
return bakname, err
328330
}
331+
332+
// normalizeNumbers rewrites base prefixes and exponents to
333+
// use lower-case letters. It leaves hexadecimal digits alone.
334+
func normalizeNumbers(n ast.Node) bool {
335+
lit, _ := n.(*ast.BasicLit)
336+
if lit == nil {
337+
return true
338+
}
339+
if len(lit.Value) < 2 {
340+
return false // only one digit - nothing to do
341+
}
342+
// lit.Value >= 2
343+
344+
switch lit.Kind {
345+
case token.INT:
346+
switch lit.Value[:2] {
347+
case "0X":
348+
lit.Value = "0x" + lit.Value[2:]
349+
case "0O":
350+
lit.Value = "0o" + lit.Value[2:]
351+
case "0B":
352+
lit.Value = "0b" + lit.Value[2:]
353+
}
354+
355+
case token.FLOAT:
356+
switch lit.Value[:2] {
357+
default:
358+
if i := strings.LastIndexByte(lit.Value, 'E'); i >= 0 {
359+
lit.Value = lit.Value[:i] + "e" + lit.Value[i+1:]
360+
}
361+
case "0x":
362+
if i := strings.LastIndexByte(lit.Value, 'P'); i >= 0 {
363+
lit.Value = lit.Value[:i] + "p" + lit.Value[i+1:]
364+
}
365+
case "0X":
366+
if i := strings.LastIndexByte(lit.Value, 'P'); i >= 0 {
367+
lit.Value = "0x" + lit.Value[2:i] + "p" + lit.Value[i+1:]
368+
} else {
369+
lit.Value = "0x" + lit.Value[2:]
370+
}
371+
}
372+
}
373+
374+
return false
375+
}

src/cmd/gofmt/testdata/go2numbers.golden

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package p
22

33
const (
4+
// 0-octals
45
_ = 0
56
_ = 0123
67
_ = 0123456
@@ -21,39 +22,39 @@ const (
2122
_ = 0x1234
2223
_ = 0xcafef00d
2324

24-
_ = 0X0
25-
_ = 0X1234
26-
_ = 0XCAFEf00d
25+
_ = 0x0
26+
_ = 0x1234
27+
_ = 0xCAFEf00d
2728

28-
_ = 0X_0
29-
_ = 0X_1234
30-
_ = 0X_CAFE_f00d
29+
_ = 0x_0
30+
_ = 0x_1234
31+
_ = 0x_CAFE_f00d
3132

3233
// octals
3334
_ = 0o0
3435
_ = 0o1234
3536
_ = 0o01234567
3637

37-
_ = 0O0
38-
_ = 0O1234
39-
_ = 0O01234567
38+
_ = 0o0
39+
_ = 0o1234
40+
_ = 0o01234567
4041

4142
_ = 0o_0
4243
_ = 0o_1234
4344
_ = 0o0123_4567
4445

45-
_ = 0O_0
46-
_ = 0O_1234
47-
_ = 0O0123_4567
46+
_ = 0o_0
47+
_ = 0o_1234
48+
_ = 0o0123_4567
4849

4950
// binaries
5051
_ = 0b0
5152
_ = 0b1011
5253
_ = 0b00101101
5354

54-
_ = 0B0
55-
_ = 0B1011
56-
_ = 0B00101101
55+
_ = 0b0
56+
_ = 0b1011
57+
_ = 0b00101101
5758

5859
_ = 0b_0
5960
_ = 0b10_11
@@ -70,26 +71,26 @@ const (
7071

7172
_ = 0e0
7273
_ = 123e+0
73-
_ = 0123E-1
74+
_ = 0123e-1
7475

7576
_ = 0e-0
76-
_ = 123E+0
77-
_ = 0123E123
77+
_ = 123e+0
78+
_ = 0123e123
7879

7980
_ = 0.e+1
80-
_ = 123.E-10
81+
_ = 123.e-10
8182
_ = 0123.e123
8283

8384
_ = .0e-1
84-
_ = .123E+10
85-
_ = .0123E123
85+
_ = .123e+10
86+
_ = .0123e123
8687

8788
_ = 0.0
8889
_ = 123.123
8990
_ = 0123.0123
9091

9192
_ = 0.0e1
92-
_ = 123.123E-10
93+
_ = 123.123e-10
9394
_ = 0123.0123e+456
9495

9596
_ = 1_2_3.
@@ -100,42 +101,43 @@ const (
100101
_ = 0_123e0
101102

102103
_ = 0e-0_0
103-
_ = 1_2_3E+0
104-
_ = 0123E1_2_3
104+
_ = 1_2_3e+0
105+
_ = 0123e1_2_3
105106

106107
_ = 0.e+1
107-
_ = 123.E-1_0
108+
_ = 123.e-1_0
108109
_ = 01_23.e123
109110

110111
_ = .0e-1
111-
_ = .123E+10
112-
_ = .0123E123
112+
_ = .123e+10
113+
_ = .0123e123
113114

114115
_ = 1_2_3.123
115116
_ = 0123.01_23
116117

117118
// hexadecimal floats
118119
_ = 0x0.p+0
119-
_ = 0Xdeadcafe.p-10
120-
_ = 0x1234.P123
120+
_ = 0xdeadcafe.p-10
121+
_ = 0x1234.p123
121122

122123
_ = 0x.1p-0
123-
_ = 0X.deadcafep2
124-
_ = 0x.1234P+10
124+
_ = 0x.deadcafep2
125+
_ = 0x.1234p+10
125126

126127
_ = 0x0p0
127-
_ = 0Xdeadcafep+1
128-
_ = 0x1234P-10
128+
_ = 0xdeadcafep+1
129+
_ = 0x1234p-10
129130

130131
_ = 0x0.0p0
131-
_ = 0Xdead.cafep+1
132-
_ = 0x12.34P-10
132+
_ = 0xdead.cafep+1
133+
_ = 0x12.34p-10
133134

134-
_ = 0Xdead_cafep+1
135-
_ = 0x_1234P-10
135+
_ = 0xdead_cafep+1
136+
_ = 0x_1234p-10
136137

137-
_ = 0X_dead_cafe.p-10
138-
_ = 0x12_34.P1_2_3
138+
_ = 0x_dead_cafe.p-10
139+
_ = 0x12_34.p1_2_3
140+
_ = 0x1_2_3_4.p-1_2_3
139141

140142
// imaginaries
141143
_ = 0i

src/cmd/gofmt/testdata/go2numbers.input

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package p
22

33
const (
4+
// 0-octals
45
_ = 0
56
_ = 0123
67
_ = 0123456
@@ -136,6 +137,7 @@ const (
136137

137138
_ = 0X_dead_cafe.p-10
138139
_ = 0x12_34.P1_2_3
140+
_ = 0X1_2_3_4.P-1_2_3
139141

140142
// imaginaries
141143
_ = 0i

0 commit comments

Comments
 (0)