collator_compare.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | http://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Authors: Vadim Savchuk <vsavchuk@productengine.com> |
  14. | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "php_intl.h"
  21. #include "collator_class.h"
  22. #include "collator_compare.h"
  23. #include "intl_convert.h"
  24. /* {{{ proto int Collator::compare( string $str1, string $str2 )
  25. * Compare two strings. }}} */
  26. /* {{{ proto int collator_compare( Collator $coll, string $str1, string $str2 )
  27. * Compare two strings.
  28. */
  29. PHP_FUNCTION( collator_compare )
  30. {
  31. char* str1 = NULL;
  32. char* str2 = NULL;
  33. int str1_len = 0;
  34. int str2_len = 0;
  35. UChar* ustr1 = NULL;
  36. UChar* ustr2 = NULL;
  37. int ustr1_len = 0;
  38. int ustr2_len = 0;
  39. UCollationResult result;
  40. COLLATOR_METHOD_INIT_VARS
  41. /* Parse parameters. */
  42. if( zend_parse_method_parameters( ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oss",
  43. &object, Collator_ce_ptr, &str1, &str1_len, &str2, &str2_len ) == FAILURE )
  44. {
  45. intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
  46. "collator_compare: unable to parse input params", 0 TSRMLS_CC );
  47. RETURN_FALSE;
  48. }
  49. /* Fetch the object. */
  50. COLLATOR_METHOD_FETCH_OBJECT;
  51. if (!co || !co->ucoll) {
  52. intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
  53. intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
  54. "Object not initialized", 0 TSRMLS_CC );
  55. php_error_docref(NULL TSRMLS_CC, E_RECOVERABLE_ERROR, "Object not initialized");
  56. RETURN_FALSE;
  57. }
  58. /*
  59. * Compare given strings (converting them to UTF-16 first).
  60. */
  61. /* First convert the strings to UTF-16. */
  62. intl_convert_utf8_to_utf16(
  63. &ustr1, &ustr1_len, str1, str1_len, COLLATOR_ERROR_CODE_P( co ) );
  64. if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
  65. {
  66. /* Set global error code. */
  67. intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
  68. /* Set error messages. */
  69. intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
  70. "Error converting first argument to UTF-16", 0 TSRMLS_CC );
  71. if (ustr1) {
  72. efree( ustr1 );
  73. }
  74. RETURN_FALSE;
  75. }
  76. intl_convert_utf8_to_utf16(
  77. &ustr2, &ustr2_len, str2, str2_len, COLLATOR_ERROR_CODE_P( co ) );
  78. if( U_FAILURE( COLLATOR_ERROR_CODE( co ) ) )
  79. {
  80. /* Set global error code. */
  81. intl_error_set_code( NULL, COLLATOR_ERROR_CODE( co ) TSRMLS_CC );
  82. /* Set error messages. */
  83. intl_errors_set_custom_msg( COLLATOR_ERROR_P( co ),
  84. "Error converting second argument to UTF-16", 0 TSRMLS_CC );
  85. if (ustr1) {
  86. efree( ustr1 );
  87. }
  88. if (ustr2) {
  89. efree( ustr2 );
  90. }
  91. RETURN_FALSE;
  92. }
  93. /* Then compare them. */
  94. result = ucol_strcoll(
  95. co->ucoll,
  96. ustr1, ustr1_len,
  97. ustr2, ustr2_len );
  98. if( ustr1 )
  99. efree( ustr1 );
  100. if( ustr2 )
  101. efree( ustr2 );
  102. /* Return result of the comparison. */
  103. RETURN_LONG( result );
  104. }
  105. /* }}} */
  106. /*
  107. * Local variables:
  108. * tab-width: 4
  109. * c-basic-offset: 4
  110. * End:
  111. * vim600: noet sw=4 ts=4 fdm=marker
  112. * vim<600: noet sw=4 ts=4
  113. */