|
1
|
#!/usr/bin/tclsh |
|
2
|
# |
|
3
|
# Generate src/casefold.h from the Unicode Character Database. |
|
4
|
# If you do not have a copy handy: |
|
5
|
# |
|
6
|
# UCVER=18.0.0 |
|
7
|
# BASEURL=https://www.unicode.org/Public/$UCVER/ucd |
|
8
|
# wget $BASEURL/UnicodeData.txt |
|
9
|
# wget $BASEURL/CaseFolding.txt |
|
10
|
# |
|
11
|
# Then: |
|
12
|
# |
|
13
|
# tclsh tools/casefold.tcl |
|
14
|
# |
|
15
|
# Pass paths to UnicodeData.txt and CaseFolding.txt (in that order) to |
|
16
|
# use files elsewhere on the system. (Take a look on yours; you will |
|
17
|
# likely find some.) Re-run whenever a new Unicode standard is released, |
|
18
|
# then commit the regenerated header. The script can be run from other |
|
19
|
# directories; it always writes ../src/casefold.h relative to tools/ |
|
20
|
# |
|
21
|
# Fullwidth Latin letters (U+FF21..U+FF3A and U+FF41..U+FF5A) fold |
|
22
|
# inside the fullwidth block. CaseFolding.txt maps them to ASCII, but |
|
23
|
# Fossil pathname comparison keeps them distinct from ASCII. |
|
24
|
# |
|
25
|
set scriptDir [file dirname [file normalize [info script]]] |
|
26
|
set outFile [file join $scriptDir ../src/casefold.h] |
|
27
|
|
|
28
|
proc usage {} { |
|
29
|
puts stderr "Usage: $::argv0 ?UnicodeData.txt? ?CaseFolding.txt?" |
|
30
|
exit 1 |
|
31
|
} |
|
32
|
set unicodeData UnicodeData.txt |
|
33
|
set caseFolding CaseFolding.txt |
|
34
|
switch -- [llength $argv] { |
|
35
|
0 {} |
|
36
|
2 { lassign $argv unicodeData caseFolding } |
|
37
|
default { usage } |
|
38
|
} |
|
39
|
if {![file exists $unicodeData] || ![file exists $caseFolding]} { |
|
40
|
puts stderr "error: cannot read $unicodeData and/or $caseFolding" |
|
41
|
usage |
|
42
|
} |
|
43
|
|
|
44
|
#-------------------------------------------------------------------------- |
|
45
|
# Parse UnicodeData.txt into: |
|
46
|
# decomp(cp) - list of code points (canonical decomposition only) |
|
47
|
# ccc(cp) - combining class when non-zero |
|
48
|
# |
|
49
|
set fd [open $unicodeData] |
|
50
|
while {[gets $fd line] >= 0} { |
|
51
|
if {$line eq ""} continue |
|
52
|
set fields [split $line ";"] |
|
53
|
if {[llength $fields] < 6} { |
|
54
|
error "parse error in UnicodeData.txt: $line" |
|
55
|
} |
|
56
|
set cp [expr {"0x[lindex $fields 0]"}] |
|
57
|
set class [lindex $fields 3] |
|
58
|
set mapping [string trim [lindex $fields 5]] |
|
59
|
if {$class ne "0" && $class ne ""} { |
|
60
|
set ccc($cp) [expr {$class}] |
|
61
|
} |
|
62
|
if {$mapping eq ""} continue |
|
63
|
# Compatibility decompositions begin with <tag> |
|
64
|
if {[string index $mapping 0] eq "<"} continue |
|
65
|
set seq [list] |
|
66
|
foreach hex $mapping { |
|
67
|
lappend seq [expr {"0x$hex"}] |
|
68
|
} |
|
69
|
set decomp($cp) $seq |
|
70
|
} |
|
71
|
close $fd |
|
72
|
|
|
73
|
# Recursive full canonical decomposition. Hangul syllables are handled |
|
74
|
# at runtime and are omitted from the table. |
|
75
|
proc nfd_one {cp} { |
|
76
|
global decomp |
|
77
|
if {$cp >= 0xAC00 && $cp <= 0xD7A3} { |
|
78
|
return [list $cp] |
|
79
|
} |
|
80
|
if {![info exists decomp($cp)]} { |
|
81
|
return [list $cp] |
|
82
|
} |
|
83
|
set out [list] |
|
84
|
foreach d $decomp($cp) { |
|
85
|
foreach x [nfd_one $d] { |
|
86
|
lappend out $x |
|
87
|
} |
|
88
|
} |
|
89
|
return $out |
|
90
|
} |
|
91
|
|
|
92
|
#-------------------------------------------------------------------------- |
|
93
|
# Parse CaseFolding.txt. Keep status C and F. Then NFD the result. |
|
94
|
# Fullwidth Latin stays in the fullwidth block. |
|
95
|
# |
|
96
|
set versionLine "" |
|
97
|
set fd [open $caseFolding] |
|
98
|
while {[gets $fd line] >= 0} { |
|
99
|
if {$versionLine eq "" && [string match "# CaseFolding-*" $line]} { |
|
100
|
set versionLine $line |
|
101
|
} |
|
102
|
if {[string index $line 0] eq "#" || $line eq ""} continue |
|
103
|
set parts [split $line ";"] |
|
104
|
if {[llength $parts] < 3} continue |
|
105
|
set src [expr {"0x[string trim [lindex $parts 0]]"}] |
|
106
|
set status [string trim [lindex $parts 1]] |
|
107
|
if {$status ne "C" && $status ne "F"} continue |
|
108
|
set mapping [list] |
|
109
|
foreach hex [string trim [lindex $parts 2]] { |
|
110
|
lappend mapping [expr {"0x$hex"}] |
|
111
|
} |
|
112
|
# Fullwidth Latin: keep folds inside U+FF21..U+FF5A. |
|
113
|
if {$src >= 0xFF21 && $src <= 0xFF3A} { |
|
114
|
set mapping [list [expr {$src + 0x20}]] |
|
115
|
} elseif {$src >= 0xFF41 && $src <= 0xFF5A} { |
|
116
|
set mapping [list $src] |
|
117
|
} |
|
118
|
set folded [list] |
|
119
|
foreach m $mapping { |
|
120
|
foreach x [nfd_one $m] { |
|
121
|
lappend folded $x |
|
122
|
} |
|
123
|
} |
|
124
|
# Identity folds after NFD are useless; skip them. |
|
125
|
if {[llength $folded] == 1 && [lindex $folded 0] == $src} continue |
|
126
|
set fold($src) $folded |
|
127
|
} |
|
128
|
close $fd |
|
129
|
|
|
130
|
# Also emit folds for characters whose NFD form differs from themselves |
|
131
|
# when the folded NFD of the character differs from NFD alone? No: |
|
132
|
# runtime does NFD first, then looks up each code point in the fold map. |
|
133
|
# So the fold map only needs entries for the code points that appear |
|
134
|
# after the first NFD pass (and for characters that are already |
|
135
|
# decomposed). Status C/F entries that expand after NFD are enough. |
|
136
|
# Characters like U+00C4 fold via C to U+00E4; after first NFD they are |
|
137
|
# already A+combining-diaeresis, so the fold of A is identity. The |
|
138
|
# table must still hold U+00C4 if someone folds before NFD of a |
|
139
|
# precomposed form that survived the first NFD... but first NFD always |
|
140
|
# decomposes U+00C4. Still keep every C/F entry: runtime looks up each |
|
141
|
# post-NFD code point, and some C entries are for already-atomic |
|
142
|
# letters (A -> a). |
|
143
|
|
|
144
|
#-------------------------------------------------------------------------- |
|
145
|
# Pack a map of code-point -> sequence into seq[] and map[] arrays. |
|
146
|
# |
|
147
|
proc pack_map {name mapVar} { |
|
148
|
upvar $mapVar map |
|
149
|
set cps [lsort -integer [array names map]] |
|
150
|
set seq [list] |
|
151
|
set entries [list] |
|
152
|
set off 0 |
|
153
|
foreach cp $cps { |
|
154
|
set s $map($cp) |
|
155
|
set n [llength $s] |
|
156
|
if {$n > 255} { |
|
157
|
error "$name: sequence for U+[format %04X $cp] has length $n (>255)" |
|
158
|
} |
|
159
|
if {$off > 65535} { |
|
160
|
error "$name: packed offset $off exceeds unsigned short" |
|
161
|
} |
|
162
|
lappend entries [list $cp $off $n] |
|
163
|
foreach x $s { lappend seq $x } |
|
164
|
incr off $n |
|
165
|
} |
|
166
|
if {$off > 65535} { |
|
167
|
error "$name: packed offset $off exceeds unsigned short" |
|
168
|
} |
|
169
|
return [list $seq $entries] |
|
170
|
} |
|
171
|
|
|
172
|
# Build NFD map for every character that has a non-trivial full NFD. |
|
173
|
array set nfdMap {} |
|
174
|
foreach cp [array names decomp] { |
|
175
|
if {$cp >= 0xAC00 && $cp <= 0xD7A3} continue |
|
176
|
set seq [nfd_one $cp] |
|
177
|
if {[llength $seq] == 1 && [lindex $seq 0] == $cp} continue |
|
178
|
set nfdMap($cp) $seq |
|
179
|
} |
|
180
|
|
|
181
|
lassign [pack_map nfd nfdMap] nfdSeq nfdEntries |
|
182
|
lassign [pack_map fold fold] foldSeq foldEntries |
|
183
|
|
|
184
|
set cccEntries [list] |
|
185
|
foreach cp [lsort -integer [array names ccc]] { |
|
186
|
lappend cccEntries [list $cp $ccc($cp)] |
|
187
|
} |
|
188
|
|
|
189
|
#-------------------------------------------------------------------------- |
|
190
|
# Emit C header. |
|
191
|
# |
|
192
|
proc emit_hex_list {fd seq perLine} { |
|
193
|
set i 0 |
|
194
|
set n [llength $seq] |
|
195
|
foreach x $seq { |
|
196
|
if {$i % $perLine == 0} { |
|
197
|
if {$i} { puts $fd "" } |
|
198
|
puts -nonewline $fd " " |
|
199
|
} |
|
200
|
puts -nonewline $fd [format "0x%04X" $x] |
|
201
|
incr i |
|
202
|
if {$i < $n} { puts -nonewline $fd "," } |
|
203
|
} |
|
204
|
if {$n} { puts $fd "" } |
|
205
|
} |
|
206
|
|
|
207
|
set out [open $outFile w] |
|
208
|
puts $out "/* DO NOT EDIT: generated by tools/casefold.tcl" |
|
209
|
puts $out "**" |
|
210
|
if {$versionLine ne ""} { |
|
211
|
puts $out "** $versionLine" |
|
212
|
} |
|
213
|
puts $out "** Canonical decomposition and case-fold tables for filename" |
|
214
|
puts $out "** comparison. Hangul syllables are decomposed in code, not" |
|
215
|
puts $out "** in these tables. Fullwidth Latin letters fold inside the" |
|
216
|
puts $out "** fullwidth block." |
|
217
|
puts $out "**" |
|
218
|
puts $out "** Re-generate with:" |
|
219
|
puts $out "** tclsh tools/casefold.tcl" |
|
220
|
puts $out "*/" |
|
221
|
puts $out "#ifndef FOSSIL_CASEFOLD_H" |
|
222
|
puts $out "#define FOSSIL_CASEFOLD_H" |
|
223
|
|
|
224
|
puts $out "" |
|
225
|
puts $out "static const unsigned int aFnNfdSeq\[\] = \{" |
|
226
|
emit_hex_list $out $nfdSeq 8 |
|
227
|
puts $out "\};" |
|
228
|
puts $out "" |
|
229
|
puts $out "static const FnMap aFnNfdMap\[\] = \{" |
|
230
|
set i 0 |
|
231
|
set n [llength $nfdEntries] |
|
232
|
foreach e $nfdEntries { |
|
233
|
lassign $e cp off len |
|
234
|
puts -nonewline $out [format " {0x%04X, %d, %d}" $cp $off $len] |
|
235
|
incr i |
|
236
|
if {$i < $n} { puts $out "," } else { puts $out "" } |
|
237
|
} |
|
238
|
puts $out "\};" |
|
239
|
|
|
240
|
puts $out "" |
|
241
|
puts $out "static const unsigned int aFnFoldSeq\[\] = \{" |
|
242
|
emit_hex_list $out $foldSeq 8 |
|
243
|
puts $out "\};" |
|
244
|
puts $out "" |
|
245
|
puts $out "static const FnMap aFnFoldMap\[\] = \{" |
|
246
|
set i 0 |
|
247
|
set n [llength $foldEntries] |
|
248
|
foreach e $foldEntries { |
|
249
|
lassign $e cp off len |
|
250
|
puts -nonewline $out [format " {0x%04X, %d, %d}" $cp $off $len] |
|
251
|
incr i |
|
252
|
if {$i < $n} { puts $out "," } else { puts $out "" } |
|
253
|
} |
|
254
|
puts $out "\};" |
|
255
|
|
|
256
|
puts $out "" |
|
257
|
puts $out "static const FnCcc aFnCcc\[\] = \{" |
|
258
|
set i 0 |
|
259
|
set n [llength $cccEntries] |
|
260
|
foreach e $cccEntries { |
|
261
|
lassign $e cp class |
|
262
|
puts -nonewline $out [format " {0x%04X, %d}" $cp $class] |
|
263
|
incr i |
|
264
|
if {$i < $n} { puts $out "," } else { puts $out "" } |
|
265
|
} |
|
266
|
puts $out "\};" |
|
267
|
|
|
268
|
puts $out "" |
|
269
|
puts $out "#endif /* FOSSIL_CASEFOLD_H */" |
|
270
|
close $out |
|
271
|
|
|
272
|
puts "wrote $outFile ([llength $nfdEntries] nfd, [llength $foldEntries] fold, [llength $cccEntries] ccc)" |
|
273
|
|