forked from b3log/lute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unesc_str.go
211 lines (187 loc) · 3.63 KB
/
unesc_str.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
205
206
207
208
209
210
211
// Lute - A structured markdown engine.
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
// http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
package lute
import (
"strconv"
"strings"
"unicode/utf8"
"github.com/b3log/lute/html"
)
func unescapeString(tokens []byte) (ret []byte) {
if nil == tokens {
return
}
tokens = strToBytes(htmlUnescapeString(bytesToStr(tokens)))
length := len(tokens)
ret = make([]byte, 0, length)
for i := 0; i < length; i++ {
if isBackslashEscapePunct(tokens, i) {
ret = ret[:len(ret)-1]
}
ret = append(ret, tokens[i])
}
return
}
func htmlUnescapeString(s string) string {
// 鸣谢 https://gitlab.com/golang-commonmark
i := strings.IndexByte(s, itemAmpersand)
if i < 0 {
return s
}
anyChanges := false
var entityStr string
var entityLen int
for i < len(s) {
if s[i] == '&' {
entityStr, entityLen = parseEntity(s[i:])
if entityLen > 0 {
anyChanges = true
break
}
}
i++
}
if !anyChanges {
return s
}
buf := make([]byte, len(s)-entityLen+len(entityStr))
copy(buf[:i], s)
n := copy(buf[i:], entityStr)
j := i + n
i += entityLen
for i < len(s) {
b := s[i]
if b == '&' {
entityStr, entityLen = parseEntity(s[i:])
if entityLen > 0 {
n = copy(buf[j:], entityStr)
j += n
i += entityLen
continue
}
}
buf[j] = b
j++
i++
}
return string(buf[:j])
}
func parseEntity(s string) (string, int) {
st := 0
var n int
for i := 1; i < len(s); i++ {
b := s[i]
switch st {
case 0: // initial state
switch {
case b == '#':
st = 1
case isASCIILetter(b):
n = 1
st = 2
default:
return "", 0
}
case 1: // &#
switch {
case b == 'x' || b == 'X':
st = 3
case isDigit(b):
n = 1
st = 4
default:
return "", 0
}
case 2: // &q
switch {
case isASCIILetterNum(b):
n++
if n > 31 {
return "", 0
}
case b == ';':
if e, ok := html.Entities[s[i-n:i]]; ok {
return e, i + 1
}
return "", 0
default:
return "", 0
}
case 3: // &#x
switch {
case isHexDigit(b):
n = 1
st = 5
default:
return "", 0
}
case 4: // �
switch {
case isDigit(b):
n++
if n > 8 {
return "", 0
}
case b == ';':
c, _ := strconv.ParseInt(s[i-n:i], 10, 32)
if !isValidEntityCode(c) {
return BadEntity, i + 1
}
return string(rune(c)), i + 1
default:
return "", 0
}
case 5: // �
switch {
case isHexDigit(b):
n++
if n > 8 {
return "", 0
}
case b == ';':
c, err := strconv.ParseInt(s[i-n:i], 16, 32)
if nil != err {
return BadEntity, i + 1
}
if !isValidEntityCode(c) {
return BadEntity, i + 1
}
return string(rune(c)), i + 1
default:
return "", 0
}
}
}
return "", 0
}
const BadEntity = string(utf8.RuneError)
func isValidEntityCode(c int64) bool {
switch {
case !utf8.ValidRune(rune(c)):
return false
// never used
case c >= 0xfdd0 && c <= 0xfdef:
return false
case c&0xffff == 0xffff || c&0xffff == 0xfffe:
return false
// control codes
case c >= 0x00 && c <= 0x08:
return false
case c == 0x0b:
return false
case c >= 0x0e && c <= 0x1f:
return false
case c >= 0x7f && c <= 0x9f:
return false
}
return true
}