Skip to content

Commit a08f4ef

Browse files
committed
ext/intl: Use C++ idioms in collator.
Same as a40fbb5 for idn, using the icu::Collator counterparts of the ucol_* calls. Close GH-26105
1 parent 8d7699d commit a08f4ef

10 files changed

Lines changed: 61 additions & 31 deletions

‎ext/intl/collator/collator_attr.cpp‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
2425

2526
#include "php_intl.h"
2627
#include "collator_class.h"
@@ -49,7 +50,7 @@ U_CFUNC PHP_FUNCTION( collator_get_attribute )
4950
RETURN_THROWS();
5051
}
5152

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

5556
RETURN_LONG( value );
@@ -78,7 +79,7 @@ U_CFUNC PHP_FUNCTION( collator_set_attribute )
7879
}
7980

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

8485
RETURN_TRUE;
@@ -105,7 +106,8 @@ U_CFUNC PHP_FUNCTION( collator_get_strength )
105106
}
106107

107108
/* Get current strength and return it. */
108-
RETURN_LONG( ucol_getStrength( co->ucoll ) );
109+
UErrorCode status = U_ZERO_ERROR;
110+
RETURN_LONG( co->ucoll->getAttribute( UCOL_STRENGTH, status ) );
109111
}
110112
/* }}} */
111113

@@ -131,7 +133,8 @@ U_CFUNC PHP_FUNCTION( collator_set_strength )
131133
}
132134

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

136139
RETURN_TRUE;
137140
}

‎ext/intl/collator/collator_class.cpp‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ END_EXTERN_C()
2424
#include "collator_sort.h"
2525
#include "collator_convert.h"
2626

27-
#include <unicode/ucol.h>
28-
2927

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

4341
if( co->ucoll )
4442
{
45-
ucol_close( co->ucoll );
43+
delete co->ucoll;
4644
co->ucoll = nullptr;
4745
}
4846

‎ext/intl/collator/collator_class.h‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@
2222
#include "../intl_error.h"
2323
#include "../intl_data.h"
2424

25-
#include <unicode/ucol.h>
25+
#ifdef __cplusplus
26+
#include <unicode/coll.h>
27+
using icu::Collator;
28+
#else
29+
typedef void Collator;
30+
#endif
2631

2732
typedef struct {
2833
// error handling
2934
intl_error err;
3035

3136
// ICU collator
32-
UCollator* ucoll;
37+
Collator* ucoll;
3338

3439
zend_object zo;
3540

‎ext/intl/collator/collator_compare.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
2425

2526
#include "php_intl.h"
2627
#include "intl_convert.h"
@@ -96,10 +97,10 @@ U_CFUNC PHP_FUNCTION( collator_compare )
9697
}
9798

9899
/* Then compare them. */
99-
result = ucol_strcoll(
100-
co->ucoll,
100+
UErrorCode status = U_ZERO_ERROR;
101+
result = co->ucoll->compare(
101102
ustr1, ustr1_len,
102-
ustr2, ustr2_len );
103+
ustr2, ustr2_len, status );
103104

104105
if( ustr1 )
105106
efree( ustr1 );

‎ext/intl/collator/collator_convert.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
2425

2526
#include "php_intl.h"
2627
#include "intl_convert.h"

‎ext/intl/collator/collator_create.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
25+
#include <unicode/locid.h>
2426

2527
#include "php_intl.h"
2628
#include "collator_class.h"
@@ -52,7 +54,7 @@ static int collator_ctor(INTERNAL_FUNCTION_PARAMETERS)
5254
}
5355

5456
/* Open ICU collator. */
55-
co->ucoll = ucol_open( locale, COLLATOR_ERROR_CODE_P( co ) );
57+
co->ucoll = Collator::createInstance( icu::Locale(locale), COLLATOR_ERROR_CODE( co ) );
5658
INTL_CTOR_CHECK_STATUS(co, "unable to open ICU collator");
5759
return SUCCESS;
5860
}

‎ext/intl/collator/collator_error.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
2425

2526
#include "php_intl.h"
2627
#include "collator_class.h"

‎ext/intl/collator/collator_locale.cpp‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
25+
#include <unicode/locid.h>
26+
#include <unicode/tblcoll.h>
2427

2528
#include "php_intl.h"
2629
#include "collator_class.h"
@@ -32,7 +35,6 @@
3235
U_CFUNC PHP_FUNCTION( collator_get_locale )
3336
{
3437
zend_long type = 0;
35-
char* locale_name = nullptr;
3638

3739
COLLATOR_METHOD_INIT_VARS
3840

@@ -51,11 +53,15 @@ U_CFUNC PHP_FUNCTION( collator_get_locale )
5153
}
5254

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

5860
/* Return it. */
61+
const char *locale_name = locale.getName();
62+
if (locale_name[0] == '\0') {
63+
locale_name = "root";
64+
}
5965
RETVAL_STRINGL( locale_name, strlen(locale_name) );
6066
}
6167
/* }}} */
@@ -76,9 +82,11 @@ U_CFUNC PHP_FUNCTION(collator_get_rules)
7682
RETURN_THROWS();
7783
}
7884

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

8492
RETURN_STR(result);

‎ext/intl/collator/collator_sort.cpp‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <string_view>
2222
#include <unicode/unistr.h>
2323
#endif
24+
#include <unicode/coll.h>
2425

2526
#include "php_intl.h"
2627
#include "intl_convert.h"
@@ -89,10 +90,10 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
8990
{
9091
/* Compare the strings using ICU. */
9192
ZEND_ASSERT(INTL_G(current_collator) != nullptr);
92-
ZVAL_LONG(result, ucol_strcoll(
93-
INTL_G(current_collator),
93+
UErrorCode status = U_ZERO_ERROR;
94+
ZVAL_LONG(result, INTL_G(current_collator)->compare(
9495
INTL_ZSTR_VAL(Z_STR_P(str1_p)), INTL_ZSTR_LEN(Z_STR_P(str1_p)),
95-
INTL_ZSTR_VAL(Z_STR_P(str2_p)), INTL_ZSTR_LEN(Z_STR_P(str2_p)) ));
96+
INTL_ZSTR_VAL(Z_STR_P(str2_p)), INTL_ZSTR_LEN(Z_STR_P(str2_p)), status ));
9697
}
9798
else
9899
{
@@ -200,10 +201,10 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
200201

201202
/* Compare the strings using ICU. */
202203
ZEND_ASSERT(INTL_G(current_collator) != nullptr);
203-
ZVAL_LONG(result, ucol_strcoll(
204-
INTL_G(current_collator),
204+
UErrorCode status = U_ZERO_ERROR;
205+
ZVAL_LONG(result, INTL_G(current_collator)->compare(
205206
INTL_ZSTR_VAL(str1), INTL_ZSTR_LEN(str1),
206-
INTL_ZSTR_VAL(str2), INTL_ZSTR_LEN(str2) ));
207+
INTL_ZSTR_VAL(str2), INTL_ZSTR_LEN(str2), status ));
207208

208209
zend_string_release(str1);
209210
zend_string_release(str2);
@@ -288,7 +289,7 @@ static collator_compare_func_t collator_get_compare_function( const zend_long so
288289
/* {{{ Common code shared by collator_sort() and collator_asort() API functions. */
289290
static void collator_sort_internal( int renumber, INTERNAL_FUNCTION_PARAMETERS )
290291
{
291-
UCollator* saved_collator;
292+
Collator* saved_collator;
292293
intl_error* saved_collator_error;
293294
intl_error sort_error;
294295
collator_compare_func_t saved_compare_func;
@@ -497,7 +498,7 @@ U_CFUNC PHP_FUNCTION( collator_sort_with_sort_keys )
497498
/* Get sort key, reallocating the buffer if needed. */
498499
bufLeft = sortKeyBufSize - sortKeyBufOffset;
499500

500-
sortKeyLen = ucol_getSortKey( co->ucoll,
501+
sortKeyLen = co->ucoll->getSortKey(
501502
utf16_buf,
502503
utf16_len,
503504
(uint8_t*)sortKeyBuf + sortKeyBufOffset,
@@ -521,7 +522,7 @@ U_CFUNC PHP_FUNCTION( collator_sort_with_sort_keys )
521522

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

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

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

617618
/* ucol_getSortKey is exception in that the key length includes the
618619
* NUL terminator*/
619-
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, nullptr, 0);
620+
key_len = co->ucoll->getSortKey(ustr, ustr_len, nullptr, 0);
620621
if(!key_len) {
621622
efree( ustr );
622623
RETURN_FALSE;
623624
}
624625
key_str = zend_string_alloc(key_len, 0);
625-
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, (uint8_t*)ZSTR_VAL(key_str), key_len);
626+
key_len = co->ucoll->getSortKey(ustr, ustr_len, (uint8_t*)ZSTR_VAL(key_str), key_len);
626627
efree( ustr );
627628
if(!key_len) {
628629
zend_string_efree(key_str);

‎ext/intl/php_intl.h‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,19 @@ extern zend_module_entry intl_module_entry;
4040
#include "TSRM.h"
4141
#endif
4242

43+
#ifdef __cplusplus
44+
U_NAMESPACE_BEGIN
45+
class Collator;
46+
U_NAMESPACE_END
47+
#endif
48+
4349
BEGIN_EXTERN_C()
4450
ZEND_BEGIN_MODULE_GLOBALS(intl)
45-
struct UCollator *current_collator;
51+
#ifdef __cplusplus
52+
icu::Collator *current_collator;
53+
#else
54+
void *current_collator;
55+
#endif
4656
intl_error *current_collator_error;
4757
char* default_locale;
4858
collator_compare_func_t compare_func;

0 commit comments

Comments
 (0)