Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ext/intl/collator/collator_attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>

#include "php_intl.h"
#include "collator_class.h"
Expand Down Expand Up @@ -49,7 +50,7 @@ U_CFUNC PHP_FUNCTION( collator_get_attribute )
RETURN_THROWS();
}

value = ucol_getAttribute( co->ucoll, static_cast<UColAttribute>(attribute), COLLATOR_ERROR_CODE_P( co ) );
value = co->ucoll->getAttribute( static_cast<UColAttribute>(attribute), COLLATOR_ERROR_CODE( co ) );
COLLATOR_CHECK_STATUS( co, "Error getting attribute value" );

RETURN_LONG( value );
Expand Down Expand Up @@ -78,7 +79,7 @@ U_CFUNC PHP_FUNCTION( collator_set_attribute )
}

/* Set new value for the given attribute. */
ucol_setAttribute( co->ucoll, static_cast<UColAttribute>(attribute), static_cast<UColAttributeValue>(value), COLLATOR_ERROR_CODE_P( co ) );
co->ucoll->setAttribute( static_cast<UColAttribute>(attribute), static_cast<UColAttributeValue>(value), COLLATOR_ERROR_CODE( co ) );
COLLATOR_CHECK_STATUS( co, "Error setting attribute value" );

RETURN_TRUE;
Expand All @@ -105,7 +106,8 @@ U_CFUNC PHP_FUNCTION( collator_get_strength )
}

/* Get current strength and return it. */
RETURN_LONG( ucol_getStrength( co->ucoll ) );
UErrorCode status = U_ZERO_ERROR;
RETURN_LONG( co->ucoll->getAttribute( UCOL_STRENGTH, status ) );
}
/* }}} */

Expand All @@ -131,7 +133,8 @@ U_CFUNC PHP_FUNCTION( collator_set_strength )
}

/* Set given strength. */
ucol_setStrength( co->ucoll, static_cast<UColAttributeValue>(strength) );
UErrorCode status = U_ZERO_ERROR;
co->ucoll->setAttribute( UCOL_STRENGTH, static_cast<UColAttributeValue>(strength), status );

RETURN_TRUE;
}
Expand Down
4 changes: 1 addition & 3 deletions ext/intl/collator/collator_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ END_EXTERN_C()
#include "collator_sort.h"
#include "collator_convert.h"

#include <unicode/ucol.h>


zend_class_entry *Collator_ce_ptr = nullptr;
static zend_object_handlers Collator_handlers;
Expand All @@ -42,7 +40,7 @@ static void collator_object_destroy( Collator_object* co )

if( co->ucoll )
{
ucol_close( co->ucoll );
delete co->ucoll;
co->ucoll = nullptr;
}

Expand Down
9 changes: 7 additions & 2 deletions ext/intl/collator/collator_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@
#include "../intl_error.h"
#include "../intl_data.h"

#include <unicode/ucol.h>
#ifdef __cplusplus
#include <unicode/coll.h>
using icu::Collator;
#else
typedef void Collator;
#endif

typedef struct {
// error handling
intl_error err;

// ICU collator
UCollator* ucoll;
Collator* ucoll;

zend_object zo;

Expand Down
7 changes: 4 additions & 3 deletions ext/intl/collator/collator_compare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>

#include "php_intl.h"
#include "intl_convert.h"
Expand Down Expand Up @@ -96,10 +97,10 @@ U_CFUNC PHP_FUNCTION( collator_compare )
}

/* Then compare them. */
result = ucol_strcoll(
co->ucoll,
UErrorCode status = U_ZERO_ERROR;
result = co->ucoll->compare(
ustr1, ustr1_len,
ustr2, ustr2_len );
ustr2, ustr2_len, status );

if( ustr1 )
efree( ustr1 );
Expand Down
1 change: 1 addition & 0 deletions ext/intl/collator/collator_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>

#include "php_intl.h"
#include "intl_convert.h"
Expand Down
4 changes: 3 additions & 1 deletion ext/intl/collator/collator_create.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>
#include <unicode/locid.h>

#include "php_intl.h"
#include "collator_class.h"
Expand Down Expand Up @@ -52,7 +54,7 @@ static int collator_ctor(INTERNAL_FUNCTION_PARAMETERS)
}

/* Open ICU collator. */
co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) );
co->ucoll = Collator::createInstance( icu::Locale(locale), COLLATOR_ERROR_CODE( co ) );
INTL_CTOR_CHECK_STATUS(co, "unable to open ICU collator");
return SUCCESS;
}
Expand Down
1 change: 1 addition & 0 deletions ext/intl/collator/collator_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>

#include "php_intl.h"
#include "collator_class.h"
Expand Down
20 changes: 14 additions & 6 deletions ext/intl/collator/collator_locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>
#include <unicode/locid.h>
#include <unicode/tblcoll.h>

#include "php_intl.h"
#include "collator_class.h"
Expand All @@ -32,7 +35,6 @@
U_CFUNC PHP_FUNCTION( collator_get_locale )
{
zend_long type = 0;
char* locale_name = nullptr;

COLLATOR_METHOD_INIT_VARS

Expand All @@ -51,11 +53,15 @@ U_CFUNC PHP_FUNCTION( collator_get_locale )
}

/* Get locale by specified type. */
locale_name = (char*) ucol_getLocaleByType(
co->ucoll, static_cast<ULocDataLocaleType>(type), COLLATOR_ERROR_CODE_P( co ) );
icu::Locale locale = co->ucoll->getLocale(
static_cast<ULocDataLocaleType>(type), COLLATOR_ERROR_CODE( co ) );
COLLATOR_CHECK_STATUS( co, "Error getting locale by type" );

/* Return it. */
const char *locale_name = locale.getName();
if (locale_name[0] == '\0') {
locale_name = "root";
}
RETVAL_STRINGL( locale_name, strlen(locale_name) );
}
/* }}} */
Expand All @@ -76,9 +82,11 @@ U_CFUNC PHP_FUNCTION(collator_get_rules)
RETURN_THROWS();
}

int32_t rules_len;
const UChar *rules = ucol_getRules(co->ucoll, &rules_len);
zend_string *result = intl_convert_utf16_to_utf8(rules, rules_len, COLLATOR_ERROR_CODE_P(co));
icu::UnicodeString rules;
if (co->ucoll->getDynamicClassID() == icu::RuleBasedCollator::getStaticClassID()) {
rules = static_cast<const icu::RuleBasedCollator *>(co->ucoll)->getRules();
}
zend_string *result = intl_convert_utf16_to_utf8(rules.getBuffer(), rules.length(), COLLATOR_ERROR_CODE_P(co));
COLLATOR_CHECK_STATUS(co, "Error converting collation rules to UTF-8");

RETURN_STR(result);
Expand Down
23 changes: 12 additions & 11 deletions ext/intl/collator/collator_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string_view>
#include <unicode/unistr.h>
#endif
#include <unicode/coll.h>

#include "php_intl.h"
#include "intl_convert.h"
Expand Down Expand Up @@ -89,10 +90,10 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
{
/* Compare the strings using ICU. */
ZEND_ASSERT(INTL_G(current_collator) != nullptr);
ZVAL_LONG(result, ucol_strcoll(
INTL_G(current_collator),
UErrorCode status = U_ZERO_ERROR;
ZVAL_LONG(result, INTL_G(current_collator)->compare(
INTL_ZSTR_VAL(Z_STR_P(str1_p)), INTL_ZSTR_LEN(Z_STR_P(str1_p)),
INTL_ZSTR_VAL(Z_STR_P(str2_p)), INTL_ZSTR_LEN(Z_STR_P(str2_p)) ));
INTL_ZSTR_VAL(Z_STR_P(str2_p)), INTL_ZSTR_LEN(Z_STR_P(str2_p)), status ));
}
else
{
Expand Down Expand Up @@ -200,10 +201,10 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)

/* Compare the strings using ICU. */
ZEND_ASSERT(INTL_G(current_collator) != nullptr);
ZVAL_LONG(result, ucol_strcoll(
INTL_G(current_collator),
UErrorCode status = U_ZERO_ERROR;
ZVAL_LONG(result, INTL_G(current_collator)->compare(
INTL_ZSTR_VAL(str1), INTL_ZSTR_LEN(str1),
INTL_ZSTR_VAL(str2), INTL_ZSTR_LEN(str2) ));
INTL_ZSTR_VAL(str2), INTL_ZSTR_LEN(str2), status ));

zend_string_release(str1);
zend_string_release(str2);
Expand Down Expand Up @@ -288,7 +289,7 @@ static collator_compare_func_t collator_get_compare_function( const zend_long so
/* {{{ Common code shared by collator_sort() and collator_asort() API functions. */
static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
{
UCollator* saved_collator;
Collator* saved_collator;
intl_error* saved_collator_error;
intl_error sort_error;
collator_compare_func_t saved_compare_func;
Expand Down Expand Up @@ -497,7 +498,7 @@ U_CFUNC PHP_FUNCTION( collator_sort_with_sort_keys )
/* Get sort key, reallocating the buffer if needed. */
bufLeft = sortKeyBufSize - sortKeyBufOffset;

sortKeyLen = ucol_getSortKey( co->ucoll,
sortKeyLen = co->ucoll->getSortKey(
utf16_buf,
utf16_len,
(uint8_t*)sortKeyBuf + sortKeyBufOffset,
Expand All @@ -521,7 +522,7 @@ U_CFUNC PHP_FUNCTION( collator_sort_with_sort_keys )

sortKeyBuf = reinterpret_cast<char *>(erealloc( sortKeyBuf, sortKeyBufSize ));

sortKeyLen = ucol_getSortKey( co->ucoll, utf16_buf, utf16_len, (uint8_t*)sortKeyBuf + sortKeyBufOffset, bufLeft );
sortKeyLen = co->ucoll->getSortKey( utf16_buf, utf16_len, (uint8_t*)sortKeyBuf + sortKeyBufOffset, bufLeft );
}

sortKeyIndxBuf[sortKeyCount].key = (char*)sortKeyBufOffset; /* remember just offset, cause address */
Expand Down Expand Up @@ -616,13 +617,13 @@ U_CFUNC PHP_FUNCTION( collator_get_sort_key )

/* ucol_getSortKey is exception in that the key length includes the
* NUL terminator*/
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, nullptr, 0);
key_len = co->ucoll->getSortKey(ustr, ustr_len, nullptr, 0);
if(!key_len) {
efree( ustr );
RETURN_FALSE;
}
key_str = zend_string_alloc(key_len, 0);
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, (uint8_t*)ZSTR_VAL(key_str), key_len);
key_len = co->ucoll->getSortKey(ustr, ustr_len, (uint8_t*)ZSTR_VAL(key_str), key_len);
efree( ustr );
if(!key_len) {
zend_string_efree(key_str);
Expand Down
12 changes: 11 additions & 1 deletion ext/intl/php_intl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,19 @@ extern zend_module_entry intl_module_entry;
#include "TSRM.h"
#endif

#ifdef __cplusplus
U_NAMESPACE_BEGIN
class Collator;
U_NAMESPACE_END
#endif

BEGIN_EXTERN_C()
ZEND_BEGIN_MODULE_GLOBALS(intl)
struct UCollator *current_collator;
#ifdef __cplusplus
icu::Collator *current_collator;
#else
void *current_collator;
#endif
intl_error *current_collator_error;
char* default_locale;
collator_compare_func_t compare_func;
Expand Down
Loading