PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/collator - collator_sort.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 172 240 71.7 %
Date: 2015-05-21 Functions: 11 12 91.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | This source file is subject to version 3.01 of the PHP license,      |
       6             :    | that is bundled with this package in the file LICENSE, and is        |
       7             :    | available through the world-wide-web at the following url:           |
       8             :    | http://www.php.net/license/3_01.txt                                  |
       9             :    | If you did not receive a copy of the PHP license and are unable to   |
      10             :    | obtain it through the world-wide-web, please send a note to          |
      11             :    | license@php.net so we can mail you a copy immediately.               |
      12             :    +----------------------------------------------------------------------+
      13             :    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
      14             :    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
      15             :    +----------------------------------------------------------------------+
      16             :  */
      17             : 
      18             : #ifdef HAVE_CONFIG_H
      19             : #include "config.h"
      20             : #endif
      21             : 
      22             : #include "php_intl.h"
      23             : #include "collator.h"
      24             : #include "collator_class.h"
      25             : #include "collator_sort.h"
      26             : #include "collator_convert.h"
      27             : #include "intl_convert.h"
      28             : 
      29             : #if !defined(HAVE_PTRDIFF_T) && !defined(_PTRDIFF_T_DEFINED)
      30             : typedef zend_long ptrdiff_t;
      31             : #endif
      32             : 
      33             : /**
      34             :  * Declare 'index' which will point to sort key in sort key
      35             :  * buffer.
      36             :  */
      37             : typedef struct _collator_sort_key_index {
      38             :         char* key;       /* pointer to sort key */
      39             :         zval* zstr;     /* pointer to original string(hash-item) */
      40             : } collator_sort_key_index_t;
      41             : 
      42             : ZEND_EXTERN_MODULE_GLOBALS( intl )
      43             : 
      44             : static const size_t DEF_SORT_KEYS_BUF_SIZE = 1048576;
      45             : static const size_t DEF_SORT_KEYS_BUF_INCREMENT = 1048576;
      46             : 
      47             : static const size_t DEF_SORT_KEYS_INDX_BUF_SIZE = 1048576;
      48             : static const size_t DEF_SORT_KEYS_INDX_BUF_INCREMENT = 1048576;
      49             : 
      50             : static const size_t DEF_UTF16_BUF_SIZE = 1024;
      51             : 
      52             : /* {{{ collator_regular_compare_function */
      53         173 : static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
      54             : {
      55         173 :         Collator_object* co = NULL;
      56         173 :         int rc = SUCCESS;
      57             :         zval str1, str2;
      58             :         zval num1, num2;
      59             :         zval norm1, norm2;
      60         173 :         zval *num1_p = NULL, *num2_p = NULL;
      61         173 :         zval *norm1_p = NULL, *norm2_p = NULL;
      62         173 :         zval* str1_p  = collator_convert_object_to_string( op1, &str1 );
      63         173 :         zval* str2_p  = collator_convert_object_to_string( op2, &str2 );
      64             : 
      65             :         /* If both args are strings AND either of args is not numeric string
      66             :          * then use ICU-compare. Otherwise PHP-compare. */
      67         639 :         if( Z_TYPE_P(str1_p) == IS_STRING && Z_TYPE_P(str2_p) == IS_STRING &&
      68         153 :                 ( str1_p == ( num1_p = collator_convert_string_to_number_if_possible( str1_p, &num1 ) ) ||
      69          12 :                   str2_p == ( num2_p = collator_convert_string_to_number_if_possible( str2_p, &num2 ) ) ) )
      70             :         {
      71             :                 /* Fetch collator object. */
      72         141 :                 co = Z_INTL_COLLATOR_P(&INTL_G(current_collator));
      73             : 
      74         141 :                 if (!co || !co->ucoll) {
      75           1 :                         intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
      76           1 :                         intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
      77             :                                 "Object not initialized", 0 );
      78           1 :                         php_error_docref(NULL, E_RECOVERABLE_ERROR, "Object not initialized");
      79             : 
      80             :                 }
      81             : 
      82             :                 /* Compare the strings using ICU. */
      83         140 :                 ZVAL_LONG(result, ucol_strcoll(
      84             :                                         co->ucoll,
      85             :                                         INTL_Z_STRVAL_P(str1_p), INTL_Z_STRLEN_P(str1_p),
      86             :                                         INTL_Z_STRVAL_P(str2_p), INTL_Z_STRLEN_P(str2_p) ));
      87             :         }
      88             :         else
      89             :         {
      90             :                 /* num1 is set if str1 and str2 are strings. */
      91          32 :                 if( num1_p )
      92             :                 {
      93          12 :                         if( num1_p == str1_p )
      94             :                         {
      95             :                                 /* str1 is string but not numeric string
      96             :                                  * just convert it to utf8.
      97             :                                  */
      98           0 :                                 norm1_p = collator_convert_zstr_utf16_to_utf8( str1_p, &norm1 );
      99             : 
     100             :                                 /* num2 is not set but str2 is string => do normalization. */
     101           0 :                                 norm2_p = collator_normalize_sort_argument( str2_p, &norm2 );
     102             :                         }
     103             :                         else
     104             :                         {
     105             :                                 /* str1 is numeric strings => passthru to PHP-compare. */
     106          12 :                                 Z_TRY_ADDREF_P(num1_p);
     107          12 :                                 norm1_p = num1_p;
     108             : 
     109             :                                 /* str2 is numeric strings => passthru to PHP-compare. */
     110          12 :                                 Z_TRY_ADDREF_P(num2_p);
     111          12 :                                 norm2_p = num2_p;
     112             :                         }
     113             :                 }
     114             :                 else
     115             :                 {
     116             :                         /* num1 is not set if str1 or str2 is not a string => do normalization. */
     117          20 :                         norm1_p = collator_normalize_sort_argument( str1_p, &norm1 );
     118             : 
     119             :                         /* if num1 is not set then num2 is not set as well => do normalization. */
     120          20 :                         norm2_p = collator_normalize_sort_argument( str2_p, &norm2 );
     121             :                 }
     122             : 
     123          32 :                 rc = compare_function( result, norm1_p, norm2_p );
     124             : 
     125          32 :                 zval_ptr_dtor( norm1_p );
     126          32 :                 zval_ptr_dtor( norm2_p );
     127             :         }
     128             : 
     129         172 :         if( num1_p )
     130         152 :                 zval_ptr_dtor( num1_p );
     131             : 
     132         172 :         if( num2_p )
     133          12 :                 zval_ptr_dtor( num2_p );
     134             : 
     135         172 :         zval_ptr_dtor( str1_p );
     136         172 :         zval_ptr_dtor( str2_p );
     137             : 
     138         172 :         return rc;
     139             : }
     140             : /* }}} */
     141             : 
     142             : /* {{{ collator_numeric_compare_function
     143             :  * Convert input args to double and compare it.
     144             :  */
     145           0 : static int collator_numeric_compare_function(zval *result, zval *op1, zval *op2)
     146             : {
     147           0 :         int rc     = SUCCESS;
     148             :         zval num1, num2;
     149           0 :         zval *num1_p = NULL;
     150           0 :         zval *num2_p = NULL;
     151             : 
     152           0 :         if( Z_TYPE_P(op1) == IS_STRING )
     153             :         {
     154           0 :                 num1_p = collator_convert_string_to_double( op1, &num1 );
     155           0 :                 op1 = num1_p;
     156             :         }
     157             : 
     158           0 :         if( Z_TYPE_P(op2) == IS_STRING )
     159             :         {
     160           0 :                 num2_p = collator_convert_string_to_double( op2, &num2 );
     161           0 :                 op2 = num2_p;
     162             :         }
     163             : 
     164           0 :         rc = numeric_compare_function( result, op1, op2);
     165             : 
     166           0 :         if( num1_p )
     167           0 :                 zval_ptr_dtor( num1_p );
     168           0 :         if( num2_p )
     169           0 :                 zval_ptr_dtor( num2_p );
     170             : 
     171           0 :         return rc;
     172             : }
     173             : /* }}} */
     174             : 
     175             : /* {{{ collator_icu_compare_function
     176             :  * Direct use of ucol_strcoll.
     177             : */
     178          36 : static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
     179             : {
     180             :         zval str1, str2;
     181          36 :         int rc              = SUCCESS;
     182          36 :         Collator_object* co = NULL;
     183          36 :         zval *str1_p        = NULL;
     184          36 :         zval *str2_p        = NULL;
     185             : 
     186          36 :         str1_p = collator_make_printable_zval( op1, &str1);
     187          36 :         str2_p = collator_make_printable_zval( op2, &str2 );
     188             : 
     189             :         /* Fetch collator object. */
     190          36 :         co = Z_INTL_COLLATOR_P(&INTL_G(current_collator));
     191             : 
     192             :         /* Compare the strings using ICU. */
     193          36 :         ZVAL_LONG(result, ucol_strcoll(
     194             :                                 co->ucoll,
     195             :                                 INTL_Z_STRVAL_P(str1_p), INTL_Z_STRLEN_P(str1_p),
     196             :                                 INTL_Z_STRVAL_P(str2_p), INTL_Z_STRLEN_P(str2_p) ));
     197             : 
     198          36 :         zval_ptr_dtor( str1_p );
     199          36 :         zval_ptr_dtor( str2_p );
     200             : 
     201          36 :         return rc;
     202             : }
     203             : /* }}} */
     204             : 
     205             : /* {{{ collator_compare_func
     206             :  * Taken from PHP7 source (array_data_compare).
     207             :  */
     208         209 : static int collator_compare_func( const void* a, const void* b )
     209             : {
     210             :         Bucket *f;
     211             :         Bucket *s;
     212             :         zval result;
     213             :         zval *first;
     214             :         zval *second;
     215             : 
     216         209 :         f = (Bucket *) a;
     217         209 :         s = (Bucket *) b;
     218             : 
     219         209 :         first = &f->val;
     220         209 :         second = &s->val;
     221             : 
     222         209 :         if( INTL_G(compare_func)( &result, first, second) == FAILURE )
     223           0 :                 return 0;
     224             : 
     225         208 :         if( Z_TYPE(result) == IS_DOUBLE )
     226             :         {
     227           0 :                 if( Z_DVAL(result) < 0 )
     228           0 :                         return -1;
     229           0 :                 else if( Z_DVAL(result) > 0 )
     230           0 :                         return 1;
     231             :                 else
     232           0 :                         return 0;
     233             :         }
     234             : 
     235         208 :         convert_to_long(&result);
     236             : 
     237         208 :         if( Z_LVAL(result) < 0 )
     238          62 :                 return -1;
     239         146 :         else if( Z_LVAL(result) > 0 )
     240         140 :                 return 1;
     241             : 
     242           6 :         return 0;
     243             : }
     244             : /* }}} */
     245             : 
     246             : /* {{{ collator_cmp_sort_keys
     247             :  * Compare sort keys
     248             :  */
     249         106 : static int collator_cmp_sort_keys( const void *p1, const void *p2 )
     250             : {
     251         106 :         char* key1 = ((collator_sort_key_index_t*)p1)->key;
     252         106 :         char* key2 = ((collator_sort_key_index_t*)p2)->key;
     253             : 
     254         106 :         return strcmp( key1, key2 );
     255             : }
     256             : /* }}} */
     257             : 
     258             : /* {{{ collator_get_compare_function
     259             :  * Choose compare function according to sort flags.
     260             :  */
     261          75 : static collator_compare_func_t collator_get_compare_function( const zend_long sort_flags )
     262             : {
     263             :         collator_compare_func_t func;
     264             : 
     265          75 :         switch( sort_flags )
     266             :         {
     267             :                 case COLLATOR_SORT_NUMERIC:
     268           0 :                         func = collator_numeric_compare_function;
     269           0 :                         break;
     270             : 
     271             :                 case COLLATOR_SORT_STRING:
     272          16 :                         func = collator_icu_compare_function;
     273          16 :                         break;
     274             : 
     275             :                 case COLLATOR_SORT_REGULAR:
     276             :                 default:
     277          59 :                         func = collator_regular_compare_function;
     278             :                         break;
     279             :         }
     280             : 
     281          75 :         return func;
     282             : }
     283             : /* }}} */
     284             : 
     285             : /* {{{ collator_sort_internal
     286             :  * Common code shared by collator_sort() and collator_asort() API functions.
     287             :  */
     288          75 : static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
     289             : {
     290             :         zval           saved_collator;
     291          75 :         zval*          array            = NULL;
     292          75 :         HashTable*     hash             = NULL;
     293          75 :         zend_long           sort_flags       = COLLATOR_SORT_REGULAR;
     294             : 
     295          75 :         COLLATOR_METHOD_INIT_VARS
     296             : 
     297             :         /* Parse parameters. */
     298          75 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa/|l",
     299             :                 &object, Collator_ce_ptr, &array, &sort_flags ) == FAILURE )
     300             :         {
     301           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     302             :                         "collator_sort_internal: unable to parse input params", 0 );
     303             : 
     304           0 :                 RETURN_FALSE;
     305             :         }
     306             : 
     307             :         /* Fetch the object. */
     308          75 :         COLLATOR_METHOD_FETCH_OBJECT;
     309             : 
     310             :         /* Set 'compare function' according to sort flags. */
     311          75 :         INTL_G(compare_func) = collator_get_compare_function( sort_flags );
     312             : 
     313          75 :         hash = HASH_OF( array );
     314             : 
     315             :         /* Convert strings in the specified array from UTF-8 to UTF-16. */
     316          75 :         collator_convert_hash_from_utf8_to_utf16( hash, COLLATOR_ERROR_CODE_P( co ) );
     317          75 :         COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-8 to UTF-16" );
     318             : 
     319             :         /* Save specified collator in the request-global (?) variable. */
     320          75 :         ZVAL_COPY_VALUE(&saved_collator, &INTL_G( current_collator ));
     321          75 :         ZVAL_COPY_VALUE(&INTL_G( current_collator ), object);
     322             : 
     323             :         /* Sort specified array. */
     324          75 :         zend_hash_sort(hash, collator_compare_func, renumber);
     325             : 
     326             :         /* Restore saved collator. */
     327          74 :         ZVAL_COPY_VALUE(&INTL_G( current_collator ), &saved_collator);
     328             : 
     329             :         /* Convert strings in the specified array back to UTF-8. */
     330          74 :         collator_convert_hash_from_utf16_to_utf8( hash, COLLATOR_ERROR_CODE_P( co ) );
     331          74 :         COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-16 to UTF-8" );
     332             : 
     333          74 :         RETURN_TRUE;
     334             : }
     335             : /* }}} */
     336             : 
     337             : /* {{{ proto bool Collator::sort( Collator $coll, array(string) $arr [, int $sort_flags] )
     338             :  * Sort array using specified collator. }}} */
     339             : /* {{{ proto bool collator_sort(  Collator $coll, array(string) $arr [, int $sort_flags] )
     340             :  * Sort array using specified collator.
     341             :  */
     342          49 : PHP_FUNCTION( collator_sort )
     343             : {
     344          49 :         collator_sort_internal( TRUE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
     345          48 : }
     346             : /* }}} */
     347             : 
     348          62 : static void collator_sortkey_swap(collator_sort_key_index_t *p, collator_sort_key_index_t *q) /* {{{ */
     349             : {
     350             :         collator_sort_key_index_t t;
     351          62 :         t = *p;
     352          62 :         *p = *q;
     353          62 :         *q = t;
     354          62 : }
     355             : /* }}} */
     356             : 
     357             : /* {{{ proto bool Collator::sortWithSortKeys( Collator $coll, array(string) $arr )
     358             :  * Equivalent to standard PHP sort using Collator.
     359             :  * Uses ICU ucol_getSortKey for performance. }}} */
     360             : /* {{{ proto bool collator_sort_with_sort_keys( Collator $coll, array(string) $arr )
     361             :  * Equivalent to standard PHP sort using Collator.
     362             :  * Uses ICU ucol_getSortKey for performance.
     363             :  */
     364          37 : PHP_FUNCTION( collator_sort_with_sort_keys )
     365             : {
     366          37 :         zval*       array                = NULL;
     367          37 :         HashTable*  hash                 = NULL;
     368          37 :         zval*       hashData             = NULL;                     /* currently processed item of input hash */
     369             : 
     370          37 :         char*       sortKeyBuf           = NULL;                     /* buffer to store sort keys */
     371          37 :         uint32_t    sortKeyBufSize       = DEF_SORT_KEYS_BUF_SIZE;   /* buffer size */
     372          37 :         ptrdiff_t   sortKeyBufOffset     = 0;                        /* pos in buffer to store sort key */
     373          37 :         int32_t     sortKeyLen           = 0;                        /* the length of currently processing key */
     374          37 :         uint32_t    bufLeft              = 0;
     375          37 :         uint32_t    bufIncrement         = 0;
     376             : 
     377          37 :         collator_sort_key_index_t* sortKeyIndxBuf = NULL;            /* buffer to store 'indexes' which will be passed to 'qsort' */
     378          37 :         uint32_t    sortKeyIndxBufSize   = DEF_SORT_KEYS_INDX_BUF_SIZE;
     379          37 :         uint32_t    sortKeyIndxSize      = sizeof( collator_sort_key_index_t );
     380             : 
     381          37 :         uint32_t    sortKeyCount         = 0;
     382          37 :         uint32_t    j                    = 0;
     383             : 
     384          37 :         UChar*      utf16_buf            = NULL;                     /* tmp buffer to hold current processing string in utf-16 */
     385          37 :         int         utf16_buf_size       = DEF_UTF16_BUF_SIZE;       /* the length of utf16_buf */
     386          37 :         int         utf16_len            = 0;                        /* length of converted string */
     387             : 
     388          37 :         HashTable* sortedHash            = NULL;
     389             : 
     390          37 :         COLLATOR_METHOD_INIT_VARS
     391             : 
     392             :         /* Parse parameters. */
     393          37 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Oa",
     394             :                 &object, Collator_ce_ptr, &array ) == FAILURE )
     395             :         {
     396           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     397             :                         "collator_sort_with_sort_keys: unable to parse input params", 0 );
     398             : 
     399           0 :                 RETURN_FALSE;
     400             :         }
     401             : 
     402             :         /* Fetch the object. */
     403          37 :         COLLATOR_METHOD_FETCH_OBJECT;
     404             : 
     405          37 :         if (!co || !co->ucoll) {
     406           1 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
     407           1 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     408             :                         "Object not initialized", 0 );
     409           1 :                 php_error_docref(NULL, E_RECOVERABLE_ERROR, "Object not initialized");
     410             : 
     411           0 :                 RETURN_FALSE;
     412             :         }
     413             : 
     414             :         /*
     415             :          * Sort specified array.
     416             :          */
     417          72 :         hash = HASH_OF( array );
     418             : 
     419          36 :         if( !hash || zend_hash_num_elements( hash ) == 0 )
     420           0 :                 RETURN_TRUE;
     421             : 
     422             :         /* Create bufers */
     423          36 :         sortKeyBuf     = ecalloc( sortKeyBufSize,     sizeof( char    ) );
     424          36 :         sortKeyIndxBuf = ecalloc( sortKeyIndxBufSize, sizeof( uint8_t ) );
     425          36 :         utf16_buf      = eumalloc( utf16_buf_size );
     426             : 
     427             :         /* Iterate through input hash and create a sort key for each value. */
     428         256 :         ZEND_HASH_FOREACH_VAL(hash, hashData) {
     429             :                 /* Convert current hash item from UTF-8 to UTF-16LE and save the result to utf16_buf. */
     430             : 
     431         110 :                 utf16_len = utf16_buf_size;
     432             : 
     433             :                 /* Process string values only. */
     434         110 :                 if( Z_TYPE_P( hashData ) == IS_STRING )
     435             :                 {
     436         108 :                         intl_convert_utf8_to_utf16( &utf16_buf, &utf16_len, Z_STRVAL_P( hashData ), Z_STRLEN_P( hashData ), COLLATOR_ERROR_CODE_P( co ) );
     437             : 
     438         108 :                         if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
     439             :                         {
     440           0 :                                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
     441           0 :                                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ), "Sort with sort keys failed", 0 );
     442             : 
     443           0 :                                 if( utf16_buf )
     444           0 :                                         efree( utf16_buf );
     445             : 
     446           0 :                                 efree( sortKeyIndxBuf );
     447           0 :                                 efree( sortKeyBuf );
     448             : 
     449           0 :                                 RETURN_FALSE;
     450             :                         }
     451             :                 }
     452             :                 else
     453             :                 {
     454             :                         /* Set empty string */
     455           2 :                         utf16_len = 0;
     456           2 :                         utf16_buf[utf16_len] = 0;
     457             :                 }
     458             : 
     459         110 :                 if( (utf16_len + 1) > utf16_buf_size )
     460           0 :                         utf16_buf_size = utf16_len + 1;
     461             : 
     462             :                 /* Get sort key, reallocating the buffer if needed. */
     463         110 :                 bufLeft = sortKeyBufSize - sortKeyBufOffset;
     464             : 
     465         110 :                 sortKeyLen = ucol_getSortKey( co->ucoll,
     466             :                                                                           utf16_buf,
     467             :                                                                           utf16_len,
     468             :                                                                           (uint8_t*)sortKeyBuf + sortKeyBufOffset,
     469             :                                                                           bufLeft );
     470             : 
     471             :                 /* check for sortKeyBuf overflow, increasing its size of the buffer if needed */
     472         110 :                 if( sortKeyLen > bufLeft )
     473             :                 {
     474           0 :                         bufIncrement = ( sortKeyLen > DEF_SORT_KEYS_BUF_INCREMENT ) ? sortKeyLen : DEF_SORT_KEYS_BUF_INCREMENT;
     475             : 
     476           0 :                         sortKeyBufSize += bufIncrement;
     477           0 :                         bufLeft += bufIncrement;
     478             : 
     479           0 :                         sortKeyBuf = erealloc( sortKeyBuf, sortKeyBufSize );
     480             : 
     481           0 :                         sortKeyLen = ucol_getSortKey( co->ucoll, utf16_buf, utf16_len, (uint8_t*)sortKeyBuf + sortKeyBufOffset, bufLeft );
     482             :                 }
     483             : 
     484             :                 /*  check sortKeyIndxBuf overflow, increasing its size of the buffer if needed */
     485         110 :                 if( ( sortKeyCount + 1 ) * sortKeyIndxSize > sortKeyIndxBufSize )
     486             :                 {
     487           0 :                         bufIncrement = ( sortKeyIndxSize > DEF_SORT_KEYS_INDX_BUF_INCREMENT ) ? sortKeyIndxSize : DEF_SORT_KEYS_INDX_BUF_INCREMENT;
     488             : 
     489           0 :                         sortKeyIndxBufSize += bufIncrement;
     490             : 
     491           0 :                         sortKeyIndxBuf = erealloc( sortKeyIndxBuf, sortKeyIndxBufSize );
     492             :                 }
     493             : 
     494         110 :                 sortKeyIndxBuf[sortKeyCount].key = (char*)sortKeyBufOffset;    /* remember just offset, cause address */
     495             :                                                                                /* of 'sortKeyBuf' may be changed due to realloc. */
     496         110 :                 sortKeyIndxBuf[sortKeyCount].zstr = hashData;
     497             : 
     498         110 :                 sortKeyBufOffset += sortKeyLen;
     499         110 :                 ++sortKeyCount;
     500             : 
     501             :         } ZEND_HASH_FOREACH_END();
     502             : 
     503             :         /* update ptrs to point to valid keys. */
     504         146 :         for( j = 0; j < sortKeyCount; j++ )
     505         110 :                 sortKeyIndxBuf[j].key = sortKeyBuf + (ptrdiff_t)sortKeyIndxBuf[j].key;
     506             : 
     507             :         /* sort it */
     508          36 :         zend_sort( sortKeyIndxBuf, sortKeyCount,
     509             :                         sortKeyIndxSize, collator_cmp_sort_keys, (swap_func_t)collator_sortkey_swap);
     510             : 
     511          36 :         zval_ptr_dtor( array );
     512             :         /* for resulting hash we'll assign new hash keys rather then reordering */
     513          36 :         array_init(array);
     514             : 
     515         146 :         for( j = 0; j < sortKeyCount; j++ )
     516             :         {
     517         110 :                 Z_TRY_ADDREF_P( sortKeyIndxBuf[j].zstr );
     518         110 :                 zend_hash_next_index_insert( Z_ARRVAL_P(array), sortKeyIndxBuf[j].zstr);
     519             :         }
     520             : 
     521          36 :         if( utf16_buf )
     522          36 :                 efree( utf16_buf );
     523             : 
     524          36 :         efree( sortKeyIndxBuf );
     525          36 :         efree( sortKeyBuf );
     526             : 
     527          36 :         RETURN_TRUE;
     528             : }
     529             : /* }}} */
     530             : 
     531             : /* {{{ proto bool Collator::asort( Collator $coll, array(string) $arr )
     532             :  * Sort array using specified collator, maintaining index association. }}} */
     533             : /* {{{ proto bool collator_asort( Collator $coll, array(string) $arr )
     534             :  * Sort array using specified collator, maintaining index association.
     535             :  */
     536          26 : PHP_FUNCTION( collator_asort )
     537             : {
     538          26 :         collator_sort_internal( FALSE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
     539          26 : }
     540             : /* }}} */
     541             : 
     542             : /* {{{ proto bool Collator::getSortKey( Collator $coll, string $str )
     543             :  * Get a sort key for a string from a Collator. }}} */
     544             : /* {{{ proto bool collator_get_sort_key( Collator $coll, string $str )
     545             :  * Get a sort key for a string from a Collator. */
     546           1 : PHP_FUNCTION( collator_get_sort_key )
     547             : {
     548           1 :         char*            str      = NULL;
     549           1 :         size_t           str_len  = 0;
     550           1 :         UChar*           ustr     = NULL;
     551           1 :         int32_t          ustr_len = 0;
     552           1 :         uint8_t*         key     = NULL;
     553           1 :         int              key_len = 0;
     554             : 
     555           1 :         COLLATOR_METHOD_INIT_VARS
     556             : 
     557             :         /* Parse parameters. */
     558           1 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS(), getThis(), "Os",
     559             :                 &object, Collator_ce_ptr, &str, &str_len ) == FAILURE )
     560             :         {
     561           0 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     562             :                          "collator_get_sort_key: unable to parse input params", 0 );
     563             : 
     564           0 :                 RETURN_FALSE;
     565             :         }
     566             : 
     567             :         /* Fetch the object. */
     568           1 :         COLLATOR_METHOD_FETCH_OBJECT;
     569             : 
     570           1 :         if (!co || !co->ucoll) {
     571           1 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
     572           1 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     573             :                         "Object not initialized", 0 );
     574           1 :                 php_error_docref(NULL, E_RECOVERABLE_ERROR, "Object not initialized");
     575             : 
     576           0 :                 RETURN_FALSE;
     577             :         }
     578             : 
     579             :         /*
     580             :          * Compare given strings (converting them to UTF-16 first).
     581             :          */
     582             : 
     583             :         /* First convert the strings to UTF-16. */
     584           0 :         intl_convert_utf8_to_utf16(
     585             :                 &ustr, &ustr_len, str, str_len, COLLATOR_ERROR_CODE_P( co ) );
     586           0 :         if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
     587             :         {
     588             :                 /* Set global error code. */
     589           0 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) );
     590             : 
     591             :                 /* Set error messages. */
     592           0 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     593             :                         "Error converting first argument to UTF-16", 0 );
     594           0 :                 efree( ustr );
     595           0 :                 RETURN_FALSE;
     596             :         }
     597             : 
     598             :         /* ucol_getSortKey is exception in that the key length includes the
     599             :          * NUL terminator*/
     600           0 :         key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, 0);
     601           0 :         if(!key_len) {
     602           0 :                 efree( ustr );
     603           0 :                 RETURN_FALSE;
     604             :         }
     605           0 :         key = emalloc(key_len);
     606           0 :         key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, key_len);
     607           0 :         efree( ustr );
     608           0 :         if(!key_len) {
     609           0 :                 RETURN_FALSE;
     610             :         }
     611           0 :         RETVAL_STRINGL((char *)key, key_len - 1);
     612             :         //????
     613           0 :         efree(key);
     614             : }
     615             : /* }}} */
     616             : 
     617             : /*
     618             :  * Local variables:
     619             :  * tab-width: 4
     620             :  * c-basic-offset: 4
     621             :  * End:
     622             :  * vim600: noet sw=4 ts=4 fdm=marker
     623             :  * vim<600: noet sw=4 ts=4
     624             :  */

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:58:56 +0000 (27 hours ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.