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