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
|
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
#
# Copyright(C) 2010 Brazil
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
$KCODE = 'u'
CUSTOM_RULE_PATH = 'nfkc-custom-rules.txt'
def gen_bc(file, hash, level)
bl = ' ' * (level * 2)
h2 = {}
hash.each{|key,val|
head = key[0]
rest = key[1..-1]
if h2[head]
h2[head][rest] = val
else
h2[head] = {rest => val}
end
}
if h2.size < 3
h2.keys.sort.each{|k|
if (0x80 < k)
file.printf("#{bl}if (str[#{level}] < 0x%02X) { return #{$lv}; }\n", k)
end
h = h2[k]
if h.keys.join =~ /^\x80*$/
$lv, = h.values
else
file.printf("#{bl}if (str[#{level}] == 0x%02X) {\n", k)
gen_bc(file, h, level + 1)
file.puts bl + '}'
end
}
file.puts bl + "return #{$lv};"
else
file.puts bl + "switch (str[#{level}]) {"
lk = 0x80
br = true
h2.keys.sort.each{|k|
if (lk < k)
for j in lk..k-1
file.printf("#{bl}case 0x%02X :\n", j)
end
br = false
end
unless br
file.puts bl + " return #{$lv};"
file.puts bl + ' break;'
end
h = h2[k]
file.printf("#{bl}case 0x%02X :\n", k)
if h.keys.join =~ /^\x80*$/
$lv, = h.values
br = false
else
gen_bc(file, h, level + 1)
file.puts bl + ' break;'
br = true
end
lk = k + 1
}
file.puts bl + 'default :'
file.puts bl + " return #{$lv};"
file.puts bl + ' break;'
file.puts bl + '}'
end
end
def generate_blockcode_char_type(file, option)
bc = {}
open("|./icudump --#{option}").each{|l|
src,_,code = l.chomp.split("\t")
str = src.split(':').collect{|c| format("%c", c.hex)}.join
bc[str] = code
}
$lv = 0
gen_bc(file, bc, 0)
end
def ccpush(hash, src, dst)
head = src.shift
hash[head] = {} unless hash[head]
if head
ccpush(hash[head], src, dst)
else
hash[head] = dst
end
end
def subst(hash, str)
cand = nil
src = str.split(//)
for i in 0..src.size-1
h = hash
for j in i..src.size-1
head = src[j]
h = h[head]
break unless h
if h[nil]
cand = src[0,i].to_s + h[nil] + src[j + 1..-1].to_s
end
end
return cand if cand
end
return str
end
def map_entry(map1, cc, src, dst)
dst.downcase! unless $case_sensitive
loop {
dst2 = subst(cc, dst)
break if dst2 == dst
dst = dst2
}
unless $keep_space
dst = $1 if dst =~ /^ +([^ ].*)$/
end
map1[src] = dst if src != dst
end
def create_map1()
cc = {}
open('|./icudump --cc').each{|l|
_,src,dst = l.chomp.split("\t")
if cc[src]
STDERR.puts "caution: ambiguous mapping #{src}|#{cc[src]}|#{dst}" if cc[src] != dst
end
ccpush(cc, src.split(//), dst)
}
map1 = {}
open('|./icudump --nfkd').each{|l|
n,src,dst = l.chomp.split("\t")
map_entry(map1, cc, src, dst)
}
if File.readable?(CUSTOM_RULE_PATH)
open(CUSTOM_RULE_PATH).each{|l|
src,dst = l.chomp.split("\t")
map_entry(map1, cc, src, dst)
}
end
unless $case_sensitive
for c in 'A'..'Z'
map1[c] = c.downcase
end
end
return map1
end
def create_map2(map1)
cc = {}
open('|./icudump --cc').each{|l|
_,src,dst = l.chomp.split("\t")
src = src.split(//).collect{|c| map1[c] || c}.join
dst = map1[dst] || dst
if cc[src] && cc[src] != dst
STDERR.puts("caution: inconsitent mapping '#{src}' => '#{cc[src]}'|'#{dst}'")
end
cc[src] = dst if src != dst
}
loop {
noccur = 0
cc2 = {}
cc.each {|src,dst|
src2 = src
chars = src.split(//)
l = chars.size - 1
for i in 0..l
for j in i..l
next if i == 0 && j == l
str = chars[i..j].join
if map1[str]
STDERR.printf("caution: recursive mapping '%s'=>'%s'\n", str, map1[str])
end
if cc[str]
src2 = (i > 0 ? chars[0..i-1].join : '') + cc[str] + (j < l ? chars[j+1..l].join : '')
noccur += 1
end
end
end
cc2[src2] = dst if src2 != dst
}
cc = cc2
STDERR.puts("substituted #{noccur} patterns.")
break if noccur == 0
STDERR.puts('try again..')
}
return cc
end
def generate_map1(file, hash, level)
bl = ' ' * ((level + 0) * 2)
if hash['']
dst = ''
hash[''].each_byte{|b| dst << format('\x%02X', b)}
file.puts "#{bl}return \"#{dst}\";"
hash.delete('')
end
return if hash.empty?
h2 = {}
hash.each{|key,val|
head = key[0]
rest = key[1..-1]
if h2[head]
h2[head][rest] = val
else
h2[head] = {rest => val}
end
}
if h2.size == 1
h2.each{|key,val|
file.printf("#{bl}if (str[#{level}] == 0x%02X) {\n", key)
generate_map1(file, val, level + 1)
file.puts bl + '}'
}
else
file.puts "#{bl}switch (str[#{level}]) {"
h2.keys.sort.each{|k|
file.printf("#{bl}case 0x%02X :\n", k)
generate_map1(file, h2[k], level + 1)
file.puts("#{bl} break;")
}
file.puts bl + '}'
end
end
def gen_map2_sub2(file, hash, level, indent)
bl = ' ' * ((level + indent + 0) * 2)
if hash['']
file.print "#{bl}return \""
hash[''].each_byte{|b| file.printf('\x%02X', b)}
file.puts "\";"
hash.delete('')
end
return if hash.empty?
h2 = {}
hash.each{|key,val|
head = key[0]
rest = key[1..-1]
if h2[head]
h2[head][rest] = val
else
h2[head] = {rest => val}
end
}
if h2.size == 1
h2.each{|key,val|
file.printf("#{bl}if (prefix[#{level}] == 0x%02X) {\n", key)
gen_map2_sub2(file, val, level + 1, indent)
file.puts bl + '}'
}
else
file.puts "#{bl}switch (prefix[#{level}]) {"
h2.keys.sort.each{|k|
file.printf("#{bl}case 0x%02X :\n", k)
gen_map2_sub2(file, h2[k], level + 1, indent)
file.puts("#{bl} break;")
}
file.puts bl + '}'
end
end
def gen_map2_sub(file, hash, level)
bl = ' ' * ((level + 0) * 2)
if hash['']
gen_map2_sub2(file, hash[''], 0, level)
hash.delete('')
end
return if hash.empty?
h2 = {}
hash.each{|key,val|
head = key[0]
rest = key[1..-1]
if h2[head]
h2[head][rest] = val
else
h2[head] = {rest => val}
end
}
if h2.size == 1
h2.each{|key,val|
file.printf("#{bl}if (suffix[#{level}] == 0x%02X) {\n", key)
gen_map2_sub(file, val, level + 1)
file.puts bl + '}'
}
else
file.puts "#{bl}switch (suffix[#{level}]) {"
h2.keys.sort.each{|k|
file.printf("#{bl}case 0x%02X :\n", k)
gen_map2_sub(file, h2[k], level + 1)
file.puts("#{bl} break;")
}
file.puts bl + '}'
end
end
def generate_map2(file, map2)
suffix = {}
map2.each{|src,dst|
chars = src.split(//)
if chars.size != 2
STDERR.puts "caution: more than two chars in pattern #{chars.join('|')}"
end
s = chars.pop
if suffix[s]
suffix[s][chars.join] = dst
else
suffix[s] = {chars.join=>dst}
end
}
gen_map2_sub(file, suffix, 0)
end
template = <<END
/* -*- c-basic-offset: 2 -*- */
/* Copyright(C) 2010 Brazil
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
don't edit this file by hand. it generated automatically by nfkc.rb
*/
#include "nfkc.h"
#ifdef GRN_WITH_NFKC
unsigned char
grn_nfkc_char_type(const unsigned char *str)
{
% return -1;
}
const char *
grn_nfkc_map1(const unsigned char *str)
{
% return 0;
}
const char *
grn_nfkc_map2(const unsigned char *prefix, const unsigned char *suffix)
{
% return 0;
}
#endif /* GRN_WITH_NFKC */
END
######## main #######
ARGV.each{|arg|
case arg
when /-*c/i
$case_sensitive = true
when /-*s/i
$keep_space = true
end
}
STDERR.puts('compiling icudump')
system('cc -Wall -O3 -o icudump icudump.c -licui18n')
STDERR.puts('creating map1..')
map1 = create_map1()
STDERR.puts('creating map2..')
map2 = create_map2(map1)
outf = open('nfkc.c', 'w')
tmps = template.split(/%/)
#STDERR.puts('generating block code..')
#outf.print(tmps.shift)
#generate_blockcode_char_type(outf, 'bc')
STDERR.puts('generating char type code..')
outf.print(tmps.shift)
generate_blockcode_char_type(outf, 'gc')
STDERR.puts('generating map1 code..')
outf.print(tmps.shift)
generate_map1(outf, map1, 0)
STDERR.puts('generating map2 code..')
outf.print(tmps.shift)
generate_map2(outf, map2)
outf.print(tmps.shift)
outf.close
STDERR.puts('done.')
|