#define SORT_DESC -1
#define SORT_ASC 1
+#define SORT_REGULAR 0
+#define SORT_NUMERIC 1
+#define SORT_STRING 2
+
PHP_MINIT_FUNCTION(array)
{
#ifdef ZTS
REGISTER_LONG_CONSTANT("SORT_ASC", SORT_ASC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SORT_DESC", SORT_DESC, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("SORT_REGULAR", SORT_REGULAR, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("SORT_NUMERIC", SORT_NUMERIC, CONST_CS | CONST_PERSISTENT);
+ REGISTER_LONG_CONSTANT("SORT_STRING", SORT_STRING, CONST_CS | CONST_PERSISTENT);
+
return SUCCESS;
}
return SUCCESS;
}
+static void set_compare_func(int sort_type)
+{
+ ARRAYLS_FETCH();
+
+ switch (sort_type) {
+ case SORT_NUMERIC:
+ ARRAYG(compare_func) = numeric_compare_function;
+ break;
+
+ case SORT_STRING:
+ ARRAYG(compare_func) = string_compare_function;
+ break;
+
+ case SORT_REGULAR:
+ default:
+ ARRAYG(compare_func) = compare_function;
+ break;
+ }
+}
+
static int array_key_compare(const void *a, const void *b)
{
Bucket *f;
pval result;
pval first;
pval second;
+ ARRAYLS_FETCH();
f = *((Bucket **) a);
s = *((Bucket **) b);
second.value.str.len = s->nKeyLength;
}
- if (compare_function(&result, &first, &second) == FAILURE) {
+ if (ARRAYG(compare_func)(&result, &first, &second) == FAILURE) {
return 0;
}
Sort an array reverse by key */
PHP_FUNCTION(krsort)
{
- pval **array;
+ zval **array, **sort_type;
+ int sort_type_val = SORT_REGULAR;
HashTable *target_hash;
- if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
+ if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+ zend_get_parameters_ex(ARG_COUNT(ht), &array, &sort_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
target_hash = HASH_OF(*array);
php_error(E_WARNING, "Wrong datatype in krsort() call");
return;
}
+ if (ARG_COUNT(ht) == 2) {
+ convert_to_long_ex(sort_type);
+ sort_type_val = Z_LVAL_PP(sort_type);
+ }
+ set_compare_func(sort_type_val);
if (zend_hash_sort(target_hash, qsort, array_reverse_key_compare, 0) == FAILURE) {
return;
}
Sort an array by key */
PHP_FUNCTION(ksort)
{
- pval **array;
+ zval **array, **sort_type;
+ int sort_type_val = SORT_REGULAR;
HashTable *target_hash;
- if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
+ if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+ zend_get_parameters_ex(ARG_COUNT(ht), &array, &sort_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
target_hash = HASH_OF(*array);
php_error(E_WARNING, "Wrong datatype in ksort() call");
return;
}
+ if (ARG_COUNT(ht) == 2) {
+ convert_to_long_ex(sort_type);
+ sort_type_val = Z_LVAL_PP(sort_type);
+ }
+ set_compare_func(sort_type_val);
if (zend_hash_sort(target_hash, qsort, array_key_compare,0) == FAILURE) {
return;
}
pval result;
pval *first;
pval *second;
+ ARRAYLS_FETCH();
f = *((Bucket **) a);
s = *((Bucket **) b);
first = *((pval **) f->pData);
second = *((pval **) s->pData);
- if (compare_function(&result, first, second) == FAILURE) {
+ if (ARRAYG(compare_func)(&result, first, second) == FAILURE) {
return 0;
}
Sort an array and maintain index association */
PHP_FUNCTION(asort)
{
- pval **array;
+ zval **array, **sort_type;
+ int sort_type_val = SORT_REGULAR;
HashTable *target_hash;
- if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
+ if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+ zend_get_parameters_ex(ARG_COUNT(ht), &array, &sort_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
target_hash = HASH_OF(*array);
php_error(E_WARNING, "Wrong datatype in asort() call");
return;
}
+ if (ARG_COUNT(ht) == 2) {
+ convert_to_long_ex(sort_type);
+ sort_type_val = Z_LVAL_PP(sort_type);
+ }
+ set_compare_func(sort_type_val);
if (zend_hash_sort(target_hash, qsort, array_data_compare,0) == FAILURE) {
return;
}
Sort an array in reverse order and maintain index association */
PHP_FUNCTION(arsort)
{
- pval **array;
+ zval **array, **sort_type;
+ int sort_type_val = SORT_REGULAR;
HashTable *target_hash;
- if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
+ if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+ zend_get_parameters_ex(ARG_COUNT(ht), &array, &sort_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
target_hash = HASH_OF(*array);
php_error(E_WARNING, "Wrong datatype in arsort() call");
RETURN_FALSE;
}
+ if (ARG_COUNT(ht) == 2) {
+ convert_to_long_ex(sort_type);
+ sort_type_val = Z_LVAL_PP(sort_type);
+ }
+ set_compare_func(sort_type_val);
if (zend_hash_sort(target_hash, qsort, array_reverse_data_compare,0) == FAILURE) {
RETURN_FALSE;
}
Sort an array */
PHP_FUNCTION(sort)
{
- pval **array;
+ zval **array, **sort_type;
+ int sort_type_val = SORT_REGULAR;
HashTable *target_hash;
- if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
+ if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+ zend_get_parameters_ex(ARG_COUNT(ht), &array, &sort_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
target_hash = HASH_OF(*array);
php_error(E_WARNING, "Wrong datatype in sort() call");
RETURN_FALSE;
}
+ if (ARG_COUNT(ht) == 2) {
+ convert_to_long_ex(sort_type);
+ sort_type_val = Z_LVAL_PP(sort_type);
+ }
+ set_compare_func(sort_type_val);
if (zend_hash_sort(target_hash, qsort, array_data_compare,1) == FAILURE) {
RETURN_FALSE;
}
Sort an array in reverse order */
PHP_FUNCTION(rsort)
{
- pval **array;
+ zval **array, **sort_type;
+ int sort_type_val = SORT_REGULAR;
HashTable *target_hash;
- if (ARG_COUNT(ht) != 1 || zend_get_parameters_ex(1, &array) == FAILURE) {
+ if (ARG_COUNT(ht) < 1 || ARG_COUNT(ht) > 2 ||
+ zend_get_parameters_ex(ARG_COUNT(ht), &array, &sort_type) == FAILURE) {
WRONG_PARAM_COUNT;
}
target_hash = HASH_OF(*array);
php_error(E_WARNING, "Wrong datatype in rsort() call");
RETURN_FALSE;
}
+ if (ARG_COUNT(ht) == 2) {
+ convert_to_long_ex(sort_type);
+ sort_type_val = Z_LVAL_PP(sort_type);
+ }
+ set_compare_func(sort_type_val);
if (zend_hash_sort(target_hash, qsort, array_reverse_data_compare,1) == FAILURE) {
RETURN_FALSE;
}
/* }}} */
-/* HashTable* _phpi_splice(HashTable *in_hash, int offset, int length,
- zval ***list, int list_count, HashTable **removed) */
-HashTable* _phpi_splice(HashTable *in_hash, int offset, int length,
- zval ***list, int list_count, HashTable **removed)
+/* HashTable* php_splice(HashTable *in_hash, int offset, int length,
+ zval ***list, int list_count, HashTable **removed) */
+HashTable* php_splice(HashTable *in_hash, int offset, int length,
+ zval ***list, int list_count, HashTable **removed)
{
HashTable *out_hash = NULL; /* Output hashtable */
int num_in, /* Number of entries in the input hashtable */
INIT_PZVAL(return_value);
/* Delete the first or last value */
- new_hash = _phpi_splice((*stack)->value.ht, (off_the_end) ? -1 : 0, 1, NULL, 0, NULL);
+ new_hash = php_splice((*stack)->value.ht, (off_the_end) ? -1 : 0, 1, NULL, 0, NULL);
zend_hash_destroy((*stack)->value.ht);
efree((*stack)->value.ht);
(*stack)->value.ht = new_hash;
/* Use splice to insert the elements at the beginning. Destroy old
hashtable and replace it with new one */
- new_hash = _phpi_splice(stack->value.ht, 0, 0, &args[1], argc-1, NULL);
+ new_hash = php_splice(stack->value.ht, 0, 0, &args[1], argc-1, NULL);
zend_hash_destroy(stack->value.ht);
efree(stack->value.ht);
stack->value.ht = new_hash;
array_init(return_value);
/* Perform splice */
- new_hash = _phpi_splice(array->value.ht, offset, length,
+ new_hash = php_splice(array->value.ht, offset, length,
repl, repl_num,
&return_value->value.ht);
/* Pad on the right or on the left */
if ((*pad_size)->value.lval > 0)
- new_hash = _phpi_splice(return_value->value.ht, input_size, 0, pads, num_pads, NULL);
+ new_hash = php_splice(return_value->value.ht, input_size, 0, pads, num_pads, NULL);
else
- new_hash = _phpi_splice(return_value->value.ht, 0, 0, pads, num_pads, NULL);
+ new_hash = php_splice(return_value->value.ht, 0, 0, pads, num_pads, NULL);
/* Copy the result hash into return value */
r = 0;
do {
- compare_function(&temp, *((zval **)ab[r]->pData), *((zval **)bb[r]->pData));
+ ARRAYG(compare_func)(&temp, *((zval **)ab[r]->pData), *((zval **)bb[r]->pData));
result = ARRAYG(multisort_flags)[r] * temp.value.lval;
if (result != 0)
return result;
for (k = 0; k < array_size; k++)
indirect[k][num_arrays] = NULL;
+ /* For now, assume it's always regular sort. */
+ set_compare_func(SORT_REGULAR);
+
/* Do the actual sort magic - bada-bim, bada-boom */
qsort(indirect, array_size, sizeof(Bucket **), multisort_compare);