]> granicus.if.org Git - php/commitdiff
add collator_get_sort_key()
authorStanislav Malyshev <stas@php.net>
Mon, 26 Oct 2009 22:51:11 +0000 (22:51 +0000)
committerStanislav Malyshev <stas@php.net>
Mon, 26 Oct 2009 22:51:11 +0000 (22:51 +0000)
ext/intl/collator/collator_class.c
ext/intl/collator/collator_sort.c
ext/intl/collator/collator_sort.h
ext/intl/php_intl.c
ext/intl/tests/collator_get_sort_key.phpt [new file with mode: 0755]
ext/intl/tests/ut_common.inc

index 99a4888a6a4de31f887a0d5b33462f23777e4aca..581681e0ae0ba403d43283d1a7963effc5ef54b9 100755 (executable)
@@ -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 }
 };
 /* }}} */
index 3cbb6070b51cdca1ba55823cf0dfef6824784be7..56f6b00304588edab9304277c4204fbf69e8ec30 100755 (executable)
@@ -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
index d406288fd73e2fb816e8680b8a4c17bfe8552431..073311cf8012a1d1e7e53ea0cb0380d51641e07a 100755 (executable)
@@ -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
index fe6fd25725f02ecab28e949ef8f1197607cfa302..590f392286b39bb404034b5b67eb00d65fa45b01 100755 (executable)
@@ -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 (executable)
index 0000000..aab7614
--- /dev/null
@@ -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
index df1bdbbc0f6bb271c85ebc58ed58205cefc9ed21..36b5585d8a7da6b4bf0562d6b5ae8a056da6997f 100755 (executable)
@@ -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 );