2 # -*- coding: <encoding name> -*-
\r
3 # Unicode Data Converter
\r
5 # This file is part of the Public Domain C Library (PDCLib).
\r
6 # Permission is granted to use, modify, and / or redistribute at will.
\r
8 Converts the character information provdied by Unicode in the UnicodeData.txt
\r
9 file from the Unicode character database into a table for use by PDCLib.
\r
11 Usage: Download the UnicodeData.txt file to the same directory as this script
\r
12 and then run it. Both Python 2 and 3 are supported.
\r
14 Download the data from
\r
15 ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
\r
19 # MUST BE KEPT SYNCHRONIZED WITH _PDCLIB_locale.h
\r
31 # Category to bitfield mapping
\r
33 'Lu': BIT_ALPHA | BIT_GRAPH | BIT_UPPER, # Uppercase
\r
34 'Ll': BIT_ALPHA | BIT_GRAPH | BIT_LOWER, # Lowercase
\r
35 'Lt': BIT_ALPHA | BIT_GRAPH | BIT_UPPER, # Title case. Upper?
\r
36 'Lm': BIT_ALPHA | BIT_GRAPH, # Modifier. Case?
\r
37 'Lo': BIT_ALPHA | BIT_GRAPH, # "Other" letter (e.g. Ideograph)
\r
38 'Nd': BIT_DIGIT | BIT_GRAPH, # Decimal digit
\r
39 'Nl': BIT_GRAPH, # Letter-like numeric character
\r
40 'No': BIT_GRAPH, # Other numeric
\r
41 'Pc': BIT_PUNCT | BIT_GRAPH, # Connecting punctuation
\r
42 'Pd': BIT_PUNCT | BIT_GRAPH, # Dash punctuation
\r
43 'Ps': BIT_PUNCT | BIT_GRAPH, # Opening punctuation
\r
44 'Pe': BIT_PUNCT | BIT_GRAPH, # Closing punctuation
\r
45 'Pi': BIT_PUNCT | BIT_GRAPH, # Opening quote
\r
46 'Pf': BIT_PUNCT | BIT_GRAPH, # Closing quote
\r
47 'Po': BIT_PUNCT | BIT_GRAPH, # Other punctuation
\r
48 'Sm': BIT_GRAPH, # Mathematical symbol
\r
49 'Sc': BIT_GRAPH, # Currency symbol
\r
50 'Sk': BIT_GRAPH, # Non-letterlike modifier symbol
\r
51 'So': BIT_GRAPH, # Other symbol
\r
52 'Zs': BIT_SPACE, # Non-zero-width space character
\r
53 'Zl': BIT_SPACE, # Line separator
\r
54 'Zp': BIT_SPACE, # Paragraph separator
\r
55 'Cc': BIT_CNTRL, # C0/C1 control codes
\r
58 # Characters with special properties
\r
61 0x0020: BIT_SPACE | BIT_BLANK, # space
\r
62 0x0009: BIT_SPACE | BIT_BLANK, # tab
\r
65 0x0030: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
66 0x0031: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
67 0x0032: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
68 0x0033: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
69 0x0034: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
70 0x0035: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
71 0x0036: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
72 0x0037: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
73 0x0038: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
74 0x0039: BIT_XDIGT | BIT_DIGIT | BIT_GRAPH,
\r
76 # A-F (hex uppercase)
\r
77 0x0041: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_UPPER,
\r
78 0x0042: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_UPPER,
\r
79 0x0043: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_UPPER,
\r
80 0x0044: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_UPPER,
\r
81 0x0045: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_UPPER,
\r
82 0x0046: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_UPPER,
\r
85 # a-f (hex lowercase)
\r
86 0x0061: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_LOWER,
\r
87 0x0062: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_LOWER,
\r
88 0x0063: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_LOWER,
\r
89 0x0064: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_LOWER,
\r
90 0x0065: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_LOWER,
\r
91 0x0066: BIT_XDIGT | BIT_ALPHA | BIT_GRAPH | BIT_LOWER,
\r
94 in_file = open('UnicodeData.txt', 'r')
\r
95 out_file = open('_PDCLIB_unicodedata.c', 'w')
\r
98 /* Unicode Character Information ** AUTOMATICALLY GENERATED FILE **
\r
100 * This file is part of the PDCLib public domain C Library, but is automatically
\r
101 * generated from the Unicode character data information file found at
\r
102 * ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt
\r
104 * As a result, the licensing that applies to that file also applies to this
\r
105 * file. The licensing which applies to the Unicode character data can be found
\r
106 * in Exhibit 1 of the Unicode Terms of Use, found at
\r
107 * http://www.unicode.org/copyright.html#Exhibit1
\r
110 #include <_PDCLIB_locale.h>
\r
112 _PDCLIB_wcinfo_t _PDCLIB_wcinfo[] = {
\r
113 // { value,\tflags,\tlower,\tupper\t}, // name
\r
115 for line in in_file:
\r
116 (num_hex, name, category, combining_class, bidi_class, decomposition,
\r
117 numeric_type, numeric_digit, numeric_value, mirrored, u1name, iso_com,
\r
118 upper_case_hex, lower_case_hex, title_case_hex) = line.split(";")
\r
120 num = int(num_hex, 16)
\r
121 upper_case = int(upper_case_hex, 16) if len(upper_case_hex) else num
\r
122 lower_case = int(lower_case_hex, 16) if len(lower_case_hex) else num
\r
123 bits = special.get(num, categories.get(category, 0))
\r
125 if upper_case == 0 and lower_case == 0 and bits == 0:
\r
128 out_file.write(" { 0x%X,\t0x%X,\t0x%X,\t0x%X }, // %s\n" % (
\r
129 num, bits, lower_case, upper_case, name))
\r
130 out_file.write('};\n\n')
\r
132 size_t _PDCLIB_wcinfo_size = sizeof(_PDCLIB_wcinfo) / sizeof(_PDCLIB_wcinfo[0]);
\r
136 #include <_PDCLIB_test.h>
\r
139 return TEST_RESULTS;
\r
147 os.remove('_PDCLIB_unicodedata.c')
\r