add collator_get_sort_key()

This commit is contained in:
Stanislav Malyshev 2009-10-26 22:35:48 +00:00
parent 8b2735632e
commit 65c1421f3f
5 changed files with 70 additions and 0 deletions

View file

@ -125,6 +125,7 @@ function_entry Collator_class_functions[] = {
PHP_NAMED_FE( getLocale, ZEND_FN( collator_get_locale ), collator_1_arg )
PHP_NAMED_FE( getErrorCode, ZEND_FN( collator_get_error_code ), collator_0_args )
PHP_NAMED_FE( getErrorMessage, ZEND_FN( collator_get_error_message ), collator_0_args )
PHP_NAMED_FE( getSortKey, ZEND_FN( collator_get_sort_key ), collator_2_args )
{ NULL, NULL, NULL }
};
/* }}} */

View file

@ -523,6 +523,69 @@ PHP_FUNCTION( collator_asort )
}
/* }}} */
/* {{{ proto bool Collator::getSortKey( Collator $coll, string $str )
* Get a sort key for a string from a Collator. }}} */
/* {{{ proto bool collator_get_sort_key( Collator $coll, string $str )
* Get a sort key for a string from a Collator. }}} */
PHP_FUNCTION( collator_get_sort_key )
{
char* str = NULL;
int str_len = 0;
UChar* ustr = NULL;
int ustr_len = 0;
uint8_t* key = NULL;
int key_len = 0;
COLLATOR_METHOD_INIT_VARS
/* Parse parameters. */
if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os",
&object, Collator_ce_ptr, &str, &str_len ) == FAILURE )
{
intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
"collator_get_sort_key: unable to parse input params", 0 TSRMLS_CC );
RETURN_FALSE;
}
/* Fetch the object. */
COLLATOR_METHOD_FETCH_OBJECT;
/*
* Compare given strings (converting them to UTF-16 first).
*/
/* First convert the strings to UTF-16. */
intl_convert_utf8_to_utf16(
&ustr, &ustr_len, str, str_len, COLLATOR_ERROR_CODE_P( co ) );
if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
{
/* Set global error code. */
intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
/* Set error messages. */
intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
"Error converting first argument to UTF-16", 0 TSRMLS_CC );
efree( ustr );
RETURN_FALSE;
}
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, 0);
if(!key_len) {
efree( ustr );
RETURN_FALSE;
}
key = emalloc(key_len);
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, key_len);
efree( ustr );
if(!key_len) {
RETURN_FALSE;
}
RETURN_STRINGL((char *)key, key_len, 0);
}
/* }}} */
/*
* Local variables:
* tab-width: 4

View file

@ -24,6 +24,7 @@ typedef int (*collator_compare_func_t)( zval *result, zval *op1, zval *op2 TSRML
PHP_FUNCTION( collator_sort );
PHP_FUNCTION( collator_sort_with_sort_keys );
PHP_FUNCTION( collator_get_sort_key );
PHP_FUNCTION( collator_asort );
#endif // COLLATOR_SORT_H

View file

@ -351,6 +351,7 @@ zend_function_entry intl_functions[] = {
PHP_FE( collator_get_locale, collator_1_arg )
PHP_FE( collator_get_error_code, collator_0_args )
PHP_FE( collator_get_error_message, collator_0_args )
PHP_FE( collator_get_sort_key, collator_2_args )
/* formatter functions */
PHP_FE( numfmt_create, arginfo_numfmt_create )

View file

@ -59,6 +59,10 @@ function ut_coll_sort_with_sort_keys( $coll, &$arr )
{
return $GLOBALS['oo-mode'] ? $coll->sortWithSortKeys( $arr ) : collator_sort_with_sort_keys( $coll, $arr );
}
function ut_coll_get_sort_key( $coll, $str )
{
return $GLOBALS['oo-mode'] ? $coll->getSortKey( $str ) : collator_get_sort_key( $coll, $str );
}
function ut_coll_asort( $coll, &$arr, $sort_flag = Collator::SORT_REGULAR )
{
return $GLOBALS['oo-mode'] ? $coll->asort( $arr, $sort_flag ) : collator_asort( $coll, $arr, $sort_flag );