@@ -35,52 +35,37 @@ type State struct {
35
35
Updater * merge.Updater
36
36
}
37
37
38
- // FixTabsOrDie counts the number of tab characters preceding the first line in
39
- // the given yaml object. It removes that many tabs from every line. It then
40
- // converts remaining tabs to spaces (two spaces per tab). It panics (it's a
41
- // test funtion) if it finds mixed tabs and spaces in front of a line, or if
42
- // some line has fewer tabs than the first line.
38
+ // FixTabsOrDie counts the number of tab characters preceding the first
39
+ // line in the given yaml object. It removes that many tabs from every
40
+ // line. It panics (it's a test funtion) if some line has fewer tabs
41
+ // than the first line.
43
42
//
44
43
// The purpose of this is to make it easier to read tests.
45
44
func FixTabsOrDie (in typed.YAMLObject ) typed.YAMLObject {
46
- consumeTabs := func (line []byte ) (tabCount int , spacesFound bool ) {
47
- for _ , c := range line {
48
- if c == ' ' {
49
- spacesFound = true
50
- }
51
- if c != '\t' {
52
- break
53
- }
54
- tabCount ++
55
- }
56
- return tabCount , spacesFound
57
- }
58
-
59
45
lines := bytes .Split ([]byte (in ), []byte {'\n' })
60
46
if len (lines [0 ]) == 0 && len (lines ) > 1 {
61
47
lines = lines [1 :]
62
48
}
63
- prefix , _ := consumeTabs (lines [0 ])
64
- var anySpacesFound bool
65
- var anyTabsFound bool
66
-
49
+ // Create prefix made of tabs that we want to remove.
50
+ var prefix []byte
51
+ for _ , c := range lines [0 ] {
52
+ if c != '\t' {
53
+ break
54
+ }
55
+ prefix = append (prefix , byte ('\t' ))
56
+ }
57
+ // Remove prefix from all tabs, fail otherwise.
67
58
for i := range lines {
68
59
line := lines [i ]
69
- indent , spacesFound := consumeTabs (line )
70
- if i == len (lines )- 1 && len (line ) <= prefix && indent == len (line ) {
71
- // It's OK for the last line to be blank (trailing \n)
60
+ // It's OK for the last line to be blank (trailing \n)
61
+ if i == len (lines )- 1 && len (line ) <= len (prefix ) && bytes .TrimSpace (line ) == nil {
72
62
lines [i ] = []byte {}
73
63
break
74
64
}
75
- anySpacesFound = anySpacesFound || spacesFound
76
- anyTabsFound = anyTabsFound || indent > 0
77
- if indent < prefix {
78
- panic (fmt .Sprintf ("line %v doesn't have %v tabs as a prefix:\n %s" , i , prefix , in ))
65
+ if ! bytes .HasPrefix (line , prefix ) {
66
+ panic (fmt .Errorf ("line %d doesn't start with expected number (%d) of tabs: %v" , i , len (prefix ), line ))
79
67
}
80
- lines [i ] = append (bytes .Repeat ([]byte {' ' , ' ' }, indent - prefix ), line [indent :]... )
81
- }
82
- if anyTabsFound && anySpacesFound {
83
- panic ("mixed tabs and spaces found:\n " + string (in ))
68
+ lines [i ] = line [len (prefix ):]
84
69
}
85
70
return typed .YAMLObject (bytes .Join (lines , []byte {'\n' }))
86
71
}
0 commit comments