Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
199 lines (161 loc) · 4.38 KB
/
Copy pathexample_test.go
File metadata and controls
199 lines (161 loc) · 4.38 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
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
package uts15_test
import (
"fmt"
"github.com/SCKelemen/unicode/v6/uts15"
)
func ExampleNFC() {
// Normalize to NFC (Canonical Composition)
// This is the recommended form for most uses
decomposed := "e\u0301" // e + combining acute accent
normalized := uts15.NFC(decomposed)
fmt.Printf("%s\n", normalized) // é (composed)
// Already composed text stays the same
composed := "\u00E9"
fmt.Printf("%s\n", uts15.NFC(composed))
// Output:
// é
// é
}
func ExampleNFD() {
// Normalize to NFD (Canonical Decomposition)
composed := "\u00E9" // é (composed)
normalized := uts15.NFD(composed)
runes := []rune(normalized)
for i, r := range runes {
if i > 0 {
fmt.Print(" ")
}
fmt.Printf("%X", r)
}
fmt.Println()
// Output:
// 65 301
}
func ExampleNFKC() {
// Normalize to NFKC (Compatibility Composition)
// Decomposes ligatures and other compatibility characters
ligature := "\uFB01" // fi ligature
normalized := uts15.NFKC(ligature)
fmt.Printf("%s\n", normalized)
// Full-width characters
fullwidth := "\uFF21\uFF10" // Full-width "A0"
fmt.Printf("%s\n", uts15.NFKC(fullwidth))
// Output:
// fi
// A0
}
func ExampleNFKD() {
// Normalize to NFKD (Compatibility Decomposition)
ligature := "\uFB01" // fi ligature
normalized := uts15.NFKD(ligature)
fmt.Printf("%s\n", normalized)
// Circled numbers
circled := "\u2460" // ①
fmt.Printf("%s\n", uts15.NFKD(circled))
// Output:
// fi
// 1
}
func ExampleNFC_stringComparison() {
// Comparing strings that may be in different normalization forms
s1 := "café" // Composed form (might be)
s2 := "cafe\u0301" // Decomposed form (e + acute)
// Direct comparison might fail
fmt.Printf("Direct comparison: %v\n", s1 == s2)
// Normalize both to NFC before comparing
fmt.Printf("After NFC: %v\n", uts15.NFC(s1) == uts15.NFC(s2))
// Output:
// Direct comparison: false
// After NFC: true
}
func ExampleIsNFC() {
// Check if a string is already in NFC form
composed := "\u00E9" // é (composed)
decomposed := "e\u0301" // e + combining acute
fmt.Printf("Composed is NFC: %v\n", uts15.IsNFC(composed))
fmt.Printf("Decomposed is NFC: %v\n", uts15.IsNFC(decomposed))
// Output:
// Composed is NFC: true
// Decomposed is NFC: false
}
func ExampleNFC_hangul() {
// Hangul composition
// Hangul jamos (L+V) compose into syllables
jamos := "\u1100\u1161" // ᄀ + ᅡ
syllable := uts15.NFC(jamos)
fmt.Printf("%s\n", syllable) // 가
// With trailing consonant (L+V+T)
jamosWithT := "\u1100\u1161\u11A8" // ᄀ + ᅡ + ᆨ
syllableWithT := uts15.NFC(jamosWithT)
fmt.Printf("%s\n", syllableWithT) // 각
// Output:
// 가
// 각
}
func ExampleNFD_hangul() {
// Hangul decomposition
syllable := "\uAC00" // 가
jamos := uts15.NFD(syllable)
printRunes([]rune(jamos))
// With trailing consonant
syllableWithT := "\uAC01" // 각
jamosWithT := uts15.NFD(syllableWithT)
printRunes([]rune(jamosWithT))
// Output:
// 1100 1161
// 1100 1161 11A8
}
func printRunes(runes []rune) {
for i, r := range runes {
if i > 0 {
fmt.Print(" ")
}
fmt.Printf("%X", r)
}
fmt.Println()
}
func ExampleNFKC_security() {
// Security: Normalize identifiers to prevent homograph attacks
// and ensure consistent comparison
identifiers := []string{
"file", // Normal ASCII
"\uFB01le", // Using fi ligature
"\uFF46ile", // Using full-width f
}
fmt.Println("Original identifiers:")
for _, id := range identifiers {
fmt.Printf(" %q\n", id)
}
fmt.Println("\nAfter NFKC normalization:")
for _, id := range identifiers {
normalized := uts15.NFKC(id)
fmt.Printf(" %q\n", normalized)
}
// All normalize to the same form
fmt.Printf("\nAll equal after NFKC: %v\n",
uts15.NFKC(identifiers[0]) == uts15.NFKC(identifiers[1]) &&
uts15.NFKC(identifiers[1]) == uts15.NFKC(identifiers[2]))
// Output:
// Original identifiers:
// "file"
// "file"
// "file"
//
// After NFKC normalization:
// "file"
// "file"
// "file"
//
// All equal after NFKC: true
}
func ExampleNFC_database() {
// Example: Normalizing text before storing in a database
// to ensure consistent searching and indexing
composed := "Jos\u00E9" // Composed form
decomposed := "Jose\u0301" // Decomposed form (e + acute)
normalized1 := uts15.NFC(composed)
normalized2 := uts15.NFC(decomposed)
fmt.Printf("Same after normalization: %v\n", normalized1 == normalized2)
// Output:
// Same after normalization: true
}