ยปCore Development>Code coverage>Objects/unicodectype.c

Python code coverage for Objects/unicodectype.c

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