]>
Commit | Line | Data |
---|---|---|
53b2ba57 DM |
1 | /*\r |
2 | Unicode character type helpers.\r | |
3 | \r | |
4 | Written by Marc-Andre Lemburg (mal@lemburg.com).\r | |
5 | Modified for Python 2.0 by Fredrik Lundh (fredrik@pythonware.com)\r | |
6 | \r | |
7 | Copyright (c) Corporation for National Research Initiatives.\r | |
8 | \r | |
9 | */\r | |
10 | \r | |
11 | #include "Python.h"\r | |
12 | #include "unicodeobject.h"\r | |
13 | \r | |
14 | #define ALPHA_MASK 0x01\r | |
15 | #define DECIMAL_MASK 0x02\r | |
16 | #define DIGIT_MASK 0x04\r | |
17 | #define LOWER_MASK 0x08\r | |
18 | #define LINEBREAK_MASK 0x10\r | |
19 | #define SPACE_MASK 0x20\r | |
20 | #define TITLE_MASK 0x40\r | |
21 | #define UPPER_MASK 0x80\r | |
22 | #define NODELTA_MASK 0x100\r | |
23 | #define NUMERIC_MASK 0x200\r | |
24 | \r | |
25 | typedef struct {\r | |
26 | const Py_UNICODE upper;\r | |
27 | const Py_UNICODE lower;\r | |
28 | const Py_UNICODE title;\r | |
29 | const unsigned char decimal;\r | |
30 | const unsigned char digit;\r | |
31 | const unsigned short flags;\r | |
32 | } _PyUnicode_TypeRecord;\r | |
33 | \r | |
34 | #include "unicodetype_db.h"\r | |
35 | \r | |
36 | static const _PyUnicode_TypeRecord *\r | |
37 | gettyperecord(Py_UNICODE code)\r | |
38 | {\r | |
39 | int index;\r | |
40 | \r | |
41 | #ifdef Py_UNICODE_WIDE\r | |
42 | if (code >= 0x110000)\r | |
43 | index = 0;\r | |
44 | else\r | |
45 | #endif\r | |
46 | {\r | |
47 | index = index1[(code>>SHIFT)];\r | |
48 | index = index2[(index<<SHIFT)+(code&((1<<SHIFT)-1))];\r | |
49 | }\r | |
50 | \r | |
51 | return &_PyUnicode_TypeRecords[index];\r | |
52 | }\r | |
53 | \r | |
54 | /* Returns the titlecase Unicode characters corresponding to ch or just\r | |
55 | ch if no titlecase mapping is known. */\r | |
56 | \r | |
57 | Py_UNICODE _PyUnicode_ToTitlecase(register Py_UNICODE ch)\r | |
58 | {\r | |
59 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
60 | int delta = ctype->title;\r | |
61 | \r | |
62 | if (ctype->flags & NODELTA_MASK)\r | |
63 | return delta;\r | |
64 | \r | |
65 | if (delta >= 32768)\r | |
66 | delta -= 65536;\r | |
67 | \r | |
68 | return ch + delta;\r | |
69 | }\r | |
70 | \r | |
71 | /* Returns 1 for Unicode characters having the category 'Lt', 0\r | |
72 | otherwise. */\r | |
73 | \r | |
74 | int _PyUnicode_IsTitlecase(Py_UNICODE ch)\r | |
75 | {\r | |
76 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
77 | \r | |
78 | return (ctype->flags & TITLE_MASK) != 0;\r | |
79 | }\r | |
80 | \r | |
81 | /* Returns the integer decimal (0-9) for Unicode characters having\r | |
82 | this property, -1 otherwise. */\r | |
83 | \r | |
84 | int _PyUnicode_ToDecimalDigit(Py_UNICODE ch)\r | |
85 | {\r | |
86 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
87 | \r | |
88 | return (ctype->flags & DECIMAL_MASK) ? ctype->decimal : -1;\r | |
89 | }\r | |
90 | \r | |
91 | int _PyUnicode_IsDecimalDigit(Py_UNICODE ch)\r | |
92 | {\r | |
93 | if (_PyUnicode_ToDecimalDigit(ch) < 0)\r | |
94 | return 0;\r | |
95 | return 1;\r | |
96 | }\r | |
97 | \r | |
98 | /* Returns the integer digit (0-9) for Unicode characters having\r | |
99 | this property, -1 otherwise. */\r | |
100 | \r | |
101 | int _PyUnicode_ToDigit(Py_UNICODE ch)\r | |
102 | {\r | |
103 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
104 | \r | |
105 | return (ctype->flags & DIGIT_MASK) ? ctype->digit : -1;\r | |
106 | }\r | |
107 | \r | |
108 | int _PyUnicode_IsDigit(Py_UNICODE ch)\r | |
109 | {\r | |
110 | if (_PyUnicode_ToDigit(ch) < 0)\r | |
111 | return 0;\r | |
112 | return 1;\r | |
113 | }\r | |
114 | \r | |
115 | /* Returns the numeric value as double for Unicode characters having\r | |
116 | this property, -1.0 otherwise. */\r | |
117 | \r | |
118 | int _PyUnicode_IsNumeric(Py_UNICODE ch)\r | |
119 | {\r | |
120 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
121 | \r | |
122 | return (ctype->flags & NUMERIC_MASK) != 0;\r | |
123 | }\r | |
124 | \r | |
125 | #ifndef WANT_WCTYPE_FUNCTIONS\r | |
126 | \r | |
127 | /* Returns 1 for Unicode characters having the category 'Ll', 0\r | |
128 | otherwise. */\r | |
129 | \r | |
130 | int _PyUnicode_IsLowercase(Py_UNICODE ch)\r | |
131 | {\r | |
132 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
133 | \r | |
134 | return (ctype->flags & LOWER_MASK) != 0;\r | |
135 | }\r | |
136 | \r | |
137 | /* Returns 1 for Unicode characters having the category 'Lu', 0\r | |
138 | otherwise. */\r | |
139 | \r | |
140 | int _PyUnicode_IsUppercase(Py_UNICODE ch)\r | |
141 | {\r | |
142 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
143 | \r | |
144 | return (ctype->flags & UPPER_MASK) != 0;\r | |
145 | }\r | |
146 | \r | |
147 | /* Returns the uppercase Unicode characters corresponding to ch or just\r | |
148 | ch if no uppercase mapping is known. */\r | |
149 | \r | |
150 | Py_UNICODE _PyUnicode_ToUppercase(Py_UNICODE ch)\r | |
151 | {\r | |
152 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
153 | int delta = ctype->upper;\r | |
154 | if (ctype->flags & NODELTA_MASK)\r | |
155 | return delta;\r | |
156 | if (delta >= 32768)\r | |
157 | delta -= 65536;\r | |
158 | return ch + delta;\r | |
159 | }\r | |
160 | \r | |
161 | /* Returns the lowercase Unicode characters corresponding to ch or just\r | |
162 | ch if no lowercase mapping is known. */\r | |
163 | \r | |
164 | Py_UNICODE _PyUnicode_ToLowercase(Py_UNICODE ch)\r | |
165 | {\r | |
166 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
167 | int delta = ctype->lower;\r | |
168 | if (ctype->flags & NODELTA_MASK)\r | |
169 | return delta;\r | |
170 | if (delta >= 32768)\r | |
171 | delta -= 65536;\r | |
172 | return ch + delta;\r | |
173 | }\r | |
174 | \r | |
175 | /* Returns 1 for Unicode characters having the category 'Ll', 'Lu', 'Lt',\r | |
176 | 'Lo' or 'Lm', 0 otherwise. */\r | |
177 | \r | |
178 | int _PyUnicode_IsAlpha(Py_UNICODE ch)\r | |
179 | {\r | |
180 | const _PyUnicode_TypeRecord *ctype = gettyperecord(ch);\r | |
181 | \r | |
182 | return (ctype->flags & ALPHA_MASK) != 0;\r | |
183 | }\r | |
184 | \r | |
185 | #else\r | |
186 | \r | |
187 | /* Export the interfaces using the wchar_t type for portability\r | |
188 | reasons: */\r | |
189 | \r | |
190 | int _PyUnicode_IsLowercase(Py_UNICODE ch)\r | |
191 | {\r | |
192 | return iswlower(ch);\r | |
193 | }\r | |
194 | \r | |
195 | int _PyUnicode_IsUppercase(Py_UNICODE ch)\r | |
196 | {\r | |
197 | return iswupper(ch);\r | |
198 | }\r | |
199 | \r | |
200 | Py_UNICODE _PyUnicode_ToLowercase(Py_UNICODE ch)\r | |
201 | {\r | |
202 | return towlower(ch);\r | |
203 | }\r | |
204 | \r | |
205 | Py_UNICODE _PyUnicode_ToUppercase(Py_UNICODE ch)\r | |
206 | {\r | |
207 | return towupper(ch);\r | |
208 | }\r | |
209 | \r | |
210 | int _PyUnicode_IsAlpha(Py_UNICODE ch)\r | |
211 | {\r | |
212 | return iswalpha(ch);\r | |
213 | }\r | |
214 | \r | |
215 | #endif\r |