isle/go/yamlutil/testutils.go

30 lines
612 B
Go
Raw Permalink Normal View History

package yamlutil
import (
"strings"
)
// ReplacePrefixTabs will replace the leading tabs of each line with two spaces.
// Tabs are not a valid indent in yaml (wtf), but they are convenient to use in
// go when using multi-line strings.
//
// ReplacePrefixTabs should only be used within tests.
func ReplacePrefixTabs(str string) string {
lines := strings.Split(str, "\n")
for i := range lines {
var n int
for _, r := range lines[i] {
if r == '\t' {
n++
} else {
break
}
}
spaces := strings.Repeat(" ", n)
lines[i] = spaces + lines[i][n:]
}
return strings.Join(lines, "\n")
}