1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
|
/*
+----------------------------------------------------------------------+
| PHP Version 6 |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andrei Zmievski <andrei@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php_unicode.h"
typedef UBool (*prop_check_func_t)(UChar32 ch);
typedef struct {
zval **args[4];
void ***thread_ctx;
zend_fcall_info fci;
zend_fcall_info_cache fci_cache;
} char_enum_context_t;
static void check_property_impl(INTERNAL_FUNCTION_PARAMETERS, prop_check_func_t checker)
{
UChar *str;
int str_len;
zend_bool result = 1;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
while (offset < str_len && result) {
U16_NEXT(str, offset, str_len, ch);
result = checker(ch);
}
RETURN_BOOL(result);
}
/* {{{ C/POSIX migration functions */
PHP_FUNCTION(char_is_lower)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_islower);
}
PHP_FUNCTION(char_is_upper)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isupper);
}
PHP_FUNCTION(char_is_digit)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isdigit);
}
PHP_FUNCTION(char_is_alpha)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isalpha);
}
PHP_FUNCTION(char_is_alnum)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isalnum);
}
PHP_FUNCTION(char_is_xdigit)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isxdigit);
}
PHP_FUNCTION(char_is_punct)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_ispunct);
}
PHP_FUNCTION(char_is_graph)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isgraph);
}
PHP_FUNCTION(char_is_blank)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isblank);
}
PHP_FUNCTION(char_is_space)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isspace);
}
PHP_FUNCTION(char_is_cntrl)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_iscntrl);
}
PHP_FUNCTION(char_is_print)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isprint);
}
/* }}} */
/* {{{ Additional binary property functions */
PHP_FUNCTION(char_is_defined)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isdefined);
}
PHP_FUNCTION(char_is_id_start)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isIDStart);
}
PHP_FUNCTION(char_is_id_part)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isIDPart);
}
PHP_FUNCTION(char_is_id_ignorable)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isIDIgnorable);
}
PHP_FUNCTION(char_is_iso_control)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isISOControl);
}
PHP_FUNCTION(char_is_mirrored)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isMirrored);
}
PHP_FUNCTION(char_is_base)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isbase);
}
PHP_FUNCTION(char_is_whitespace)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isUWhiteSpace);
}
PHP_FUNCTION(char_is_alphabetic)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isUAlphabetic);
}
PHP_FUNCTION(char_is_uppercase)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isUUppercase);
}
PHP_FUNCTION(char_is_lowercase)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_isULowercase);
}
PHP_FUNCTION(char_is_titlecase)
{
check_property_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, u_istitle);
}
/* }}} */
/* {{{ Single character properties */
PHP_FUNCTION(char_get_numeric_value)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
RETURN_DOUBLE(u_getNumericValue(ch));
}
PHP_FUNCTION(char_get_combining_class)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
RETURN_LONG((long)u_getCombiningClass(ch));
}
PHP_FUNCTION(char_get_digit_value)
{
UChar *str;
int str_len;
int radix = 0;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u|l", &str, &str_len, &radix) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
if (ZEND_NUM_ARGS() > 1) {
if (radix < 2 || radix > 36) {
php_error(E_WARNING, "Radix has to be in 2-36 range");
return;
}
RETURN_LONG(u_digit(ch, radix));
} else {
RETURN_LONG(u_charDigitValue(ch));
}
}
PHP_FUNCTION(char_get_mirrored)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
RETURN_UCHAR32(u_charMirror(ch));
}
PHP_FUNCTION(char_get_direction)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
RETURN_LONG((long)u_charDirection(ch));
}
PHP_FUNCTION(char_get_age)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
UVersionInfo version;
char buf[18] = { 0, };
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
u_charAge(ch, version);
u_versionToString(version, buf);
RETURN_ASCII_STRING(buf, ZSTR_DUPLICATE);
}
PHP_FUNCTION(char_get_type)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
RETURN_LONG(u_charType(ch));
}
PHP_FUNCTION(char_is_valid)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u", &str, &str_len) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
RETURN_BOOL(U_IS_UNICODE_CHAR(ch));
}
PHP_FUNCTION(char_from_digit)
{
int digit;
int radix = 10;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &digit, &radix) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() > 1) {
if (radix < 2 || radix > 36) {
php_error(E_WARNING, "Radix has to be in 2-36 range");
return;
}
}
ch = u_forDigit(digit, radix);
if (ch == (UChar32)0) {
RETURN_FALSE;
}
RETURN_UCHAR32(ch);
}
PHP_FUNCTION(char_from_name)
{
void *name;
int name_len;
zend_uchar name_type;
UChar32 ch;
UCharNameChoice choice = U_UNICODE_CHAR_NAME;
zend_bool extended = FALSE;
char *buf;
UErrorCode status = U_ZERO_ERROR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|b", &name, &name_len, &name_type, &extended) == FAILURE) {
return;
}
if (extended) {
choice = U_EXTENDED_CHAR_NAME;
}
if (name_type == IS_UNICODE) {
buf = zend_unicode_to_ascii(name, name_len TSRMLS_CC);
if (buf == NULL) {
php_error(E_WARNING, "Character name has to consist only of ASCII characters");
RETURN_FALSE;
}
} else {
buf = (char *) name;
}
ch = u_charFromName(choice, buf, &status);
if (name_type == IS_UNICODE) {
efree(buf);
}
if (U_SUCCESS(status)) {
RETURN_UCHAR32(ch);
} else {
RETURN_FALSE;
}
}
PHP_FUNCTION(char_get_name)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
zend_bool extended = FALSE;
UCharNameChoice choice = U_UNICODE_CHAR_NAME;
char *buf;
int buf_len = 128;
UErrorCode status = U_ZERO_ERROR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "u|b", &str, &str_len, &extended) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
if (extended) {
choice = U_EXTENDED_CHAR_NAME;
}
U16_NEXT(str, offset, str_len, ch);
buf = emalloc(buf_len);
buf_len = u_charName(ch, choice, buf, buf_len, &status);
if (buf_len == 0) {
efree(buf);
RETURN_FALSE;
} else if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
buf = erealloc(buf, buf_len+1);
buf_len = u_charName(ch, choice, buf, buf_len+1, &status);
}
RETURN_ASCII_STRINGL(buf, buf_len, ZSTR_AUTOFREE);
}
/* }}} */
/* {{{ Other property functions */
PHP_FUNCTION(char_has_binary_property)
{
UChar *str = NULL;
int str_len;
long prop;
UProperty uprop;
int offset = 0;
zend_bool result = 1;
UChar32 ch;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ul", &str, &str_len, &prop) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
uprop = (UProperty)prop;
while (offset < str_len && result) {
U16_NEXT(str, offset, str_len, ch);
result = u_hasBinaryProperty(ch, uprop);
}
RETURN_BOOL(result);
}
PHP_FUNCTION(char_get_property_value)
{
UChar *str;
int str_len;
int offset = 0;
UChar32 ch;
long prop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ul", &str, &str_len, &prop) == FAILURE) {
return;
}
if (str_len == 0) {
RETURN_FALSE;
}
U16_NEXT(str, offset, str_len, ch);
if (prop >= UCHAR_BINARY_START && prop < UCHAR_BINARY_LIMIT) {
RETURN_BOOL((zend_bool)u_getIntPropertyValue(ch, (UProperty)prop));
} else {
RETURN_LONG(u_getIntPropertyValue(ch, (UProperty)prop));
}
}
PHP_FUNCTION(char_get_property_min_value)
{
long prop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &prop) == FAILURE) {
return;
}
RETURN_LONG(u_getIntPropertyMinValue((UProperty)prop));
}
PHP_FUNCTION(char_get_property_max_value)
{
long prop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &prop) == FAILURE) {
return;
}
RETURN_LONG(u_getIntPropertyMaxValue((UProperty)prop));
}
PHP_FUNCTION(char_get_property_name)
{
long prop;
long name_choice = U_LONG_PROPERTY_NAME;
const char *name;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|l", &prop, &name_choice) == FAILURE) {
return;
}
if (name_choice < 0) {
name_choice = U_LONG_PROPERTY_NAME;
}
name = u_getPropertyName((UProperty) prop, (UPropertyNameChoice) name_choice);
if (name) {
RETURN_ASCII_STRING((char *)name, ZSTR_DUPLICATE);
} else {
RETURN_FALSE;
}
}
PHP_FUNCTION(char_get_property_from_name)
{
void *name;
int name_len;
zend_uchar name_type;
char *buf;
UProperty prop;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t", &name, &name_len, &name_type) == FAILURE) {
return;
}
if (name_type == IS_UNICODE) {
buf = zend_unicode_to_ascii(name, name_len TSRMLS_CC);
if (buf == NULL) {
php_error(E_WARNING, "Property name has to consist only of ASCII characters");
RETURN_FALSE;
}
} else {
buf = (char *) name;
}
prop = u_getPropertyEnum(buf);
if (name_type == IS_UNICODE) {
efree(buf);
}
RETURN_LONG(prop);
}
PHP_FUNCTION(char_get_property_value_name)
{
long prop;
long value;
long name_choice = U_LONG_PROPERTY_NAME;
const char *name;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll|l", &prop, &value, &name_choice) == FAILURE) {
return;
}
if (name_choice < 0) {
name_choice = U_LONG_PROPERTY_NAME;
}
name = u_getPropertyValueName((UProperty) prop, (int32_t) value, (UPropertyNameChoice) name_choice);
if (name) {
RETURN_ASCII_STRING((char *)name, ZSTR_DUPLICATE);
} else {
RETURN_FALSE;
}
}
PHP_FUNCTION(char_get_property_value_from_name)
{
long prop;
void *name;
int name_len;
zend_uchar name_type;
char *buf;
long value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lt", &prop, &name, &name_len, &name_type) == FAILURE) {
return;
}
if (name_type == IS_UNICODE) {
buf = zend_unicode_to_ascii(name, name_len TSRMLS_CC);
if (buf == NULL) {
php_error(E_WARNING, "Property value name has to consist only of ASCII characters");
RETURN_FALSE;
}
} else {
buf = (char *) name;
}
value = u_getPropertyValueEnum((UProperty)prop, buf);
if (name_type == IS_UNICODE) {
efree(buf);
}
RETURN_LONG(value);
}
/* }}} */
/* {{{ Enumerator functions */
static UBool php_enum_char_names(void *context,
UChar32 code,
UCharNameChoice nameChoice,
const char *name,
int32_t length)
{
char_enum_context_t *ctx = (char_enum_context_t *)context;
zval *retval_ptr = NULL;
int status;
UBool result = FALSE;
TSRMLS_FETCH_FROM_CTX(ctx->thread_ctx);
convert_to_long_ex(ctx->args[0]);
convert_to_unicode_ex(ctx->args[1]);
convert_to_boolean_ex(ctx->args[2]);
ZVAL_LONG(*ctx->args[0], code);
if (Z_USTRVAL_PP(ctx->args[1])) {
efree(Z_USTRVAL_PP(ctx->args[1]));
}
ZVAL_ASCII_STRINGL(*ctx->args[1], (char *)name, length, ZSTR_DUPLICATE);
ZVAL_BOOL(*ctx->args[2], nameChoice == U_EXTENDED_CHAR_NAME);
ctx->fci.retval_ptr_ptr = &retval_ptr;
status = zend_call_function(&ctx->fci, &ctx->fci_cache TSRMLS_CC);
if (status == SUCCESS && retval_ptr && !EG(exception)) {
convert_to_boolean(retval_ptr);
result = (UBool)Z_BVAL_P(retval_ptr);
} else {
if (!EG(exception)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Enumeration callback encountered an error");
}
result = FALSE;
}
if (retval_ptr) {
zval_ptr_dtor(&retval_ptr);
}
return result;
}
PHP_FUNCTION(char_enum_names)
{
zval *callback;
long start, limit;
zend_bool extended = FALSE;
zval *zcode, *zname, *zextended;
char_enum_context_t ectx;
UCharNameChoice choice = U_UNICODE_CHAR_NAME;
UErrorCode status = U_ZERO_ERROR;
if (zend_parse_parameters(ZEND_NUM_ARGS()TSRMLS_CC, "zll|l", &callback, &start, &limit, &extended)) {
return;
}
if (!zend_is_callable(callback, 0, NULL)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid enumeration callback");
return;
}
if (extended) {
choice = U_EXTENDED_CHAR_NAME;
}
/* Do all the heavy lifing once, instead of in the callback */
MAKE_STD_ZVAL(zcode);
MAKE_STD_ZVAL(zextended);
MAKE_STD_ZVAL(zname);
ZVAL_LONG(zcode, 0);
ZVAL_BOOL(zextended, 0);
Z_TYPE_P(zname) = IS_UNICODE;
Z_USTRVAL_P(zname) = NULL;
memset(&ectx, 0, sizeof(char_enum_context_t));
ectx.fci.size = sizeof(ectx.fci);
ectx.fci.function_table = EG(function_table);
ectx.fci.function_name = callback;
ectx.fci.no_separation = 1;
ectx.fci_cache = empty_fcall_info_cache;
ectx.args[0] = &zcode;
ectx.args[1] = &zname;
ectx.args[2] = &zextended;
ectx.fci.param_count = 3;
ectx.fci.params = ectx.args;
TSRMLS_SET_CTX(ectx.thread_ctx);
u_enumCharNames(start, limit, (UEnumCharNamesFn *)php_enum_char_names,
(void *)&ectx, choice, &status);
zval_ptr_dtor(&zcode);
zval_ptr_dtor(&zextended);
zval_ptr_dtor(&zname);
if (U_SUCCESS(status)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/
|