pkg debug/pe, const IMAGE_SUBSYSTEM_WINDOWS_GUI ideal-int
pkg debug/pe, const IMAGE_SUBSYSTEM_XBOX = 14
pkg debug/pe, const IMAGE_SUBSYSTEM_XBOX ideal-int
-pkg go/printer, const StdFormat = 16
-pkg go/printer, const StdFormat Mode
pkg math/big, method (*Int) FillBytes([]uint8) []uint8
pkg net, method (*Resolver) LookupIP(context.Context, string, string) ([]IP, error)
pkg net/url, method (*URL) EscapedFragment() string
</dd>
</dl><!-- fmt -->
-<dl id="go/printer"><dt><a href="/pkg/go/printer/">go/printer</a></dt>
+<dl id="go/format"><dt><a href="/pkg/go/format/">go/format</a></dt>
<dd>
- <p><!-- CL 231461 -->
- The new <a href="/pkg/go/printer/#Mode"><code>Mode</code></a>
- value <a href="/pkg/go/printer/#StdFormat"><code>StdFormat</code></a>
- directs the printer to apply standard formatting changes while
- printing the output.
+ <p><!-- golang.org/issue/37476, CL 231461, CL 240683 -->
+ The <a href="/pkg/go/format/#Source"><code>Source</code></a> and
+ <a href="/pkg/go/format/#Node"><code>Node</code></a> functions
+ now canonicalize number literal prefixes and exponents as part
+ of formatting Go source code. This matches the behavior of the
+ <a href="/pkg/cmd/gofmt/"><code>gofmt</code></a> command as it
+ was implemented <a href="/doc/go1.13#gofmt">since Go 1.13</a>.
+ </p>
</dd>
-</dl><!-- go/printer -->
+</dl><!-- go/format -->
<dl id="html/template"><dt><a href="/pkg/html/template/">html/template</a></dt>
<dd>
// Keep these in sync with go/format/format.go.
const (
tabWidth = 8
- printerMode = printer.UseSpaces | printer.TabIndent | printer.StdFormat
+ printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
+
+ // printerNormalizeNumbers means to canonicalize number literal prefixes
+ // and exponents while printing. See https://golang.org/doc/go1.13#gofmt.
+ //
+ // This value is defined in go/printer specifically for go/format and cmd/gofmt.
+ printerNormalizeNumbers = 1 << 30
)
var (
// Keep these in sync with cmd/gofmt/gofmt.go.
const (
tabWidth = 8
- printerMode = printer.UseSpaces | printer.TabIndent | printer.StdFormat
+ printerMode = printer.UseSpaces | printer.TabIndent | printerNormalizeNumbers
+
+ // printerNormalizeNumbers means to canonicalize number literal prefixes
+ // and exponents while printing. See https://golang.org/doc/go1.13#gofmt.
+ //
+ // This value is defined in go/printer specifically for go/format and cmd/gofmt.
+ printerNormalizeNumbers = 1 << 30
)
var config = printer.Config{Mode: printerMode, Tabwidth: tabWidth}
diff(t, buf.Bytes(), src)
}
-// Node is documented to not modify the AST. Test that it is so, even when
-// formatting changes are applied due to printer.StdFormat mode being used.
+// Node is documented to not modify the AST.
+// Test that it is so even when numbers are normalized.
func TestNodeNoModify(t *testing.T) {
const (
src = "package p\n\nconst _ = 0000000123i\n"
}
case *ast.BasicLit:
- if p.Config.Mode&StdFormat != 0 {
- x = normalizeNumbers(x)
+ if p.Config.Mode&normalizeNumbers != 0 {
+ x = normalizedNumber(x)
}
p.print(x)
}
}
-// normalizeNumbers rewrites base prefixes and exponents to
-// use lower-case letters, and removes leading 0's from
-// integer imaginary literals. It leaves hexadecimal digits
-// alone.
-func normalizeNumbers(lit *ast.BasicLit) *ast.BasicLit {
+// normalizedNumber rewrites base prefixes and exponents
+// of numbers to use lower-case letters (0X123 to 0x123 and 1.2E3 to 1.2e3),
+// and removes leading 0's from integer imaginary literals (0765i to 765i).
+// It leaves hexadecimal digits alone.
+//
+// normalizedNumber doesn't modify the ast.BasicLit value lit points to.
+// If lit is not a number or a number in canonical format already,
+// lit is returned as is. Otherwise a new ast.BasicLit is created.
+func normalizedNumber(lit *ast.BasicLit) *ast.BasicLit {
if lit.Kind != token.INT && lit.Kind != token.FLOAT && lit.Kind != token.IMAG {
return lit // not a number - nothing to do
}
var testfile *ast.File
func testprint(out io.Writer, file *ast.File) {
- if err := (&Config{TabIndent | UseSpaces | StdFormat, 8, 0}).Fprint(out, fset, file); err != nil {
+ if err := (&Config{TabIndent | UseSpaces | normalizeNumbers, 8, 0}).Fprint(out, fset, file); err != nil {
log.Fatalf("print error: %s", err)
}
}
TabIndent // use tabs for indentation independent of UseSpaces
UseSpaces // use spaces instead of tabs for alignment
SourcePos // emit //line directives to preserve original source positions
- StdFormat // apply standard formatting changes (exact byte output may change between versions of Go)
+)
+
+// The mode below is not included in printer's public API because
+// editing code text is deemed out of scope. Because this mode is
+// unexported, it's also possible to modify or remove it based on
+// the evolving needs of go/format and cmd/gofmt without breaking
+// users. See discussion in CL 240683.
+const (
+ // normalizeNumbers means to canonicalize number
+ // literal prefixes and exponents while printing.
+ //
+ // This value is known in and used by go/format and cmd/gofmt.
+ // It is currently more convenient and performant for those
+ // packages to apply number normalization during printing,
+ // rather than by modifying the AST in advance.
+ normalizeNumbers Mode = 1 << 30
)
// A Config node controls the output of Fprint.
const (
export checkMode = 1 << iota
rawFormat
- stdFormat
+ normNumber
idempotent
)
if mode&rawFormat != 0 {
cfg.Mode |= RawFormat
}
- if mode&stdFormat != 0 {
- cfg.Mode |= StdFormat
+ if mode&normNumber != 0 {
+ cfg.Mode |= normalizeNumbers
}
// print AST
{"slow.input", "slow.golden", idempotent},
{"complit.input", "complit.x", export},
{"go2numbers.input", "go2numbers.golden", idempotent},
- {"go2numbers.input", "go2numbers.stdfmt", stdFormat | idempotent},
+ {"go2numbers.input", "go2numbers.norm", normNumber | idempotent},
}
func TestFiles(t *testing.T) {