-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson5_string.go
More file actions
117 lines (113 loc) · 2.07 KB
/
Copy pathjson5_string.go
File metadata and controls
117 lines (113 loc) · 2.07 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
package tojson
import "unicode/utf8"
func appendRecodeString(dst []byte, src []byte) []byte {
dst = append(dst, '"')
start := 0
for i := 0; i < len(src); {
if b := src[i]; b < utf8.RuneSelf {
if safeSet[b] {
i++
continue
}
dst = append(dst, src[start:i]...)
if b == '\\' && i+1 < len(src) {
switch src[i+1] {
case 'u':
dst = append(dst, '\\')
i++
start = i
continue
case 'n':
b = '\n'
i++
case 't':
b = '\t'
i++
case 'b':
b = '\b'
i++
case 'f':
b = '\f'
i++
case '"':
b = '"'
i++
case 'r':
b = '\r'
i++
case '\\':
b = '\\'
i++
case '/':
b = '/'
i++
case 'a':
b = '\a'
i++
case 'v':
b = '\v'
i++
case 'x':
if i+3 < len(src) {
if v1, v2 := hexVal(src[i+2]), hexVal(src[i+3]); v1 >= 0 && v2 >= 0 {
b = byte(v1<<4 | v2)
i += 3
}
}
case '\n':
b = '\n'
i++
case '\'':
b = '\''
i++
}
}
switch b {
case '\\', '"':
dst = append(dst, '\\', b)
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
if i+1 < len(src) && src[i+1] == '\n' {
break
}
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
default:
if b < utf8.RuneSelf && safeSet[b] {
dst = append(dst, b)
} else {
dst = append(dst, '\\', 'u', '0', '0', hex[b>>4], hex[b&0xF])
}
}
i++
start = i
continue
}
n := min(len(src)-i, utf8.UTFMax)
c, size := utf8.DecodeRune(src[i : i+n])
if c == utf8.RuneError && size == 1 {
dst = append(dst, src[start:i]...)
dst = append(dst, `\ufffd`...)
i += size
start = i
continue
}
if c == '\u2028' || c == '\u2029' {
dst = append(dst, src[start:i]...)
dst = append(dst, '\\', 'u', '2', '0', '2', hex[c&0xF])
i += size
start = i
continue
}
i += size
}
dst = append(dst, src[start:]...)
dst = append(dst, '"')
return dst
}