summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStanislav Malyshev <stas@php.net>2009-10-26 22:35:48 +0000
committerStanislav Malyshev <stas@php.net>2009-10-26 22:35:48 +0000
commit65c1421f3fcde57d33a4224e5e6c06aa7bf77f87 (patch)
treed91eb5001ee0ed0a5d068c720a6c9ffc800ca8da
parent8b2735632ea432de903a39e15dc17dcce122263f (diff)
downloadphp-git-65c1421f3fcde57d33a4224e5e6c06aa7bf77f87.tar.gz
add collator_get_sort_key()
-rwxr-xr-xext/intl/collator/collator_class.c1
-rwxr-xr-xext/intl/collator/collator_sort.c63
-rwxr-xr-xext/intl/collator/collator_sort.h1
-rwxr-xr-xext/intl/php_intl.c1
-rwxr-xr-xext/intl/tests/ut_common.inc4
5 files changed, 70 insertions, 0 deletions
diff --git a/ext/intl/collator/collator_class.c b/ext/intl/collator/collator_class.c
index d064a2bd5d..ee16ee03e1 100755
--- a/ext/intl/collator/collator_class.c
+++ b/ext/intl/collator/collator_class.c
@@ -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 }
};
/* }}} */
diff --git a/ext/intl/collator/collator_sort.c b/ext/intl/collator/collator_sort.c
index efe4ca6e19..929a9c09d5 100755
--- a/ext/intl/collator/collator_sort.c
+++ b/ext/intl/collator/collator_sort.c
@@ -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
diff --git a/ext/intl/collator/collator_sort.h b/ext/intl/collator/collator_sort.h
index 0fafb9f35a..a990cdf089 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 831ace4d58..f4d1dcc525 100755
--- a/ext/intl/php_intl.c
+++ b/ext/intl/php_intl.c
@@ -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 )
diff --git a/ext/intl/tests/ut_common.inc b/ext/intl/tests/ut_common.inc
index 4f2036123d..c59d1770ee 100755
--- a/ext/intl/tests/ut_common.inc
+++ b/ext/intl/tests/ut_common.inc
@@ -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 );