forked from valyala/quicktemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonstring.go
More file actions
40 lines (39 loc) · 714 Bytes
/
jsonstring.go
File metadata and controls
40 lines (39 loc) · 714 Bytes
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 quicktemplate
// appendJSONString is a synonym to strconv.AppendQuote, but works 3x faster.
func appendJSONString(dst []byte, s string) []byte {
j := 0
out := ""
dst = append(dst, `"`...)
for i, n := 0, len(s); i < n; i++ {
switch s[i] {
case '"':
out = `\"`
case '\\':
out = `\\`
case '\n':
out = `\n`
case '\r':
out = `\r`
case '\t':
out = `\t`
case '\f':
out = `\u000c`
case '\b':
out = `\u0008`
case '<':
out = `\u003c`
case '\'':
out = `\u0027`
case 0:
out = `\u0000`
}
if len(out) > 0 {
dst = append(dst, s[j:i]...)
dst = append(dst, out...)
j = i + 1
out = ""
}
}
dst = append(dst, s[j:]...)
return append(dst, `"`...)
}