Skip to content

Commit 844799c

Browse files
author
Andrei Zmievski
committed
MFH
1 parent d6b537e commit 844799c

File tree

7 files changed

+70
-71
lines changed

7 files changed

+70
-71
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2009, PHP 5.2.9
4+
- Added optional sorting type flag parameter to array_unique(), default is
5+
SORT_REGULAR. (Andrei)
46
- Fixed security issue in imagerotate(), background colour isn't validated
57
correctly with a non truecolour image. (Scott)
68

ext/standard/array.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,47 +2813,43 @@ PHP_FUNCTION(array_change_key_case)
28132813
}
28142814
/* }}} */
28152815

2816-
/* {{{ proto array array_unique(array input)
2816+
/* {{{ proto array array_unique(array input [, int sort_flags])
28172817
Removes duplicate values from array */
28182818
PHP_FUNCTION(array_unique)
28192819
{
2820-
zval **array, *tmp;
2821-
HashTable *target_hash;
2820+
zval *array, *tmp;
28222821
Bucket *p;
28232822
struct bucketindex {
28242823
Bucket *b;
28252824
unsigned int i;
28262825
};
28272826
struct bucketindex *arTmp, *cmpdata, *lastkept;
28282827
unsigned int i;
2828+
long sort_type = SORT_REGULAR;
28292829

2830-
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
2831-
WRONG_PARAM_COUNT;
2832-
}
2833-
target_hash = HASH_OF(*array);
2834-
if (!target_hash) {
2835-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The argument should be an array");
2836-
RETURN_FALSE;
2830+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
2831+
return;
28372832
}
28382833

2834+
set_compare_func(sort_type TSRMLS_CC);
2835+
28392836
array_init(return_value);
2840-
zend_hash_copy(Z_ARRVAL_P(return_value), target_hash, (copy_ctor_func_t) zval_add_ref, (void *)&tmp, sizeof(zval*));
2837+
zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_P(array), (copy_ctor_func_t) zval_add_ref, (void *)&tmp, sizeof(zval*));
28412838

2842-
if (target_hash->nNumOfElements <= 1) { /* nothing to do */
2839+
if (Z_ARRVAL_P(array)->nNumOfElements <= 1) { /* nothing to do */
28432840
return;
28442841
}
28452842

28462843
/* create and sort array with pointers to the target_hash buckets */
2847-
arTmp = (struct bucketindex *) pemalloc((target_hash->nNumOfElements + 1) * sizeof(struct bucketindex), target_hash->persistent);
2844+
arTmp = (struct bucketindex *) pemalloc((Z_ARRVAL_P(array)->nNumOfElements + 1) * sizeof(struct bucketindex), Z_ARRVAL_P(array)->persistent);
28482845
if (!arTmp) {
28492846
RETURN_FALSE;
28502847
}
2851-
for (i = 0, p = target_hash->pListHead; p; i++, p = p->pListNext) {
2848+
for (i = 0, p = Z_ARRVAL_P(array)->pListHead; p; i++, p = p->pListNext) {
28522849
arTmp[i].b = p;
28532850
arTmp[i].i = i;
28542851
}
28552852
arTmp[i].b = NULL;
2856-
set_compare_func(SORT_STRING TSRMLS_CC);
28572853
zend_qsort((void *) arTmp, i, sizeof(struct bucketindex), array_data_compare TSRMLS_CC);
28582854

28592855
/* go through the sorted array and delete duplicates from the copy */
@@ -2879,7 +2875,7 @@ PHP_FUNCTION(array_unique)
28792875
}
28802876
}
28812877
}
2882-
pefree(arTmp, target_hash->persistent);
2878+
pefree(arTmp, Z_ARRVAL_P(array)->persistent);
28832879
}
28842880
/* }}} */
28852881

ext/standard/tests/array/array_unique_error.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var_dump( array_unique() );
1717
echo "\n-- Testing array_unique() function with more than expected no. of arguments --\n";
1818
$input = array(1, 2);
1919
$extra_arg = 10;
20-
var_dump( array_unique($input, $extra_arg) );
20+
var_dump( array_unique($input, SORT_NUMERIC, $extra_arg) );
2121

2222
echo "Done";
2323
?>
@@ -26,11 +26,11 @@ echo "Done";
2626

2727
-- Testing array_unique() function with zero arguments --
2828

29-
Warning: Wrong parameter count for array_unique() in %s on line %d
29+
Warning: array_unique() expects at least 1 parameter, 0 given in %s on line %d
3030
NULL
3131

3232
-- Testing array_unique() function with more than expected no. of arguments --
3333

34-
Warning: Wrong parameter count for array_unique() in %s on line %d
34+
Warning: array_unique() expects at most 2 parameters, 3 given in %s on line %d
3535
NULL
3636
Done

ext/standard/tests/array/array_unique_variation1.phpt

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -98,97 +98,98 @@ echo "Done";
9898
*** Testing array_unique() : Passing non array values to $input argument ***
9999
-- Iteration 1 --
100100

101-
Warning: array_unique(): The argument should be an array in %s on line %d
102-
bool(false)
101+
Warning: array_unique() expects parameter 1 to be array, integer given in %s on line %d
102+
NULL
103103
-- Iteration 2 --
104104

105-
Warning: array_unique(): The argument should be an array in %s on line %d
106-
bool(false)
105+
Warning: array_unique() expects parameter 1 to be array, integer given in %s on line %d
106+
NULL
107107
-- Iteration 3 --
108108

109-
Warning: array_unique(): The argument should be an array in %s on line %d
110-
bool(false)
109+
Warning: array_unique() expects parameter 1 to be array, integer given in %s on line %d
110+
NULL
111111
-- Iteration 4 --
112112

113-
Warning: array_unique(): The argument should be an array in %s on line %d
114-
bool(false)
113+
Warning: array_unique() expects parameter 1 to be array, integer given in %s on line %d
114+
NULL
115115
-- Iteration 5 --
116116

117-
Warning: array_unique(): The argument should be an array in %s on line %d
118-
bool(false)
117+
Warning: array_unique() expects parameter 1 to be array, double given in %s on line %d
118+
NULL
119119
-- Iteration 6 --
120120

121-
Warning: array_unique(): The argument should be an array in %s on line %d
122-
bool(false)
121+
Warning: array_unique() expects parameter 1 to be array, double given in %s on line %d
122+
NULL
123123
-- Iteration 7 --
124124

125-
Warning: array_unique(): The argument should be an array in %s on line %d
126-
bool(false)
125+
Warning: array_unique() expects parameter 1 to be array, double given in %s on line %d
126+
NULL
127127
-- Iteration 8 --
128128

129-
Warning: array_unique(): The argument should be an array in %s on line %d
130-
bool(false)
129+
Warning: array_unique() expects parameter 1 to be array, double given in %s on line %d
130+
NULL
131131
-- Iteration 9 --
132132

133-
Warning: array_unique(): The argument should be an array in %s on line %d
134-
bool(false)
133+
Warning: array_unique() expects parameter 1 to be array, double given in %s on line %d
134+
NULL
135135
-- Iteration 10 --
136136

137-
Warning: array_unique(): The argument should be an array in %s on line %d
138-
bool(false)
137+
Warning: array_unique() expects parameter 1 to be array, null given in %s on line %d
138+
NULL
139139
-- Iteration 11 --
140140

141-
Warning: array_unique(): The argument should be an array in %s on line %d
142-
bool(false)
141+
Warning: array_unique() expects parameter 1 to be array, null given in %s on line %d
142+
NULL
143143
-- Iteration 12 --
144144

145-
Warning: array_unique(): The argument should be an array in %s on line %d
146-
bool(false)
145+
Warning: array_unique() expects parameter 1 to be array, boolean given in %s on line %d
146+
NULL
147147
-- Iteration 13 --
148148

149-
Warning: array_unique(): The argument should be an array in %s on line %d
150-
bool(false)
149+
Warning: array_unique() expects parameter 1 to be array, boolean given in %s on line %d
150+
NULL
151151
-- Iteration 14 --
152152

153-
Warning: array_unique(): The argument should be an array in %s on line %d
154-
bool(false)
153+
Warning: array_unique() expects parameter 1 to be array, boolean given in %s on line %d
154+
NULL
155155
-- Iteration 15 --
156156

157-
Warning: array_unique(): The argument should be an array in %s on line %d
158-
bool(false)
157+
Warning: array_unique() expects parameter 1 to be array, boolean given in %s on line %d
158+
NULL
159159
-- Iteration 16 --
160160

161-
Warning: array_unique(): The argument should be an array in %s on line %d
162-
bool(false)
161+
Warning: array_unique() expects parameter 1 to be array, string given in %s on line %d
162+
NULL
163163
-- Iteration 17 --
164164

165-
Warning: array_unique(): The argument should be an array in %s on line %d
166-
bool(false)
165+
Warning: array_unique() expects parameter 1 to be array, string given in %s on line %d
166+
NULL
167167
-- Iteration 18 --
168168

169-
Warning: array_unique(): The argument should be an array in %s on line %d
170-
bool(false)
169+
Warning: array_unique() expects parameter 1 to be array, string given in %s on line %d
170+
NULL
171171
-- Iteration 19 --
172172

173-
Warning: array_unique(): The argument should be an array in %s on line %d
174-
bool(false)
173+
Warning: array_unique() expects parameter 1 to be array, string given in %s on line %d
174+
NULL
175175
-- Iteration 20 --
176176

177-
Warning: array_unique(): The argument should be an array in %s on line %d
178-
bool(false)
177+
Warning: array_unique() expects parameter 1 to be array, string given in %s on line %d
178+
NULL
179179
-- Iteration 21 --
180-
array(0) {
181-
}
180+
181+
Warning: array_unique() expects parameter 1 to be array, object given in %s on line %d
182+
NULL
182183
-- Iteration 22 --
183184

184-
Warning: array_unique(): The argument should be an array in %s on line %d
185-
bool(false)
185+
Warning: array_unique() expects parameter 1 to be array, null given in %s on line %d
186+
NULL
186187
-- Iteration 23 --
187188

188-
Warning: array_unique(): The argument should be an array in %s on line %d
189-
bool(false)
189+
Warning: array_unique() expects parameter 1 to be array, null given in %s on line %d
190+
NULL
190191
-- Iteration 24 --
191192

192-
Warning: array_unique(): The argument should be an array in %s on line %d
193-
bool(false)
194-
Done
193+
Warning: array_unique() expects parameter 1 to be array, resource given in %s on line %d
194+
NULL
195+
Done

ext/standard/tests/array/array_unique_variation2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ $inputs = array (
7474
$iterator = 1;
7575
foreach($inputs as $input) {
7676
echo "-- Iteration $iterator --\n";
77-
var_dump( array_unique($input) );
77+
var_dump( array_unique($input, SORT_STRING) );
7878
$iterator++;
7979
}
8080

ext/standard/tests/array/array_unique_variation6.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $input = array(
2929
5 => $value4
3030
);
3131

32-
var_dump( array_unique($input) );
32+
var_dump( array_unique($input, SORT_STRING) );
3333

3434
echo "Done";
3535
?>

ext/standard/tests/array/array_unique_variation8.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ $input = array(
2222
array(1, 2, 3, 1)
2323
);
2424

25-
var_dump( array_unique($input) );
25+
var_dump( array_unique($input, SORT_STRING) );
2626

2727
echo "Done";
2828
?>

0 commit comments

Comments
 (0)