-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfusables.go
350 lines (272 loc) · 7.41 KB
/
confusables.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Package confusables provides functions for identifying words that appear to
// be similar but use different characters.
package confusables
//go:generate go run scripts/build-tables.go > tables.go
import (
"bufio"
"errors"
"io"
"strconv"
"strings"
"unicode"
"golang.org/x/text/runes"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
// ErrIgnoreLine is raised when processing a line which should be ignored.
var ErrIgnoreLine = errors.New("line should be ignored")
const (
base = 16
bitsize = 64
)
// ConfusableEntry defines a parsed entry from a confusable mapping file.
type ConfusableEntry struct {
Description Description
Source rune
Target string
}
// Confusables provides functions for identifying words that appear to be similar but use different characters.
type Confusables struct {
removeMarks transform.Transformer
}
// Description describes a mapping for a confusable.
type Description struct {
From string
To string
}
// Diff details the mapping from a rune to its confusable if it exists.
type Diff struct {
Confusable *string
Description *Description
Rune rune
}
// New creates a new instance of Confusables.
func New() *Confusables {
return &Confusables{
removeMarks: transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC),
}
}
// ToASCII converts characters in a string to their ASCII equivalent if possible.
func (c *Confusables) ToASCII(s string) string {
a, _ := c.toASCII(s)
return a
}
func (c *Confusables) ToASCIIDiff(s string) (string, []Diff) {
return c.toASCII(s)
}
// ToNumber converts characters in a string that look like numbers into numbers.
func (c *Confusables) ToNumber(s string) string {
s = c.ToASCII(s)
var number strings.Builder
for _, r := range s {
switch strings.ToLower(string(r)) {
case "o":
r = '0'
case "i", "l", "!":
r = '1'
}
number.WriteRune(r)
}
return number.String()
}
func (c *Confusables) processRune(r rune) *Diff {
diff := &Diff{}
diff.Rune = r
if r <= unicode.MaxASCII {
return diff
}
if v, ok := confusables[r]; ok {
c.removeMarks.Reset()
v, _, _ := transform.String(c.removeMarks, v)
if isASCII(v) {
diff.Confusable = &v
diff.Description = getDescriptionMapping(r, &v)
return diff
}
}
c.removeMarks.Reset()
v, _, _ := transform.String(c.removeMarks, string(r))
if isASCII(v) {
diff.Confusable = &v
diff.Description = getDescriptionMapping(r, &v)
}
return diff
}
func (c *Confusables) toASCII(s string) (string, []Diff) {
if isASCII(s) {
return s, noDiff(s)
}
var ascii strings.Builder
diffs := make([]Diff, 0, len(s))
for _, r := range s {
diff := c.processRune(r)
diffs = append(diffs, *diff)
if diff.Confusable != nil {
ascii.WriteString(*diff.Confusable)
} else {
ascii.WriteRune(r)
}
}
return norm.NFKC.String(ascii.String()), diffs
}
// AddMapping allows custom mappings to be defined for a rune.
func AddMapping(r rune, confusable string) {
confusables[r] = confusable
}
// AddMappingWithDesc allows a custom mapping to be defined between a rune and its confusable and for a description to
// be provided for that mapping.
func AddMappingWithDesc(r rune, confusable, runeDesc, confusableDesc string) {
AddMapping(r, confusable)
descriptions[runeDesc] = confusableDesc
}
// IsConfusable checks if two strings are confusable of one another.
func IsConfusable(s1, s2 string) bool {
return ToSkeleton(s1) == ToSkeleton(s2)
}
// LoadMappings reads r and loads in confusable mappings. Where a confusable already exists, this will override the
// mapping.
func LoadMappings(r io.Reader) error {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
confusableEntry, err := ParseLine(scanner.Text())
if err != nil {
if errors.Is(err, ErrIgnoreLine) {
continue
}
return err
}
AddMappingWithDesc(confusableEntry.Source, confusableEntry.Target, confusableEntry.Description.From,
confusableEntry.Description.To)
}
return nil
}
// ParseLine takes a confusable line and returns a ConfusableEntry.
// If a line should be skipped an ErrIgnoreLine error is raised.
func ParseLine(line string) (*ConfusableEntry, error) {
// Remove BOM, skip comments and blank lines
line = strings.TrimPrefix(line, string([]byte{0xEF, 0xBB, 0xBF}))
if strings.HasPrefix(line, "#") || line == "" {
return nil, ErrIgnoreLine
}
// Extract source -> target mapping
fields := strings.Split(line, " ;\t")
sourceRunes, err := codepointsToRunes(fields[0])
if err != nil {
return nil, err
}
target, err := codepointsToRunes(fields[1])
if err != nil {
return nil, err
}
return &ConfusableEntry{
Description: Description{
From: strings.TrimSpace(strings.Split(strings.Split(fields[2], " → ")[1], " ) ")[1]),
To: strings.TrimSpace(strings.Split(strings.Split(fields[2], " → ")[2], "#")[0]),
},
Source: sourceRunes[0],
Target: string(target),
}, nil
}
// ToASCII converts characters in a string to their ASCII equivalent if possible.
func ToASCII(s string) string {
return New().ToASCII(s)
}
// ToASCIIDiff converts characters in a string to their ASCII equivalent if possible.
func ToASCIIDiff(s string) (string, []Diff) {
return New().ToASCIIDiff(s)
}
// ToNumber converts characters in a string to their numeric values if possible.
func ToNumber(s string) string {
return New().ToNumber(s)
}
// ToSkeleton converts a string to its skeleton form as defined by the skeleton
// algorithm in https://www.unicode.org/reports/tr39/#def-skeleton.
func ToSkeleton(s string) string {
nfd := norm.NFD.String(s)
var skeleton strings.Builder
for _, r := range nfd {
if c, ok := confusables[r]; ok {
skeleton.WriteString(c)
} else {
skeleton.WriteRune(r)
}
}
return skeleton.String()
}
// ToSkeletonDiff returns a slice of Diff detailing the changes that have been
// made within the string to reach its skeleton form.
func ToSkeletonDiff(s string) []Diff {
nfd := norm.NFD.String(s)
if len(nfd) == 0 {
return nil
}
diffs := make([]Diff, len(nfd))
for i, r := range nfd {
var confusable *string
if c, ok := confusables[r]; ok {
confusable = &c
}
diffs[i] = Diff{
Confusable: confusable,
Description: getDescriptionMapping(r, confusable),
Rune: r,
}
}
return diffs
}
func codepointsToRunes(s string) ([]rune, error) {
codePoints := strings.Split(s, " ")
runes := make([]rune, 0, len(codePoints))
for _, unicodeCodePoint := range codePoints {
codePoint, err := strconv.ParseUint(unicodeCodePoint, base, bitsize)
if err != nil {
return nil, err
}
runes = append(runes, rune(codePoint))
}
return runes, nil
}
// Get the mapping between a rune and its confusable.
func getDescriptionMapping(r rune, confusable *string) *Description {
if confusable == nil {
return nil
}
rDesc := descriptions[string(r)]
if rDesc == "" {
nfd := norm.NFD.String(string(r))
parts := make([]string, 0, len(nfd))
for _, c := range nfd {
cDesc := descriptions[string(c)]
if cDesc == "" {
return nil
}
parts = append(parts, cDesc)
}
rDesc = strings.Join(parts, ", ")
}
confusableDesc := descriptions[*confusable]
if confusableDesc == "" {
return nil
}
return &Description{
From: rDesc,
To: confusableDesc,
}
}
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] > unicode.MaxASCII {
return false
}
}
return true
}
func noDiff(s string) []Diff {
diff := make([]Diff, len(s))
for i, r := range s {
diff[i] = Diff{
Rune: r,
}
}
return diff
}