Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(880)

Delta Between Two Patch Sets: src/cmd/gofix/stringssplit.go

Issue 4661051: code review 4661051: strings.Split: make the default to split all. (Closed)
Left Patch Set: Created 12 years, 9 months ago
Right Patch Set: diff -r eaa696629d4d https://go.googlecode.com/hg/ Created 12 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « src/cmd/gofix/main.go ('k') | src/cmd/gofix/stringssplit_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "go/ast"
9 "go/token"
10 )
11
12 var stringssplitFix = fix{
13 "stringssplit",
14 stringssplit,
15 `Restore strings.Split to its original meaning and add strings.SplitN. B ytes too.
16
17 http://codereview.appspot.com/4661051
18 `,
19 }
20
21 func init() {
22 register(stringssplitFix)
23 }
24
25 func stringssplit(f *ast.File) bool {
26 if !imports(f, "bytes") && !imports(f, "strings") {
27 return false
28 }
29
30 fixed := false
31 walk(f, func(n interface{}) {
32 call, ok := n.(*ast.CallExpr)
33 // func Split(s, sep string, n int) []string
34 // func SplitAfter(s, sep string, n int) []string
35 if !ok || len(call.Args) != 3 {
36 return
37 }
38 // Is this our function?
39 switch {
40 case isPkgDot(call.Fun, "bytes", "Split"):
41 case isPkgDot(call.Fun, "bytes", "SplitAfter"):
42 case isPkgDot(call.Fun, "strings", "Split"):
43 case isPkgDot(call.Fun, "strings", "SplitAfter"):
44 default:
45 return
46 }
47
48 sel := call.Fun.(*ast.SelectorExpr)
49 args := call.Args
50 fixed = true // We're committed.
51
52 // Is the last argument -1? If so, drop the arg.
53 // (Actually we just look for a negative integer literal.)
54 // Otherwise, Split->SplitN and keep the arg.
55 final := args[2]
56 if unary, ok := final.(*ast.UnaryExpr); ok && unary.Op == token. SUB {
57 if lit, ok := unary.X.(*ast.BasicLit); ok {
58 // Is it an integer? If so, it's a negative inte ger and that's what we're after.
59 if lit.Kind == token.INT {
60 // drop the last arg.
61 call.Args = args[0:2]
62 return
63 }
64 }
65 }
66
67 // If not, rename and keep the argument list.
68 sel.Sel.Name += "N"
69 })
70 return fixed
71 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b