-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy patharray.c
6959 lines (6208 loc) · 192 KB
/
array.c
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| 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: |
| https://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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <[email protected]> |
| Zeev Suraski <[email protected]> |
| Rasmus Lerdorf <[email protected]> |
| Andrei Zmievski <[email protected]> |
| Stig Venaas <[email protected]> |
| Jason Greene <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_ini.h"
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "zend_globals.h"
#include "zend_interfaces.h"
#include "php_array.h"
#include "basic_functions.h"
#include "php_string.h"
#include "php_math.h"
#include "zend_smart_str.h"
#include "zend_bitset.h"
#include "zend_exceptions.h"
#include "ext/random/php_random.h"
#include "zend_frameless_function.h"
/* {{{ defines */
#define DIFF_NORMAL 1
#define DIFF_KEY 2
#define DIFF_ASSOC 6
#define DIFF_COMP_DATA_NONE -1
#define DIFF_COMP_DATA_INTERNAL 0
#define DIFF_COMP_DATA_USER 1
#define DIFF_COMP_KEY_INTERNAL 0
#define DIFF_COMP_KEY_USER 1
#define INTERSECT_NORMAL 1
#define INTERSECT_KEY 2
#define INTERSECT_ASSOC 6
#define INTERSECT_COMP_DATA_NONE -1
#define INTERSECT_COMP_DATA_INTERNAL 0
#define INTERSECT_COMP_DATA_USER 1
#define INTERSECT_COMP_KEY_INTERNAL 0
#define INTERSECT_COMP_KEY_USER 1
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(array)
/* {{{ php_array_init_globals */
static void php_array_init_globals(zend_array_globals *array_globals)
{
memset(array_globals, 0, sizeof(zend_array_globals));
}
/* }}} */
PHP_MINIT_FUNCTION(array) /* {{{ */
{
ZEND_INIT_MODULE_GLOBALS(array, php_array_init_globals, NULL);
return SUCCESS;
}
/* }}} */
PHP_MSHUTDOWN_FUNCTION(array) /* {{{ */
{
#ifdef ZTS
ts_free_id(array_globals_id);
#endif
return SUCCESS;
}
/* }}} */
static zend_never_inline ZEND_COLD int stable_sort_fallback(Bucket *a, Bucket *b) {
if (Z_EXTRA(a->val) > Z_EXTRA(b->val)) {
return 1;
} else if (Z_EXTRA(a->val) < Z_EXTRA(b->val)) {
return -1;
} else {
return 0;
}
}
#define RETURN_STABLE_SORT(a, b, result) do { \
int _result = (result); \
if (EXPECTED(_result)) { \
return _result; \
} \
return stable_sort_fallback((a), (b)); \
} while (0)
/* Generate inlined unstable and stable variants, and non-inlined reversed variants. */
#define DEFINE_SORT_VARIANTS(name) \
static zend_never_inline int php_array_##name##_unstable(Bucket *a, Bucket *b) { \
return php_array_##name##_unstable_i(a, b); \
} \
static zend_never_inline int php_array_##name(Bucket *a, Bucket *b) { \
RETURN_STABLE_SORT(a, b, php_array_##name##_unstable_i(a, b)); \
} \
static zend_never_inline int php_array_reverse_##name##_unstable(Bucket *a, Bucket *b) { \
return php_array_##name##_unstable(a, b) * -1; \
} \
static zend_never_inline int php_array_reverse_##name(Bucket *a, Bucket *b) { \
RETURN_STABLE_SORT(a, b, php_array_reverse_##name##_unstable(a, b)); \
} \
static zend_always_inline int php_array_key_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
zval first;
zval second;
if (f->key == NULL && s->key == NULL) {
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
} else if (f->key && s->key) {
return zendi_smart_strcmp(f->key, s->key);
}
if (f->key) {
ZVAL_STR(&first, f->key);
} else {
ZVAL_LONG(&first, f->h);
}
if (s->key) {
ZVAL_STR(&second, s->key);
} else {
ZVAL_LONG(&second, s->h);
}
return zend_compare(&first, &second);
}
/* }}} */
static zend_always_inline int php_array_key_compare_numeric_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
if (f->key == NULL && s->key == NULL) {
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
} else {
double d1, d2;
if (f->key) {
d1 = zend_strtod(f->key->val, NULL);
} else {
d1 = (double)(zend_long)f->h;
}
if (s->key) {
d2 = zend_strtod(s->key->val, NULL);
} else {
d2 = (double)(zend_long)s->h;
}
return ZEND_THREEWAY_COMPARE(d1, d2);
}
}
/* }}} */
static zend_always_inline int php_array_key_compare_string_case_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
const char *s1, *s2;
size_t l1, l2;
char buf1[MAX_LENGTH_OF_LONG + 1];
char buf2[MAX_LENGTH_OF_LONG + 1];
if (f->key) {
s1 = f->key->val;
l1 = f->key->len;
} else {
s1 = zend_print_long_to_buf(buf1 + sizeof(buf1) - 1, f->h);
l1 = buf1 + sizeof(buf1) - 1 - s1;
}
if (s->key) {
s2 = s->key->val;
l2 = s->key->len;
} else {
s2 = zend_print_long_to_buf(buf2 + sizeof(buf2) - 1, s->h);
l2 = buf2 + sizeof(buf2) - 1 - s2;
}
return zend_binary_strcasecmp_l(s1, l1, s2, l2);
}
/* }}} */
static zend_always_inline int php_array_key_compare_string_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
const char *s1, *s2;
size_t l1, l2;
char buf1[MAX_LENGTH_OF_LONG + 1];
char buf2[MAX_LENGTH_OF_LONG + 1];
if (f->key) {
s1 = f->key->val;
l1 = f->key->len;
} else {
s1 = zend_print_long_to_buf(buf1 + sizeof(buf1) - 1, f->h);
l1 = buf1 + sizeof(buf1) - 1 - s1;
}
if (s->key) {
s2 = s->key->val;
l2 = s->key->len;
} else {
s2 = zend_print_long_to_buf(buf2 + sizeof(buf2) - 1, s->h);
l2 = buf2 + sizeof(buf2) - 1 - s2;
}
return zend_binary_strcmp(s1, l1, s2, l2);
}
/* }}} */
static int php_array_key_compare_string_natural_general(Bucket *f, Bucket *s, int fold_case) /* {{{ */
{
const char *s1, *s2;
size_t l1, l2;
char buf1[MAX_LENGTH_OF_LONG + 1];
char buf2[MAX_LENGTH_OF_LONG + 1];
if (f->key) {
s1 = f->key->val;
l1 = f->key->len;
} else {
s1 = zend_print_long_to_buf(buf1 + sizeof(buf1) - 1, f->h);
l1 = buf1 + sizeof(buf1) - 1 - s1;
}
if (s->key) {
s2 = s->key->val;
l2 = s->key->len;
} else {
s2 = zend_print_long_to_buf(buf2 + sizeof(buf2) - 1, s->h);
l2 = buf2 + sizeof(buf2) - 1 - s2;
}
return strnatcmp_ex(s1, l1, s2, l2, fold_case);
}
/* }}} */
static int php_array_key_compare_string_natural_case(Bucket *a, Bucket *b) /* {{{ */
{
RETURN_STABLE_SORT(a, b, php_array_key_compare_string_natural_general(a, b, 1));
}
/* }}} */
static int php_array_reverse_key_compare_string_natural_case(Bucket *a, Bucket *b) /* {{{ */
{
RETURN_STABLE_SORT(a, b, php_array_key_compare_string_natural_general(b, a, 1));
}
/* }}} */
static int php_array_key_compare_string_natural(Bucket *a, Bucket *b) /* {{{ */
{
RETURN_STABLE_SORT(a, b, php_array_key_compare_string_natural_general(a, b, 0));
}
/* }}} */
static int php_array_reverse_key_compare_string_natural(Bucket *a, Bucket *b) /* {{{ */
{
RETURN_STABLE_SORT(a, b, php_array_key_compare_string_natural_general(b, a, 0));
}
/* }}} */
static zend_always_inline int php_array_key_compare_string_locale_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
const char *s1, *s2;
char buf1[MAX_LENGTH_OF_LONG + 1];
char buf2[MAX_LENGTH_OF_LONG + 1];
if (f->key) {
s1 = f->key->val;
} else {
s1 = zend_print_long_to_buf(buf1 + sizeof(buf1) - 1, f->h);
}
if (s->key) {
s2 = s->key->val;
} else {
s2 = zend_print_long_to_buf(buf2 + sizeof(buf2) - 1, s->h);
}
return strcoll(s1, s2);
}
/* }}} */
static zend_always_inline int php_array_data_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
int result = zend_compare(&f->val, &s->val);
/* Special enums handling for array_unique. We don't want to add this logic to zend_compare as
* that would be observable via comparison operators. */
zval *rhs = &s->val;
ZVAL_DEREF(rhs);
if (UNEXPECTED(Z_TYPE_P(rhs) == IS_OBJECT)
&& result == ZEND_UNCOMPARABLE
&& (Z_OBJCE_P(rhs)->ce_flags & ZEND_ACC_ENUM)) {
zval *lhs = &f->val;
ZVAL_DEREF(lhs);
if (Z_TYPE_P(lhs) == IS_OBJECT && (Z_OBJCE_P(lhs)->ce_flags & ZEND_ACC_ENUM)) {
// Order doesn't matter, we just need to group the same enum values
uintptr_t lhs_uintptr = (uintptr_t)Z_OBJ_P(lhs);
uintptr_t rhs_uintptr = (uintptr_t)Z_OBJ_P(rhs);
return lhs_uintptr == rhs_uintptr ? 0 : (lhs_uintptr < rhs_uintptr ? -1 : 1);
} else {
// Shift enums to the end of the array
return -1;
}
}
return result;
}
/* }}} */
static zend_always_inline int php_array_data_compare_numeric_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
return numeric_compare_function(&f->val, &s->val);
}
/* }}} */
static zend_always_inline int php_array_data_compare_string_case_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
return string_case_compare_function(&f->val, &s->val);
}
/* }}} */
static zend_always_inline int php_array_data_compare_string_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
return string_compare_function(&f->val, &s->val);
}
/* }}} */
static int php_array_natural_general_compare(Bucket *f, Bucket *s, int fold_case) /* {{{ */
{
zend_string *tmp_str1, *tmp_str2;
zend_string *str1 = zval_get_tmp_string(&f->val, &tmp_str1);
zend_string *str2 = zval_get_tmp_string(&s->val, &tmp_str2);
int result = strnatcmp_ex(ZSTR_VAL(str1), ZSTR_LEN(str1), ZSTR_VAL(str2), ZSTR_LEN(str2), fold_case);
zend_tmp_string_release(tmp_str1);
zend_tmp_string_release(tmp_str2);
return result;
}
/* }}} */
static zend_always_inline int php_array_natural_compare_unstable_i(Bucket *a, Bucket *b) /* {{{ */
{
return php_array_natural_general_compare(a, b, 0);
}
/* }}} */
static zend_always_inline int php_array_natural_case_compare_unstable_i(Bucket *a, Bucket *b) /* {{{ */
{
return php_array_natural_general_compare(a, b, 1);
}
/* }}} */
static int php_array_data_compare_string_locale_unstable_i(Bucket *f, Bucket *s) /* {{{ */
{
return string_locale_compare_function(&f->val, &s->val);
}
/* }}} */
DEFINE_SORT_VARIANTS(key_compare);
DEFINE_SORT_VARIANTS(key_compare_numeric);
DEFINE_SORT_VARIANTS(key_compare_string_case);
DEFINE_SORT_VARIANTS(key_compare_string);
DEFINE_SORT_VARIANTS(key_compare_string_locale);
DEFINE_SORT_VARIANTS(data_compare);
DEFINE_SORT_VARIANTS(data_compare_numeric);
DEFINE_SORT_VARIANTS(data_compare_string_case);
DEFINE_SORT_VARIANTS(data_compare_string);
DEFINE_SORT_VARIANTS(data_compare_string_locale);
DEFINE_SORT_VARIANTS(natural_compare);
DEFINE_SORT_VARIANTS(natural_case_compare);
static bucket_compare_func_t php_get_key_compare_func(zend_long sort_type, int reverse) /* {{{ */
{
switch (sort_type & ~PHP_SORT_FLAG_CASE) {
case PHP_SORT_NUMERIC:
if (reverse) {
return php_array_reverse_key_compare_numeric;
} else {
return php_array_key_compare_numeric;
}
break;
case PHP_SORT_STRING:
if (sort_type & PHP_SORT_FLAG_CASE) {
if (reverse) {
return php_array_reverse_key_compare_string_case;
} else {
return php_array_key_compare_string_case;
}
} else {
if (reverse) {
return php_array_reverse_key_compare_string;
} else {
return php_array_key_compare_string;
}
}
break;
case PHP_SORT_NATURAL:
if (sort_type & PHP_SORT_FLAG_CASE) {
if (reverse) {
return php_array_reverse_key_compare_string_natural_case;
} else {
return php_array_key_compare_string_natural_case;
}
} else {
if (reverse) {
return php_array_reverse_key_compare_string_natural;
} else {
return php_array_key_compare_string_natural;
}
}
break;
case PHP_SORT_LOCALE_STRING:
if (reverse) {
return php_array_reverse_key_compare_string_locale;
} else {
return php_array_key_compare_string_locale;
}
break;
case PHP_SORT_REGULAR:
default:
if (reverse) {
return php_array_reverse_key_compare;
} else {
return php_array_key_compare;
}
break;
}
return NULL;
}
/* }}} */
static bucket_compare_func_t php_get_data_compare_func(zend_long sort_type, int reverse) /* {{{ */
{
switch (sort_type & ~PHP_SORT_FLAG_CASE) {
case PHP_SORT_NUMERIC:
if (reverse) {
return php_array_reverse_data_compare_numeric;
} else {
return php_array_data_compare_numeric;
}
break;
case PHP_SORT_STRING:
if (sort_type & PHP_SORT_FLAG_CASE) {
if (reverse) {
return php_array_reverse_data_compare_string_case;
} else {
return php_array_data_compare_string_case;
}
} else {
if (reverse) {
return php_array_reverse_data_compare_string;
} else {
return php_array_data_compare_string;
}
}
break;
case PHP_SORT_NATURAL:
if (sort_type & PHP_SORT_FLAG_CASE) {
if (reverse) {
return php_array_reverse_natural_case_compare;
} else {
return php_array_natural_case_compare;
}
} else {
if (reverse) {
return php_array_reverse_natural_compare;
} else {
return php_array_natural_compare;
}
}
break;
case PHP_SORT_LOCALE_STRING:
if (reverse) {
return php_array_reverse_data_compare_string_locale;
} else {
return php_array_data_compare_string_locale;
}
break;
case PHP_SORT_REGULAR:
default:
if (reverse) {
return php_array_reverse_data_compare;
} else {
return php_array_data_compare;
}
break;
}
return NULL;
}
/* }}} */
static bucket_compare_func_t php_get_data_compare_func_unstable(zend_long sort_type, int reverse) /* {{{ */
{
switch (sort_type & ~PHP_SORT_FLAG_CASE) {
case PHP_SORT_NUMERIC:
if (reverse) {
return php_array_reverse_data_compare_numeric_unstable;
} else {
return php_array_data_compare_numeric_unstable;
}
break;
case PHP_SORT_STRING:
if (sort_type & PHP_SORT_FLAG_CASE) {
if (reverse) {
return php_array_reverse_data_compare_string_case_unstable;
} else {
return php_array_data_compare_string_case_unstable;
}
} else {
if (reverse) {
return php_array_reverse_data_compare_string_unstable;
} else {
return php_array_data_compare_string_unstable;
}
}
break;
case PHP_SORT_NATURAL:
if (sort_type & PHP_SORT_FLAG_CASE) {
if (reverse) {
return php_array_reverse_natural_case_compare_unstable;
} else {
return php_array_natural_case_compare_unstable;
}
} else {
if (reverse) {
return php_array_reverse_natural_compare_unstable;
} else {
return php_array_natural_compare_unstable;
}
}
break;
case PHP_SORT_LOCALE_STRING:
if (reverse) {
return php_array_reverse_data_compare_string_locale_unstable;
} else {
return php_array_data_compare_string_locale_unstable;
}
break;
case PHP_SORT_REGULAR:
default:
if (reverse) {
return php_array_reverse_data_compare_unstable;
} else {
return php_array_data_compare_unstable;
}
break;
}
return NULL;
}
/* }}} */
/* {{{ Sort an array by key value in reverse order */
PHP_FUNCTION(krsort)
{
zval *array;
zend_long sort_type = PHP_SORT_REGULAR;
bucket_compare_func_t cmp;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_EX(array, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_type)
ZEND_PARSE_PARAMETERS_END();
cmp = php_get_key_compare_func(sort_type, 1);
zend_hash_sort(Z_ARRVAL_P(array), cmp, 0);
RETURN_TRUE;
}
/* }}} */
/* {{{ Sort an array by key */
PHP_FUNCTION(ksort)
{
zval *array;
zend_long sort_type = PHP_SORT_REGULAR;
bucket_compare_func_t cmp;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_EX(array, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_type)
ZEND_PARSE_PARAMETERS_END();
cmp = php_get_key_compare_func(sort_type, 0);
zend_hash_sort(Z_ARRVAL_P(array), cmp, 0);
RETURN_TRUE;
}
/* }}} */
PHPAPI zend_long php_count_recursive(HashTable *ht) /* {{{ */
{
zend_long cnt = 0;
zval *element;
if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
if (GC_IS_RECURSIVE(ht)) {
php_error_docref(NULL, E_WARNING, "Recursion detected");
return 0;
}
GC_PROTECT_RECURSION(ht);
}
cnt = zend_hash_num_elements(ht);
ZEND_HASH_FOREACH_VAL(ht, element) {
ZVAL_DEREF(element);
if (Z_TYPE_P(element) == IS_ARRAY) {
cnt += php_count_recursive(Z_ARRVAL_P(element));
}
} ZEND_HASH_FOREACH_END();
GC_TRY_UNPROTECT_RECURSION(ht);
return cnt;
}
/* }}} */
/* Consumes `zv` */
static zend_always_inline zend_long php_get_long(zval *zv)
{
if (EXPECTED(Z_TYPE_P(zv) == IS_LONG)) {
return Z_LVAL_P(zv);
} else {
zend_long ret = zval_get_long_func(zv, false);
zval_ptr_dtor(zv);
return ret;
}
}
/* {{{ Count the number of elements in a variable (usually an array) */
PHP_FUNCTION(count)
{
zval *array;
zend_long mode = PHP_COUNT_NORMAL;
zend_long cnt;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ZVAL(array)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(mode)
ZEND_PARSE_PARAMETERS_END();
if (mode != PHP_COUNT_NORMAL && mode != PHP_COUNT_RECURSIVE) {
zend_argument_value_error(2, "must be either COUNT_NORMAL or COUNT_RECURSIVE");
RETURN_THROWS();
}
switch (Z_TYPE_P(array)) {
case IS_ARRAY:
if (mode != PHP_COUNT_RECURSIVE) {
cnt = zend_hash_num_elements(Z_ARRVAL_P(array));
} else {
cnt = php_count_recursive(Z_ARRVAL_P(array));
}
RETURN_LONG(cnt);
break;
case IS_OBJECT: {
zval retval;
/* first, we check if the handler is defined */
zend_object *zobj = Z_OBJ_P(array);
if (zobj->handlers->count_elements) {
RETVAL_LONG(1);
if (SUCCESS == zobj->handlers->count_elements(zobj, &Z_LVAL_P(return_value))) {
return;
}
if (EG(exception)) {
RETURN_THROWS();
}
}
/* if not and the object implements Countable we call its count() method */
if (instanceof_function(zobj->ce, zend_ce_countable)) {
zend_function *count_fn = zend_hash_find_ptr(&zobj->ce->function_table, ZSTR_KNOWN(ZEND_STR_COUNT));
zend_call_known_instance_method_with_0_params(count_fn, zobj, &retval);
if (Z_TYPE(retval) != IS_UNDEF) {
RETVAL_LONG(php_get_long(&retval));
}
return;
}
}
ZEND_FALLTHROUGH;
default:
zend_argument_type_error(1, "must be of type Countable|array, %s given", zend_zval_value_name(array));
RETURN_THROWS();
}
}
/* }}} */
static void php_natsort(INTERNAL_FUNCTION_PARAMETERS, int fold_case) /* {{{ */
{
zval *array;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_ARRAY_EX(array, 0, 1)
ZEND_PARSE_PARAMETERS_END();
if (fold_case) {
zend_array_sort(Z_ARRVAL_P(array), php_array_natural_case_compare, 0);
} else {
zend_array_sort(Z_ARRVAL_P(array), php_array_natural_compare, 0);
}
RETURN_TRUE;
}
/* }}} */
/* {{{ Sort an array using natural sort */
PHP_FUNCTION(natsort)
{
php_natsort(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Sort an array using case-insensitive natural sort */
PHP_FUNCTION(natcasesort)
{
php_natsort(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ Sort an array and maintain index association */
PHP_FUNCTION(asort)
{
zval *array;
zend_long sort_type = PHP_SORT_REGULAR;
bucket_compare_func_t cmp;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_EX(array, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_type)
ZEND_PARSE_PARAMETERS_END();
cmp = php_get_data_compare_func(sort_type, 0);
zend_array_sort(Z_ARRVAL_P(array), cmp, 0);
RETURN_TRUE;
}
/* }}} */
/* {{{ Sort an array in reverse order and maintain index association */
PHP_FUNCTION(arsort)
{
zval *array;
zend_long sort_type = PHP_SORT_REGULAR;
bucket_compare_func_t cmp;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_EX(array, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_type)
ZEND_PARSE_PARAMETERS_END();
cmp = php_get_data_compare_func(sort_type, 1);
zend_array_sort(Z_ARRVAL_P(array), cmp, 0);
RETURN_TRUE;
}
/* }}} */
/* {{{ Sort an array */
PHP_FUNCTION(sort)
{
zval *array;
zend_long sort_type = PHP_SORT_REGULAR;
bucket_compare_func_t cmp;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_EX(array, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_type)
ZEND_PARSE_PARAMETERS_END();
cmp = php_get_data_compare_func(sort_type, 0);
zend_array_sort(Z_ARRVAL_P(array), cmp, 1);
RETURN_TRUE;
}
/* }}} */
/* {{{ Sort an array in reverse order */
PHP_FUNCTION(rsort)
{
zval *array;
zend_long sort_type = PHP_SORT_REGULAR;
bucket_compare_func_t cmp;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_ARRAY_EX(array, 0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(sort_type)
ZEND_PARSE_PARAMETERS_END();
cmp = php_get_data_compare_func(sort_type, 1);
zend_array_sort(Z_ARRVAL_P(array), cmp, 1);
RETURN_TRUE;
}
/* }}} */
static inline int php_array_user_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
{
zval args[2];
zval retval;
ZVAL_COPY_VALUE(&args[0], &f->val);
ZVAL_COPY_VALUE(&args[1], &s->val);
BG(user_compare_fci).param_count = 2;
BG(user_compare_fci).params = args;
BG(user_compare_fci).retval = &retval;
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
if (UNEXPECTED(Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE)) {
if (!ARRAYG(compare_deprecation_thrown)) {
php_error_docref(NULL, E_DEPRECATED,
"Returning bool from comparison function is deprecated, "
"return an integer less than, equal to, or greater than zero");
ARRAYG(compare_deprecation_thrown) = 1;
}
if (Z_TYPE(retval) == IS_FALSE) {
/* Retry with swapped operands. */
ZVAL_COPY_VALUE(&args[0], &s->val);
ZVAL_COPY_VALUE(&args[1], &f->val);
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
zend_long ret = php_get_long(&retval);
return -ZEND_NORMALIZE_BOOL(ret);
}
}
zend_long ret = php_get_long(&retval);
return ZEND_NORMALIZE_BOOL(ret);
}
/* }}} */
static int php_array_user_compare(Bucket *a, Bucket *b) /* {{{ */
{
RETURN_STABLE_SORT(a, b, php_array_user_compare_unstable(a, b));
}
/* }}} */
#define PHP_ARRAY_CMP_FUNC_VARS \
zend_fcall_info old_user_compare_fci; \
zend_fcall_info_cache old_user_compare_fci_cache \
#define PHP_ARRAY_CMP_FUNC_BACKUP() \
old_user_compare_fci = BG(user_compare_fci); \
old_user_compare_fci_cache = BG(user_compare_fci_cache); \
ARRAYG(compare_deprecation_thrown) = 0; \
BG(user_compare_fci_cache) = empty_fcall_info_cache; \
#define PHP_ARRAY_CMP_FUNC_RESTORE() \
BG(user_compare_fci) = old_user_compare_fci; \
BG(user_compare_fci_cache) = old_user_compare_fci_cache; \
static void php_usort(INTERNAL_FUNCTION_PARAMETERS, bucket_compare_func_t compare_func, bool renumber) /* {{{ */
{
zval *array;
zend_array *arr;
PHP_ARRAY_CMP_FUNC_VARS;
PHP_ARRAY_CMP_FUNC_BACKUP();
ZEND_PARSE_PARAMETERS_START(2, 2)
Z_PARAM_ARRAY_EX2(array, 0, 1, 0)
Z_PARAM_FUNC(BG(user_compare_fci), BG(user_compare_fci_cache))
ZEND_PARSE_PARAMETERS_END_EX( PHP_ARRAY_CMP_FUNC_RESTORE(); return );
arr = Z_ARR_P(array);
if (zend_hash_num_elements(arr) == 0) {
PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_TRUE;
}
/* Copy array, so the in-place modifications will not be visible to the callback function */
arr = zend_array_dup(arr);
zend_array_sort(arr, compare_func, renumber);
zval garbage;
ZVAL_COPY_VALUE(&garbage, array);
ZVAL_ARR(array, arr);
zval_ptr_dtor(&garbage);
PHP_ARRAY_CMP_FUNC_RESTORE();
RETURN_TRUE;
}
/* }}} */
/* {{{ Sort an array by values using a user-defined comparison function */
PHP_FUNCTION(usort)
{
php_usort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_array_user_compare, 1);
}
/* }}} */
/* {{{ Sort an array with a user-defined comparison function and maintain index association */
PHP_FUNCTION(uasort)
{
php_usort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_array_user_compare, 0);
}
/* }}} */
static inline int php_array_user_key_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
{
zval args[2];
zval retval;
if (f->key == NULL) {
ZVAL_LONG(&args[0], f->h);
} else {
ZVAL_STR(&args[0], f->key);
}
if (s->key == NULL) {
ZVAL_LONG(&args[1], s->h);
} else {
ZVAL_STR(&args[1], s->key);
}
BG(user_compare_fci).param_count = 2;
BG(user_compare_fci).params = args;
BG(user_compare_fci).retval = &retval;
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
if (UNEXPECTED(Z_TYPE(retval) == IS_FALSE || Z_TYPE(retval) == IS_TRUE)) {
if (!ARRAYG(compare_deprecation_thrown)) {
php_error_docref(NULL, E_DEPRECATED,
"Returning bool from comparison function is deprecated, "
"return an integer less than, equal to, or greater than zero");
ARRAYG(compare_deprecation_thrown) = 1;
}
if (Z_TYPE(retval) == IS_FALSE) {
/* Retry with swapped operands. */
if (s->key == NULL) {
ZVAL_LONG(&args[0], s->h);
} else {
ZVAL_STR(&args[0], s->key);
}
if (f->key == NULL) {
ZVAL_LONG(&args[1], f->h);
} else {
ZVAL_STR(&args[1], f->key);
}
zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache));
zend_long ret = php_get_long(&retval);
return -ZEND_NORMALIZE_BOOL(ret);
}
}
zend_long result = php_get_long(&retval);
return ZEND_NORMALIZE_BOOL(result);
}
/* }}} */
static int php_array_user_key_compare(Bucket *a, Bucket *b) /* {{{ */
{
RETURN_STABLE_SORT(a, b, php_array_user_key_compare_unstable(a, b));
}
/* }}} */
/* {{{ Sort an array by keys using a user-defined comparison function */
PHP_FUNCTION(uksort)
{
php_usort(INTERNAL_FUNCTION_PARAM_PASSTHRU, php_array_user_key_compare, 0);
}
/* }}} */
static inline HashTable *get_ht_for_iap(zval *zv, bool separate) {
if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) {
return Z_ARRVAL_P(zv);
}
ZEND_ASSERT(Z_TYPE_P(zv) == IS_OBJECT);
php_error_docref(NULL, E_DEPRECATED,
"Calling %s() on an object is deprecated", get_active_function_name());