Elementary analysis of PHP natsort kernel function 1th/2 page _php Tutorial

Source: Internet
Author: User
Official Handbook (http://us.php.net/manual/en/function.natsort.php)
Copy CodeThe code is as follows:
BOOL Natsort (array & $array)
This function implements a sort algorithm it orders alphanumeric strings in the the the-a human being would while Maintainin G Key/value Associations. This is described as a "natural ordering". A 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, such results can be obtained:

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

This is obviously a good fit for sorting similar file names. From the results of this natural algorithm should be to turn around and the tail of the non-digital parts, and then left to the number of parts to sort, whether or not, or to look at the PHP source bar.
Copy CodeThe code is 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 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 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 look at the kernel code of PHP, but with years of experience in code, it is easy to find the core of this natural sorting algorithm is the function: STRNATCMP_EX (in the Ext/standard/strnatcmp.c file).

http://www.bkjia.com/PHPjc/320396.html www.bkjia.com true http://www.bkjia.com/PHPjc/320396.html techarticle Official Manual (http://us.php.net/manual/en/function.natsort.php) copy the code code as follows: BOOL Natsort (array nbsp; Bucket *f, *s; Zval *fval, *sval; Zval first, second; int ...

  • 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.