1
0
mirror of https://github.com/php/php-src.git synced 2026-04-27 10:16:41 +02:00

add collator_get_sort_key()

This commit is contained in:
Stanislav Malyshev
2009-10-26 22:51:11 +00:00
parent fce960743d
commit 4a488eeffc
6 changed files with 145 additions and 0 deletions
+1
View File
@@ -126,6 +126,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 }
};
/* }}} */
+39
View File
@@ -489,6 +489,45 @@ 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 )
{
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(), "Ou",
&object, Collator_ce_ptr, &ustr, &ustr_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;
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, 0);
if(!key_len) {
RETURN_FALSE;
}
key = emalloc(key_len);
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, key_len);
if(!key_len) {
RETURN_FALSE;
}
RETURN_STRINGL((char *)key, key_len, 0);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
+1
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
+1
View File
@@ -320,6 +320,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 )
+99
View File
@@ -0,0 +1,99 @@
--TEST--
collator_get_sort_key()
--INI--
unicode.runtime_encoding="utf-8"
--SKIPIF--
<?php if( !extension_loaded( 'intl' ) ) print 'skip'; ?>
--FILE--
<?php
/*
* Get sort keys using various locales
*/
function sort_arrays( $locale, $data )
{
$res_str = '';
$coll = ut_coll_create( $locale );
foreach($data as $value) {
$res_val = ut_coll_get_sort_key( $coll, $value );
$res_str .= "source: ".urlencode((binary)$value)."\n".
"key: ".urlencode($res_val)."\n";
}
return $res_str;
}
function ut_main()
{
$res_str = '';
// Regular strings keys
$test_params = array(
'abc', 'abd', 'aaa',
'аа', 'а', 'z',
'', null , '3',
'y' , 'i' , 'k'
);
$res_str .= sort_arrays( 'en_US', $test_params );
// Sort a non-ASCII array using ru_RU locale.
$test_params = array(
'абг', 'абв', 'жжж', 'эюя'
);
$res_str .= sort_arrays( 'ru_RU', $test_params );
// Sort an array using Lithuanian locale.
$res_str .= sort_arrays( 'lt_LT', $test_params );
return $res_str . "\n";
}
include_once( 'ut_common.inc' );
ut_run();
?>
--EXPECT--
source: abc
key: %29%2B-%01%07%01%07%00
source: abd
key: %29%2B%2F%01%07%01%07%00
source: aaa
key: %29%29%29%01%07%01%07%00
source: %D0%B0%D0%B0
key: _++%01%06%01%06%00
source: %D0%B0
key: _+%01%05%01%05%00
source: z
key: %5B%01%05%01%05%00
source:
key: %01%01%00
source:
key: %01%01%00
source: 3
key: %26%80%01%05%01%05%00
source: y
key: Y%01%05%01%05%00
source: i
key: 9%01%05%01%05%00
source: k
key: %3D%01%05%01%05%00
source: %D0%B0%D0%B1%D0%B3
key: _+%2C0%01%07%01%07%00
source: %D0%B0%D0%B1%D0%B2
key: _+%2C.%01%07%01%07%00
source: %D0%B6%D0%B6%D0%B6
key: _LLL%01%07%01%07%00
source: %D1%8D%D1%8E%D1%8F
key: %60%05%09%0B%01%07%01%07%00
source: %D0%B0%D0%B1%D0%B3
key: _+%2C0%01%07%01%07%00
source: %D0%B0%D0%B1%D0%B2
key: _+%2C.%01%07%01%07%00
source: %D0%B6%D0%B6%D0%B6
key: _LLL%01%07%01%07%00
source: %D1%8D%D1%8E%D1%8F
key: %60%05%09%0B%01%07%01%07%00
+4
View File
@@ -138,6 +138,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 );