3 # Parameter $zName must be a path to the file UnicodeData.txt. This command
4 # reads the file and returns a list of mappings required to remove all
5 # diacritical marks from a unicode string. Each mapping is itself a list
6 # consisting of two elements - the unicode codepoint and the single ASCII
7 # character that it should be replaced with, or an empty string if the
8 # codepoint should simply be removed from the input. Examples:
10 # { 224 a } (replace codepoint 224 to "a")
11 # { 769 "" } (remove codepoint 769 from input)
13 # Mappings are only returned for non-upper case codepoints. It is assumed
14 # that the input has already been folded to lower case.
16 proc rd_load_unicodedata_text
{zName
} {
17 global tl_lookup_table
24 canonical_combining_classes
25 bidirectional_category
26 character_decomposition_mapping
32 iso10646_comment_field
39 while { ![eof $fd] } {
41 if {$line == ""} continue
43 set fields
[split $line ";"]
44 if {[llength $fields] != [llength $lField]} { error "parse error: $line" }
45 foreach $lField $fields {}
46 if { [llength $character_decomposition_mapping]!=2
47 ||
[string is xdigit
[lindex $character_decomposition_mapping 0]]==0
52 set iCode
[expr "0x$code"]
53 set iAscii
[expr "0x[lindex $character_decomposition_mapping 0]"]
54 set iDia
[expr "0x[lindex $character_decomposition_mapping 1]"]
56 if {[info exists tl_lookup_table
($iCode)]} continue
58 if { ($iAscii >= 97 && $iAscii <= 122)
59 ||
($iAscii >= 65 && $iAscii <= 90)
61 lappend lRet
[list $iCode [string tolower
[format %c
$iAscii]]]
66 foreach d
[array names dia
] {
67 lappend lRet
[list $d ""]
69 set lRet
[lsort -integer -index 0 $lRet]
77 global tl_lookup_table
82 set iFirst
[lindex $map 0 0]
83 set cPrev
[lindex $map 0 1]
85 foreach m
[lrange $map 1 end
] {
89 for {set j
[expr $iFirst+$nRange]} {$j<$i} {incr j
} {
90 if {[info exists tl_lookup_table
($j)]==0} break
94 set nNew
[expr {(1 + $i - $iFirst)}]
102 lappend lRange
[list $iFirst $nRange]
109 lappend lRange
[list $iFirst $nRange]
113 puts "** If the argument is a codepoint corresponding to a lowercase letter"
114 puts "** in the ASCII range with a diacritic added, return the codepoint"
115 puts "** of the ASCII letter only. For example, if passed 235 - \"LATIN"
116 puts "** SMALL LETTER E WITH DIAERESIS\" - return 65 (\"LATIN SMALL LETTER"
117 puts "** E\"). The resuls of passing a codepoint that corresponds to an"
118 puts "** uppercase letter are undefined."
120 puts "static int remove_diacritic(int c)\{"
121 puts " unsigned short aDia\[\] = \{"
122 puts -nonewline " 0, "
125 foreach {iCode nRange
} $r {}
126 if {($i % 8)==0} {puts "" ; puts -nonewline " " }
129 puts -nonewline [format "%5d" [expr ($iCode<<3) + $nRange-1]]
134 puts " char aChar\[\] = \{"
135 puts -nonewline " '\\0', "
139 if {$c == ""} { set str
"'\\0', " }
141 if {($i % 12)==0} {puts "" ; puts -nonewline " " }
143 puts -nonewline "$str"
148 unsigned int key
= (((unsigned int
)c
)<<3) |
0x00000007;
150 int iHi
= sizeof
(aDia
)/sizeof
(aDia
[0]) - 1;
153 int iTest
= (iHi
+ iLo
) / 2;
154 if( key
>= aDia
[iTest
] ){
161 assert
( key
>=aDia
[iRes
] );
162 return ((c
> (aDia
[iRes
]>>3) + (aDia
[iRes
]&0x07)) ? c
: (int
)aChar
[iRes
]);}
166 proc print_isdiacritic
{zFunc map
} {
170 foreach {code char
} $m {}
171 if {$code && $char == ""} { lappend lCode
$code }
173 set lCode
[lsort -integer $lCode]
174 set iFirst
[lindex $lCode 0]
175 set iLast
[lindex $lCode end
]
181 set i
[expr $c - $iFirst]
183 set i1
[expr {$i1 |
(1<<$i)}]
185 set i2
[expr {$i2 |
(1<<($i-32))}]
190 puts "** Return true if the argument interpreted as a unicode codepoint"
191 puts "** is a diacritical modifier character."
193 puts "int ${zFunc}\(int c)\{"
194 puts " unsigned int mask0 = [format "0x
%08X
" $i1];"
195 puts " unsigned int mask1 = [format "0x
%08X
" $i2];"
197 puts " if( c<$iFirst || c>$iLast ) return 0;"
198 puts " return (c < $iFirst+32) ?"
199 puts " (mask0 & (1 << (c-$iFirst))) :"
200 puts " (mask1 & (1 << (c-$iFirst-32)));"
205 #-------------------------------------------------------------------------
207 # Parameter $zName must be a path to the file UnicodeData.txt. This command
208 # reads the file and returns a list of codepoints (integers). The list
209 # contains all codepoints in the UnicodeData.txt assigned to any "General
210 # Category" that is not a "Letter" or "Number".
212 proc an_load_unicodedata_text
{zName
} {
218 canonical_combining_classes
219 bidirectional_category
220 character_decomposition_mapping
226 iso10646_comment_field
233 while { ![eof $fd] } {
235 if {$line == ""} continue
237 set fields
[split $line ";"]
238 if {[llength $fields] != [llength $lField]} { error "parse error: $line" }
239 foreach $lField $fields {}
241 set iCode
[expr "0x$code"]
242 set bAlnum
[expr {[lsearch {L N
} [string range
$general_category 0 0]]>=0}]
244 if { !$bAlnum } { lappend lRet
$iCode }
251 proc an_load_separator_ranges
{} {
252 global unicodedata.txt
253 set lSep
[an_load_unicodedata_text
${unicodedata.txt
}]
254 unset -nocomplain iFirst
255 unset -nocomplain nRange
258 if {0==[info exists iFirst
]} {
261 } elseif
{ $sep == ($iFirst+$nRange) } {
264 lappend lRange
[list $iFirst $nRange]
269 lappend lRange
[list $iFirst $nRange]
273 proc an_print_range_array
{lRange
} {
276 foreach range
$lRange {
277 foreach {iFirst nRange
} $range {}
278 if {$iFirst > $iFirstMax} {set iFirstMax
$iFirst}
279 if {$nRange > $nRangeMax} {set nRangeMax
$nRange}
281 if {$iFirstMax >= (1<<22)} {error "first-max is too large for format"}
282 if {$nRangeMax >= (1<<10)} {error "range-max is too large for format"}
286 /* Each unsigned integer in the following
array corresponds to a contiguous
287 ** range of unicode codepoints that are not either letters or numbers
(i.e.
288 ** codepoints
for which this function should
return 0).
290 ** The most significant
22 bits in each
32-bit value contain the first
291 ** codepoint in the range. The least significant
10 bits are used to store
292 ** the size of the range
(always at least
1). In other words
, the value
293 ** ((C
<<22) + N
) represents a range of N codepoints starting with codepoint
294 ** C. It is not possible to represent a range larger than
1023 codepoints
295 ** using this
format.
298 puts -nonewline " const static unsigned int aEntry\[\] = \{"
300 foreach range
$lRange {
301 foreach {iFirst nRange
} $range {}
302 set u32
[format "0x%08X" [expr ($iFirst<<10) + $nRange]]
304 if {($i % 5)==0} {puts "" ; puts -nonewline " "}
305 puts -nonewline " $u32,"
312 proc an_print_ascii_bitmap
{lRange
} {
313 foreach range
$lRange {
314 foreach {iFirst nRange
} $range {}
315 for {set i
$iFirst} {$i < ($iFirst+$nRange)} {incr i
} {
316 if {$i<=127} { set a
($i) 1 }
320 set aAscii
[list 0 0 0 0]
321 foreach key
[array names a
] {
322 set idx
[expr $key >> 5]
323 lset aAscii
$idx [expr [lindex $aAscii $idx] |
(1 << ($key&0x001F))]
326 puts " static const unsigned int aAscii\[4\] = \{"
328 foreach v
$aAscii { puts -nonewline [format " 0x%08X," $v] }
333 proc print_isalnum
{zFunc lRange
} {
335 puts "** Return true if the argument corresponds to a unicode codepoint"
336 puts "** classified as either a letter or a number. Otherwise false."
338 puts "** The results are undefined if the value passed to this function"
339 puts "** is less than zero."
341 puts "int ${zFunc}\(int c)\{"
342 an_print_range_array
$lRange
343 an_print_ascii_bitmap
$lRange
346 return ( (aAscii
[c
>> 5] & (1 << (c
& 0x001F)))==0 );
347 }else if( c
<(1<<22) ){
348 unsigned int key
= (((unsigned int
)c
)<<10) |
0x000003FF;
350 int iHi
= sizeof
(aEntry
)/sizeof
(aEntry
[0]) - 1;
353 int iTest
= (iHi
+ iLo
) / 2;
354 if( key
>= aEntry
[iTest
] ){
361 assert
( aEntry
[0]<key
);
362 assert
( key
>=aEntry
[iRes
] );
363 return (c
>= ((aEntry
[iRes
]>>10) + (aEntry
[iRes
]&0x3FF)));
369 proc print_test_isalnum
{zFunc lRange
} {
370 foreach range
$lRange {
371 foreach {iFirst nRange
} $range {}
372 for {set i
$iFirst} {$i < ($iFirst+$nRange)} {incr i
} { set a
($i) 1 }
375 puts "static int isalnum_test(int *piCode)\{"
376 puts -nonewline " unsigned char aAlnum\[\] = \{"
377 for {set i
0} {$i < 70000} {incr i
} {
378 if {($i % 32)==0} { puts "" ; puts -nonewline " " }
379 set bFlag
[expr ![info exists a
($i)]]
380 puts -nonewline "${bFlag},"
385 puts -nonewline " int aLargeSep\[\] = \{"
387 foreach iSep
[lsort -integer [array names a
]] {
388 if {$iSep<70000} continue
389 if {($i % 8)==0} { puts "" ; puts -nonewline " " }
390 puts -nonewline " $iSep,"
395 puts -nonewline " int aLargeOther\[\] = \{"
397 foreach iSep
[lsort -integer [array names a
]] {
398 if {$iSep<70000} continue
399 if {[info exists a
([expr $iSep-1])]==0} {
400 if {($i % 8)==0} { puts "" ; puts -nonewline " " }
401 puts -nonewline " [expr $iSep-1],"
404 if {[info exists a
([expr $iSep+1])]==0} {
405 if {($i % 8)==0} { puts "" ; puts -nonewline " " }
406 puts -nonewline " [expr $iSep+1],"
413 puts [subst -nocommands {
415 for(i
=0; i
<sizeof
(aAlnum
)/sizeof
(aAlnum
[0]); i
++){
416 if( ${zFunc
}(i
)!=aAlnum
[i
] ){
421 for(i
=0; i
<sizeof
(aLargeSep
)/sizeof
(aLargeSep
[0]); i
++){
422 if( ${zFunc
}(aLargeSep
[i
])!=0 ){
423 *piCode
= aLargeSep
[i
];
427 for(i
=0; i
<sizeof
(aLargeOther
)/sizeof
(aLargeOther
[0]); i
++){
428 if( ${zFunc
}(aLargeOther
[i
])!=1 ){
429 *piCode
= aLargeOther
[i
];
438 #-------------------------------------------------------------------------
440 proc tl_load_casefolding_txt
{zName
} {
441 global tl_lookup_table
444 while { ![eof $fd] } {
446 if {[string range
$line 0 0] == "#"} continue
447 if {$line == ""} continue
449 foreach x
{a b c d
} {unset -nocomplain $x}
450 foreach {a b c d
} [split $line ";"] {}
454 foreach elem
$a { lappend a2
[expr "0x[string trim $elem]"] }
455 foreach elem
$c { lappend c2
[expr "0x[string trim $elem]"] }
456 set b
[string trim
$b]
457 set d
[string trim
$d]
459 if {$b=="C" ||
$b=="S"} { set tl_lookup_table
($a2) $c2 }
463 proc tl_create_records
{} {
464 global tl_lookup_table
472 foreach code
[lsort -integer [array names tl_lookup_table
]] {
473 set mapping
$tl_lookup_table($code)
476 set nOff
[expr $mapping - $code]
480 set diff
[expr $code - ($iFirst + ($nIncr * ($nRange - 1)))]
481 if { $nRange==1 && ($diff==1 ||
$diff==2) } {
485 if {$diff != $nIncr ||
($mapping - $code)!=$nOff} {
486 if { $nRange==1 } {set nIncr
1}
487 lappend lRecord
[list $iFirst $nIncr $nRange $nOff]
489 set nOff
[expr $mapping - $code]
498 lappend lRecord
[list $iFirst $nIncr $nRange $nOff]
503 proc tl_print_table_header
{} {
506 /* Each
entry in the following
array defines a rule
for folding a range
507 ** of codepoints to
lower case. The rule applies to a range of nRange
508 ** codepoints starting at codepoint iCode.
510 ** If the least significant bit in flags is clear
, then the rule applies
511 ** to all nRange codepoints
(i.e. all nRange codepoints are upper case and
512 ** need to be folded
). Or
, if it is
set, then the rule only applies to
513 ** every second codepoint in the range
, starting with codepoint C.
515 ** The
7 most significant bits in flags are an index into the aiOff
[]
516 ** array. If a specific codepoint C does require folding
, then its
lower
517 ** case equivalent is
((C
+ aiOff
[flags
>>1]) & 0xFFFF).
519 ** The contents of this
array are generated by parsing the CaseFolding.txt
520 ** file distributed as part of the
"Unicode Character Database". See
521 ** http://www.unicode.org
for details.
524 puts " static const struct TableEntry \{"
525 puts " unsigned short iCode;"
526 puts " unsigned char flags;"
527 puts " unsigned char nRange;"
528 puts " \} aEntry\[\] = \{"
531 proc tl_print_table_entry
{togglevar
entry liOff
} {
533 foreach {iFirst nIncr nRange nOff
} $entry {}
535 if {$iFirst > (1<<16)} { return 1 }
537 if {[info exists t
]==0} {set t
0}
538 if {$t==0} { puts -nonewline " " }
541 if {$nIncr==2} { set flags
1 ; set nRange
[expr $nRange * 2]}
542 if {$nOff<0} { incr nOff
[expr (1<<16)] }
544 set idx
[lsearch $liOff $nOff]
545 if {$idx<0} {error "malfunction generating aiOff"}
546 set flags
[expr $flags + $idx*2]
548 set txt
"{$iFirst, $flags, $nRange},"
552 puts -nonewline [format "% -23s" $txt]
554 set t
[expr ($t+1)%3]
559 proc tl_print_table_footer
{togglevar
} {
565 proc tl_print_if_entry
{entry} {
566 foreach {iFirst nIncr nRange nOff
} $entry {}
567 if {$nIncr==2} {error "tl_print_if_entry needs improvement!"}
569 puts " else if( c>=$iFirst && c<[expr $iFirst+$nRange] )\{"
570 puts " ret = c + $nOff;"
574 proc tl_generate_ioff_table
{lRecord
} {
575 foreach entry $lRecord {
576 foreach {iFirst nIncr nRange iOff
} $entry {}
577 if {$iOff<0} { incr iOff
[expr (1<<16)] }
578 if {[info exists a
($iOff)]} continue
582 set liOff
[lsort -integer [array names a
]]
583 if {[llength $liOff]>128} { error "Too many distinct ioffs" }
587 proc tl_print_ioff_table
{liOff
} {
588 puts -nonewline " static const unsigned short aiOff\[\] = \{"
591 if {($i % 8)==0} {puts "" ; puts -nonewline " "}
592 puts -nonewline [format "% -7s" "$off,"]
600 proc print_fold
{zFunc
} {
602 set lRecord
[tl_create_records
]
606 puts "** Interpret the argument as a unicode codepoint. If the codepoint"
607 puts "** is an upper case character that has a lower case equivalent,"
608 puts "** return the codepoint corresponding to the lower case version."
609 puts "** Otherwise, return a copy of the argument."
611 puts "** The results are undefined if the value passed to this function"
612 puts "** is less than zero."
614 puts "int ${zFunc}\(int c, int bRemoveDiacritic)\{"
616 set liOff
[tl_generate_ioff_table
$lRecord]
617 tl_print_table_header
618 foreach entry $lRecord {
619 if {[tl_print_table_entry toggle
$entry $liOff]} {
623 tl_print_table_footer toggle
624 tl_print_ioff_table
$liOff
630 assert
( sizeof
(unsigned short
)==2 && sizeof
(unsigned char
)==1 );
633 if( c
>='A'
&& c
<='Z'
) ret
= c
+ ('a'
- 'A'
);
635 int iHi
= sizeof
(aEntry
)/sizeof
(aEntry
[0]) - 1;
640 int iTest
= (iHi
+ iLo
) / 2;
641 int cmp
= (c
- aEntry
[iTest
].iCode
);
649 assert
( iRes
<0 || c
>=aEntry
[iRes
].iCode
);
652 const struct TableEntry
*p
= &aEntry
[iRes
];
653 if( c
<(p-
>iCode
+ p-
>nRange
) && 0==(0x01 & p-
>flags
& (p-
>iCode ^ c
)) ){
654 ret
= (c
+ (aiOff
[p-
>flags
>>1])) & 0x0000FFFF;
659 if( bRemoveDiacritic
) ret
= remove_diacritic
(ret
);
663 foreach entry $lHigh {
664 tl_print_if_entry
$entry
672 proc print_fold_test
{zFunc mappings
} {
673 global tl_lookup_table
675 foreach m
$mappings {
678 set extra
([lindex $m 0]) 0
681 set extra
([lindex $m 0]) $i
685 puts "static int fold_test(int *piCode)\{"
686 puts -nonewline " static int aLookup\[\] = \{"
687 for {set i
0} {$i < 70000} {incr i
} {
690 catch { set expected
$tl_lookup_table($i) }
691 set expected2
$expected
692 catch { set expected2
$extra($expected2) }
694 if {($i % 4)==0} { puts "" ; puts -nonewline " " }
695 puts -nonewline "$expected, $expected2, "
699 puts " for(i=0; i<sizeof(aLookup)/sizeof(aLookup\[0\]); i++)\{"
700 puts " int iCode = (i/2);"
701 puts " int bFlag = i & 0x0001;"
702 puts " if( ${zFunc}\(iCode, bFlag)!=aLookup\[i\] )\{"
703 puts " *piCode = iCode;"
712 proc print_fileheader
{} {
717 ** The author disclaims copyright to this
source code. In
place of
718 ** a legal notice
, here is a blessing
:
720 ** May you do good and not evil.
721 ** May you find forgiveness
for yourself and forgive others.
722 ** May you share freely
, never taking more than you give.
724 ******************************************************************************
728 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
732 puts "#if !defined(SQLITE_DISABLE_FTS3_UNICODE)"
733 puts "#if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)"
735 puts "#include <assert.h>"
739 proc print_test_main
{} {
741 puts "#include <stdio.h>"
743 puts "int main(int argc, char **argv)\{"
746 puts " r1 = isalnum_test(&code);"
747 puts " if( r1 ) printf(\"isalnum(): Problem with code %d\\n\",code);"
748 puts " else printf(\"isalnum(): test passed\\n\");"
749 puts " r2 = fold_test(&code);"
750 puts " if( r2 ) printf(\"fold(): Problem with code %d\\n\",code);"
751 puts " else printf(\"fold(): test passed\\n\");"
752 puts " return (r1 || r2);"
756 # Proces the command line arguments. Exit early if they are not to
760 puts -nonewline stderr
"Usage: $::argv0 ?-test? "
761 puts stderr
"<CaseFolding.txt file> <UnicodeData.txt file>"
764 if {[llength $argv]!=2 && [llength $argv]!=3} usage
765 if {[llength $argv]==3 && [lindex $argv 0]!="-test"} usage
766 set unicodedata.txt
[lindex $argv end
]
767 set casefolding.txt
[lindex $argv end-1
]
768 set generate_test_code
[expr {[llength $argv]==3}]
772 # Print the isalnum() function to stdout.
774 set lRange
[an_load_separator_ranges
]
775 print_isalnum sqlite3FtsUnicodeIsalnum
$lRange
777 # Leave a gap between the two generated C functions.
782 # Load the fold data. This is used by the [rd_XXX] commands
783 # as well as [print_fold].
784 tl_load_casefolding_txt
${casefolding.txt
}
786 set mappings
[rd_load_unicodedata_text
${unicodedata.txt
}]
790 print_isdiacritic sqlite3FtsUnicodeIsdiacritic
$mappings
794 # Print the fold() function to stdout.
796 print_fold sqlite3FtsUnicodeFold
798 # Print the test routines and main() function to stdout, if -test
801 if {$::generate_test_code} {
802 print_test_isalnum sqlite3FtsUnicodeIsalnum
$lRange
803 print_fold_test sqlite3FtsUnicodeFold
$mappings
807 puts "#endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */"
808 puts "#endif /* !defined(SQLITE_DISABLE_FTS3_UNICODE) */"