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: 173 235 73.6 %
Date: 2014-10-22 Functions: 10 11 90.9 %
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         197 : static int collator_regular_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
      54             : {
      55         197 :         Collator_object* co = NULL;
      56         197 :         int rc = SUCCESS;
      57             :         zval str1, str2;
      58             :         zval num1, num2;
      59             :         zval norm1, norm2;
      60         197 :         zval *num1_p = NULL, *num2_p = NULL;
      61         197 :         zval *norm1_p = NULL, *norm2_p = NULL;
      62         197 :         zval* str1_p  = collator_convert_object_to_string( op1, &str1 TSRMLS_CC );
      63         197 :         zval* str2_p  = collator_convert_object_to_string( op2, &str2 TSRMLS_CC );
      64             : 
      65             :         /* If both args are strings AND either of args is not numeric string
      66             :          * then use ICU-compare. Otherwise PHP-compare. */
      67         735 :         if( Z_TYPE_P(str1_p) == IS_STRING && Z_TYPE_P(str2_p) == IS_STRING &&
      68         175 :                 ( str1_p == ( num1_p = collator_convert_string_to_number_if_possible( str1_p, &num1 ) ) ||
      69          20 :                   str2_p == ( num2_p = collator_convert_string_to_number_if_possible( str2_p, &num2 ) ) ) )
      70             :         {
      71             :                 /* Fetch collator object. */
      72         159 :                 co = Z_INTL_COLLATOR_P(&INTL_G(current_collator));
      73             : 
      74         159 :                 if (!co || !co->ucoll) {
      75           1 :                         intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
      76           1 :                         intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
      77             :                                 "Object not initialized", 0 TSRMLS_CC );
      78           1 :                         php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
      79             : 
      80             :                 }
      81             : 
      82             :                 /* Compare the strings using ICU. */
      83         158 :                 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          38 :                 if( num1_p )
      92             :                 {
      93          16 :                         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          16 :                                 zval_add_ref( num1_p );
     107          16 :                                 norm1_p = num1_p;
     108             : 
     109             :                                 /* str2 is numeric strings => passthru to PHP-compare. */
     110          16 :                                 zval_add_ref( num2_p );
     111          16 :                                 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          22 :                         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          22 :                         norm2_p = collator_normalize_sort_argument( str2_p, &norm2 );
     121             :                 }
     122             : 
     123          38 :                 rc = compare_function( result, norm1_p, norm2_p TSRMLS_CC );
     124             : 
     125          38 :                 zval_ptr_dtor( norm1_p );
     126          38 :                 zval_ptr_dtor( norm2_p );
     127             :         }
     128             : 
     129         196 :         if( num1_p )
     130         174 :                 zval_ptr_dtor( num1_p );
     131             : 
     132         196 :         if( num2_p )
     133          20 :                 zval_ptr_dtor( num2_p );
     134             : 
     135         196 :         zval_ptr_dtor( str1_p );
     136         196 :         zval_ptr_dtor( str2_p );
     137             : 
     138         196 :         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 TSRMLS_DC)
     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 TSRMLS_CC);
     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          40 : static int collator_icu_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
     179             : {
     180             :         zval str1, str2;
     181          40 :         int rc              = SUCCESS;
     182          40 :         Collator_object* co = NULL;
     183          40 :         zval *str1_p        = NULL;
     184          40 :         zval *str2_p        = NULL;
     185             : 
     186          40 :         str1_p = collator_make_printable_zval( op1, &str1);
     187          40 :         str2_p = collator_make_printable_zval( op2, &str2 );
     188             : 
     189             :         /* Fetch collator object. */
     190          40 :         co = Z_INTL_COLLATOR_P(&INTL_G(current_collator));
     191             : 
     192             :         /* Compare the strings using ICU. */
     193          40 :         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          40 :         zval_ptr_dtor( str1_p );
     199          40 :         zval_ptr_dtor( str2_p );
     200             : 
     201          40 :         return rc;
     202             : }
     203             : /* }}} */
     204             : 
     205             : /* {{{ collator_compare_func
     206             :  * Taken from PHP7 source (array_data_compare).
     207             :  */
     208         237 : static int collator_compare_func( const void* a, const void* b TSRMLS_DC )
     209             : {
     210             :         Bucket *f;
     211             :         Bucket *s;
     212             :         zval result;
     213             :         zval *first;
     214             :         zval *second;
     215             : 
     216         237 :         f = (Bucket *) a;
     217         237 :         s = (Bucket *) b;
     218             : 
     219         237 :         first = &f->val;
     220         237 :         second = &s->val;
     221             : 
     222         237 :         if( INTL_G(compare_func)( &result, first, second TSRMLS_CC) == FAILURE )
     223           0 :                 return 0;
     224             : 
     225         236 :         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         236 :         convert_to_long(&result);
     236             : 
     237         236 :         if( Z_LVAL(result) < 0 )
     238         120 :                 return -1;
     239         116 :         else if( Z_LVAL(result) > 0 )
     240         110 :                 return 1;
     241             : 
     242           6 :         return 0;
     243             : }
     244             : /* }}} */
     245             : 
     246             : /* {{{ collator_cmp_sort_keys
     247             :  * Compare sort keys
     248             :  */
     249         120 : static int collator_cmp_sort_keys( const void *p1, const void *p2 TSRMLS_DC )
     250             : {
     251         120 :         char* key1 = ((collator_sort_key_index_t*)p1)->key;
     252         120 :         char* key2 = ((collator_sort_key_index_t*)p2)->key;
     253             : 
     254         120 :         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          77 : static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
     289             : {
     290             :         zval           saved_collator;
     291          77 :         zval*          array            = NULL;
     292          77 :         HashTable*     hash             = NULL;
     293          77 :         zend_long           sort_flags       = COLLATOR_SORT_REGULAR;
     294             : 
     295          77 :         COLLATOR_METHOD_INIT_VARS
     296             : 
     297             :         /* Parse parameters. */
     298          77 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa/|l",
     299             :                 &object, Collator_ce_ptr, &array, &sort_flags ) == FAILURE )
     300             :         {
     301           2 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     302             :                         "collator_sort_internal: unable to parse input params", 0 TSRMLS_CC );
     303             : 
     304           2 :                 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         150 :         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, zend_qsort, collator_compare_func, renumber TSRMLS_CC );
     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          50 : PHP_FUNCTION( collator_sort )
     343             : {
     344          50 :         collator_sort_internal( TRUE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
     345          49 : }
     346             : /* }}} */
     347             : 
     348             : /* {{{ proto bool Collator::sortWithSortKeys( Collator $coll, array(string) $arr )
     349             :  * Equivalent to standard PHP sort using Collator.
     350             :  * Uses ICU ucol_getSortKey for performance. }}} */
     351             : /* {{{ proto bool collator_sort_with_sort_keys( Collator $coll, array(string) $arr )
     352             :  * Equivalent to standard PHP sort using Collator.
     353             :  * Uses ICU ucol_getSortKey for performance.
     354             :  */
     355          38 : PHP_FUNCTION( collator_sort_with_sort_keys )
     356             : {
     357          38 :         zval*       array                = NULL;
     358          38 :         HashTable*  hash                 = NULL;
     359          38 :         zval*       hashData             = NULL;                     /* currently processed item of input hash */
     360             : 
     361          38 :         char*       sortKeyBuf           = NULL;                     /* buffer to store sort keys */
     362          38 :         uint32_t    sortKeyBufSize       = DEF_SORT_KEYS_BUF_SIZE;   /* buffer size */
     363          38 :         ptrdiff_t   sortKeyBufOffset     = 0;                        /* pos in buffer to store sort key */
     364          38 :         int32_t     sortKeyLen           = 0;                        /* the length of currently processing key */
     365          38 :         uint32_t    bufLeft              = 0;
     366          38 :         uint32_t    bufIncrement         = 0;
     367             : 
     368          38 :         collator_sort_key_index_t* sortKeyIndxBuf = NULL;            /* buffer to store 'indexes' which will be passed to 'qsort' */
     369          38 :         uint32_t    sortKeyIndxBufSize   = DEF_SORT_KEYS_INDX_BUF_SIZE;
     370          38 :         uint32_t    sortKeyIndxSize      = sizeof( collator_sort_key_index_t );
     371             : 
     372          38 :         uint32_t    sortKeyCount         = 0;
     373          38 :         uint32_t    j                    = 0;
     374             : 
     375          38 :         UChar*      utf16_buf            = NULL;                     /* tmp buffer to hold current processing string in utf-16 */
     376          38 :         int         utf16_buf_size       = DEF_UTF16_BUF_SIZE;       /* the length of utf16_buf */
     377          38 :         int         utf16_len            = 0;                        /* length of converted string */
     378             : 
     379          38 :         HashTable* sortedHash            = NULL;
     380             : 
     381          38 :         COLLATOR_METHOD_INIT_VARS
     382             : 
     383             :         /* Parse parameters. */
     384          38 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oa",
     385             :                 &object, Collator_ce_ptr, &array ) == FAILURE )
     386             :         {
     387           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     388             :                         "collator_sort_with_sort_keys: unable to parse input params", 0 TSRMLS_CC );
     389             : 
     390           1 :                 RETURN_FALSE;
     391             :         }
     392             : 
     393             :         /* Fetch the object. */
     394          37 :         COLLATOR_METHOD_FETCH_OBJECT;
     395             : 
     396          37 :         if (!co || !co->ucoll) {
     397           1 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
     398           1 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     399             :                         "Object not initialized", 0 TSRMLS_CC );
     400           1 :                 php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
     401             : 
     402           0 :                 RETURN_FALSE;
     403             :         }
     404             : 
     405             :         /*
     406             :          * Sort specified array.
     407             :          */
     408          72 :         hash = HASH_OF( array );
     409             : 
     410          36 :         if( !hash || zend_hash_num_elements( hash ) == 0 )
     411           0 :                 RETURN_TRUE;
     412             : 
     413             :         /* Create bufers */
     414          36 :         sortKeyBuf     = ecalloc( sortKeyBufSize,     sizeof( char    ) );
     415          36 :         sortKeyIndxBuf = ecalloc( sortKeyIndxBufSize, sizeof( uint8_t ) );
     416          36 :         utf16_buf      = eumalloc( utf16_buf_size );
     417             : 
     418             :         /* Iterate through input hash and create a sort key for each value. */
     419         256 :         ZEND_HASH_FOREACH_VAL(hash, hashData) {
     420             :                 /* Convert current hash item from UTF-8 to UTF-16LE and save the result to utf16_buf. */
     421             : 
     422         110 :                 utf16_len = utf16_buf_size;
     423             : 
     424             :                 /* Process string values only. */
     425         110 :                 if( Z_TYPE_P( hashData ) == IS_STRING )
     426             :                 {
     427         108 :                         intl_convert_utf8_to_utf16( &utf16_buf, &utf16_len, Z_STRVAL_P( hashData ), Z_STRLEN_P( hashData ), COLLATOR_ERROR_CODE_P( co ) );
     428             : 
     429         108 :                         if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
     430             :                         {
     431           0 :                                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
     432           0 :                                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ), "Sort with sort keys failed", 0 TSRMLS_CC );
     433             : 
     434           0 :                                 if( utf16_buf )
     435           0 :                                         efree( utf16_buf );
     436             : 
     437           0 :                                 efree( sortKeyIndxBuf );
     438           0 :                                 efree( sortKeyBuf );
     439             : 
     440           0 :                                 RETURN_FALSE;
     441             :                         }
     442             :                 }
     443             :                 else
     444             :                 {
     445             :                         /* Set empty string */
     446           2 :                         utf16_len = 0;
     447           2 :                         utf16_buf[utf16_len] = 0;
     448             :                 }
     449             : 
     450         110 :                 if( (utf16_len + 1) > utf16_buf_size )
     451           0 :                         utf16_buf_size = utf16_len + 1;
     452             : 
     453             :                 /* Get sort key, reallocating the buffer if needed. */
     454         110 :                 bufLeft = sortKeyBufSize - sortKeyBufOffset;
     455             : 
     456         110 :                 sortKeyLen = ucol_getSortKey( co->ucoll,
     457             :                                                                           utf16_buf,
     458             :                                                                           utf16_len,
     459             :                                                                           (uint8_t*)sortKeyBuf + sortKeyBufOffset,
     460             :                                                                           bufLeft );
     461             : 
     462             :                 /* check for sortKeyBuf overflow, increasing its size of the buffer if needed */
     463         110 :                 if( sortKeyLen > bufLeft )
     464             :                 {
     465           0 :                         bufIncrement = ( sortKeyLen > DEF_SORT_KEYS_BUF_INCREMENT ) ? sortKeyLen : DEF_SORT_KEYS_BUF_INCREMENT;
     466             : 
     467           0 :                         sortKeyBufSize += bufIncrement;
     468           0 :                         bufLeft += bufIncrement;
     469             : 
     470           0 :                         sortKeyBuf = erealloc( sortKeyBuf, sortKeyBufSize );
     471             : 
     472           0 :                         sortKeyLen = ucol_getSortKey( co->ucoll, utf16_buf, utf16_len, (uint8_t*)sortKeyBuf + sortKeyBufOffset, bufLeft );
     473             :                 }
     474             : 
     475             :                 /*  check sortKeyIndxBuf overflow, increasing its size of the buffer if needed */
     476         110 :                 if( ( sortKeyCount + 1 ) * sortKeyIndxSize > sortKeyIndxBufSize )
     477             :                 {
     478           0 :                         bufIncrement = ( sortKeyIndxSize > DEF_SORT_KEYS_INDX_BUF_INCREMENT ) ? sortKeyIndxSize : DEF_SORT_KEYS_INDX_BUF_INCREMENT;
     479             : 
     480           0 :                         sortKeyIndxBufSize += bufIncrement;
     481             : 
     482           0 :                         sortKeyIndxBuf = erealloc( sortKeyIndxBuf, sortKeyIndxBufSize );
     483             :                 }
     484             : 
     485         110 :                 sortKeyIndxBuf[sortKeyCount].key = (char*)sortKeyBufOffset;    /* remeber just offset, cause address */
     486             :                                                                                /* of 'sortKeyBuf' may be changed due to realloc. */
     487         110 :                 sortKeyIndxBuf[sortKeyCount].zstr = hashData;
     488             : 
     489         110 :                 sortKeyBufOffset += sortKeyLen;
     490         110 :                 ++sortKeyCount;
     491             : 
     492             :         } ZEND_HASH_FOREACH_END();
     493             : 
     494             :         /* update ptrs to point to valid keys. */
     495         146 :         for( j = 0; j < sortKeyCount; j++ )
     496         110 :                 sortKeyIndxBuf[j].key = sortKeyBuf + (ptrdiff_t)sortKeyIndxBuf[j].key;
     497             : 
     498             :         /* sort it */
     499          36 :         zend_qsort( sortKeyIndxBuf, sortKeyCount, sortKeyIndxSize, collator_cmp_sort_keys TSRMLS_CC );
     500             : 
     501          36 :         zval_ptr_dtor( array );
     502             :         /* for resulting hash we'll assign new hash keys rather then reordering */
     503          36 :         array_init(array);
     504             : 
     505         146 :         for( j = 0; j < sortKeyCount; j++ )
     506             :         {
     507         110 :                 Z_TRY_ADDREF_P( sortKeyIndxBuf[j].zstr );
     508         110 :                 zend_hash_next_index_insert( Z_ARRVAL_P(array), sortKeyIndxBuf[j].zstr);
     509             :         }
     510             : 
     511          36 :         if( utf16_buf )
     512          36 :                 efree( utf16_buf );
     513             : 
     514          36 :         efree( sortKeyIndxBuf );
     515          36 :         efree( sortKeyBuf );
     516             : 
     517          36 :         RETURN_TRUE;
     518             : }
     519             : /* }}} */
     520             : 
     521             : /* {{{ proto bool Collator::asort( Collator $coll, array(string) $arr )
     522             :  * Sort array using specified collator, maintaining index association. }}} */
     523             : /* {{{ proto bool collator_asort( Collator $coll, array(string) $arr )
     524             :  * Sort array using specified collator, maintaining index association.
     525             :  */
     526          27 : PHP_FUNCTION( collator_asort )
     527             : {
     528          27 :         collator_sort_internal( FALSE, INTERNAL_FUNCTION_PARAM_PASSTHRU );
     529          27 : }
     530             : /* }}} */
     531             : 
     532             : /* {{{ proto bool Collator::getSortKey( Collator $coll, string $str )
     533             :  * Get a sort key for a string from a Collator. }}} */
     534             : /* {{{ proto bool collator_get_sort_key( Collator $coll, string $str )
     535             :  * Get a sort key for a string from a Collator. }}} */
     536           2 : PHP_FUNCTION( collator_get_sort_key )
     537             : {
     538           2 :         char*            str      = NULL;
     539           2 :         size_t              str_len  = 0;
     540           2 :         UChar*           ustr     = NULL;
     541           2 :         int              ustr_len = 0;
     542           2 :         uint8_t*         key     = NULL;
     543           2 :         int              key_len = 0;
     544             : 
     545           2 :         COLLATOR_METHOD_INIT_VARS
     546             : 
     547             :         /* Parse parameters. */
     548           2 :         if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
     549             :                 &object, Collator_ce_ptr, &str, &str_len ) == FAILURE )
     550             :         {
     551           1 :                 intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
     552             :                          "collator_get_sort_key: unable to parse input params", 0 TSRMLS_CC );
     553             : 
     554           1 :                 RETURN_FALSE;
     555             :         }
     556             : 
     557             :         /* Fetch the object. */
     558           1 :         COLLATOR_METHOD_FETCH_OBJECT;
     559             : 
     560           1 :         if (!co || !co->ucoll) {
     561           1 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
     562           1 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     563             :                         "Object not initialized", 0 TSRMLS_CC );
     564           1 :                 php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
     565             : 
     566           0 :                 RETURN_FALSE;
     567             :         }
     568             : 
     569             :         /*
     570             :          * Compare given strings (converting them to UTF-16 first).
     571             :          */
     572             : 
     573             :         /* First convert the strings to UTF-16. */
     574           0 :         intl_convert_utf8_to_utf16(
     575             :                 &ustr, &ustr_len, str, str_len, COLLATOR_ERROR_CODE_P( co ) );
     576           0 :         if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
     577             :         {
     578             :                 /* Set global error code. */
     579           0 :                 intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
     580             : 
     581             :                 /* Set error messages. */
     582           0 :                 intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
     583             :                         "Error converting first argument to UTF-16", 0 TSRMLS_CC );
     584           0 :                 efree( ustr );
     585           0 :                 RETURN_FALSE;
     586             :         }
     587             : 
     588             :         /* ucol_getSortKey is exception in that the key length includes the 
     589             :          * NUL terminator*/
     590           0 :         key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, 0);
     591           0 :         if(!key_len) {
     592           0 :                 efree( ustr );
     593           0 :                 RETURN_FALSE;
     594             :         }
     595           0 :         key = emalloc(key_len);
     596           0 :         key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, key_len);
     597           0 :         efree( ustr );
     598           0 :         if(!key_len) {
     599           0 :                 RETURN_FALSE;
     600             :         }
     601           0 :         RETVAL_STRINGL((char *)key, key_len - 1);
     602             :         //????
     603           0 :         efree(key);
     604             : }
     605             : /* }}} */
     606             : 
     607             : /*
     608             :  * Local variables:
     609             :  * tab-width: 4
     610             :  * c-basic-offset: 4
     611             :  * End:
     612             :  * vim600: noet sw=4 ts=4 fdm=marker
     613             :  * vim<600: noet sw=4 ts=4
     614             :  */

Generated by: LCOV version 1.10

Generated at Wed, 22 Oct 2014 07:24:50 +0000 (2 days ago)

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