Back to index

php5  5.3.10
collator_compare.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,      |
00006    | that is bundled with this package in the file LICENSE, and is        |
00007    | available through the world-wide-web at the following url:           |
00008    | http://www.php.net/license/3_01.txt                                  |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to          |
00011    | license@php.net so we can mail you a copy immediately.               |
00012    +----------------------------------------------------------------------+
00013    | Authors: Vadim Savchuk <vsavchuk@productengine.com>                  |
00014    |          Dmitry Lakhtyuk <dlakhtyuk@productengine.com>               |
00015    +----------------------------------------------------------------------+
00016  */
00017 
00018 #ifdef HAVE_CONFIG_H
00019 #include "config.h"
00020 #endif
00021 
00022 #include "php_intl.h"
00023 #include "collator_class.h"
00024 #include "collator_compare.h"
00025 #include "intl_convert.h"
00026 
00027 /* {{{ proto int Collator::compare( string $str1, string $str2 )
00028  * Compare two strings. }}} */
00029 /* {{{ proto int collator_compare( Collator $coll, string $str1, string $str2 )
00030  * Compare two strings.
00031  */
00032 PHP_FUNCTION( collator_compare )
00033 {
00034        char*            str1      = NULL;
00035        char*            str2      = NULL;
00036        int              str1_len  = 0;
00037        int              str2_len  = 0;
00038 
00039        UChar*           ustr1     = NULL;
00040        UChar*           ustr2     = NULL;
00041        int              ustr1_len = 0;
00042        int              ustr2_len = 0;
00043 
00044        UCollationResult result;
00045 
00046        COLLATOR_METHOD_INIT_VARS
00047 
00048        /* Parse parameters. */
00049        if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
00050               &object, Collator_ce_ptr, &str1, &str1_len, &str2, &str2_len ) == FAILURE )
00051        {
00052               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00053                       "collator_compare: unable to parse input params", 0 TSRMLS_CC );
00054 
00055               RETURN_FALSE;
00056        }
00057 
00058        /* Fetch the object. */
00059        COLLATOR_METHOD_FETCH_OBJECT;
00060 
00061        if (!co || !co->ucoll) {
00062               intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
00063               intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
00064                      "Object not initialized", 0 TSRMLS_CC );
00065               php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
00066 
00067               RETURN_FALSE;
00068        }
00069 
00070        /*
00071         * Compare given strings (converting them to UTF-16 first).
00072         */
00073 
00074        /* First convert the strings to UTF-16. */
00075        intl_convert_utf8_to_utf16(
00076               &ustr1, &ustr1_len, str1, str1_len, COLLATOR_ERROR_CODE_P( co ) );
00077        if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
00078        {
00079               /* Set global error code. */
00080               intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
00081 
00082               /* Set error messages. */
00083               intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
00084                      "Error converting first argument to UTF-16", 0 TSRMLS_CC );
00085               if (ustr1) {
00086                      efree( ustr1 );
00087               }
00088               RETURN_FALSE;
00089        }
00090 
00091        intl_convert_utf8_to_utf16(
00092               &ustr2, &ustr2_len, str2, str2_len, COLLATOR_ERROR_CODE_P( co ) );
00093        if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
00094        {
00095               /* Set global error code. */
00096               intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
00097 
00098               /* Set error messages. */
00099               intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
00100                      "Error converting second argument to UTF-16", 0 TSRMLS_CC );
00101               if (ustr1) {
00102                      efree( ustr1 );
00103               }
00104               if (ustr2) {
00105                      efree( ustr2 );
00106               }
00107               RETURN_FALSE;
00108        }
00109 
00110        /* Then compare them. */
00111        result = ucol_strcoll(
00112               co->ucoll,
00113               ustr1, ustr1_len,
00114               ustr2, ustr2_len );
00115 
00116        if( ustr1 )
00117               efree( ustr1 );
00118        if( ustr2 )
00119               efree( ustr2 );
00120 
00121        /* Return result of the comparison. */
00122        RETURN_LONG( result );
00123 }
00124 /* }}} */
00125 
00126 /*
00127  * Local variables:
00128  * tab-width: 4
00129  * c-basic-offset: 4
00130  * End:
00131  * vim600: noet sw=4 ts=4 fdm=marker
00132  * vim<600: noet sw=4 ts=4
00133  */