forked from ohler55/ojg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
204 lines (194 loc) · 4.39 KB
/
string.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2021, Peter Ohler, All rights reserved.
package ojg
import (
"unicode/utf8"
)
const hex = "0123456789abcdef"
var (
maxTokenLen = 64
// Copied from sen/maps.go
// 0123456789abcdef0123456789abcdef
jMap = "" +
`........btn.fr..................` + // 0x00
`oo"ooohooooooooooooooooooooohoho` + // 0x20
`oooooooooooooooooooooooooooo\ooo` + // 0x40
"ooooooooooooooooooooooooooooooo." + // 0x60
`88888888888888888888888888888888` + // 0x80
`88888888888888888888888888888888` + // 0xa0
`88888888888888888888888888888888` + // 0xc0
`88888888888888888888888888888888` // 0xe0
// 0123456789abcdef0123456789abcdef
senMap = "" +
`........bxx.fr..................` + // 0x00
`xx"xoxhxxxooxoox0000000000xxhxho` + // 0x20
`ooooooooooooooooooooooooooox\xoo` + // 0x40
"oooooooooooooooooooooooooooxoxo." + // 0x60
`88888888888888888888888888888888` + // 0x80
`88888888888888888888888888888888` + // 0xa0
`88888888888888888888888888888888` + // 0xc0
`88888888888888888888888888888888` // 0xe0
)
// AppendJSONString appends a JSON encoding of a string to the provided byte
// slice.
func AppendJSONString(buf []byte, s string, htmlSafe bool) []byte {
buf = append(buf, '"')
start := 0
skip := 0
for i, b := range []byte(s) {
if i < skip {
continue
}
c := jMap[b]
switch c {
case 'o':
continue
case '.':
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u00`...)
buf = append(buf, hex[(b>>4)&0x0f])
buf = append(buf, hex[b&0x0f])
start = i + 1
case 'h':
if htmlSafe {
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u00`...)
buf = append(buf, hex[(b>>4)&0x0f])
buf = append(buf, hex[b&0x0f])
start = i + 1
}
case '8':
r, cnt := utf8.DecodeRuneInString(s[i:])
switch r {
case '\u2028':
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u2028`...)
start = i + cnt
skip = start
case '\u2029':
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u2029`...)
start = i + cnt
skip = start
case utf8.RuneError:
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\ufffd`...)
start = i + cnt
skip = start
default:
skip = i + cnt
}
default:
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, '\\')
buf = append(buf, c)
start = i + 1
}
}
if start < len(s) {
buf = append(buf, s[start:]...)
}
return append(buf, '"')
}
// AppendSENString appends a SEN encoding of a string to the provided byte
// slice.
func AppendSENString(buf []byte, s string, htmlSafe bool) []byte {
if len(s) == 0 {
return append(buf, `""`...)
}
b0 := len(buf)
m := senMap[s[0]]
quote := maxTokenLen < len(s) || (m != 'o' && m != '8' && !(!htmlSafe && m == 'h'))
buf = append(buf, '"')
start := 0
skip := 0
for i, b := range []byte(s) {
if i < skip {
continue
}
c := senMap[b]
switch c {
case 'o', '0':
continue
case 'x':
quote = true
case '.':
quote = true
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u00`...)
buf = append(buf, hex[(b>>4)&0x0f])
buf = append(buf, hex[b&0x0f])
start = i + 1
case 'h':
if htmlSafe {
quote = true
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u00`...)
buf = append(buf, hex[(b>>4)&0x0f])
buf = append(buf, hex[b&0x0f])
start = i + 1
}
case '8':
r, cnt := utf8.DecodeRuneInString(s[i:])
switch r {
case '\u2028':
quote = true
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u2028`...)
start = i + cnt
skip = start
case '\u2029':
quote = true
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\u2029`...)
start = i + cnt
skip = start
case utf8.RuneError:
quote = true
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, `\ufffd`...)
start = i + cnt
skip = start
default:
skip = i + cnt
}
default:
if start < i {
buf = append(buf, s[start:i]...)
}
buf = append(buf, '\\')
buf = append(buf, c)
start = i + 1
quote = true
}
}
if start < len(s) {
buf = append(buf, s[start:]...)
}
if quote {
return append(buf, '"')
}
copy(buf[b0:], buf[b0+1:])
return buf[:len(buf)-1]
}