1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Wez Furlong (wez@thebrainroom.com) |
16 +----------------------------------------------------------------------+
17
18 Based on code from ucdata-2.5, which has the following Copyright:
19
20 Copyright 2001 Computing Research Labs, New Mexico State University
21
22 Permission is hereby granted, free of charge, to any person obtaining a
23 copy of this software and associated documentation files (the "Software"),
24 to deal in the Software without restriction, including without limitation
25 the rights to use, copy, modify, merge, publish, distribute, sublicense,
26 and/or sell copies of the Software, and to permit persons to whom the
27 Software is furnished to do so, subject to the following conditions:
28
29 The above copyright notice and this permission notice shall be included in
30 all copies or substantial portions of the Software.
31 */
32
33 #ifndef PHP_UNICODE_H
34 #define PHP_UNICODE_H
35
36 #if HAVE_MBSTRING
37
38 #define UC_MN 0 /* Mark, Non-Spacing */
39 #define UC_MC 1 /* Mark, Spacing Combining */
40 #define UC_ME 2 /* Mark, Enclosing */
41 #define UC_ND 3 /* Number, Decimal Digit */
42 #define UC_NL 4 /* Number, Letter */
43 #define UC_NO 5 /* Number, Other */
44 #define UC_ZS 6 /* Separator, Space */
45 #define UC_ZL 7 /* Separator, Line */
46 #define UC_ZP 8 /* Separator, Paragraph */
47 #define UC_CC 9 /* Other, Control */
48 #define UC_CF 10 /* Other, Format */
49 #define UC_OS 11 /* Other, Surrogate */
50 #define UC_CO 12 /* Other, Private Use */
51 #define UC_CN 13 /* Other, Not Assigned */
52 #define UC_LU 14 /* Letter, Uppercase */
53 #define UC_LL 15 /* Letter, Lowercase */
54 #define UC_LT 16 /* Letter, Titlecase */
55 #define UC_LM 17 /* Letter, Modifier */
56 #define UC_LO 18 /* Letter, Other */
57 #define UC_PC 19 /* Punctuation, Connector */
58 #define UC_PD 20 /* Punctuation, Dash */
59 #define UC_PS 21 /* Punctuation, Open */
60 #define UC_PE 22 /* Punctuation, Close */
61 #define UC_PO 23 /* Punctuation, Other */
62 #define UC_SM 24 /* Symbol, Math */
63 #define UC_SC 25 /* Symbol, Currency */
64 #define UC_SK 26 /* Symbol, Modifier */
65 #define UC_SO 27 /* Symbol, Other */
66 #define UC_L 28 /* Left-To-Right */
67 #define UC_R 29 /* Right-To-Left */
68 #define UC_EN 30 /* European Number */
69 #define UC_ES 31 /* European Number Separator */
70 #define UC_ET 32 /* European Number Terminator */
71 #define UC_AN 33 /* Arabic Number */
72 #define UC_CS 34 /* Common Number Separator */
73 #define UC_B 35 /* Block Separator */
74 #define UC_S 36 /* Segment Separator */
75 #define UC_WS 37 /* Whitespace */
76 #define UC_ON 38 /* Other Neutrals */
77 #define UC_PI 39 /* Punctuation, Initial */
78 #define UC_PF 40 /* Punctuation, Final */
79 #define UC_AL 41 /* Arabic Letter */
80
81 /* Derived properties from DerivedCoreProperties.txt */
82 #define UC_CASED 42
83 #define UC_CASE_IGNORABLE 43
84
85
86 MBSTRING_API int php_unicode_is_prop(unsigned long code, ...);
87 MBSTRING_API int php_unicode_is_prop1(unsigned long code, int prop);
88
89 MBSTRING_API char *php_unicode_convert_case(
90 int case_mode, const char *srcstr, size_t srclen, size_t *ret_len,
91 const mbfl_encoding *src_encoding, int illegal_mode, int illegal_substchar);
92
93 #define PHP_UNICODE_CASE_UPPER 0
94 #define PHP_UNICODE_CASE_LOWER 1
95 #define PHP_UNICODE_CASE_TITLE 2
96 #define PHP_UNICODE_CASE_FOLD 3
97 #define PHP_UNICODE_CASE_UPPER_SIMPLE 4
98 #define PHP_UNICODE_CASE_LOWER_SIMPLE 5
99 #define PHP_UNICODE_CASE_TITLE_SIMPLE 6
100 #define PHP_UNICODE_CASE_FOLD_SIMPLE 7
101 #define PHP_UNICODE_CASE_MODE_MAX 7
102
103 /* Optimize the common ASCII case for lower/upper */
104
php_unicode_is_lower(unsigned long code)105 static inline int php_unicode_is_lower(unsigned long code) {
106 if (code < 0x80) {
107 return code >= 0x61 && code <= 0x7A;
108 } else {
109 return php_unicode_is_prop1(code, UC_LL);
110 }
111 }
112
php_unicode_is_upper(unsigned long code)113 static inline int php_unicode_is_upper(unsigned long code) {
114 if (code < 0x80) {
115 return code >= 0x41 && code <= 0x5A;
116 } else {
117 return php_unicode_is_prop1(code, UC_LU);
118 }
119 }
120
121 #define php_unicode_is_alpha(cc) php_unicode_is_prop(cc, UC_LU, UC_LL, UC_LM, UC_LO, UC_LT, -1)
122 #define php_unicode_is_digit(cc) php_unicode_is_prop1(cc, UC_ND)
123 #define php_unicode_is_alnum(cc) php_unicode_is_prop(cc, UC_LU, UC_LL, UC_LM, UC_LO, UC_LT, UC_ND, -1)
124 #define php_unicode_is_cntrl(cc) php_unicode_is_prop(cc, UC_CC, UC_CF, -1)
125 #define php_unicode_is_blank(cc) php_unicode_is_prop1(cc, UC_ZS)
126 #define php_unicode_is_punct(cc) php_unicode_is_prop(cc, UC_PD, UC_PS, UC_PE, UC_PO, UC_PI, UC_PF, -1)
127 #define php_unicode_is_graph(cc) php_unicode_is_prop(cc, UC_MN, UC_MC, UC_ME, UC_ND, UC_NL, UC_NO, \
128 UC_LU, UC_LL, UC_LT, UC_LM, UC_LO, UC_PC, UC_PD, \
129 UC_PS, UC_PE, UC_PO, UC_SM, UC_SM, UC_SC, UC_SK, \
130 UC_SO, UC_PI, UC_PF, -1)
131 #define php_unicode_is_print(cc) php_unicode_is_prop(cc, UC_MN, UC_MC, UC_ME, UC_ND, UC_NL, UC_NO, \
132 UC_LU, UC_LL, UC_LT, UC_LM, UC_LO, UC_PC, UC_PD, \
133 UC_PS, UC_PE, UC_PO, UC_SM, UC_SM, UC_SC, UC_SK, \
134 UC_SO, UC_ZS, UC_PI, UC_PF, -1)
135 #define php_unicode_is_title(cc) php_unicode_is_prop1(cc, UC_LT)
136
137 #define php_unicode_is_isocntrl(cc) php_unicode_is_prop1(cc, UC_CC)
138 #define php_unicode_is_fmtcntrl(cc) php_unicode_is_prop1(cc, UC_CF)
139
140 #define php_unicode_is_symbol(cc) php_unicode_is_prop(cc, UC_SM, UC_SC, UC_SO, UC_SK, -1)
141 #define php_unicode_is_number(cc) php_unicode_is_prop(cc, UC_ND, UC_NO, UC_NL, -1)
142 #define php_unicode_is_nonspacing(cc) php_unicode_is_prop1(cc, UC_MN)
143 #define php_unicode_is_openpunct(cc) php_unicode_is_prop1(cc, UC_PS)
144 #define php_unicode_is_closepunct(cc) php_unicode_is_prop1(cc, UC_PE)
145 #define php_unicode_is_initialpunct(cc) php_unicode_is_prop1(cc, UC_PI)
146 #define php_unicode_is_finalpunct(cc) php_unicode_is_prop1(cc, UC_PF)
147
148 /*
149 * Directionality macros.
150 */
151 #define php_unicode_is_rtl(cc) php_unicode_is_prop1(cc, UC_R)
152 #define php_unicode_is_ltr(cc) php_unicode_is_prop1(cc, UC_L)
153 #define php_unicode_is_strong(cc) php_unicode_is_prop(cc, UC_L, UC_R, -1)
154 #define php_unicode_is_weak(cc) php_unicode_is_prop(cc, UC_EN, UC_ES, UC_ET, UC_AN, UC_CS, -1)
155 #define php_unicode_is_neutral(cc) php_unicode_is_prop(cc, UC_B, UC_S, UC_WS, UC_ON, -1)
156 #define php_unicode_is_separator(cc) php_unicode_is_prop(cc, UC_B, UC_S, -1)
157
158 /*
159 * Other macros inspired by John Cowan.
160 */
161 #define php_unicode_is_mark(cc) php_unicode_is_prop(cc, UC_MN, UC_MC, UC_ME, -1)
162 #define php_unicode_is_modif(cc) php_unicode_is_prop1(cc, UC_LM)
163 #define php_unicode_is_letnum(cc) php_unicode_is_prop1(cc, UC_NL)
164 #define php_unicode_is_connect(cc) php_unicode_is_prop1(cc, UC_PC)
165 #define php_unicode_is_dash(cc) php_unicode_is_prop1(cc, UC_PD)
166 #define php_unicode_is_math(cc) php_unicode_is_prop1(cc, UC_SM)
167 #define php_unicode_is_currency(cc) php_unicode_is_prop1(cc, UC_SC)
168 #define php_unicode_is_modifsymbol(cc) php_unicode_is_prop1(cc, UC_SK)
169 #define php_unicode_is_nsmark(cc) php_unicode_is_prop1(cc, UC_MN)
170 #define php_unicode_is_spmark(cc) php_unicode_is_prop1(cc, UC_MC)
171 #define php_unicode_is_enclosing(cc) php_unicode_is_prop1(cc, UC_ME)
172 #define php_unicode_is_private(cc) php_unicode_is_prop1(cc, UC_CO)
173 #define php_unicode_is_surrogate(cc) php_unicode_is_prop1(cc, UC_OS)
174 #define php_unicode_is_lsep(cc) php_unicode_is_prop1(cc, UC_ZL)
175 #define php_unicode_is_psep(cc) php_unicode_is_prop1(cc, UC_ZP)
176
177 #define php_unicode_is_identstart(cc) php_unicode_is_prop(cc, UC_LU, UC_LL, UC_LT, UC_LO, UC_NL, -1)
178 #define php_unicode_is_identpart(cc) php_unicode_is_prop(cc, UC_LU, UC_LL, UC_LT, UC_LO, UC_NL, \
179 UC_MN, UC_MC, UC_ND, UC_PC, UC_CF, -1)
180
181 /*
182 * Other miscellaneous character property macros.
183 */
184 #define php_unicode_is_han(cc) (((cc) >= 0x4e00 && (cc) <= 0x9fff) ||\
185 ((cc) >= 0xf900 && (cc) <= 0xfaff))
186 #define php_unicode_is_hangul(cc) ((cc) >= 0xac00 && (cc) <= 0xd7ff)
187
188 /*
189 * Derived core properties.
190 */
191
192 #define php_unicode_is_cased(cc) php_unicode_is_prop1(cc, UC_CASED)
193 #define php_unicode_is_case_ignorable(cc) php_unicode_is_prop1(cc, UC_CASE_IGNORABLE)
194
195
196 #endif
197
198
199 #endif /* PHP_UNICODE_H */
200