-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
68 lines (55 loc) · 1.61 KB
/
helpers.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package atv
import (
"fmt"
"reflect"
"strings"
)
// spacer generates a whitespace string that can be used for indenting lines up to the specified level.
func spacer(level int) string {
return strings.Repeat(" ", level)
}
// isNil checks whether the value of the specified interface is nil.
func isNil(v interface{}) bool {
return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil())
}
// charsToQuote contains characters that are quoted in string in ATV documents.
const charsToQuote = `"\`
// quote quotes the specified string in the ATV specific fashion.
func quote(s string) string {
quoted := strings.Builder{}
quoted.WriteRune('"')
for _, c := range s {
if strings.ContainsRune(charsToQuote, c) {
quoted.WriteRune('\\')
}
quoted.WriteRune(c)
}
quoted.WriteRune('"')
return quoted.String()
}
// unquote unquotes the specified string in the ATV specific fashion.
func unquote(s string) (string, error) {
// check whether the string contains the expected embracing quotes
if len(s) < 2 || s[0] != '"' || s[len(s)-1] != '"' {
return "", fmt.Errorf("String is not properly quoted")
}
// strip embracing quotes
s = s[1 : len(s)-1]
unquoted := strings.Builder{}
unquotePending := false
for _, c := range s {
if !unquotePending && c == '\\' {
unquotePending = true
continue
}
if unquotePending && !strings.ContainsRune(charsToQuote, c) {
unquoted.WriteRune('\\')
}
unquoted.WriteRune(c)
unquotePending = false
}
if unquotePending {
return "", fmt.Errorf("Unpaired backslash found when unquoting string (%s)", s)
}
return unquoted.String(), nil
}