xref: /PHP-8.2/ext/intl/uchar/uchar.c (revision 5fbda730)
1 #include "uchar.h"
2 #include "intl_data.h"
3 #include "intl_convert.h"
4 
5 #include <unicode/uchar.h>
6 #include <unicode/utf8.h>
7 
8 #include "uchar_arginfo.h"
9 
10 #define IC_METHOD(mname) PHP_METHOD(IntlChar, mname)
11 
convert_cp(UChar32 * pcp,zend_string * string_codepoint,zend_long int_codepoint)12 static inline int convert_cp(UChar32* pcp, zend_string *string_codepoint, zend_long int_codepoint) {
13 	if (string_codepoint != NULL) {
14 		int32_t i = 0;
15 		size_t string_codepoint_length = ZSTR_LEN(string_codepoint);
16 
17 		if (ZEND_SIZE_T_INT_OVFL(string_codepoint_length)) {
18 			intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
19 			intl_error_set_custom_msg(NULL, "Input string is too long.", 0);
20 			return FAILURE;
21 		}
22 
23 		U8_NEXT(ZSTR_VAL(string_codepoint), i, string_codepoint_length, int_codepoint);
24 		if ((size_t)i != string_codepoint_length) {
25 			intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
26 			intl_error_set_custom_msg(NULL, "Passing a UTF-8 character for codepoint requires a string which is exactly one UTF-8 codepoint long.", 0);
27 			return FAILURE;
28 		}
29 	}
30 
31 	if ((int_codepoint < UCHAR_MIN_VALUE) || (int_codepoint > UCHAR_MAX_VALUE)) {
32 		intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
33 		intl_error_set_custom_msg(NULL, "Codepoint out of range", 0);
34 		return FAILURE;
35 	}
36 	*pcp = (UChar32)int_codepoint;
37 	return SUCCESS;
38 }
39 
parse_code_point_param(INTERNAL_FUNCTION_PARAMETERS,UChar32 * cp)40 static zend_never_inline int parse_code_point_param(INTERNAL_FUNCTION_PARAMETERS, UChar32 *cp) {
41 	zend_string *string_codepoint;
42 	zend_long int_codepoint = 0;
43 	ZEND_PARSE_PARAMETERS_START(1, 1)
44 		Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint)
45 	ZEND_PARSE_PARAMETERS_END_EX(return FAILURE);
46 	return convert_cp(cp, string_codepoint, int_codepoint);
47 }
48 
49 /* {{{ Converts a numeric codepoint to UTF-8
50  * Acts as an identify function when given a valid UTF-8 encoded codepoint
51  */
IC_METHOD(chr)52 IC_METHOD(chr) {
53 	UChar32 cp;
54 	char buffer[5];
55 	int buffer_len = 0;
56 
57 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) {
58 		RETURN_NULL();
59 	}
60 
61 	/* We can use unsafe because we know the codepoint is in valid range
62 	 * and that 4 bytes is enough for any unicode point
63 	 */
64 	U8_APPEND_UNSAFE(buffer, buffer_len, cp);
65 	buffer[buffer_len] = 0;
66 	RETURN_STRINGL(buffer, buffer_len);
67 }
68 /* }}} */
69 
70 /* {{{ Converts a UTf-8 encoded codepoint to its integer U32 value
71  * Acts as an identity function when passed a valid integer codepoint
72  */
IC_METHOD(ord)73 IC_METHOD(ord) {
74 	UChar32 cp;
75 
76 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) {
77 		RETURN_NULL();
78 	}
79 
80 	RETURN_LONG(cp);
81 }
82 /* }}} */
83 
84 /* {{{ */
IC_METHOD(hasBinaryProperty)85 IC_METHOD(hasBinaryProperty) {
86 	UChar32 cp;
87 	zend_long prop;
88 	zend_string *string_codepoint;
89 	zend_long int_codepoint;
90 
91 	ZEND_PARSE_PARAMETERS_START(2, 2)
92 		Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint)
93 		Z_PARAM_LONG(prop)
94 	ZEND_PARSE_PARAMETERS_END();
95 
96 	if (convert_cp(&cp, string_codepoint, int_codepoint) == FAILURE) {
97 		RETURN_NULL();
98 	}
99 
100 	RETURN_BOOL(u_hasBinaryProperty(cp, (UProperty)prop));
101 }
102 /* }}} */
103 
104 /* {{{ */
IC_METHOD(getIntPropertyValue)105 IC_METHOD(getIntPropertyValue) {
106 	UChar32 cp;
107 	zend_long prop;
108 	zend_string *string_codepoint;
109 	zend_long int_codepoint;
110 
111 	ZEND_PARSE_PARAMETERS_START(2, 2)
112 		Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint)
113 		Z_PARAM_LONG(prop)
114 	ZEND_PARSE_PARAMETERS_END();
115 
116 	if (convert_cp(&cp, string_codepoint, int_codepoint) == FAILURE) {
117 		RETURN_NULL();
118 	}
119 
120 	RETURN_LONG(u_getIntPropertyValue(cp, (UProperty)prop));
121 }
122 /* }}} */
123 
124 /* {{{ */
IC_METHOD(getIntPropertyMinValue)125 IC_METHOD(getIntPropertyMinValue) {
126 	zend_long prop;
127 
128 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &prop) == FAILURE) {
129 		RETURN_THROWS();
130 	}
131 
132 	RETURN_LONG(u_getIntPropertyMinValue((UProperty)prop));
133 }
134 /* }}} */
135 
136 /* {{{ */
IC_METHOD(getIntPropertyMaxValue)137 IC_METHOD(getIntPropertyMaxValue) {
138 	zend_long prop;
139 
140 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &prop) == FAILURE) {
141 		RETURN_THROWS();
142 	}
143 
144 	RETURN_LONG(u_getIntPropertyMaxValue((UProperty)prop));
145 }
146 /* }}} */
147 
148 /* {{{ */
IC_METHOD(getNumericValue)149 IC_METHOD(getNumericValue) {
150 	UChar32 cp;
151 
152 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) {
153 		RETURN_NULL();
154 	}
155 
156 	RETURN_DOUBLE(u_getNumericValue(cp));
157 }
158 /* }}} */
159 
160 /* {{{ */
161 typedef struct _enumCharType_data {
162 	zend_fcall_info fci;
163 	zend_fcall_info_cache fci_cache;
164 } enumCharType_data;
enumCharType_callback(enumCharType_data * context,UChar32 start,UChar32 limit,UCharCategory type)165 static UBool enumCharType_callback(enumCharType_data *context,
166 		UChar32 start, UChar32 limit, UCharCategory type) {
167 	zval retval;
168 	zval args[3];
169 
170 	ZVAL_NULL(&retval);
171 	/* Note that $start is INclusive, while $limit is EXclusive
172 	 * Therefore (0, 32, 15) means CPs 0..31 are of type 15
173 	 */
174 	ZVAL_LONG(&args[0], start);
175 	ZVAL_LONG(&args[1], limit);
176 	ZVAL_LONG(&args[2], type);
177 
178 	context->fci.retval = &retval;
179 	context->fci.param_count = 3;
180 	context->fci.params = args;
181 
182 	if (zend_call_function(&context->fci, &context->fci_cache) == FAILURE) {
183 		intl_error_set_code(NULL, U_INTERNAL_PROGRAM_ERROR);
184 		intl_errors_set_custom_msg(NULL, "enumCharTypes callback failed", 0);
185 		zval_ptr_dtor(&retval);
186 		return 0;
187 	}
188 	zval_ptr_dtor(&retval);
189 	return 1;
190 }
IC_METHOD(enumCharTypes)191 IC_METHOD(enumCharTypes) {
192 	enumCharType_data context;
193 
194 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "f", &context.fci, &context.fci_cache) == FAILURE) {
195 		RETURN_THROWS();
196 	}
197 	u_enumCharTypes((UCharEnumTypeRange*)enumCharType_callback, &context);
198 }
199 /* }}} */
200 
201 /* {{{ */
IC_METHOD(getBlockCode)202 IC_METHOD(getBlockCode) {
203 	UChar32 cp;
204 
205 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) {
206 		RETURN_NULL();
207 	}
208 
209 	RETURN_LONG(ublock_getCode(cp));
210 }
211 /* }}} */
212 
213 /* {{{ */
IC_METHOD(charName)214 IC_METHOD(charName) {
215 	UChar32 cp;
216 	zend_string *string_codepoint;
217 	zend_long int_codepoint;
218 	UErrorCode error = U_ZERO_ERROR;
219 	zend_long nameChoice = U_UNICODE_CHAR_NAME;
220 	zend_string *buffer = NULL;
221 	int32_t buffer_len;
222 
223 	ZEND_PARSE_PARAMETERS_START(1, 2)
224 		Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint)
225 		Z_PARAM_OPTIONAL
226 		Z_PARAM_LONG(nameChoice)
227 	ZEND_PARSE_PARAMETERS_END();
228 
229 	if (convert_cp(&cp, string_codepoint, int_codepoint) == FAILURE) {
230 		RETURN_NULL();
231 	}
232 
233 	buffer_len = u_charName(cp, (UCharNameChoice)nameChoice, NULL, 0, &error);
234 	buffer = zend_string_alloc(buffer_len, 0);
235 	error = U_ZERO_ERROR;
236 	buffer_len = u_charName(cp, (UCharNameChoice)nameChoice, ZSTR_VAL(buffer), ZSTR_LEN(buffer) + 1, &error);
237 	if (U_FAILURE(error)) {
238 		zend_string_efree(buffer);
239 		INTL_CHECK_STATUS_OR_NULL(error, "Failure getting character name");
240 	}
241 	RETURN_NEW_STR(buffer);
242 }
243 /* }}} */
244 
245 /* {{{ */
IC_METHOD(charFromName)246 IC_METHOD(charFromName) {
247 	char *name;
248 	size_t name_len;
249 	zend_long nameChoice = U_UNICODE_CHAR_NAME;
250 	UChar32 ret;
251 	UErrorCode error = U_ZERO_ERROR;
252 
253 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &name, &name_len, &nameChoice) == FAILURE) {
254 		RETURN_THROWS();
255 	}
256 
257 	ret = u_charFromName((UCharNameChoice)nameChoice, name, &error);
258 	INTL_CHECK_STATUS_OR_NULL(error, NULL);
259 	RETURN_LONG(ret);
260 }
261 /* }}} */
262 
263 /* {{{ void void IntlChar::enumCharNames(int|string $start, int|string $limit, callable $callback, int $nameChoice = IntlChar::UNICODE_CHAR_NAME) */
264 typedef struct _enumCharNames_data {
265 	zend_fcall_info fci;
266 	zend_fcall_info_cache fci_cache;
267 } enumCharNames_data;
enumCharNames_callback(enumCharNames_data * context,UChar32 code,UCharNameChoice nameChoice,const char * name,int32_t length)268 static UBool enumCharNames_callback(enumCharNames_data *context,
269 									UChar32 code, UCharNameChoice nameChoice,
270 									const char *name, int32_t length) {
271 	zval retval;
272 	zval args[3];
273 
274 	ZVAL_NULL(&retval);
275 	ZVAL_LONG(&args[0], code);
276 	ZVAL_LONG(&args[1], nameChoice);
277 	ZVAL_STRINGL(&args[2], name, length);
278 
279 	context->fci.retval = &retval;
280 	context->fci.param_count = 3;
281 	context->fci.params = args;
282 
283 	if (zend_call_function(&context->fci, &context->fci_cache) == FAILURE) {
284 		intl_error_set_code(NULL, U_INTERNAL_PROGRAM_ERROR);
285 		intl_error_set_custom_msg(NULL, "enumCharNames callback failed", 0);
286 		zval_ptr_dtor(&retval);
287 		zval_ptr_dtor_str(&args[2]);
288 		return 0;
289 	}
290 	zval_ptr_dtor(&retval);
291 	zval_ptr_dtor_str(&args[2]);
292 	return 1;
293 }
IC_METHOD(enumCharNames)294 IC_METHOD(enumCharNames) {
295 	UChar32 start, limit;
296 	zend_string *string_start, *string_limit;
297 	zend_long int_start, int_limit;
298 	enumCharNames_data context;
299 	zend_long nameChoice = U_UNICODE_CHAR_NAME;
300 	UErrorCode error = U_ZERO_ERROR;
301 
302 
303 	ZEND_PARSE_PARAMETERS_START(3, 4)
304 		Z_PARAM_STR_OR_LONG(string_start, int_start)
305 		Z_PARAM_STR_OR_LONG(string_limit, int_limit)
306 		Z_PARAM_FUNC(context.fci, context.fci_cache)
307 		Z_PARAM_OPTIONAL
308 		Z_PARAM_LONG(nameChoice)
309 	ZEND_PARSE_PARAMETERS_END();
310 
311 	if (convert_cp(&start, string_start, int_start) == FAILURE || convert_cp(&limit, string_limit, int_limit) == FAILURE) {
312 		RETURN_NULL();
313 	}
314 
315 	u_enumCharNames(start, limit, (UEnumCharNamesFn*)enumCharNames_callback, &context, nameChoice, &error);
316 	INTL_CHECK_STATUS(error, NULL);
317 }
318 /* }}} */
319 
320 /* {{{ */
IC_METHOD(getPropertyName)321 IC_METHOD(getPropertyName) {
322 	zend_long property;
323 	zend_long nameChoice = U_LONG_PROPERTY_NAME;
324 	const char *ret;
325 
326 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &property, &nameChoice) == FAILURE) {
327 		RETURN_THROWS();
328 	}
329 
330 	ret = u_getPropertyName((UProperty)property, (UPropertyNameChoice)nameChoice);
331 	if (ret) {
332 		RETURN_STRING(ret);
333 	} else {
334 		intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
335 		intl_error_set_custom_msg(NULL, "Failed to get property name", 0);
336 		RETURN_FALSE;
337 	}
338 }
339 /* }}} */
340 
341 /* {{{ */
IC_METHOD(getPropertyEnum)342 IC_METHOD(getPropertyEnum) {
343 	char *alias;
344 	size_t alias_len;
345 
346 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == FAILURE) {
347 		RETURN_THROWS();
348 	}
349 
350 	RETURN_LONG(u_getPropertyEnum(alias));
351 }
352 /* }}} */
353 
354 /* {{{ */
IC_METHOD(getPropertyValueName)355 IC_METHOD(getPropertyValueName) {
356 	zend_long property, value, nameChoice = U_LONG_PROPERTY_NAME;
357 	const char *ret;
358 
359 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ll|l", &property, &value, &nameChoice) == FAILURE) {
360 		RETURN_THROWS();
361 	}
362 
363 	ret = u_getPropertyValueName((UProperty)property, value, (UPropertyNameChoice)nameChoice);
364 	if (ret) {
365 		RETURN_STRING(ret);
366 	} else {
367 		intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
368 		intl_error_set_custom_msg(NULL, "Failed to get property name", 0);
369 		RETURN_FALSE;
370 	}
371 }
372 /* }}} */
373 
374 /* {{{ */
IC_METHOD(getPropertyValueEnum)375 IC_METHOD(getPropertyValueEnum) {
376 	zend_long property;
377 	char *name;
378 	size_t name_len;
379 
380 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls", &property, &name, &name_len) == FAILURE) {
381 		RETURN_THROWS();
382 	}
383 
384 	RETURN_LONG(u_getPropertyValueEnum((UProperty)property, name));
385 }
386 /* }}} */
387 
388 /* {{{ */
IC_METHOD(foldCase)389 IC_METHOD(foldCase) {
390 	UChar32 cp, ret;
391 	zend_long options = U_FOLD_CASE_DEFAULT;
392 	zend_string *string_codepoint;
393 	zend_long int_codepoint;
394 
395 	ZEND_PARSE_PARAMETERS_START(1, 2)
396 		Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint)
397 		Z_PARAM_OPTIONAL
398 		Z_PARAM_LONG(options)
399 	ZEND_PARSE_PARAMETERS_END();
400 
401 	if (convert_cp(&cp, string_codepoint, int_codepoint) == FAILURE) {
402 		RETURN_NULL();
403 	}
404 
405 	ret = u_foldCase(cp, options);
406 	if (string_codepoint != NULL) {
407 		char buffer[5];
408 		int buffer_len = 0;
409 		U8_APPEND_UNSAFE(buffer, buffer_len, ret);
410 		buffer[buffer_len] = 0;
411 		RETURN_STRINGL(buffer, buffer_len);
412 	} else {
413 		RETURN_LONG(ret);
414 	}
415 }
416 /* }}} */
417 
418 /* {{{ */
IC_METHOD(digit)419 IC_METHOD(digit) {
420 	UChar32 cp;
421 	zend_long radix = 10;
422 	int ret;
423 	zend_string *string_codepoint;
424 	zend_long int_codepoint;
425 
426 	ZEND_PARSE_PARAMETERS_START(1, 2)
427 		Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint)
428 		Z_PARAM_OPTIONAL
429 		Z_PARAM_LONG(radix)
430 	ZEND_PARSE_PARAMETERS_END();
431 
432 	if (convert_cp(&cp, string_codepoint, int_codepoint) == FAILURE) {
433 		RETURN_NULL();
434 	}
435 
436 	ret = u_digit(cp, radix);
437 	if (ret < 0) {
438 		intl_error_set_code(NULL, U_ILLEGAL_ARGUMENT_ERROR);
439 		intl_error_set_custom_msg(NULL, "Invalid digit", 0);
440 		RETURN_FALSE;
441 	}
442 	RETURN_LONG(ret);
443 }
444 /* }}} */
445 
446 /* {{{ */
IC_METHOD(forDigit)447 IC_METHOD(forDigit) {
448 	zend_long digit, radix = 10;
449 
450 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &digit, &radix) == FAILURE) {
451 		RETURN_THROWS();
452 	}
453 
454 	RETURN_LONG(u_forDigit(digit, radix));
455 }
456 /* }}} */
457 
458 /* {{{ */
IC_METHOD(charAge)459 IC_METHOD(charAge) {
460 	UChar32 cp;
461 	UVersionInfo version;
462 	int i;
463 
464 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) {
465 		RETURN_NULL();
466 	}
467 
468 	u_charAge(cp, version);
469 	array_init(return_value);
470 	for(i = 0; i < U_MAX_VERSION_LENGTH; ++i) {
471 		add_next_index_long(return_value, version[i]);
472 	}
473 }
474 /* }}} */
475 
476 /* {{{ */
IC_METHOD(getUnicodeVersion)477 IC_METHOD(getUnicodeVersion) {
478 	UVersionInfo version;
479 	int i;
480 
481 	if (zend_parse_parameters_none() == FAILURE) {
482 		RETURN_THROWS();
483 	}
484 
485 	u_getUnicodeVersion(version);
486 	array_init(return_value);
487 	for(i = 0; i < U_MAX_VERSION_LENGTH; ++i) {
488 		add_next_index_long(return_value, version[i]);
489 	}
490 }
491 /* }}} */
492 
493 /* {{{ */
IC_METHOD(getFC_NFKC_Closure)494 IC_METHOD(getFC_NFKC_Closure) {
495 	UChar32 cp;
496 	UChar *closure;
497 	zend_string *u8str;
498 	int32_t closure_len;
499 	UErrorCode error = U_ZERO_ERROR;
500 
501 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) {
502 		RETURN_NULL();
503 	}
504 
505 	closure_len = u_getFC_NFKC_Closure(cp, NULL, 0, &error);
506 	if (closure_len == 0) {
507 		RETURN_EMPTY_STRING();
508 	}
509 	closure = safe_emalloc(sizeof(UChar), closure_len + 1, 0);
510 	error = U_ZERO_ERROR;
511 	closure_len = u_getFC_NFKC_Closure(cp, closure, closure_len, &error);
512 	if (U_FAILURE(error)) {
513 		efree(closure);
514 		INTL_CHECK_STATUS(error, "Failed getting closure");
515 	}
516 
517 	error = U_ZERO_ERROR;
518 	u8str = intl_convert_utf16_to_utf8(closure, closure_len, &error);
519 	INTL_CHECK_STATUS(error, "Failed converting output to UTF8");
520 	efree(closure);
521 	RETVAL_NEW_STR(u8str);
522 }
523 /* }}} */
524 
525 /* {{{ */
526 #define IC_BOOL_METHOD_CHAR(name) \
527 IC_METHOD(name) { \
528 	UChar32 cp; \
529 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) { \
530 		RETURN_NULL(); \
531 	} \
532 	RETURN_BOOL(u_##name(cp)); \
533 }
534 IC_BOOL_METHOD_CHAR(isUAlphabetic)
IC_BOOL_METHOD_CHAR(isULowercase)535 IC_BOOL_METHOD_CHAR(isULowercase)
536 IC_BOOL_METHOD_CHAR(isUUppercase)
537 IC_BOOL_METHOD_CHAR(isUWhiteSpace)
538 IC_BOOL_METHOD_CHAR(islower)
539 IC_BOOL_METHOD_CHAR(isupper)
540 IC_BOOL_METHOD_CHAR(istitle)
541 IC_BOOL_METHOD_CHAR(isdigit)
542 IC_BOOL_METHOD_CHAR(isalpha)
543 IC_BOOL_METHOD_CHAR(isalnum)
544 IC_BOOL_METHOD_CHAR(isxdigit)
545 IC_BOOL_METHOD_CHAR(ispunct)
546 IC_BOOL_METHOD_CHAR(isgraph)
547 IC_BOOL_METHOD_CHAR(isblank)
548 IC_BOOL_METHOD_CHAR(isdefined)
549 IC_BOOL_METHOD_CHAR(isspace)
550 IC_BOOL_METHOD_CHAR(isJavaSpaceChar)
551 IC_BOOL_METHOD_CHAR(isWhitespace)
552 IC_BOOL_METHOD_CHAR(iscntrl)
553 IC_BOOL_METHOD_CHAR(isISOControl)
554 IC_BOOL_METHOD_CHAR(isprint)
555 IC_BOOL_METHOD_CHAR(isbase)
556 IC_BOOL_METHOD_CHAR(isMirrored)
557 IC_BOOL_METHOD_CHAR(isIDStart)
558 IC_BOOL_METHOD_CHAR(isIDPart)
559 IC_BOOL_METHOD_CHAR(isIDIgnorable)
560 IC_BOOL_METHOD_CHAR(isJavaIDStart)
561 IC_BOOL_METHOD_CHAR(isJavaIDPart)
562 #undef IC_BOOL_METHOD_CHAR
563 /* }}} */
564 
565 /* {{{ */
566 #define IC_INT_METHOD_CHAR(name) \
567 IC_METHOD(name) { \
568 	UChar32 cp; \
569 	if (parse_code_point_param(INTERNAL_FUNCTION_PARAM_PASSTHRU, &cp) == FAILURE) { \
570 		RETURN_NULL(); \
571 	} \
572 	RETURN_LONG(u_##name(cp)); \
573 }
574 IC_INT_METHOD_CHAR(charDirection)
575 IC_INT_METHOD_CHAR(charType)
576 IC_INT_METHOD_CHAR(getCombiningClass)
577 IC_INT_METHOD_CHAR(charDigitValue)
578 #undef IC_INT_METHOD_CHAR
579 /* }}} */
580 
581 /* {{{ Returns a utf-8 character if codepoint was passed as a utf-8 sequence
582  * Returns an int otherwise
583  */
584 #define IC_CHAR_METHOD_CHAR(name) \
585 IC_METHOD(name) { \
586 	UChar32 cp, ret; \
587 	zend_string *string_codepoint; \
588 		zend_long int_codepoint = -1; \
589 		ZEND_PARSE_PARAMETERS_START(1, 1) \
590 			Z_PARAM_STR_OR_LONG(string_codepoint, int_codepoint) \
591 		ZEND_PARSE_PARAMETERS_END(); \
592 		if (convert_cp(&cp, string_codepoint, int_codepoint) == FAILURE) { \
593 			RETURN_NULL(); \
594 		} \
595 	ret = u_##name(cp); \
596 	if (string_codepoint != NULL) { \
597 		char buffer[5]; \
598 		int buffer_len = 0; \
599 		U8_APPEND_UNSAFE(buffer, buffer_len, ret); \
600 		buffer[buffer_len] = 0; \
601 		RETURN_STRINGL(buffer, buffer_len); \
602 	} else { \
603 		RETURN_LONG(ret); \
604 	} \
605 }
606 IC_CHAR_METHOD_CHAR(charMirror)
607 IC_CHAR_METHOD_CHAR(tolower)
608 IC_CHAR_METHOD_CHAR(toupper)
609 IC_CHAR_METHOD_CHAR(totitle)
610 #if U_ICU_VERSION_MAJOR_NUM >= 52
611 IC_CHAR_METHOD_CHAR(getBidiPairedBracket)
612 #endif /* ICU >= 52 */
613 #undef IC_CHAR_METHOD_CHAR
614 /* }}} */
615 
616 int php_uchar_minit(INIT_FUNC_ARGS) {
617 	register_class_IntlChar();
618 
619 	return SUCCESS;
620 }
621