-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
40 lines (38 loc) · 895 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package gspec
import "strings"
// Unindent is a utility function that unindents Go's raw string literal by the
// indent guessed from the first nonblank line, so the raw string literal can be
// indented as normal code and looks better. A prefix and a suffix newline '\n'
// will be added if there is none.
func Unindent(s string) string {
lines := strings.Split(s, "\n")
indent := ""
done := false
for _, line := range lines {
if strings.TrimSpace(line) != "" {
for _, r := range line {
if r == ' ' || r == '\t' {
indent += string(r)
} else {
done = true
break
}
}
}
if done {
break
}
}
for i := range lines {
if strings.HasPrefix(lines[i], indent) {
lines[i] = lines[i][len(indent):]
} else {
lines[i] = strings.TrimLeft(lines[i], "\t")
}
}
if lines[0] == "" {
lines = lines[1:]
}
s = strings.Join(lines, "\n")
return s
}