diff options
Diffstat (limited to 'libgo/go/go/printer/printer.go')
-rw-r--r-- | libgo/go/go/printer/printer.go | 27 |
1 files changed, 12 insertions, 15 deletions
diff --git a/libgo/go/go/printer/printer.go b/libgo/go/go/printer/printer.go index ba61f782267..e4679b0021d 100644 --- a/libgo/go/go/printer/printer.go +++ b/libgo/go/go/printer/printer.go @@ -104,7 +104,7 @@ func (p *printer) init(cfg *Config, fset *token.FileSet, nodeSizes map[ast.Node] p.cachedPos = -1 } -func (p *printer) internalError(msg ...interface{}) { +func (p *printer) internalError(msg ...any) { if debug { fmt.Print(p.pos.String() + ": ") fmt.Println(msg...) @@ -559,12 +559,9 @@ func stripCommonPrefix(lines []string) { * Check for vertical "line of stars" and correct prefix accordingly. */ lineOfStars := false - if i := strings.Index(prefix, "*"); i >= 0 { - // Line of stars present. - if i > 0 && prefix[i-1] == ' ' { - i-- // remove trailing blank from prefix so stars remain aligned - } - prefix = prefix[0:i] + if p, _, ok := strings.Cut(prefix, "*"); ok { + // remove trailing blank from prefix so stars remain aligned + prefix = strings.TrimSuffix(p, " ") lineOfStars = true } else { // No line of stars present. @@ -616,8 +613,8 @@ func stripCommonPrefix(lines []string) { // lines. last := lines[len(lines)-1] closing := "*/" - i := strings.Index(last, closing) // i >= 0 (closing is always present) - if isBlank(last[0:i]) { + before, _, _ := strings.Cut(last, closing) // closing always present + if isBlank(before) { // last line only contains closing */ if lineOfStars { closing = " */" // add blank to align final star @@ -881,7 +878,7 @@ func mayCombine(prev token.Token, next byte) (b bool) { // space for best comment placement. Then, any leftover whitespace is // printed, followed by the actual token. // -func (p *printer) print(args ...interface{}) { +func (p *printer) print(args ...any) { for _, arg := range args { // information about the current arg var data string @@ -1078,7 +1075,7 @@ func getLastComment(n ast.Node) *ast.CommentGroup { return nil } -func (p *printer) printNode(node interface{}) error { +func (p *printer) printNode(node any) error { // unpack *CommentedNode, if any var comments []*ast.CommentGroup if cnode, ok := node.(*CommentedNode); ok { @@ -1312,7 +1309,7 @@ type Config struct { } // fprint implements Fprint and takes a nodesSizes map for setting up the printer state. -func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{}, nodeSizes map[ast.Node]int) (err error) { +func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node any, nodeSizes map[ast.Node]int) (err error) { // print node var p printer p.init(cfg, fset, nodeSizes) @@ -1368,7 +1365,7 @@ func (cfg *Config) fprint(output io.Writer, fset *token.FileSet, node interface{ // It may be provided as argument to any of the Fprint functions. // type CommentedNode struct { - Node interface{} // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt + Node any // *ast.File, or ast.Expr, ast.Decl, ast.Spec, or ast.Stmt Comments []*ast.CommentGroup } @@ -1377,7 +1374,7 @@ type CommentedNode struct { // The node type must be *ast.File, *CommentedNode, []ast.Decl, []ast.Stmt, // or assignment-compatible to ast.Expr, ast.Decl, ast.Spec, or ast.Stmt. // -func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{}) error { +func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node any) error { return cfg.fprint(output, fset, node, make(map[ast.Node]int)) } @@ -1386,6 +1383,6 @@ func (cfg *Config) Fprint(output io.Writer, fset *token.FileSet, node interface{ // Note that gofmt uses tabs for indentation but spaces for alignment; // use format.Node (package go/format) for output that matches gofmt. // -func Fprint(output io.Writer, fset *token.FileSet, node interface{}) error { +func Fprint(output io.Writer, fset *token.FileSet, node any) error { return (&Config{Tabwidth: 8}).Fprint(output, fset, node) } |