Phpnatsort kernel function analysis page 1/2 _ PHP Tutorial

Source: Internet
Author: User
Phpnatsort kernel function analysis page 12th. Official Manual (us. php. netmanualenfunction. natsort. php) copy the code as follows: boolnatsort (arraynbsp; Bucket * f, * s; zval * fval, * sval; zvalfirst, second; int official manual (http://us.php.net/manual/en/function.natsort.php)

The code is as follows:


Bool natsort (array & $ array)
This function implements a sort algorithm that orders alphanumeric strings in the way a human being wocould while maintaining 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 () can be seen in the example below.


According to the official manual, the following results can be obtained:

Img1.png img2.png img10.png img12.png

Obviously, this is suitable for sorting similar file names. From the results, we can see that this kind of natural algorithm should be to turn around and end the non-numeric part, and then sort the remaining numeric part. Is it true? let's take a look at the php source code.

The code is as follows:


// The 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 **) );
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 was the first time to view the php kernel code, with years of experience in code reading, it is easy to find that the core of this natural sorting algorithm is the function: strnatcmp_ex (located in ext/standard/strnatcmp. c file ).

Encode (http://us.php.net/manual/en/function.natsort.php) Code: 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.