-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
array.c
4533 lines (3918 loc) · 132 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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2013 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: |
| 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: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
| Rasmus Lerdorf <rasmus@php.net> |
| Andrei Zmievski <andrei@php.net> |
| Stig Venaas <venaas@php.net> |
| Jason Greene <jason@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "php.h"
#include "php_ini.h"
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#if HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#ifdef PHP_WIN32
#include "win32/unistd.h"
#endif
#include "zend_globals.h"
#include "zend_interfaces.h"
#include "php_globals.h"
#include "php_array.h"
#include "basic_functions.h"
#include "php_string.h"
#include "php_rand.h"
#include "php_smart_str.h"
#ifdef HAVE_SPL
#include "ext/spl/spl_array.h"
#endif
/* {{{ defines */
#define EXTR_OVERWRITE 0
#define EXTR_SKIP 1
#define EXTR_PREFIX_SAME 2
#define EXTR_PREFIX_ALL 3
#define EXTR_PREFIX_INVALID 4
#define EXTR_PREFIX_IF_EXISTS 5
#define EXTR_IF_EXISTS 6
#define EXTR_REFS 0x100
#define CASE_LOWER 0
#define CASE_UPPER 1
#define COUNT_NORMAL 0
#define COUNT_RECURSIVE 1
#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
#define DOUBLE_DRIFT_FIX 0.000000000000001
/* }}} */
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);
REGISTER_LONG_CONSTANT("EXTR_OVERWRITE", EXTR_OVERWRITE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_SKIP", EXTR_SKIP, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_PREFIX_SAME", EXTR_PREFIX_SAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_PREFIX_ALL", EXTR_PREFIX_ALL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_PREFIX_INVALID", EXTR_PREFIX_INVALID, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_PREFIX_IF_EXISTS", EXTR_PREFIX_IF_EXISTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_IF_EXISTS", EXTR_IF_EXISTS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("EXTR_REFS", EXTR_REFS, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_ASC", PHP_SORT_ASC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_DESC", PHP_SORT_DESC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_REGULAR", PHP_SORT_REGULAR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_NUMERIC", PHP_SORT_NUMERIC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_STRING", PHP_SORT_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_LOCALE_STRING", PHP_SORT_LOCALE_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("CASE_LOWER", CASE_LOWER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("CASE_UPPER", CASE_UPPER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("COUNT_NORMAL", COUNT_NORMAL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("COUNT_RECURSIVE", COUNT_RECURSIVE, CONST_CS | CONST_PERSISTENT);
return SUCCESS;
}
/* }}} */
PHP_MSHUTDOWN_FUNCTION(array) /* {{{ */
{
#ifdef ZTS
ts_free_id(array_globals_id);
#endif
return SUCCESS;
}
/* }}} */
static void php_set_compare_func(int sort_type TSRMLS_DC) /* {{{ */
{
switch (sort_type) {
case PHP_SORT_NUMERIC:
ARRAYG(compare_func) = numeric_compare_function;
break;
case PHP_SORT_STRING:
ARRAYG(compare_func) = string_compare_function;
break;
#if HAVE_STRCOLL
case PHP_SORT_LOCALE_STRING:
ARRAYG(compare_func) = string_locale_compare_function;
break;
#endif
case PHP_SORT_REGULAR:
default:
ARRAYG(compare_func) = compare_function;
break;
}
}
/* }}} */
static int php_array_key_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
Bucket *f;
Bucket *s;
zval result;
zval first;
zval second;
f = *((Bucket **) a);
s = *((Bucket **) b);
if (f->nKeyLength == 0) {
Z_TYPE(first) = IS_LONG;
Z_LVAL(first) = f->h;
} else {
Z_TYPE(first) = IS_STRING;
Z_STRVAL(first) = f->arKey;
Z_STRLEN(first) = f->nKeyLength - 1;
}
if (s->nKeyLength == 0) {
Z_TYPE(second) = IS_LONG;
Z_LVAL(second) = s->h;
} else {
Z_TYPE(second) = IS_STRING;
Z_STRVAL(second) = s->arKey;
Z_STRLEN(second) = s->nKeyLength - 1;
}
if (ARRAYG(compare_func)(&result, &first, &second TSRMLS_CC) == FAILURE) {
return 0;
}
if (Z_TYPE(result) == IS_DOUBLE) {
if (Z_DVAL(result) < 0) {
return -1;
} else if (Z_DVAL(result) > 0) {
return 1;
} else {
return 0;
}
}
convert_to_long(&result);
if (Z_LVAL(result) < 0) {
return -1;
} else if (Z_LVAL(result) > 0) {
return 1;
}
return 0;
}
/* }}} */
static int php_array_reverse_key_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
return php_array_key_compare(a, b TSRMLS_CC) * -1;
}
/* }}} */
/* {{{ proto bool krsort(array &array_arg [, int sort_flags])
Sort an array by key value in reverse order */
PHP_FUNCTION(krsort)
{
zval *array;
long sort_type = PHP_SORT_REGULAR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
RETURN_FALSE;
}
php_set_compare_func(sort_type TSRMLS_CC);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_reverse_key_compare, 0 TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool ksort(array &array_arg [, int sort_flags])
Sort an array by key */
PHP_FUNCTION(ksort)
{
zval *array;
long sort_type = PHP_SORT_REGULAR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
RETURN_FALSE;
}
php_set_compare_func(sort_type TSRMLS_CC);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_key_compare, 0 TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
static int php_count_recursive(zval *array, long mode TSRMLS_DC) /* {{{ */
{
long cnt = 0;
zval **element;
if (Z_TYPE_P(array) == IS_ARRAY) {
if (Z_ARRVAL_P(array)->nApplyCount > 1) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
return 0;
}
cnt = zend_hash_num_elements(Z_ARRVAL_P(array));
if (mode == COUNT_RECURSIVE) {
HashPosition pos;
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(array), (void **) &element, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(array), &pos)
) {
Z_ARRVAL_P(array)->nApplyCount++;
cnt += php_count_recursive(*element, COUNT_RECURSIVE TSRMLS_CC);
Z_ARRVAL_P(array)->nApplyCount--;
}
}
}
return cnt;
}
/* }}} */
/* {{{ proto int count(mixed var [, int mode])
Count the number of elements in a variable (usually an array) */
PHP_FUNCTION(count)
{
zval *array;
long mode = COUNT_NORMAL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|l", &array, &mode) == FAILURE) {
return;
}
switch (Z_TYPE_P(array)) {
case IS_NULL:
RETURN_LONG(0);
break;
case IS_ARRAY:
RETURN_LONG (php_count_recursive (array, mode TSRMLS_CC));
break;
case IS_OBJECT: {
#ifdef HAVE_SPL
zval *retval;
#endif
/* first, we check if the handler is defined */
if (Z_OBJ_HT_P(array)->count_elements) {
RETVAL_LONG(1);
if (SUCCESS == Z_OBJ_HT(*array)->count_elements(array, &Z_LVAL_P(return_value) TSRMLS_CC)) {
return;
}
}
#ifdef HAVE_SPL
/* if not and the object implements Countable we call its count() method */
if (Z_OBJ_HT_P(array)->get_class_entry && instanceof_function(Z_OBJCE_P(array), spl_ce_Countable TSRMLS_CC)) {
zend_call_method_with_0_params(&array, NULL, NULL, "count", &retval);
if (retval) {
convert_to_long_ex(&retval);
RETVAL_LONG(Z_LVAL_P(retval));
zval_ptr_dtor(&retval);
}
return;
}
#endif
}
default:
RETURN_LONG(1);
break;
}
}
/* }}} */
/* Numbers are always smaller than strings int this function as it
* anyway doesn't make much sense to compare two different data types.
* This keeps it consistant and simple.
*
* This is not correct any more, depends on what compare_func is set to.
*/
static int php_array_data_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
Bucket *f;
Bucket *s;
zval result;
zval *first;
zval *second;
f = *((Bucket **) a);
s = *((Bucket **) b);
first = *((zval **) f->pData);
second = *((zval **) s->pData);
if (ARRAYG(compare_func)(&result, first, second TSRMLS_CC) == FAILURE) {
return 0;
}
if (Z_TYPE(result) == IS_DOUBLE) {
if (Z_DVAL(result) < 0) {
return -1;
} else if (Z_DVAL(result) > 0) {
return 1;
} else {
return 0;
}
}
convert_to_long(&result);
if (Z_LVAL(result) < 0) {
return -1;
} else if (Z_LVAL(result) > 0) {
return 1;
}
return 0;
}
/* }}} */
static int php_array_reverse_data_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
return php_array_data_compare(a, b TSRMLS_CC) * -1;
}
/* }}} */
static int php_array_natural_general_compare(const void *a, const void *b, int fold_case) /* {{{ */
{
Bucket *f, *s;
zval *fval, *sval;
zval first, second;
int result;
f = *((Bucket **) a);
s = *((Bucket **) b);
fval = *((zval **) f->pData);
sval = *((zval **) s->pData);
first = *fval;
second = *sval;
if (Z_TYPE_P(fval) != IS_STRING) {
zval_copy_ctor(&first);
convert_to_string(&first);
}
if (Z_TYPE_P(sval) != IS_STRING) {
zval_copy_ctor(&second);
convert_to_string(&second);
}
result = strnatcmp_ex(Z_STRVAL(first), Z_STRLEN(first), Z_STRVAL(second), Z_STRLEN(second), fold_case);
if (Z_TYPE_P(fval) != IS_STRING) {
zval_dtor(&first);
}
if (Z_TYPE_P(sval) != IS_STRING) {
zval_dtor(&second);
}
return result;
}
/* }}} */
static int php_array_natural_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
return php_array_natural_general_compare(a, b, 0);
}
/* }}} */
static int php_array_natural_case_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
return php_array_natural_general_compare(a, b, 1);
}
/* }}} */
static void php_natsort(INTERNAL_FUNCTION_PARAMETERS, int fold_case) /* {{{ */
{
zval *array;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == FAILURE) {
return;
}
if (fold_case) {
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_natural_case_compare, 0 TSRMLS_CC) == FAILURE) {
return;
}
} else {
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_natural_compare, 0 TSRMLS_CC) == FAILURE) {
return;
}
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto void natsort(array &array_arg)
Sort an array using natural sort */
PHP_FUNCTION(natsort)
{
php_natsort(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto void natcasesort(array &array_arg)
Sort an array using case-insensitive natural sort */
PHP_FUNCTION(natcasesort)
{
php_natsort(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto bool asort(array &array_arg [, int sort_flags])
Sort an array and maintain index association */
PHP_FUNCTION(asort)
{
zval *array;
long sort_type = PHP_SORT_REGULAR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
RETURN_FALSE;
}
php_set_compare_func(sort_type TSRMLS_CC);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_data_compare, 0 TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool arsort(array &array_arg [, int sort_flags])
Sort an array in reverse order and maintain index association */
PHP_FUNCTION(arsort)
{
zval *array;
long sort_type = PHP_SORT_REGULAR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
RETURN_FALSE;
}
php_set_compare_func(sort_type TSRMLS_CC);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_reverse_data_compare, 0 TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool sort(array &array_arg [, int sort_flags])
Sort an array */
PHP_FUNCTION(sort)
{
zval *array;
long sort_type = PHP_SORT_REGULAR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
RETURN_FALSE;
}
php_set_compare_func(sort_type TSRMLS_CC);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_data_compare, 1 TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool rsort(array &array_arg [, int sort_flags])
Sort an array in reverse order */
PHP_FUNCTION(rsort)
{
zval *array;
long sort_type = PHP_SORT_REGULAR;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
RETURN_FALSE;
}
php_set_compare_func(sort_type TSRMLS_CC);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_reverse_data_compare, 1 TSRMLS_CC) == FAILURE) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/* }}} */
static int php_array_user_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
Bucket *f;
Bucket *s;
zval **args[2];
zval *retval_ptr = NULL;
f = *((Bucket **) a);
s = *((Bucket **) b);
args[0] = (zval **) f->pData;
args[1] = (zval **) s->pData;
BG(user_compare_fci).param_count = 2;
BG(user_compare_fci).params = args;
BG(user_compare_fci).retval_ptr_ptr = &retval_ptr;
BG(user_compare_fci).no_separation = 0;
if (zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache) TSRMLS_CC) == SUCCESS && retval_ptr) {
long retval;
convert_to_long_ex(&retval_ptr);
retval = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
return retval < 0 ? -1 : retval > 0 ? 1 : 0;
} else {
return 0;
}
}
/* }}} */
/* check if comparison function is valid */
#define PHP_ARRAY_CMP_FUNC_CHECK(func_name) \
if (!zend_is_callable(*func_name, 0, NULL TSRMLS_CC)) { \
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid comparison function"); \
BG(user_compare_fci) = old_user_compare_fci; \
BG(user_compare_fci_cache) = old_user_compare_fci_cache; \
RETURN_FALSE; \
} \
/* Clear FCI cache otherwise : for example the same or other array with
* (partly) the same key values has been sorted with uasort() or
* other sorting function the comparison is cached, however the name
* of the function for comparison is not respected. see bug #28739 AND #33295
*
* Following defines will assist in backup / restore values. */
#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); \
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; \
/* {{{ proto bool usort(array array_arg, string cmp_function)
Sort an array by values using a user-defined comparison function */
PHP_FUNCTION(usort)
{
zval *array;
int refcount;
PHP_ARRAY_CMP_FUNC_VARS;
PHP_ARRAY_CMP_FUNC_BACKUP();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af", &array, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
PHP_ARRAY_CMP_FUNC_RESTORE();
return;
}
/* Clear the is_ref flag, so the attemts to modify the array in user
* comparison function will create a copy of array and won't affect the
* original array. The fact of modification is detected using refcount
* comparison. The result of sorting in such case is undefined and the
* function returns FALSE.
*/
Z_UNSET_ISREF_P(array);
refcount = Z_REFCOUNT_P(array);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_user_compare, 1 TSRMLS_CC) == FAILURE) {
RETVAL_FALSE;
} else {
if (refcount > Z_REFCOUNT_P(array)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array was modified by the user comparison function");
RETVAL_FALSE;
} else {
RETVAL_TRUE;
}
}
if (Z_REFCOUNT_P(array) > 1) {
Z_SET_ISREF_P(array);
}
PHP_ARRAY_CMP_FUNC_RESTORE();
}
/* }}} */
/* {{{ proto bool uasort(array array_arg, string cmp_function)
Sort an array with a user-defined comparison function and maintain index association */
PHP_FUNCTION(uasort)
{
zval *array;
int refcount;
PHP_ARRAY_CMP_FUNC_VARS;
PHP_ARRAY_CMP_FUNC_BACKUP();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af", &array, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
PHP_ARRAY_CMP_FUNC_RESTORE();
return;
}
/* Clear the is_ref flag, so the attemts to modify the array in user
* comaprison function will create a copy of array and won't affect the
* original array. The fact of modification is detected using refcount
* comparison. The result of sorting in such case is undefined and the
* function returns FALSE.
*/
Z_UNSET_ISREF_P(array);
refcount = Z_REFCOUNT_P(array);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_user_compare, 0 TSRMLS_CC) == FAILURE) {
RETVAL_FALSE;
} else {
if (refcount > Z_REFCOUNT_P(array)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array was modified by the user comparison function");
RETVAL_FALSE;
} else {
RETVAL_TRUE;
}
}
if (Z_REFCOUNT_P(array) > 1) {
Z_SET_ISREF_P(array);
}
PHP_ARRAY_CMP_FUNC_RESTORE();
}
/* }}} */
static int php_array_user_key_compare(const void *a, const void *b TSRMLS_DC) /* {{{ */
{
Bucket *f;
Bucket *s;
zval *key1, *key2;
zval **args[2];
zval *retval_ptr = NULL;
long result;
ALLOC_INIT_ZVAL(key1);
ALLOC_INIT_ZVAL(key2);
args[0] = &key1;
args[1] = &key2;
f = *((Bucket **) a);
s = *((Bucket **) b);
if (f->nKeyLength == 0) {
Z_LVAL_P(key1) = f->h;
Z_TYPE_P(key1) = IS_LONG;
} else {
Z_STRVAL_P(key1) = estrndup(f->arKey, f->nKeyLength - 1);
Z_STRLEN_P(key1) = f->nKeyLength - 1;
Z_TYPE_P(key1) = IS_STRING;
}
if (s->nKeyLength == 0) {
Z_LVAL_P(key2) = s->h;
Z_TYPE_P(key2) = IS_LONG;
} else {
Z_STRVAL_P(key2) = estrndup(s->arKey, s->nKeyLength - 1);
Z_STRLEN_P(key2) = s->nKeyLength - 1;
Z_TYPE_P(key2) = IS_STRING;
}
BG(user_compare_fci).param_count = 2;
BG(user_compare_fci).params = args;
BG(user_compare_fci).retval_ptr_ptr = &retval_ptr;
BG(user_compare_fci).no_separation = 0;
if (zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache) TSRMLS_CC) == SUCCESS && retval_ptr) {
convert_to_long_ex(&retval_ptr);
result = Z_LVAL_P(retval_ptr);
zval_ptr_dtor(&retval_ptr);
} else {
result = 0;
}
zval_ptr_dtor(&key1);
zval_ptr_dtor(&key2);
return result;
}
/* }}} */
/* {{{ proto bool uksort(array array_arg, string cmp_function)
Sort an array by keys using a user-defined comparison function */
PHP_FUNCTION(uksort)
{
zval *array;
int refcount;
PHP_ARRAY_CMP_FUNC_VARS;
PHP_ARRAY_CMP_FUNC_BACKUP();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "af", &array, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
PHP_ARRAY_CMP_FUNC_RESTORE();
return;
}
/* Clear the is_ref flag, so the attemts to modify the array in user
* comaprison function will create a copy of array and won't affect the
* original array. The fact of modification is detected using refcount
* comparison. The result of sorting in such case is undefined and the
* function returns FALSE.
*/
Z_UNSET_ISREF_P(array);
refcount = Z_REFCOUNT_P(array);
if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_user_key_compare, 0 TSRMLS_CC) == FAILURE) {
RETVAL_FALSE;
} else {
if (refcount > Z_REFCOUNT_P(array)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array was modified by the user comparison function");
RETVAL_FALSE;
} else {
RETVAL_TRUE;
}
}
if (Z_REFCOUNT_P(array) > 1) {
Z_SET_ISREF_P(array);
}
PHP_ARRAY_CMP_FUNC_RESTORE();
}
/* }}} */
/* {{{ proto mixed end(array array_arg)
Advances array argument's internal pointer to the last element and return it */
PHP_FUNCTION(end)
{
HashTable *array;
zval **entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
return;
}
zend_hash_internal_pointer_end(array);
if (return_value_used) {
if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
RETURN_FALSE;
}
RETURN_ZVAL(*entry, 1, 0);
}
}
/* }}} */
/* {{{ proto mixed prev(array array_arg)
Move array argument's internal pointer to the previous element and return it */
PHP_FUNCTION(prev)
{
HashTable *array;
zval **entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
return;
}
zend_hash_move_backwards(array);
if (return_value_used) {
if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
RETURN_FALSE;
}
RETURN_ZVAL(*entry, 1, 0);
}
}
/* }}} */
/* {{{ proto mixed next(array array_arg)
Move array argument's internal pointer to the next element and return it */
PHP_FUNCTION(next)
{
HashTable *array;
zval **entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
return;
}
zend_hash_move_forward(array);
if (return_value_used) {
if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
RETURN_FALSE;
}
RETURN_ZVAL(*entry, 1, 0);
}
}
/* }}} */
/* {{{ proto mixed reset(array array_arg)
Set array argument's internal pointer to the first element and return it */
PHP_FUNCTION(reset)
{
HashTable *array;
zval **entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
return;
}
zend_hash_internal_pointer_reset(array);
if (return_value_used) {
if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
RETURN_FALSE;
}
RETURN_ZVAL(*entry, 1, 0);
}
}
/* }}} */
/* {{{ proto mixed current(array array_arg)
Return the element currently pointed to by the internal array pointer */
PHP_FUNCTION(current)
{
HashTable *array;
zval **entry;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
return;
}
if (zend_hash_get_current_data(array, (void **) &entry) == FAILURE) {
RETURN_FALSE;
}
RETURN_ZVAL(*entry, 1, 0);
}
/* }}} */
/* {{{ proto mixed key(array array_arg)
Return the key of the element currently pointed to by the internal array pointer */
PHP_FUNCTION(key)
{
HashTable *array;
char *string_key;
uint string_length;
ulong num_key;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &array) == FAILURE) {
return;
}
switch (zend_hash_get_current_key_ex(array, &string_key, &string_length, &num_key, 0, NULL)) {
case HASH_KEY_IS_STRING:
RETVAL_STRINGL(string_key, string_length - 1, 1);
break;
case HASH_KEY_IS_LONG:
RETVAL_LONG(num_key);
break;
case HASH_KEY_NON_EXISTANT:
return;
}
}
/* }}} */
/* {{{ proto mixed min(mixed arg1 [, mixed arg2 [, mixed ...]])
Return the lowest value in an array or a series of arguments */
PHP_FUNCTION(min)
{
int argc;
zval ***args = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "+", &args, &argc) == FAILURE) {
return;
}
php_set_compare_func(PHP_SORT_REGULAR TSRMLS_CC);
/* mixed min ( array $values ) */
if (argc == 1) {
zval **result;
if (Z_TYPE_PP(args[0]) != IS_ARRAY) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "When only one parameter is given, it must be an array");
RETVAL_NULL();
} else {
if (zend_hash_minmax(Z_ARRVAL_PP(args[0]), php_array_data_compare, 0, (void **) &result TSRMLS_CC) == SUCCESS) {
RETVAL_ZVAL(*result, 1, 0);
} else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Array must contain at least one element");
RETVAL_FALSE;
}
}
} else {
/* mixed min ( mixed $value1 , mixed $value2 [, mixed $value3... ] ) */
zval **min, result;
int i;
min = args[0];
for (i = 1; i < argc; i++) {
is_smaller_function(&result, *args[i], *min TSRMLS_CC);
if (Z_LVAL(result) == 1) {
min = args[i];
}
}
RETVAL_ZVAL(*min, 1, 0);
}
if (args) {
efree(args);
}
}
/* }}} */
/* {{{ proto mixed max(mixed arg1 [, mixed arg2 [, mixed ...]])
Return the highest value in an array or a series of arguments */
PHP_FUNCTION(max)
{
zval ***args = NULL;