-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunicode.go
634 lines (537 loc) · 12.8 KB
/
unicode.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
package dirsyn
/*
unicode.go handles rune analysis and unicode ranging.
*/
import (
"unicode"
"unicode/utf16"
"unicode/utf8"
)
/*
UTF8String implements the UTF8 String syntax and abstraction.
From [§ 1.4 of RFC 4512]:
UTF8 = UTF1 / UTFMB
UTFMB = UTF2 / UTF3 / UTF4
UTF0 = %x80-BF
UTF1 = %x00-7F
UTF2 = %xC2-DF UTF0
UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
%xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
%xF4 %x80-8F 2(UTF0)
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
*/
type UTF8String string
/*
UTF8String returns an instance of [UTF8String] alongside an error
following an analysis of x in the context of a UTF8-compliant string.
*/
func (r RFC4512) UTF8String(x any) (UTF8String, error) {
return assertUTF8String(x)
}
func assertUTF8String(x any) (u UTF8String, err error) {
var raw string
switch tv := x.(type) {
case UTF8String:
raw = string(tv)
case []byte:
raw = string(tv)
case string:
raw = tv
default:
err = errorBadType("UTF8String")
return
}
u, err = uTF8(raw)
return
}
/*
String returns the string representation of the receiver instance.
*/
func (r UTF8String) String() string { return string(r) }
func (r UTF8String) IsZero() bool { return len(r) == 0 }
var (
runeLen func(rune) int = utf8.RuneLen
decRune func([]byte) (rune, int) = utf8.DecodeRune
ucIs func(*unicode.RangeTable, rune) bool = unicode.Is
uc1Of func([]*unicode.RangeTable, rune) bool = unicode.IsOneOf
sfold func(rune) rune = unicode.SimpleFold
utf8OK func(string) bool = utf8.ValidString
utf16Enc func([]rune) []uint16 = utf16.Encode
isSpace func(rune) bool = unicode.IsSpace
isPunct func(rune) bool = unicode.IsPunct
)
var runeSelf rune = utf8.RuneSelf
var maxASCII rune = unicode.MaxASCII
var t61NonContiguous []rune
var (
digits,
lAlphas,
uAlphas,
uCSRange,
octRange,
iA5Range,
ttxRange,
t61Ranges,
uTF8SubsetRange,
lineCharRange,
substrRange,
asciiRange,
utf0Range,
utf1Range,
utf2Range,
utf2aSafeRange,
utf2bSafeRange,
utf3aRange,
utf3SafeRange,
utf3bRange,
utf3cRange,
utf3dRange,
utf4aRange,
utf4SafeRange,
utf4bRange,
utf4cRange *unicode.RangeTable
)
var telephoneNumberRunes []rune
var printableStringRunes []rune
func isDigit(r rune) bool {
return '0' <= r && r <= '9'
}
func isAlpha(r rune) bool {
return isLAlpha(r) || isUAlpha(r)
}
func isUAlpha(r rune) bool {
return 'A' <= r && r <= 'Z'
}
func isLAlpha(r rune) bool {
return 'a' <= r && r <= 'z'
}
func isBinChar(r rune) bool {
return r == '0' || r == '1'
}
func runeInSlice(r rune, slice []rune) bool {
for i := 0; i < len(slice); i++ {
if r == slice[i] {
return true
}
}
return false
}
func isAlphaNumeric(r rune) bool {
return ucIs(lAlphas, r) ||
ucIs(uAlphas, r) ||
ucIs(digits, r)
}
/*
isT61RangedRune returns a Boolean value whether rune r matches an allowed
Unicode codepoint range.
*/
func isT61RangedRune(r rune) bool {
return uc1Of([]*unicode.RangeTable{t61Ranges}, r)
}
/*
UTF8String = StringValue
StringValue = dquote *SafeUTF8Character dquote
dquote = %x22 ; " (double quote)
SafeUTF8Character = %x00-21 / %x23-7F / ; ASCII minus dquote
dquote dquote / ; escaped double quote
%xC0-DF %x80-BF / ; 2 byte UTF-8 character
%xE0-EF 2(%x80-BF) / ; 3 byte UTF-8 character
%xF0-F7 3(%x80-BF) ; 4 byte UTF-8 character
*/
func isSafeUTF8(x any) (err error) {
var raw []rune
if raw, err = assertRunes(x); err != nil {
return
}
funcs := map[int]func(string) error{
2: isSafeUTF2,
3: isSafeUTF3,
4: isSafeUTF4,
}
var last rune
for i := 0; i < len(raw) && err == nil; i++ {
r := raw[i]
switch rL := runeLen(r); rL {
case 1:
// ASCII range w/o double-quote
err = isSafeUTF1(string(r))
if '"' == r && last != '\u005C' {
err = errorTxt("Unescaped double-quote; not a UTF8 Safe Character")
}
last = r
case 2, 3, 4:
// UTF2/3/4
err = funcs[rL](string(r))
}
}
return
}
func isSafeUTF1(x string) (err error) {
z := rune([]byte(x)[0])
if !(ucIs(asciiRange, z) && z != '"') {
err = errorTxt("Incompatible char for UTF0 (in ASCII Safe Range):" + x)
}
return
}
func isSafeUTF2(x string) (err error) {
z := []byte(string(x))
ch1 := rune(z[0])
ch2 := rune(z[1])
if !(ucIs(utf2aSafeRange, ch1) &&
ucIs(utf2bSafeRange, ch2)) {
err = errorTxt("Incompatible chars for UTF2 (in UTF2 Safe Range): " + x)
}
return
}
func isSafeUTF3(x string) (err error) {
z := []byte(string(x))
ch1 := rune(z[0])
ch2 := rune(z[1])
ch3 := rune(z[2])
if !(ucIs(utf3SafeRange, ch1) &&
ucIs(utf2bSafeRange, ch2) &&
ucIs(utf2bSafeRange, ch3)) {
err = errorTxt("Incompatible chars for UTF3 (in UTF3 Safe Range): " + x)
}
return
}
func isSafeUTF4(x string) (err error) {
z := []byte(string(x))
ch1 := rune(z[0])
ch2 := rune(z[1])
ch3 := rune(z[2])
ch4 := rune(z[3])
if !(ucIs(utf4SafeRange, ch1) &&
ucIs(utf2bSafeRange, ch2) &&
ucIs(utf2bSafeRange, ch3) &&
ucIs(utf2bSafeRange, ch4)) {
err = errorTxt("Incompatible chars for UTF4 (in UTF4 Safe Range): " + x)
}
return
}
/*
uTF8 returns an error following an analysis of x in the context of
one (1) or more UTF8 characters.
From [§ 1.4 of RFC 4512]:
UTF8 = UTF1 / UTFMB
UTFMB = UTF2 / UTF3 / UTF4
UTF0 = %x80-BF
UTF1 = %x00-7F
UTF2 = %xC2-DF UTF0
UTF3 = %xE0 %xA0-BF UTF0 / %xE1-EC 2(UTF0) /
%xED %x80-9F UTF0 / %xEE-EF 2(UTF0)
UTF4 = %xF0 %x90-BF 2(UTF0) / %xF1-F3 3(UTF0) /
%xF4 %x80-8F 2(UTF0)
[§ 1.4 of RFC 4512]: https://datatracker.ietf.org/doc/html/rfc4512#section-1.4
*/
func uTF8(x any, zok ...bool) (u UTF8String, err error) {
var raw []rune
if raw, err = assertRunes(x, zok...); err != nil {
return
}
for i := 0; i < len(raw) && err == nil; i++ {
if !ucIs(utf1Range, rune(raw[i])) {
err = uTFMB(rune(raw[i]))
}
}
if err == nil {
for i := 0; i < len(raw); i++ {
u += UTF8String(raw[i])
}
}
return
}
func assertRunes(x any, zok ...bool) (runes []rune, err error) {
var zerook bool
if len(zok) > 0 {
zerook = zok[0]
}
switch tv := x.(type) {
case []rune:
runes = tv
case rune:
runes = append(runes, tv)
case byte:
runes = append(runes, rune(tv))
case []byte:
runes, err = assertRunes(string(tv))
case string:
if len(tv) == 0 && !zerook {
err = errorBadLength("Zero length rune", 0)
break
}
runes = []rune(tv)
default:
err = errorBadType("Not rune compatible")
}
return
}
func assertionValueRunes(x any, zok ...bool) (err error) {
var raw []rune
if raw, err = assertRunes(x, zok...); err != nil {
return
}
_err := errorTxt("Invalid assertionvalue characters")
for i := 0; i < len(raw) && err == nil; i++ {
if raw[i] == '\\' {
// Check if there are at least
// two more characters
if i+3 > len(raw) {
err = _err
} else if !isHex(rune(raw[i+1])) || !isHex(rune(raw[i+2])) {
// the next two characters are not hex
err = _err
}
// Skip the next two characters, as
// we've already vetted them
i += 2
} else if !ucIs(uTF8SubsetRange, rune(raw[i])) {
err = uTFMB(rune(raw[i]))
}
}
return
}
func isHex(char rune) bool {
return ('0' <= char && char <= '9') ||
('A' <= char && char <= 'F') ||
('a' <= char && char <= 'f')
}
/*
uTFMB returns an error following an analysis of x in the context of
one (1) or more UTFMB characters.
*/
func uTFMB(x any) (err error) {
var raw []rune
if raw, err = assertRunes(x); err == nil {
for i := 0; i < len(raw) && err == nil; i++ {
r := rune(raw[i])
var valid bool
if valid, err = isUTFMB(r); !valid {
err = errorTxt("Invalid UTFMB char: " + string(r))
break
}
}
}
return
}
func isUTF0(b byte) bool {
return b >= 0x80 && b <= 0xBF
}
func isUTF2(ub []byte) bool {
return ub[0] >= 0xC2 && ub[0] <= 0xDF && isUTF0(ub[1])
}
func isUTF3(ub []byte) (b bool) {
switch ub[0] {
case 0xE0:
b = ucIs(utf3aRange, rune(ub[1])) && isUTF0(ub[2])
case 0xED:
b = ucIs(utf3cRange, rune(ub[1])) && isUTF0(ub[2])
case 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xEE, 0xEF:
b = isUTF0(ub[1]) && isUTF0(ub[2])
}
return
}
func isUTF4(ub []byte) (b bool) {
switch ub[0] {
case 0xF0:
b = ub[1] >= 0x90 && ub[1] <= 0xBF && isUTF0(ub[2]) && isUTF0(ub[3])
case 0xF1, 0xF2, 0xF3:
b = isUTF0(ub[1]) && isUTF0(ub[2]) && isUTF0(ub[3])
case 0xF4:
b = ucIs(utf4cRange, rune(ub[1])) && isUTF0(ub[2]) && isUTF0(ub[3])
}
return
}
func isUTFMB(r rune) (b bool, err error) {
ub := make([]byte, 4)
n := utf8.EncodeRune(ub, r)
switch n {
case 2:
b = isUTF2(ub)
case 3:
b = isUTF3(ub)
case 4:
b = isUTF4(ub)
}
if !b {
err = errorTxt("invalid leading byte for " + itoa(n) + "-byte sequence, or bad length")
}
return
}
func init() {
uTF8SubsetRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0001, 0x0027, 1},
{0x002B, 0x005B, 1},
{0x005D, 0x007F, 1},
}}
iA5Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x0000, 0x00FF, 1},
}}
ttxRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0000, 0x0023, 1},
{0x0025, 0x005B, 1},
{0x005D, 0x00FF, 1},
}}
octRange = iA5Range
digits = &unicode.RangeTable{R16: []unicode.Range16{
{0x0030, 0x0039, 1},
}}
lAlphas = &unicode.RangeTable{R16: []unicode.Range16{
{0x0041, 0x005A, 1},
}}
uAlphas = &unicode.RangeTable{R16: []unicode.Range16{
{0x0061, 0x007A, 1},
}}
uCSRange = &unicode.RangeTable{R32: []unicode.Range32{
{0x0000, 0xFFFF, 1},
}}
/*
t61NonContiguous contains all non-contiguous characters (i.e.: those NOT incorporated
through the t61Ranges *unicode.RangeTable instance) that are allowed per T.61. These
characters are as follows:
- '\u009B' (�, npc)
- '\u005C' (\)
- '\u005D' (])
- '\u005F' (_)
- '\u003F' (?)
- '\u007C' ([)
- '\u007F' (])
- '\u001d' (SS3, npc)
- '\u0111' (đ)
- '\u0138' (ĸ)
- '\u0332' ( ̲)
- '\u2126' (Ω)
- '\u013F' (Ŀ)
- '\u014B' (ŋ)
*/
t61NonContiguous = []rune{
'\u009B',
'\u005C',
'\u005D',
'\u005F',
'\u003F',
'\u007C',
'\u007F',
'\u001d',
'\u0111',
'\u0138',
'\u0332',
'\u2126',
'\u013F',
'\u014B',
}
printableStringRunes = []rune{
'\'',
'(',
')',
'+',
',',
'-',
'.',
'=',
'/',
':',
'?',
' ',
}
telephoneNumberRunes = []rune{
'\'',
'\\',
'"',
'(',
')',
'+',
',',
'-',
'.',
'/',
':',
'?',
' ',
}
/*
t61Ranges defines a *unicode.RangeTable instance containing specific
16-bit and 32-bit character ranges that (partially) describe allowed
Unicode codepoints within a given T.61 value.
See also the t61NonContiguous global variable.
*/
t61Ranges = &unicode.RangeTable{
// 16-bit Unicode codepoints.
R16: []unicode.Range16{
{0x0009, 0x000f, 1}, // TAB through SHIFT-IN
{0x0020, 0x0039, 1}, // ' ' .. '9'
{0x0041, 0x005B, 1}, // 'a' .. '['
{0x0061, 0x007A, 1}, // 'A' .. 'Z'
{0x00A0, 0x00FF, 1},
{0x008B, 0x008C, 1},
},
// 32-bit Unicode codepoints.
R32: []unicode.Range32{
{0x0126, 0x0127, 1},
{0x0131, 0x0132, 1},
{0x0140, 0x0142, 1},
{0x0149, 0x014A, 1},
{0x0152, 0x0153, 1},
{0x0166, 0x0167, 1},
{0x0300, 0x0304, 1},
{0x0306, 0x0308, 1},
{0x030A, 0x030C, 1},
{0x0327, 0x0328, 1},
},
}
lineCharRange = &unicode.RangeTable{R16: []unicode.Range16{
// ASCII 00 through 7F with two exclusions ...
{0x0000, 0x0023, 1}, // skip DOLLAR
{0x0025, 0x005B, 1}, // skip ESC
{0x005D, 0x007F, 1},
}}
substrRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0000, 0x0029, 1},
{0x002B, 0x005B, 1},
{0x005D, 0x007F, 1},
}}
utf0Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x00BF, 1},
}}
utf1Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x0000, 0x007F, 1},
}}
asciiRange = utf1Range
utf2Range = &unicode.RangeTable{R16: []unicode.Range16{
{0x00C2, 0x00DF, 1},
}}
utf2aSafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00C0, 0x00DF, 1},
}}
utf2bSafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x00BF, 1},
}}
utf3aRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00A0, 0x00BF, 1},
}}
utf3SafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00E0, 0x00EF, 1},
}}
utf3bRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00E1, 0x00EC, 1},
}}
utf3cRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x009F, 1},
}}
utf3dRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00EE, 0x00EF, 1},
}}
utf4aRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0090, 0x00BF, 1},
}}
utf4SafeRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00F0, 0x00F7, 1},
}}
utf4bRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x00F1, 0x00F3, 1},
}}
utf4cRange = &unicode.RangeTable{R16: []unicode.Range16{
{0x0080, 0x008F, 1},
}}
}