~wallyworld/gwacl/ensure-all-roles-have-costs

« back to all changes in this revision

Viewing changes to dedent/dedent.go

  • Committer: Ian Booth
  • Date: 2014-12-02 00:36:45 UTC
  • Revision ID: ian.booth@canonical.com-20141202003645-ye8a5akifuf2wjk3
Ensure all regions have costs and remove custom formatting

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package dedent
5
5
 
6
6
import (
7
 
    "regexp"
8
 
    "strings"
 
7
        "regexp"
 
8
        "strings"
9
9
)
10
10
 
11
11
const emptyString = ""
14
14
 
15
15
// Split the given text into lines.
16
16
func splitLines(text string) []string {
17
 
    return reLine.FindAllString(text, -1)
 
17
        return reLine.FindAllString(text, -1)
18
18
}
19
19
 
20
20
// Match leading whitespace or tabs. \p{Zs} is a Unicode character class:
23
23
 
24
24
// Find the longest leading margin common between the given lines.
25
25
func calculateMargin(lines []string) string {
26
 
    var margin string
27
 
    var first bool = true
28
 
    for _, line := range lines {
29
 
        indent := reLeadingWhitespace.FindString(line)
30
 
        switch {
31
 
        case len(indent) == len(line):
32
 
            // The line is either empty or whitespace and will be ignored for
33
 
            // the purposes of calculating the margin.
34
 
        case first:
35
 
            // This is the first line with an indent, so start from here.
36
 
            margin = indent
37
 
            first = false
38
 
        case strings.HasPrefix(indent, margin):
39
 
            // This line's indent is longer or equal to the margin. The
40
 
            // current margin remains unalterered.
41
 
        case strings.HasPrefix(margin, indent):
42
 
            // This line's indent is compatible with the margin but shorter
43
 
            // (strictly it could be equal, however that condition is handled
44
 
            // earlier in this switch). The current indent becomes the margin.
45
 
            margin = indent
46
 
        default:
47
 
            // There is no common margin so stop scanning.
48
 
            return emptyString
49
 
        }
50
 
    }
51
 
    return margin
 
26
        var margin string
 
27
        var first bool = true
 
28
        for _, line := range lines {
 
29
                indent := reLeadingWhitespace.FindString(line)
 
30
                switch {
 
31
                case len(indent) == len(line):
 
32
                        // The line is either empty or whitespace and will be ignored for
 
33
                        // the purposes of calculating the margin.
 
34
                case first:
 
35
                        // This is the first line with an indent, so start from here.
 
36
                        margin = indent
 
37
                        first = false
 
38
                case strings.HasPrefix(indent, margin):
 
39
                        // This line's indent is longer or equal to the margin. The
 
40
                        // current margin remains unalterered.
 
41
                case strings.HasPrefix(margin, indent):
 
42
                        // This line's indent is compatible with the margin but shorter
 
43
                        // (strictly it could be equal, however that condition is handled
 
44
                        // earlier in this switch). The current indent becomes the margin.
 
45
                        margin = indent
 
46
                default:
 
47
                        // There is no common margin so stop scanning.
 
48
                        return emptyString
 
49
                }
 
50
        }
 
51
        return margin
52
52
}
53
53
 
54
54
// Remove a prefix from each line, if present.
55
55
func trimPrefix(lines []string, prefix string) {
56
 
    trim := len(prefix)
57
 
    for i, line := range lines {
58
 
        if strings.HasPrefix(line, prefix) {
59
 
            lines[i] = line[trim:]
60
 
        }
61
 
    }
 
56
        trim := len(prefix)
 
57
        for i, line := range lines {
 
58
                if strings.HasPrefix(line, prefix) {
 
59
                        lines[i] = line[trim:]
 
60
                }
 
61
        }
62
62
}
63
63
 
64
64
func Dedent(text string) string {
65
 
    lines := splitLines(text)
66
 
    trimPrefix(lines, calculateMargin(lines))
67
 
    return strings.Join(lines, "\n")
 
65
        lines := splitLines(text)
 
66
        trimPrefix(lines, calculateMargin(lines))
 
67
        return strings.Join(lines, "\n")
68
68
}