From: Stanislav Malyshev Date: Mon, 26 Oct 2009 22:51:11 +0000 (+0000) Subject: add collator_get_sort_key() X-Git-Tag: php-5.4.0alpha1~191^2~2459 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4a488eeffca2189b8bbe0873e813d897b280d101;p=php add collator_get_sort_key() --- diff --git a/ext/intl/collator/collator_class.c b/ext/intl/collator/collator_class.c index 99a4888a6a..581681e0ae 100755 --- a/ext/intl/collator/collator_class.c +++ b/ext/intl/collator/collator_class.c @@ -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 } }; /* }}} */ diff --git a/ext/intl/collator/collator_sort.c b/ext/intl/collator/collator_sort.c index 3cbb6070b5..56f6b00304 100755 --- a/ext/intl/collator/collator_sort.c +++ b/ext/intl/collator/collator_sort.c @@ -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 diff --git a/ext/intl/collator/collator_sort.h b/ext/intl/collator/collator_sort.h index d406288fd7..073311cf80 100755 --- a/ext/intl/collator/collator_sort.h +++ b/ext/intl/collator/collator_sort.h @@ -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 diff --git a/ext/intl/php_intl.c b/ext/intl/php_intl.c index fe6fd25725..590f392286 100755 --- a/ext/intl/php_intl.c +++ b/ext/intl/php_intl.c @@ -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 ) diff --git a/ext/intl/tests/collator_get_sort_key.phpt b/ext/intl/tests/collator_get_sort_key.phpt new file mode 100755 index 0000000000..aab761475b --- /dev/null +++ b/ext/intl/tests/collator_get_sort_key.phpt @@ -0,0 +1,99 @@ +--TEST-- +collator_get_sort_key() +--INI-- +unicode.runtime_encoding="utf-8" +--SKIPIF-- + +--FILE-- + +--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 diff --git a/ext/intl/tests/ut_common.inc b/ext/intl/tests/ut_common.inc index df1bdbbc0f..36b5585d8a 100755 --- a/ext/intl/tests/ut_common.inc +++ b/ext/intl/tests/ut_common.inc @@ -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 );