-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringify.go
More file actions
134 lines (127 loc) · 2.73 KB
/
stringify.go
File metadata and controls
134 lines (127 loc) · 2.73 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package maml
import (
"fmt"
"math"
"strings"
"unicode/utf8"
)
// Stringify converts a Value to its MAML string representation.
func Stringify(v Value) string {
return doStringify(v, 0)
}
func doStringify(v Value, level int) string {
switch val := v.v.(type) {
case nil:
return "null"
case bool:
if val {
return "true"
}
return "false"
case int64:
return fmt.Sprintf("%d", val)
case float64:
if val == 0 && math.Signbit(val) {
return "-0"
}
s := fmt.Sprintf("%g", val)
if !strings.Contains(s, ".") && !strings.Contains(s, "e") && !strings.Contains(s, "E") {
s += ".0"
}
return s
case string:
return QuoteString(val)
case []Value:
if len(val) == 0 {
return "[]"
}
childIndent := getIndent(level + 1)
parentIndent := getIndent(level)
var sb strings.Builder
sb.WriteString("[\n")
for i, item := range val {
if i > 0 {
sb.WriteByte('\n')
}
sb.WriteString(childIndent)
sb.WriteString(doStringify(item, level+1))
}
sb.WriteByte('\n')
sb.WriteString(parentIndent)
sb.WriteByte(']')
return sb.String()
case *OrderedMap:
if val.Len() == 0 {
return "{}"
}
childIndent := getIndent(level + 1)
parentIndent := getIndent(level)
var sb strings.Builder
sb.WriteString("{\n")
for i, key := range val.keys {
if i > 0 {
sb.WriteByte('\n')
}
sb.WriteString(childIndent)
sb.WriteString(stringifyKey(key))
sb.WriteString(": ")
sb.WriteString(doStringify(val.values[i], level+1))
}
sb.WriteByte('\n')
sb.WriteString(parentIndent)
sb.WriteByte('}')
return sb.String()
default:
return "null"
}
}
// IsIdentifierKey returns true if the key can be written as a bare identifier.
func IsIdentifierKey(key string) bool {
if key == "" {
return false
}
for i := 0; i < len(key); i++ {
ch := key[i]
if !((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-') {
return false
}
}
return true
}
func stringifyKey(key string) string {
if IsIdentifierKey(key) {
return key
}
return QuoteString(key)
}
// QuoteString quotes a string value with proper MAML escape sequences.
func QuoteString(s string) string {
var sb strings.Builder
sb.Grow(len(s) + 2)
sb.WriteByte('"')
for i := 0; i < len(s); {
r, size := utf8.DecodeRuneInString(s[i:])
switch {
case r == '"':
sb.WriteString("\\\"")
case r == '\\':
sb.WriteString("\\\\")
case r == '\n':
sb.WriteString("\\n")
case r == '\r':
sb.WriteString("\\r")
case r == '\t':
sb.WriteString("\\t")
case r < 0x20 || r == 0x7F:
sb.WriteString(fmt.Sprintf("\\u{%X}", r))
default:
sb.WriteRune(r)
}
i += size
}
sb.WriteByte('"')
return sb.String()
}
func getIndent(level int) string {
return strings.Repeat(" ", level)
}