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: 189 250 75.6 %
Date: 2014-07-21 Functions: 10 11 90.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       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 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         197 : static int collator_regular_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
      54             : {
      55         197 :         Collator_object* co = NULL;
      56             : 
      57         197 :         int rc      = SUCCESS;
      58             : 
      59         197 :         zval* str1  = collator_convert_object_to_string( op1 TSRMLS_CC );
      60         197 :         zval* str2  = collator_convert_object_to_string( op2 TSRMLS_CC );
      61             : 
      62         197 :         zval* num1  = NULL;
      63         197 :         zval* num2  = NULL;
      64         197 :         zval* norm1 = NULL;
      65         197 :         zval* norm2 = NULL;
      66             : 
      67             :         /* If both args are strings AND either of args is not numeric string
      68             :          * then use ICU-compare. Otherwise PHP-compare. */
      69         745 :         if( Z_TYPE_P(str1) == IS_STRING && Z_TYPE_P(str2) == IS_STRING &&
      70         350 :                 ( str1 == ( num1 = collator_convert_string_to_number_if_possible( str1 ) ) ||
      71          40 :                   str2 == ( num2 = collator_convert_string_to_number_if_possible( str2 ) ) ) )
      72             :         {
      73             :                 /* Fetch collator object. */
      74         159 :                 co = (Collator_object *) zend_object_store_get_object( INTL_G(current_collator) TSRMLS_CC );
      75             : 
      76         159 :                 if (!co || !co->ucoll) {
      77           1 :                         intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
      78           1 :                         intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
      79             :                                 "Object not initialized", 0 TSRMLS_CC );
      80           1 :                         php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
      81             : 
      82             :                 }
      83             : 
      84             :                 /* Compare the strings using ICU. */
      85         790 :                 result->value.lval = ucol_strcoll(
      86         158 :                                 co->ucoll,
      87         316 :                                 INTL_Z_STRVAL_P(str1), INTL_Z_STRLEN_P(str1),
      88         316 :                                 INTL_Z_STRVAL_P(str2), INTL_Z_STRLEN_P(str2) );
      89         158 :                 result->type = IS_LONG;
      90             :         }
      91             :         else
      92             :         {
      93             :                 /* num1 is set if str1 and str2 are strings. */
      94          38 :                 if( num1 )
      95             :                 {
      96          16 :                         if( num1 == str1 )
      97             :                         {
      98             :                                 /* str1 is string but not numeric string
      99             :                                  * just convert it to utf8. 
     100             :                                  */
     101           0 :                                 norm1 = collator_convert_zstr_utf16_to_utf8( str1 );
     102             : 
     103             :                                 /* num2 is not set but str2 is string => do normalization. */
     104           0 :                                 norm2 = collator_normalize_sort_argument( str2 );
     105             :                         }
     106             :                         else
     107             :                         {
     108             :                                 /* str1 is numeric strings => passthru to PHP-compare. */
     109          16 :                                 zval_add_ref( &num1 );
     110          16 :                                 norm1 = num1;
     111             : 
     112             :                                 /* str2 is numeric strings => passthru to PHP-compare. */
     113          16 :                                 zval_add_ref( &num2 );
     114          16 :                                 norm2 = num2;
     115             :                         }
     116             :                 }
     117             :                 else
     118             :                 {
     119             :                         /* num1 is not set if str1 or str2 is not a string => do normalization. */
     120          22 :                         norm1 = collator_normalize_sort_argument( str1 );
     121             : 
     122             :                         /* if num1 is not set then num2 is not set as well => do normalization. */
     123          22 :                         norm2 = collator_normalize_sort_argument( str2 );
     124             :                 }
     125             : 
     126          38 :                 rc = compare_function( result, norm1, norm2 TSRMLS_CC );
     127             : 
     128          38 :                 zval_ptr_dtor( &norm1 );
     129          38 :                 zval_ptr_dtor( &norm2 );
     130             :         }
     131             : 
     132         196 :         if( num1 )
     133         174 :                 zval_ptr_dtor( &num1 );
     134             : 
     135         196 :         if( num2 )
     136          20 :                 zval_ptr_dtor( &num2 );
     137             : 
     138         196 :         zval_ptr_dtor( &str1 );
     139         196 :         zval_ptr_dtor( &str2 );
     140             : 
     141         196 :         return rc;
     142             : }
     143             : /* }}} */
     144             : 
     145             : /* {{{ collator_numeric_compare_function
     146             :  * Convert input args to double and compare it.
     147             :  */
     148           0 : static int collator_numeric_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
     149             : {
     150           0 :         int rc     = SUCCESS;
     151           0 :         zval* num1 = NULL;
     152           0 :         zval* num2 = NULL;
     153             : 
     154           0 :         if( Z_TYPE_P(op1) == IS_STRING )
     155             :         {
     156           0 :                 num1 = collator_convert_string_to_double( op1 );
     157           0 :                 op1 = num1;
     158             :         }
     159             : 
     160           0 :         if( Z_TYPE_P(op2) == IS_STRING )
     161             :         {
     162           0 :                 num2 = collator_convert_string_to_double( op2 );
     163           0 :                 op2 = num2;
     164             :         }
     165             : 
     166           0 :         rc = numeric_compare_function( result, op1, op2 TSRMLS_CC);
     167             : 
     168           0 :         if( num1 )
     169           0 :                 zval_ptr_dtor( &num1 );
     170           0 :         if( num2 )
     171           0 :                 zval_ptr_dtor( &num2 );
     172             : 
     173           0 :         return rc;
     174             : }
     175             : /* }}} */
     176             : 
     177             : /* {{{ collator_icu_compare_function
     178             :  * Direct use of ucol_strcoll.
     179             : */
     180          40 : static int collator_icu_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
     181             : {
     182          40 :         int rc              = SUCCESS;
     183          40 :         Collator_object* co = NULL;
     184          40 :         zval* str1          = NULL;
     185          40 :         zval* str2          = NULL;
     186             : 
     187          40 :         str1 = collator_make_printable_zval( op1 );
     188          40 :         str2 = collator_make_printable_zval( op2 );
     189             : 
     190             :         /* Fetch collator object. */
     191          40 :         co = (Collator_object *) zend_object_store_get_object( INTL_G(current_collator) TSRMLS_CC );
     192             : 
     193             :         /* Compare the strings using ICU. */
     194         200 :         result->value.lval = ucol_strcoll(
     195          40 :                         co->ucoll,
     196          80 :                         INTL_Z_STRVAL_P(str1), INTL_Z_STRLEN_P(str1),
     197          80 :                         INTL_Z_STRVAL_P(str2), INTL_Z_STRLEN_P(str2) );
     198          40 :         result->type = IS_LONG;
     199             : 
     200          40 :         zval_ptr_dtor( &str1 );
     201          40 :         zval_ptr_dtor( &str2 );
     202             : 
     203          40 :         return rc;
     204             : }
     205             : /* }}} */
     206             : 
     207             : /* {{{ collator_compare_func
     208             :  * Taken from PHP5 source (array_data_compare).
     209             :  */
     210         237 : static int collator_compare_func( const void* a, const void* b TSRMLS_DC )
     211             : {
     212             :         Bucket *f;
     213             :         Bucket *s;
     214             :         zval result;
     215             :         zval *first;
     216             :         zval *second;
     217             : 
     218         237 :         f = *((Bucket **) a);
     219         237 :         s = *((Bucket **) b);
     220             : 
     221         237 :         first = *((zval **) f->pData);
     222         237 :         second = *((zval **) s->pData);
     223             : 
     224         237 :         if( INTL_G(compare_func)( &result, first, second TSRMLS_CC) == FAILURE )
     225           0 :                 return 0;
     226             : 
     227         236 :         if( Z_TYPE(result) == IS_DOUBLE )
     228             :         {
     229           0 :                 if( Z_DVAL(result) < 0 )
     230           0 :                         return -1;
     231           0 :                 else if( Z_DVAL(result) > 0 )
     232           0 :                         return 1;
     233             :                 else
     234           0 :                         return 0;
     235             :         }
     236             : 
     237         236 :         convert_to_long(&result);
     238             : 
     239         236 :         if( Z_LVAL(result) < 0 )
     240         120 :                 return -1;
     241         116 :         else if( Z_LVAL(result) > 0 )
     242         110 :                 return 1;
     243             : 
     244           6 :         return 0;
     245             : }
     246             : /* }}} */
     247             : 
     248             : /* {{{ collator_cmp_sort_keys
     249             :  * Compare sort keys
     250             :  */
     251         120 : static int collator_cmp_sort_keys( const void *p1, const void *p2 TSRMLS_DC )
     252             : {
     253         120 :         char* key1 = ((collator_sort_key_index_t*)p1)->key;
     254         120 :         char* key2 = ((collator_sort_key_index_t*)p2)->key;
     255             : 
     256         120 :         return strcmp( key1, key2 );
     257             : }
     258             : /* }}} */
     259             : 
     260             : /* {{{ collator_get_compare_function
     261             :  * Choose compare function according to sort flags.
     262             :  */
     263          75 : static collator_compare_func_t collator_get_compare_function( const long sort_flags )
     264             : {
     265             :         collator_compare_func_t func;
     266             : 
     267          75 :         switch( sort_flags )
     268             :         {
     269             :                 case COLLATOR_SORT_NUMERIC:
     270           0 :                         func = collator_numeric_compare_function;
     271           0 :                         break;
     272             : 
     273             :                 case COLLATOR_SORT_STRING:
     274          16 :                         func = collator_icu_compare_function;
     275          16 :                         break;
     276             : 
     277             :                 case COLLATOR_SORT_REGULAR:
     278             :                 default:
     279          59 :                         func = collator_regular_compare_function;
     280             :                         break;
     281             :         }
     282             : 
     283          75 :         return func;
     284             : }
     285             : /* }}} */
     286             : 
     287             : /* {{{ collator_sort_internal
     288             :  * Common code shared by collator_sort() and collator_asort() API functions.
     289             :  */
     290          77 : static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
     291             : {
     292          77 :         zval*          array            = NULL;
     293          77 :         HashTable*     hash             = NULL;
     294          77 :         zval*          saved_collator   = NULL;
     295          77 :         long           sort_flags       = COLLATOR_SORT_REGULAR;
     296             : 
     297          77 :         COLLATOR_METHOD_INIT_VARS
     298             : 
     299             :         /* Parse parameters. */
     300          77 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa|l",
     301             :                 &object, Collator_ce_ptr, &array, &sort_flags ) == FAILURE )
     302             :         {
     303           2 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     304             :                         "collator_sort_internal: unable to parse input params", 0 TSRMLS_CC );
     305             : 
     306           2 :                 RETURN_FALSE;
     307             :         }
     308             : 
     309             :         /* Fetch the object. */
     310          75 :         COLLATOR_METHOD_FETCH_OBJECT;
     311             : 
     312             :         /* Set 'compare function' according to sort flags. */
     313          75 :         INTL_G(compare_func) = collator_get_compare_function( sort_flags );
     314             : 
     315          75 :         hash = HASH_OF( array );
     316             : 
     317             :         /* Convert strings in the specified array from UTF-8 to UTF-16. */
     318          75 :         collator_convert_hash_from_utf8_to_utf16( hash, COLLATOR_ERROR_CODE_P( co ) );
     319          75 :         COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-8 to UTF-16" );
     320             : 
     321             :         /* Save specified collator in the request-global (?) variable. */
     322          75 :         saved_collator = INTL_G( current_collator );
     323          75 :         INTL_G( current_collator ) = object;
     324             : 
     325             :         /* Sort specified array. */
     326          75 :         zend_hash_sort( hash, zend_qsort, collator_compare_func, renumber TSRMLS_CC );
     327             : 
     328             :         /* Restore saved collator. */
     329          74 :         INTL_G( current_collator ) = saved_collator;
     330             : 
     331             :         /* Convert strings in the specified array back to UTF-8. */
     332          74 :         collator_convert_hash_from_utf16_to_utf8( hash, COLLATOR_ERROR_CODE_P( co ) );
     333          74 :         COLLATOR_CHECK_STATUS( co, "Error converting hash from UTF-16 to UTF-8" );
     334             : 
     335          74 :         RETURN_TRUE;
     336             : }
     337             : /* }}} */
     338             : 
     339             : /* {{{ proto bool Collator::sort( Collator $coll, array(string) $arr [, int $sort_flags] )
     340             :  * Sort array using specified collator. }}} */
     341             : /* {{{ proto bool collator_sort(  Collator $coll, array(string) $arr [, int $sort_flags] )
     342             :  * Sort array using specified collator.
     343             :  */
     344          50 : PHP_FUNCTION( collator_sort )
     345             : {
     346          50 :         collator_sort_internal( TRUE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
     347          49 : }
     348             : /* }}} */
     349             : 
     350             : /* {{{ proto bool Collator::sortWithSortKeys( Collator $coll, array(string) $arr )
     351             :  * Equivalent to standard PHP sort using Collator.
     352             :  * Uses ICU ucol_getSortKey for performance. }}} */
     353             : /* {{{ proto bool collator_sort_with_sort_keys( Collator $coll, array(string) $arr )
     354             :  * Equivalent to standard PHP sort using Collator.
     355             :  * Uses ICU ucol_getSortKey for performance.
     356             :  */
     357          38 : PHP_FUNCTION( collator_sort_with_sort_keys )
     358             : {
     359          38 :         zval*       array                = NULL;
     360          38 :         HashTable*  hash                 = NULL;
     361          38 :         zval**      hashData             = NULL;                     /* currently processed item of input hash */
     362             : 
     363          38 :         char*       sortKeyBuf           = NULL;                     /* buffer to store sort keys */
     364          38 :         uint32_t    sortKeyBufSize       = DEF_SORT_KEYS_BUF_SIZE;   /* buffer size */
     365          38 :         ptrdiff_t   sortKeyBufOffset     = 0;                        /* pos in buffer to store sort key */
     366          38 :         int32_t     sortKeyLen           = 0;                        /* the length of currently processing key */
     367          38 :         uint32_t    bufLeft              = 0;
     368          38 :         uint32_t    bufIncrement         = 0;
     369             : 
     370          38 :         collator_sort_key_index_t* sortKeyIndxBuf = NULL;            /* buffer to store 'indexes' which will be passed to 'qsort' */
     371          38 :         uint32_t    sortKeyIndxBufSize   = DEF_SORT_KEYS_INDX_BUF_SIZE;
     372          38 :         uint32_t    sortKeyIndxSize      = sizeof( collator_sort_key_index_t );
     373             : 
     374          38 :         uint32_t    sortKeyCount         = 0;
     375          38 :         uint32_t    j                    = 0;
     376             : 
     377          38 :         UChar*      utf16_buf            = NULL;                     /* tmp buffer to hold current processing string in utf-16 */
     378          38 :         int         utf16_buf_size       = DEF_UTF16_BUF_SIZE;       /* the length of utf16_buf */
     379          38 :         int         utf16_len            = 0;                        /* length of converted string */
     380             : 
     381          38 :         HashTable* sortedHash            = NULL;
     382             : 
     383          38 :         COLLATOR_METHOD_INIT_VARS
     384             : 
     385             :         /* Parse parameters. */
     386          38 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
     387             :                 &object, Collator_ce_ptr, &array ) == FAILURE )
     388             :         {
     389           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     390             :                         "collator_sort_with_sort_keys: unable to parse input params", 0 TSRMLS_CC );
     391             : 
     392           1 :                 RETURN_FALSE;
     393             :         }
     394             : 
     395             :         /* Fetch the object. */
     396          37 :         COLLATOR_METHOD_FETCH_OBJECT;
     397             : 
     398          37 :         if (!co || !co->ucoll) {
     399           1 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
     400           1 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     401             :                         "Object not initialized", 0 TSRMLS_CC );
     402           1 :                 php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
     403             : 
     404           0 :                 RETURN_FALSE;
     405             :         }
     406             : 
     407             :         /*
     408             :          * Sort specified array.
     409             :          */
     410          36 :         hash = HASH_OF( array );
     411             : 
     412          36 :         if( !hash || zend_hash_num_elements( hash ) == 0 )
     413           0 :                 RETURN_TRUE;
     414             : 
     415             :         /* Create bufers */
     416          36 :         sortKeyBuf     = ecalloc( sortKeyBufSize,     sizeof( char    ) );
     417          36 :         sortKeyIndxBuf = ecalloc( sortKeyIndxBufSize, sizeof( uint8_t ) );
     418          36 :         utf16_buf      = eumalloc( utf16_buf_size );
     419             : 
     420             :         /* Iterate through input hash and create a sort key for each value. */
     421          36 :         zend_hash_internal_pointer_reset( hash );
     422         182 :         while( zend_hash_get_current_data( hash, (void**) &hashData ) == SUCCESS )
     423             :         {
     424             :                 /* Convert current hash item from UTF-8 to UTF-16LE and save the result to utf16_buf. */
     425             : 
     426         110 :                 utf16_len = utf16_buf_size;
     427             : 
     428             :                 /* Process string values only. */
     429         110 :                 if( Z_TYPE_PP( hashData ) == IS_STRING )
     430             :                 {
     431         108 :                         intl_convert_utf8_to_utf16( &utf16_buf, &utf16_len, Z_STRVAL_PP( hashData ), Z_STRLEN_PP( hashData ), COLLATOR_ERROR_CODE_P( co ) );
     432             : 
     433         108 :                         if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
     434             :                         {
     435           0 :                                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
     436           0 :                                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ), "Sort with sort keys failed", 0 TSRMLS_CC );
     437             : 
     438           0 :                                 if( utf16_buf )
     439           0 :                                         efree( utf16_buf );
     440             : 
     441           0 :                                 efree( sortKeyIndxBuf );
     442           0 :                                 efree( sortKeyBuf );
     443             : 
     444           0 :                                 RETURN_FALSE;
     445             :                         }
     446             :                 }
     447             :                 else
     448             :                 {
     449             :                         /* Set empty string */
     450           2 :                         utf16_len = 0;
     451           2 :                         utf16_buf[utf16_len] = 0;
     452             :                 }
     453             : 
     454         110 :                 if( (utf16_len + 1) > utf16_buf_size )
     455           0 :                         utf16_buf_size = utf16_len + 1;
     456             : 
     457             :                 /* Get sort key, reallocating the buffer if needed. */
     458         110 :                 bufLeft = sortKeyBufSize - sortKeyBufOffset;
     459             : 
     460         110 :                 sortKeyLen = ucol_getSortKey( co->ucoll,
     461             :                                                                           utf16_buf,
     462             :                                                                           utf16_len,
     463             :                                                                           (uint8_t*)sortKeyBuf + sortKeyBufOffset,
     464             :                                                                           bufLeft );
     465             : 
     466             :                 /* check for sortKeyBuf overflow, increasing its size of the buffer if needed */
     467         110 :                 if( sortKeyLen > bufLeft )
     468             :                 {
     469           0 :                         bufIncrement = ( sortKeyLen > DEF_SORT_KEYS_BUF_INCREMENT ) ? sortKeyLen : DEF_SORT_KEYS_BUF_INCREMENT;
     470             : 
     471           0 :                         sortKeyBufSize += bufIncrement;
     472           0 :                         bufLeft += bufIncrement;
     473             : 
     474           0 :                         sortKeyBuf = erealloc( sortKeyBuf, sortKeyBufSize );
     475             : 
     476           0 :                         sortKeyLen = ucol_getSortKey( co->ucoll, utf16_buf, utf16_len, (uint8_t*)sortKeyBuf + sortKeyBufOffset, bufLeft );
     477             :                 }
     478             : 
     479             :                 /*  check sortKeyIndxBuf overflow, increasing its size of the buffer if needed */
     480         110 :                 if( ( sortKeyCount + 1 ) * sortKeyIndxSize > sortKeyIndxBufSize )
     481             :                 {
     482           0 :                         bufIncrement = ( sortKeyIndxSize > DEF_SORT_KEYS_INDX_BUF_INCREMENT ) ? sortKeyIndxSize : DEF_SORT_KEYS_INDX_BUF_INCREMENT;
     483             : 
     484           0 :                         sortKeyIndxBufSize += bufIncrement;
     485             : 
     486           0 :                         sortKeyIndxBuf = erealloc( sortKeyIndxBuf, sortKeyIndxBufSize );
     487             :                 }
     488             : 
     489         110 :                 sortKeyIndxBuf[sortKeyCount].key = (char*)sortKeyBufOffset;    /* remeber just offset, cause address */
     490             :                                                                                /* of 'sortKeyBuf' may be changed due to realloc. */
     491         110 :                 sortKeyIndxBuf[sortKeyCount].zstr = hashData;
     492             : 
     493         110 :                 sortKeyBufOffset += sortKeyLen;
     494         110 :                 ++sortKeyCount;
     495             : 
     496         110 :                 zend_hash_move_forward( hash );
     497             :         }
     498             : 
     499             :         /* update ptrs to point to valid keys. */
     500         146 :         for( j = 0; j < sortKeyCount; j++ )
     501         110 :                 sortKeyIndxBuf[j].key = sortKeyBuf + (ptrdiff_t)sortKeyIndxBuf[j].key;
     502             : 
     503             :         /* sort it */
     504          36 :         zend_qsort( sortKeyIndxBuf, sortKeyCount, sortKeyIndxSize, collator_cmp_sort_keys TSRMLS_CC );
     505             : 
     506             :         /* for resulting hash we'll assign new hash keys rather then reordering */
     507          36 :         ALLOC_HASHTABLE( sortedHash );
     508          36 :         zend_hash_init( sortedHash, 0, NULL, ZVAL_PTR_DTOR, 0 );
     509             : 
     510         146 :         for( j = 0; j < sortKeyCount; j++ )
     511             :         {
     512         110 :                 zval_add_ref( sortKeyIndxBuf[j].zstr );
     513         110 :                 zend_hash_next_index_insert( sortedHash, sortKeyIndxBuf[j].zstr, sizeof(zval **), NULL );
     514             :         }
     515             : 
     516             :         /* Save sorted hash into return variable. */
     517          36 :         zval_dtor( array );
     518          36 :         (array)->value.ht = sortedHash;
     519          36 :         (array)->type = IS_ARRAY;
     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          27 : PHP_FUNCTION( collator_asort )
     537             : {
     538          27 :         collator_sort_internal( FALSE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
     539          27 : }
     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           2 : PHP_FUNCTION( collator_get_sort_key )
     547             : {
     548           2 :         char*            str      = NULL;
     549           2 :         int              str_len  = 0;
     550           2 :         UChar*           ustr     = NULL;
     551           2 :         int              ustr_len = 0;
     552           2 :         uint8_t*         key     = NULL;
     553           2 :         int              key_len = 0;
     554             : 
     555           2 :         COLLATOR_METHOD_INIT_VARS
     556             : 
     557             :         /* Parse parameters. */
     558           2 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
     559             :                 &object, Collator_ce_ptr, &str, &str_len ) == FAILURE )
     560             :         {
     561           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     562             :                          "collator_get_sort_key: unable to parse input params", 0 TSRMLS_CC );
     563             : 
     564           1 :                 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 ) TSRMLS_CC );
     572           1 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     573             :                         "Object not initialized", 0 TSRMLS_CC );
     574           1 :                 php_error_docref(NULL TSRMLS_CC, 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 ) TSRMLS_CC );
     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 TSRMLS_CC );
     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 :         RETURN_STRINGL((char *)key, key_len - 1, 0);
     612             : }
     613             : /* }}} */
     614             : 
     615             : /*
     616             :  * Local variables:
     617             :  * tab-width: 4
     618             :  * c-basic-offset: 4
     619             :  * End:
     620             :  * vim600: noet sw=4 ts=4 fdm=marker
     621             :  * vim<600: noet sw=4 ts=4
     622             :  */

Generated by: LCOV version 1.10

Generated at Tue, 22 Jul 2014 01:33:11 +0000 (10 days ago)

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