| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |
for data in ucd.parse_ucd_data(ucd_rootdir, 'UnicodeData'): |
| 29 |
for codepoint in data['CodePoint']: |
| 30 |
unicode_chars[codepoint] = data['GeneralCategory'] |
| 31 |
if '--with-csur' in sys.argv: |
| 32 |
for csur in ['Klingon']: |
| 33 |
for data in ucd.parse_ucd_data('data/csur', csur): |
| 34 |
for codepoint in data['CodePoint']: |
| 35 |
unicode_chars[codepoint] = data['GeneralCategory'] |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
category_sets = [ |
| 41 |
(ucd.CodeRange('000000..00D7FF'), None, 'Multiple Blocks'), |
| 42 |
(ucd.CodeRange('00D800..00DFFF'), 'Cs', 'Surrogates'), |
| 43 |
(ucd.CodeRange('00E000..00F7FF'), 'Co', 'Private Use Area'), |
| 44 |
(ucd.CodeRange('00F800..02FAFF'), None, 'Multiple Blocks'), |
| 45 |
(ucd.CodeRange('02FB00..0DFFFF'), 'Cn', 'Unassigned'), |
| 46 |
(ucd.CodeRange('0E0000..0E01FF'), None, 'Multiple Blocks'), |
| 47 |
(ucd.CodeRange('0E0200..0EFFFF'), 'Cn', 'Unassigned'), |
| 48 |
(ucd.CodeRange('0F0000..0FFFFD'), 'Co', 'Plane 15 Private Use'), |
| 49 |
(ucd.CodeRange('0FFFFE..0FFFFF'), 'Cn', 'Plane 15 Private Use'), |
| 50 |
(ucd.CodeRange('100000..10FFFD'), 'Co', 'Plane 16 Private Use'), |
| 51 |
(ucd.CodeRange('10FFFE..10FFFF'), 'Cn', 'Plane 16 Private Use'), |
| 52 |
] |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
special_categories = ['Cn', 'Co', 'Lo', 'Sm', 'So'] |
| 58 |
|
| 59 |
category_tables = {} |
| 60 |
for codepoints, category, comment in category_sets: |
| 61 |
if not category: |
| 62 |
table = {} |
| 63 |
table_entry = None |
| 64 |
table_codepoint = None |
| 65 |
table_category = None |
| 66 |
for i, codepoint in enumerate(codepoints): |
| 67 |
try: |
| 68 |
category = unicode_chars[codepoint] |
| 69 |
except KeyError: |
| 70 |
category = 'Cn' |
| 71 |
if (i % 256) == 0: |
| 72 |
if table_entry: |
| 73 |
if table_category in special_categories: |
| 74 |
table[table_codepoint] = table_category |
| 75 |
elif table_category: |
| 76 |
raise Exception('%s only table not in the special_categories list.' % table_category) |
| 77 |
else: |
| 78 |
table[table_codepoint] = table_entry |
| 79 |
table_entry = [] |
| 80 |
table_codepoint = codepoint |
| 81 |
table_category = category |
| 82 |
if category != table_category: |
| 83 |
table_category = None |
| 84 |
table_entry.append(category) |
| 85 |
if table_entry: |
| 86 |
if table_category in special_categories: |
| 87 |
table[table_codepoint] = table_category |
| 88 |
else: |
| 89 |
table[table_codepoint] = table_entry |
| 90 |
category_tables['%s_%s' % (codepoints.first, codepoints.last)] = table |
| 91 |
|
| 92 |
if __name__ == '__main__': |
| 93 |
sys.stdout.write("""/* Unicode General Categories |
| 94 |
* |
| 95 |
* Copyright (C) 2012-2018 Reece H. Dunn |
| 96 |
* |
| 97 |
* This file is part of ucd-tools. |
| 98 |
* |
| 99 |
* ucd-tools is free software: you can redistribute it and/or modify |
| 100 |
* it under the terms of the GNU General Public License as published by |
| 101 |
* the Free Software Foundation, either version 3 of the License, or |
| 102 |
* (at your option) any later version. |
| 103 |
* |
| 104 |
* ucd-tools is distributed in the hope that it will be useful, |
| 105 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 106 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 107 |
* GNU General Public License for more details. |
| 108 |
* |
| 109 |
* You should have received a copy of the GNU General Public License |
| 110 |
* along with ucd-tools. If not, see <http://www.gnu.org/licenses/>. |
| 111 |
*/ |
| 112 |
|
| 113 |
/* NOTE: This file is automatically generated from the UnicodeData.txt file in |
| 114 |
* the Unicode Character database by the ucd-tools/tools/categories.py script. |
| 115 |
*/ |
| 116 |
|
| 117 |
#include "ucd/ucd.h" |
| 118 |
|
| 119 |
#include <stddef.h> |
| 120 |
|
| 121 |
#define Cc UCD_CATEGORY_Cc |
| 122 |
#define Cf UCD_CATEGORY_Cf |
| 123 |
#define Cn UCD_CATEGORY_Cn |
| 124 |
#define Co UCD_CATEGORY_Co |
| 125 |
#define Cs UCD_CATEGORY_Cs |
| 126 |
#define Ll UCD_CATEGORY_Ll |
| 127 |
#define Lm UCD_CATEGORY_Lm |
| 128 |
#define Lo UCD_CATEGORY_Lo |
| 129 |
#define Lt UCD_CATEGORY_Lt |
| 130 |
#define Lu UCD_CATEGORY_Lu |
| 131 |
#define Mc UCD_CATEGORY_Mc |
| 132 |
#define Me UCD_CATEGORY_Me |
| 133 |
#define Mn UCD_CATEGORY_Mn |
| 134 |
#define Nd UCD_CATEGORY_Nd |
| 135 |
#define Nl UCD_CATEGORY_Nl |
| 136 |
#define No UCD_CATEGORY_No |
| 137 |
#define Pc UCD_CATEGORY_Pc |
| 138 |
#define Pd UCD_CATEGORY_Pd |
| 139 |
#define Pe UCD_CATEGORY_Pe |
| 140 |
#define Pf UCD_CATEGORY_Pf |
| 141 |
#define Pi UCD_CATEGORY_Pi |
| 142 |
#define Po UCD_CATEGORY_Po |
| 143 |
#define Ps UCD_CATEGORY_Ps |
| 144 |
#define Sc UCD_CATEGORY_Sc |
| 145 |
#define Sk UCD_CATEGORY_Sk |
| 146 |
#define Sm UCD_CATEGORY_Sm |
| 147 |
#define So UCD_CATEGORY_So |
| 148 |
#define Zl UCD_CATEGORY_Zl |
| 149 |
#define Zp UCD_CATEGORY_Zp |
| 150 |
#define Zs UCD_CATEGORY_Zs |
| 151 |
#define Ii UCD_CATEGORY_Ii |
| 152 |
|
| 153 |
/* Unicode Character Data %s */ |
| 154 |
""" % ucd_version) |
| 155 |
|
| 156 |
for category in special_categories: |
| 157 |
sys.stdout.write('\n') |
| 158 |
sys.stdout.write('static const uint8_t categories_%s[256] =\n' % category) |
| 159 |
sys.stdout.write('{') |
| 160 |
for i in range(0, 256): |
| 161 |
if (i % 16) == 0: |
| 162 |
sys.stdout.write('\n\t/* %02X */' % i) |
| 163 |
sys.stdout.write(' %s,' % category) |
| 164 |
sys.stdout.write('\n};\n') |
| 165 |
|
| 166 |
for codepoints, category, comment in category_sets: |
| 167 |
if not category: |
| 168 |
tables = category_tables['%s_%s' % (codepoints.first, codepoints.last)] |
| 169 |
for codepoint in sorted(tables.keys()): |
| 170 |
table = tables[codepoint] |
| 171 |
if table in special_categories: |
| 172 |
continue |
| 173 |
|
| 174 |
sys.stdout.write('\n') |
| 175 |
sys.stdout.write('static const uint8_t categories_%s[256] =\n' % codepoint) |
| 176 |
sys.stdout.write('{') |
| 177 |
for i, category in enumerate(table): |
| 178 |
if (i % 16) == 0: |
| 179 |
sys.stdout.write('\n\t/* %02X */' % i) |
| 180 |
sys.stdout.write(' %s,' % category) |
| 181 |
sys.stdout.write('\n};\n') |
| 182 |
|
| 183 |
for codepoints, category, comment in category_sets: |
| 184 |
if not category: |
| 185 |
table_index = '%s_%s' % (codepoints.first, codepoints.last) |
| 186 |
sys.stdout.write('\n') |
| 187 |
sys.stdout.write('static const uint8_t *categories_%s[] =\n' % table_index) |
| 188 |
sys.stdout.write('{\n') |
| 189 |
for codepoint, table in sorted(category_tables[table_index].items()): |
| 190 |
if isinstance(table, str): |
| 191 |
sys.stdout.write('\tcategories_%s, /* %s */\n' % (table, codepoint)) |
| 192 |
else: |
| 193 |
sys.stdout.write('\tcategories_%s,\n' % codepoint) |
| 194 |
sys.stdout.write('};\n') |
| 195 |
|
| 196 |
sys.stdout.write('\n') |
| 197 |
sys.stdout.write('ucd_category ucd_lookup_category(codepoint_t c)\n') |
| 198 |
sys.stdout.write('{\n') |
| 199 |
for codepoints, category, comment in category_sets: |
| 200 |
if category: |
| 201 |
sys.stdout.write('\tif (c <= 0x%s) return %s; /* %s : %s */\n' % (codepoints.last, category, codepoints, comment)) |
| 202 |
else: |
| 203 |
sys.stdout.write('\tif (c <= 0x%s) /* %s */\n' % (codepoints.last, codepoints)) |
| 204 |
sys.stdout.write('\t{\n') |
| 205 |
sys.stdout.write('\t\tconst uint8_t *table = categories_%s_%s[(c - 0x%s) / 256];\n' % (codepoints.first, codepoints.last, codepoints.first)) |
| 206 |
sys.stdout.write('\t\treturn (ucd_category)table[c % 256];\n') |
| 207 |
sys.stdout.write('\t}\n') |
| 208 |
sys.stdout.write('\treturn Ii; /* Invalid Unicode Codepoint */\n') |
| 209 |
sys.stdout.write('}\n') |
| 210 |
|
| 211 |
sys.stdout.write(""" |
| 212 |
ucd_category_group ucd_get_category_group_for_category(ucd_category c) |
| 213 |
{ |
| 214 |
switch (c) |
| 215 |
{ |
| 216 |
case Cc: case Cf: case Cn: case Co: case Cs: |
| 217 |
return UCD_CATEGORY_GROUP_C; |
| 218 |
case Ll: case Lm: case Lo: case Lt: case Lu: |
| 219 |
return UCD_CATEGORY_GROUP_L; |
| 220 |
case Mc: case Me: case Mn: |
| 221 |
return UCD_CATEGORY_GROUP_M; |
| 222 |
case Nd: case Nl: case No: |
| 223 |
return UCD_CATEGORY_GROUP_N; |
| 224 |
case Pc: case Pd: case Pe: case Pf: case Pi: case Po: case Ps: |
| 225 |
return UCD_CATEGORY_GROUP_P; |
| 226 |
case Sc: case Sk: case Sm: case So: |
| 227 |
return UCD_CATEGORY_GROUP_S; |
| 228 |
case Zl: case Zp: case Zs: |
| 229 |
return UCD_CATEGORY_GROUP_Z; |
| 230 |
case Ii: |
| 231 |
default: |
| 232 |
return UCD_CATEGORY_GROUP_I; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
ucd_category_group ucd_lookup_category_group(codepoint_t c) |
| 237 |
{ |
| 238 |
return (ucd_category_group)ucd_get_category_group_for_category(ucd_lookup_category(c)); |
| 239 |
} |
| 240 |
""") |