1
2
3
4
5 package json
6
7 import "bytes"
8
9
10
11
12
13
14 func HTMLEscape(dst *bytes.Buffer, src []byte) {
15 dst.Grow(len(src))
16 dst.Write(appendHTMLEscape(dst.AvailableBuffer(), src))
17 }
18
19 func appendHTMLEscape(dst, src []byte) []byte {
20
21
22 start := 0
23 for i, c := range src {
24 if c == '<' || c == '>' || c == '&' {
25 dst = append(dst, src[start:i]...)
26 dst = append(dst, '\\', 'u', '0', '0', hex[c>>4], hex[c&0xF])
27 start = i + 1
28 }
29
30 if c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
31 dst = append(dst, src[start:i]...)
32 dst = append(dst, '\\', 'u', '2', '0', '2', hex[src[i+2]&0xF])
33 start = i + len("\u2029")
34 }
35 }
36 return append(dst, src[start:]...)
37 }
38
39
40
41 func Compact(dst *bytes.Buffer, src []byte) error {
42 dst.Grow(len(src))
43 b := dst.AvailableBuffer()
44 b, err := appendCompact(b, src, false)
45 dst.Write(b)
46 return err
47 }
48
49 func appendCompact(dst, src []byte, escape bool) ([]byte, error) {
50 origLen := len(dst)
51 scan := newScanner()
52 defer freeScanner(scan)
53 start := 0
54 for i, c := range src {
55 if escape && (c == '<' || c == '>' || c == '&') {
56 if start < i {
57 dst = append(dst, src[start:i]...)
58 }
59 dst = append(dst, '\\', 'u', '0', '0', hex[c>>4], hex[c&0xF])
60 start = i + 1
61 }
62
63 if escape && c == 0xE2 && i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xA8 {
64 if start < i {
65 dst = append(dst, src[start:i]...)
66 }
67 dst = append(dst, '\\', 'u', '2', '0', '2', hex[src[i+2]&0xF])
68 start = i + 3
69 }
70 v := scan.step(scan, c)
71 if v >= scanSkipSpace {
72 if v == scanError {
73 break
74 }
75 if start < i {
76 dst = append(dst, src[start:i]...)
77 }
78 start = i + 1
79 }
80 }
81 if scan.eof() == scanError {
82 return dst[:origLen], scan.err
83 }
84 if start < len(src) {
85 dst = append(dst, src[start:]...)
86 }
87 return dst, nil
88 }
89
90 func appendNewline(dst []byte, prefix, indent string, depth int) []byte {
91 dst = append(dst, '\n')
92 dst = append(dst, prefix...)
93 for i := 0; i < depth; i++ {
94 dst = append(dst, indent...)
95 }
96 return dst
97 }
98
99
100
101
102
103
104
105 const indentGrowthFactor = 2
106
107
108
109
110
111
112
113
114
115
116
117
118 func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
119 dst.Grow(indentGrowthFactor * len(src))
120 b := dst.AvailableBuffer()
121 b, err := appendIndent(b, src, prefix, indent)
122 dst.Write(b)
123 return err
124 }
125
126 func appendIndent(dst, src []byte, prefix, indent string) ([]byte, error) {
127 origLen := len(dst)
128 scan := newScanner()
129 defer freeScanner(scan)
130 needIndent := false
131 depth := 0
132 for _, c := range src {
133 scan.bytes++
134 v := scan.step(scan, c)
135 if v == scanSkipSpace {
136 continue
137 }
138 if v == scanError {
139 break
140 }
141 if needIndent && v != scanEndObject && v != scanEndArray {
142 needIndent = false
143 depth++
144 dst = appendNewline(dst, prefix, indent, depth)
145 }
146
147
148
149 if v == scanContinue {
150 dst = append(dst, c)
151 continue
152 }
153
154
155 switch c {
156 case '{', '[':
157
158 needIndent = true
159 dst = append(dst, c)
160 case ',':
161 dst = append(dst, c)
162 dst = appendNewline(dst, prefix, indent, depth)
163 case ':':
164 dst = append(dst, c, ' ')
165 case '}', ']':
166 if needIndent {
167
168 needIndent = false
169 } else {
170 depth--
171 dst = appendNewline(dst, prefix, indent, depth)
172 }
173 dst = append(dst, c)
174 default:
175 dst = append(dst, c)
176 }
177 }
178 if scan.eof() == scanError {
179 return dst[:origLen], scan.err
180 }
181 return dst, nil
182 }
183
View as plain text