1 | n/a | /* |
---|
2 | n/a | Unicode character type helpers. |
---|
3 | n/a | |
---|
4 | n/a | Written by Marc-Andre Lemburg (mal@lemburg.com). |
---|
5 | n/a | Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com) |
---|
6 | n/a | |
---|
7 | n/a | Copyright (c) Corporation for National Research Initiatives. |
---|
8 | n/a | |
---|
9 | n/a | */ |
---|
10 | n/a | |
---|
11 | n/a | #include "Python.h" |
---|
12 | n/a | |
---|
13 | n/a | #define ALPHA_MASK 0x01 |
---|
14 | n/a | #define DECIMAL_MASK 0x02 |
---|
15 | n/a | #define DIGIT_MASK 0x04 |
---|
16 | n/a | #define LOWER_MASK 0x08 |
---|
17 | n/a | #define LINEBREAK_MASK 0x10 |
---|
18 | n/a | #define SPACE_MASK 0x20 |
---|
19 | n/a | #define TITLE_MASK 0x40 |
---|
20 | n/a | #define UPPER_MASK 0x80 |
---|
21 | n/a | #define XID_START_MASK 0x100 |
---|
22 | n/a | #define XID_CONTINUE_MASK 0x200 |
---|
23 | n/a | #define PRINTABLE_MASK 0x400 |
---|
24 | n/a | #define NUMERIC_MASK 0x800 |
---|
25 | n/a | #define CASE_IGNORABLE_MASK 0x1000 |
---|
26 | n/a | #define CASED_MASK 0x2000 |
---|
27 | n/a | #define EXTENDED_CASE_MASK 0x4000 |
---|
28 | n/a | |
---|
29 | n/a | typedef struct { |
---|
30 | n/a | /* |
---|
31 | n/a | These are either deltas to the character or offsets in |
---|
32 | n/a | _PyUnicode_ExtendedCase. |
---|
33 | n/a | */ |
---|
34 | n/a | const int upper; |
---|
35 | n/a | const int lower; |
---|
36 | n/a | const int title; |
---|
37 | n/a | /* Note if more flag space is needed, decimal and digit could be unified. */ |
---|
38 | n/a | const unsigned char decimal; |
---|
39 | n/a | const unsigned char digit; |
---|
40 | n/a | const unsigned short flags; |
---|
41 | n/a | } _PyUnicode_TypeRecord; |
---|
42 | n/a | |
---|
43 | n/a | #include "unicodetype_db.h" |
---|
44 | n/a | |
---|
45 | n/a | static const _PyUnicode_TypeRecord * |
---|
46 | n/a | gettyperecord(Py_UCS4 code) |
---|
47 | n/a | { |
---|
48 | n/a | int index; |
---|
49 | n/a | |
---|
50 | n/a | if (code >= 0x110000) |
---|
51 | n/a | index = 0; |
---|
52 | n/a | else |
---|
53 | n/a | { |
---|
54 | n/a | index = index1[(code>>SHIFT)]; |
---|
55 | n/a | index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))]; |
---|
56 | n/a | } |
---|
57 | n/a | |
---|
58 | n/a | return &_PyUnicode_TypeRecords[index]; |
---|
59 | n/a | } |
---|
60 | n/a | |
---|
61 | n/a | /* Returns the titlecase Unicode characters corresponding to ch or just |
---|
62 | n/a | ch if no titlecase mapping is known. */ |
---|
63 | n/a | |
---|
64 | n/a | Py_UCS4 _PyUnicode_ToTitlecase(Py_UCS4 ch) |
---|
65 | n/a | { |
---|
66 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
67 | n/a | |
---|
68 | n/a | if (ctype->flags & EXTENDED_CASE_MASK) |
---|
69 | n/a | return _PyUnicode_ExtendedCase[ctype->title & 0xFFFF]; |
---|
70 | n/a | return ch + ctype->title; |
---|
71 | n/a | } |
---|
72 | n/a | |
---|
73 | n/a | /* Returns 1 for Unicode characters having the category 'Lt', 0 |
---|
74 | n/a | otherwise. */ |
---|
75 | n/a | |
---|
76 | n/a | int _PyUnicode_IsTitlecase(Py_UCS4 ch) |
---|
77 | n/a | { |
---|
78 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
79 | n/a | |
---|
80 | n/a | return (ctype->flags & TITLE_MASK) != 0; |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | /* Returns 1 for Unicode characters having the XID_Start property, 0 |
---|
84 | n/a | otherwise. */ |
---|
85 | n/a | |
---|
86 | n/a | int _PyUnicode_IsXidStart(Py_UCS4 ch) |
---|
87 | n/a | { |
---|
88 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
89 | n/a | |
---|
90 | n/a | return (ctype->flags & XID_START_MASK) != 0; |
---|
91 | n/a | } |
---|
92 | n/a | |
---|
93 | n/a | /* Returns 1 for Unicode characters having the XID_Continue property, |
---|
94 | n/a | 0 otherwise. */ |
---|
95 | n/a | |
---|
96 | n/a | int _PyUnicode_IsXidContinue(Py_UCS4 ch) |
---|
97 | n/a | { |
---|
98 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
99 | n/a | |
---|
100 | n/a | return (ctype->flags & XID_CONTINUE_MASK) != 0; |
---|
101 | n/a | } |
---|
102 | n/a | |
---|
103 | n/a | /* Returns the integer decimal (0-9) for Unicode characters having |
---|
104 | n/a | this property, -1 otherwise. */ |
---|
105 | n/a | |
---|
106 | n/a | int _PyUnicode_ToDecimalDigit(Py_UCS4 ch) |
---|
107 | n/a | { |
---|
108 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
109 | n/a | |
---|
110 | n/a | return (ctype->flags & DECIMAL_MASK) ? ctype->decimal : -1; |
---|
111 | n/a | } |
---|
112 | n/a | |
---|
113 | n/a | int _PyUnicode_IsDecimalDigit(Py_UCS4 ch) |
---|
114 | n/a | { |
---|
115 | n/a | if (_PyUnicode_ToDecimalDigit(ch) < 0) |
---|
116 | n/a | return 0; |
---|
117 | n/a | return 1; |
---|
118 | n/a | } |
---|
119 | n/a | |
---|
120 | n/a | /* Returns the integer digit (0-9) for Unicode characters having |
---|
121 | n/a | this property, -1 otherwise. */ |
---|
122 | n/a | |
---|
123 | n/a | int _PyUnicode_ToDigit(Py_UCS4 ch) |
---|
124 | n/a | { |
---|
125 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
126 | n/a | |
---|
127 | n/a | return (ctype->flags & DIGIT_MASK) ? ctype->digit : -1; |
---|
128 | n/a | } |
---|
129 | n/a | |
---|
130 | n/a | int _PyUnicode_IsDigit(Py_UCS4 ch) |
---|
131 | n/a | { |
---|
132 | n/a | if (_PyUnicode_ToDigit(ch) < 0) |
---|
133 | n/a | return 0; |
---|
134 | n/a | return 1; |
---|
135 | n/a | } |
---|
136 | n/a | |
---|
137 | n/a | /* Returns the numeric value as double for Unicode characters having |
---|
138 | n/a | this property, -1.0 otherwise. */ |
---|
139 | n/a | |
---|
140 | n/a | int _PyUnicode_IsNumeric(Py_UCS4 ch) |
---|
141 | n/a | { |
---|
142 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
143 | n/a | |
---|
144 | n/a | return (ctype->flags & NUMERIC_MASK) != 0; |
---|
145 | n/a | } |
---|
146 | n/a | |
---|
147 | n/a | /* Returns 1 for Unicode characters to be hex-escaped when repr()ed, |
---|
148 | n/a | 0 otherwise. |
---|
149 | n/a | All characters except those characters defined in the Unicode character |
---|
150 | n/a | database as following categories are considered printable. |
---|
151 | n/a | * Cc (Other, Control) |
---|
152 | n/a | * Cf (Other, Format) |
---|
153 | n/a | * Cs (Other, Surrogate) |
---|
154 | n/a | * Co (Other, Private Use) |
---|
155 | n/a | * Cn (Other, Not Assigned) |
---|
156 | n/a | * Zl Separator, Line ('\u2028', LINE SEPARATOR) |
---|
157 | n/a | * Zp Separator, Paragraph ('\u2029', PARAGRAPH SEPARATOR) |
---|
158 | n/a | * Zs (Separator, Space) other than ASCII space('\x20'). |
---|
159 | n/a | */ |
---|
160 | n/a | int _PyUnicode_IsPrintable(Py_UCS4 ch) |
---|
161 | n/a | { |
---|
162 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
163 | n/a | |
---|
164 | n/a | return (ctype->flags & PRINTABLE_MASK) != 0; |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | /* Returns 1 for Unicode characters having the category 'Ll', 0 |
---|
168 | n/a | otherwise. */ |
---|
169 | n/a | |
---|
170 | n/a | int _PyUnicode_IsLowercase(Py_UCS4 ch) |
---|
171 | n/a | { |
---|
172 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
173 | n/a | |
---|
174 | n/a | return (ctype->flags & LOWER_MASK) != 0; |
---|
175 | n/a | } |
---|
176 | n/a | |
---|
177 | n/a | /* Returns 1 for Unicode characters having the category 'Lu', 0 |
---|
178 | n/a | otherwise. */ |
---|
179 | n/a | |
---|
180 | n/a | int _PyUnicode_IsUppercase(Py_UCS4 ch) |
---|
181 | n/a | { |
---|
182 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
183 | n/a | |
---|
184 | n/a | return (ctype->flags & UPPER_MASK) != 0; |
---|
185 | n/a | } |
---|
186 | n/a | |
---|
187 | n/a | /* Returns the uppercase Unicode characters corresponding to ch or just |
---|
188 | n/a | ch if no uppercase mapping is known. */ |
---|
189 | n/a | |
---|
190 | n/a | Py_UCS4 _PyUnicode_ToUppercase(Py_UCS4 ch) |
---|
191 | n/a | { |
---|
192 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
193 | n/a | |
---|
194 | n/a | if (ctype->flags & EXTENDED_CASE_MASK) |
---|
195 | n/a | return _PyUnicode_ExtendedCase[ctype->upper & 0xFFFF]; |
---|
196 | n/a | return ch + ctype->upper; |
---|
197 | n/a | } |
---|
198 | n/a | |
---|
199 | n/a | /* Returns the lowercase Unicode characters corresponding to ch or just |
---|
200 | n/a | ch if no lowercase mapping is known. */ |
---|
201 | n/a | |
---|
202 | n/a | Py_UCS4 _PyUnicode_ToLowercase(Py_UCS4 ch) |
---|
203 | n/a | { |
---|
204 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
205 | n/a | |
---|
206 | n/a | if (ctype->flags & EXTENDED_CASE_MASK) |
---|
207 | n/a | return _PyUnicode_ExtendedCase[ctype->lower & 0xFFFF]; |
---|
208 | n/a | return ch + ctype->lower; |
---|
209 | n/a | } |
---|
210 | n/a | |
---|
211 | n/a | int _PyUnicode_ToLowerFull(Py_UCS4 ch, Py_UCS4 *res) |
---|
212 | n/a | { |
---|
213 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
214 | n/a | |
---|
215 | n/a | if (ctype->flags & EXTENDED_CASE_MASK) { |
---|
216 | n/a | int index = ctype->lower & 0xFFFF; |
---|
217 | n/a | int n = ctype->lower >> 24; |
---|
218 | n/a | int i; |
---|
219 | n/a | for (i = 0; i < n; i++) |
---|
220 | n/a | res[i] = _PyUnicode_ExtendedCase[index + i]; |
---|
221 | n/a | return n; |
---|
222 | n/a | } |
---|
223 | n/a | res[0] = ch + ctype->lower; |
---|
224 | n/a | return 1; |
---|
225 | n/a | } |
---|
226 | n/a | |
---|
227 | n/a | int _PyUnicode_ToTitleFull(Py_UCS4 ch, Py_UCS4 *res) |
---|
228 | n/a | { |
---|
229 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
230 | n/a | |
---|
231 | n/a | if (ctype->flags & EXTENDED_CASE_MASK) { |
---|
232 | n/a | int index = ctype->title & 0xFFFF; |
---|
233 | n/a | int n = ctype->title >> 24; |
---|
234 | n/a | int i; |
---|
235 | n/a | for (i = 0; i < n; i++) |
---|
236 | n/a | res[i] = _PyUnicode_ExtendedCase[index + i]; |
---|
237 | n/a | return n; |
---|
238 | n/a | } |
---|
239 | n/a | res[0] = ch + ctype->title; |
---|
240 | n/a | return 1; |
---|
241 | n/a | } |
---|
242 | n/a | |
---|
243 | n/a | int _PyUnicode_ToUpperFull(Py_UCS4 ch, Py_UCS4 *res) |
---|
244 | n/a | { |
---|
245 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
246 | n/a | |
---|
247 | n/a | if (ctype->flags & EXTENDED_CASE_MASK) { |
---|
248 | n/a | int index = ctype->upper & 0xFFFF; |
---|
249 | n/a | int n = ctype->upper >> 24; |
---|
250 | n/a | int i; |
---|
251 | n/a | for (i = 0; i < n; i++) |
---|
252 | n/a | res[i] = _PyUnicode_ExtendedCase[index + i]; |
---|
253 | n/a | return n; |
---|
254 | n/a | } |
---|
255 | n/a | res[0] = ch + ctype->upper; |
---|
256 | n/a | return 1; |
---|
257 | n/a | } |
---|
258 | n/a | |
---|
259 | n/a | int _PyUnicode_ToFoldedFull(Py_UCS4 ch, Py_UCS4 *res) |
---|
260 | n/a | { |
---|
261 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
262 | n/a | |
---|
263 | n/a | if (ctype->flags & EXTENDED_CASE_MASK && (ctype->lower >> 20) & 7) { |
---|
264 | n/a | int index = (ctype->lower & 0xFFFF) + (ctype->lower >> 24); |
---|
265 | n/a | int n = (ctype->lower >> 20) & 7; |
---|
266 | n/a | int i; |
---|
267 | n/a | for (i = 0; i < n; i++) |
---|
268 | n/a | res[i] = _PyUnicode_ExtendedCase[index + i]; |
---|
269 | n/a | return n; |
---|
270 | n/a | } |
---|
271 | n/a | return _PyUnicode_ToLowerFull(ch, res); |
---|
272 | n/a | } |
---|
273 | n/a | |
---|
274 | n/a | int _PyUnicode_IsCased(Py_UCS4 ch) |
---|
275 | n/a | { |
---|
276 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
277 | n/a | |
---|
278 | n/a | return (ctype->flags & CASED_MASK) != 0; |
---|
279 | n/a | } |
---|
280 | n/a | |
---|
281 | n/a | int _PyUnicode_IsCaseIgnorable(Py_UCS4 ch) |
---|
282 | n/a | { |
---|
283 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
284 | n/a | |
---|
285 | n/a | return (ctype->flags & CASE_IGNORABLE_MASK) != 0; |
---|
286 | n/a | } |
---|
287 | n/a | |
---|
288 | n/a | /* Returns 1 for Unicode characters having the category 'Ll', 'Lu', 'Lt', |
---|
289 | n/a | 'Lo' or 'Lm', 0 otherwise. */ |
---|
290 | n/a | |
---|
291 | n/a | int _PyUnicode_IsAlpha(Py_UCS4 ch) |
---|
292 | n/a | { |
---|
293 | n/a | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch); |
---|
294 | n/a | |
---|
295 | n/a | return (ctype->flags & ALPHA_MASK) != 0; |
---|
296 | n/a | } |
---|
297 | n/a | |
---|