Debian Codegraph @

src/ucd-tools/tools/case.py

raw symbols SCIP
1 #!/usr/bin/python
2
3 # Copyright (C) 2012-2018, 2021 Reece H. Dunn
4 #
5 # This file is part of ucd-tools.
6 #
7 # ucd-tools is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # ucd-tools is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
19
20 import os
21 import sys
22 import ucd
23
24 ucd_rootdir = sys.argv[1]
25 ucd_version = sys.argv[2]
26
27 unicode_chars = {}
28 null = ucd.CodePoint('0000')
29 for data in ucd.parse_ucd_data(ucd_rootdir, 'UnicodeData'):
30 if data['LowerCase'] != null or data['UpperCase'] != null or data['TitleCase'] != null:
31 unicode_chars[data['CodePoint']] = (data['LowerCase'], data['UpperCase'], data['TitleCase'])
32
33 if __name__ == '__main__':
34 sys.stdout.write("""/* Unicode Case Conversion
35 *
36 * Copyright (C) 2012-2018, 2021 Reece H. Dunn
37 *
38 * This file is part of ucd-tools.
39 *
40 * ucd-tools is free software: you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation, either version 3 of the License, or
43 * (at your option) any later version.
44 *
45 * ucd-tools is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
49 *
50 * You should have received a copy of the GNU General Public License
51 * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
52 */
53
54 /* NOTE: This file is automatically generated from the UnicodeData.txt file in
55 * the Unicode Character database by the ucd-tools/tools/case.py script.
56 */
57
58 #include "ucd/ucd.h"
59
60 #include <stddef.h>
61
62 /* Unicode Character Data %s */
63
64 struct case_conversion_entry
65 {
66 codepoint_t codepoint;
67 codepoint_t uppercase;
68 codepoint_t lowercase;
69 codepoint_t titlecase;
70 };
71 """ % ucd_version)
72
73 sys.stdout.write('\n')
74 sys.stdout.write('static const struct case_conversion_entry case_conversion_data[] =\n')
75 sys.stdout.write('{\n')
76 for codepoint in sorted(unicode_chars.keys()):
77 lower, upper, title = unicode_chars[codepoint]
78 sys.stdout.write('\t{ 0x%s, 0x%s, 0x%s, 0x%s },\n' % (codepoint, upper, lower, title))
79 sys.stdout.write('};\n')
80
81 for case in ['upper', 'lower', 'title']:
82 sys.stdout.write('\n')
83 sys.stdout.write('codepoint_t ucd_to%s(codepoint_t c)\n' % case)
84 sys.stdout.write('{\n')
85 sys.stdout.write('\tint begin = 0;\n')
86 sys.stdout.write('\tint end = sizeof(case_conversion_data)/sizeof(case_conversion_data[0]) - 1;\n')
87 sys.stdout.write('\twhile (begin <= end)\n')
88 sys.stdout.write('\t{\n')
89 sys.stdout.write('\t\tint pos = (begin + end) / 2;\n')
90 sys.stdout.write('\t\tconst struct case_conversion_entry *item = (case_conversion_data + pos);\n')
91 sys.stdout.write('\t\tif (c == item->codepoint)\n')
92 sys.stdout.write('\t\t\treturn item->%scase == 0 ? c : item->%scase;\n' % (case, case))
93 sys.stdout.write('\t\telse if (c > item->codepoint)\n')
94 sys.stdout.write('\t\t\tbegin = pos + 1;\n')
95 sys.stdout.write('\t\telse\n')
96 sys.stdout.write('\t\t\tend = pos - 1;\n')
97 sys.stdout.write('\t}\n')
98 sys.stdout.write('\treturn c;\n')
99 sys.stdout.write('}\n')