Analysis of PHP natsort kernel function 1th/2 page _php tips

Source: Internet
Author: User
Tags php source code

Official Handbook (http://us.php.net/manual/en/function.natsort.php)

Copy Code code as follows:

BOOL Natsort (array & $array)
This function implements a sort algorithm so orders alphanumeric strings in the way a human being would while Maintainin G Key/value Associations. This is described as a "natural ordering". An example of the difference between this algorithm and the regular computer string sorting algorithms (used in sort ()) CA N is seen in the example below.

According to the Official Handbook, the results are as follows:

Img1.png img2.png img10.png Img12.png

Obviously this is a good fit for a sort of similar file name. From the results of this natural algorithm should be to turn around and the end of the non-digital parts, and then the number of the remaining part of the order, is not, or look at the PHP source code it.
Copy Code code as follows:

The relevant code extracted from EXT/STANDARD/ARRAY.C is as follows
static int php_array_natural_general_compare (const void *a, const void *b, int fold_case)/* {{* * *
{
Bucket *f, *s;
Zval *fval, *sval;
Zval, second;
int result;
f = * ((Bucket * *) a);
s = * ((Bucket * *) b);
fval = * (zval * *) f->pdata);
Sval = * (zval * *) s->pdata);
i = *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 (a), Z_strlen (a), 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 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);
}
/* }}} */

Although it is the first time to view the PHP kernel code, but with years of experience in the code, it is easy to find the core of the natural sorting algorithm is the function: STRNATCMP_EX (in the Ext/standard/strnatcmp.c file).
Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.