Skip to content

Commit 5c5727a

Browse files
committed
Avoid make_printable_zval() in intl collator
Use zval_get_string() instead. This code is a real mess though, and could use some more thorough cleanup.
1 parent 3b18c06 commit 5c5727a

File tree

4 files changed

+35
-67
lines changed

4 files changed

+35
-67
lines changed

ext/intl/collator/collator_convert.c

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -174,39 +174,27 @@ zval* collator_convert_zstr_utf16_to_utf8( zval* utf16_zval, zval *rv )
174174
}
175175
/* }}} */
176176

177-
/* {{{ collator_convert_zstr_utf8_to_utf16
178-
*
179-
* Convert string from utf8 to utf16.
180-
*
181-
* @param zval* utf8_zval String to convert.
182-
*
183-
* @return zval* Converted string.
184-
*/
185-
zval* collator_convert_zstr_utf8_to_utf16( zval* utf8_zval, zval *rv )
177+
zend_string *collator_convert_zstr_utf8_to_utf16(zend_string *utf8_str)
186178
{
187-
zval* zstr = NULL;
188-
UChar* ustr = NULL;
189-
int32_t ustr_len = 0;
179+
UChar *ustr = NULL;
180+
int32_t ustr_len = 0;
190181
UErrorCode status = U_ZERO_ERROR;
191182

192183
/* Convert the string to UTF-16. */
193184
intl_convert_utf8_to_utf16(
194185
&ustr, &ustr_len,
195-
Z_STRVAL_P( utf8_zval ), Z_STRLEN_P( utf8_zval ),
196-
&status );
186+
ZSTR_VAL(utf8_str), ZSTR_LEN(utf8_str),
187+
&status);
197188
// FIXME Or throw error or use intl internal error handler
198-
if( U_FAILURE( status ) )
199-
php_error( E_WARNING, "Error casting object to string in collator_convert_zstr_utf8_to_utf16()" );
189+
if (U_FAILURE(status)) {
190+
php_error(E_WARNING,
191+
"Error casting object to string in collator_convert_zstr_utf8_to_utf16()");
192+
}
200193

201-
/* Set string. */
202-
zstr = rv;
203-
ZVAL_STRINGL( zstr, (char*)ustr, UBYTES(ustr_len));
204-
//???
194+
zend_string *zstr = zend_string_init((char *) ustr, UBYTES(ustr_len), 0);
205195
efree((char *)ustr);
206-
207196
return zstr;
208197
}
209-
/* }}} */
210198

211199
/* {{{ collator_convert_object_to_string
212200
* Convert object to UTF16-encoded string.
@@ -346,42 +334,26 @@ zval* collator_convert_string_to_number_if_possible( zval* str, zval *rv )
346334
}
347335
/* }}} */
348336

349-
/* {{{ collator_make_printable_zval
350-
*
351-
* Returns string from input zval.
337+
/* Returns string from input zval.
352338
*
353339
* @param zval* arg zval to get string from
354340
*
355-
* @return zval* UTF16 string.
341+
* @return zend_string* UTF16 string.
356342
*/
357-
zval* collator_make_printable_zval( zval* arg, zval *rv)
343+
zend_string *collator_zval_to_string(zval *arg)
358344
{
359-
zval arg_copy;
360-
zval* str = NULL;
361-
362-
if( Z_TYPE_P(arg) != IS_STRING )
363-
{
364-
365-
int use_copy = zend_make_printable_zval(arg, &arg_copy);
366-
367-
if( use_copy )
368-
{
369-
str = collator_convert_zstr_utf8_to_utf16( &arg_copy, rv );
370-
zval_ptr_dtor_str( &arg_copy );
371-
}
372-
else
373-
{
374-
str = collator_convert_zstr_utf8_to_utf16( arg, rv );
375-
}
376-
}
377-
else
378-
{
379-
COLLATOR_CONVERT_RETURN_FAILED( arg );
345+
// TODO: This is extremely weird in that it leaves pre-existing strings alone and does not
346+
// perform a UTF-8 to UTF-16 conversion for them. The assumption is that values that are
347+
// already strings have already been converted beforehand. It would be good to clean this up.
348+
if (Z_TYPE_P(arg) == IS_STRING) {
349+
return zend_string_copy(Z_STR_P(arg));
380350
}
381351

382-
return str;
352+
zend_string *utf8_str = zval_get_string(arg);
353+
zend_string *utf16_str = collator_convert_zstr_utf8_to_utf16(utf8_str);
354+
zend_string_release(utf8_str);
355+
return utf16_str;
383356
}
384-
/* }}} */
385357

386358
/* {{{ collator_normalize_sort_argument
387359
*

ext/intl/collator/collator_convert.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ void collator_convert_hash_from_utf8_to_utf16( HashTable* hash, UErrorCode* stat
2323
void collator_convert_hash_from_utf16_to_utf8( HashTable* hash, UErrorCode* status );
2424

2525
zval* collator_convert_zstr_utf16_to_utf8( zval* utf16_zval, zval *rv );
26-
zval* collator_convert_zstr_utf8_to_utf16( zval* utf8_zval, zval *rv );
26+
zend_string *collator_convert_zstr_utf8_to_utf16(zend_string *utf8_str);
2727

2828
zval* collator_normalize_sort_argument( zval* arg, zval *rv );
2929
zval* collator_convert_object_to_string( zval* obj, zval *rv );
3030
zval* collator_convert_string_to_number( zval* arg, zval *rv );
3131
zval* collator_convert_string_to_number_if_possible( zval* str, zval *rv );
3232
zval* collator_convert_string_to_double( zval* str, zval *rv );
3333

34-
zval* collator_make_printable_zval( zval* arg, zval *rv );
34+
zend_string *collator_zval_to_string(zval *arg);
3535

3636
#endif // COLLATOR_CONVERT_H

ext/intl/collator/collator_sort.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ static int collator_regular_compare_function(zval *result, zval *op1, zval *op2)
7373
ZEND_ASSERT(INTL_G(current_collator) != NULL);
7474
ZVAL_LONG(result, ucol_strcoll(
7575
INTL_G(current_collator),
76-
INTL_Z_STRVAL_P(str1_p), INTL_Z_STRLEN_P(str1_p),
77-
INTL_Z_STRVAL_P(str2_p), INTL_Z_STRLEN_P(str2_p) ));
76+
INTL_ZSTR_VAL(Z_STR_P(str1_p)), INTL_ZSTR_LEN(Z_STR_P(str1_p)),
77+
INTL_ZSTR_VAL(Z_STR_P(str2_p)), INTL_ZSTR_LEN(Z_STR_P(str2_p)) ));
7878
}
7979
else
8080
{
@@ -167,23 +167,19 @@ static int collator_numeric_compare_function(zval *result, zval *op1, zval *op2)
167167
*/
168168
static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
169169
{
170-
zval str1, str2;
171-
int rc = SUCCESS;
172-
zval *str1_p = NULL;
173-
zval *str2_p = NULL;
174-
175-
str1_p = collator_make_printable_zval( op1, &str1);
176-
str2_p = collator_make_printable_zval( op2, &str2 );
170+
int rc = SUCCESS;
171+
zend_string *str1 = collator_zval_to_string(op1);
172+
zend_string *str2 = collator_zval_to_string(op2);
177173

178174
/* Compare the strings using ICU. */
179175
ZEND_ASSERT(INTL_G(current_collator) != NULL);
180176
ZVAL_LONG(result, ucol_strcoll(
181177
INTL_G(current_collator),
182-
INTL_Z_STRVAL_P(str1_p), INTL_Z_STRLEN_P(str1_p),
183-
INTL_Z_STRVAL_P(str2_p), INTL_Z_STRLEN_P(str2_p) ));
178+
INTL_ZSTR_VAL(str1), INTL_ZSTR_LEN(str1),
179+
INTL_ZSTR_VAL(str2), INTL_ZSTR_LEN(str2) ));
184180

185-
zval_ptr_dtor( str1_p );
186-
zval_ptr_dtor( str2_p );
181+
zend_string_release(str1);
182+
zend_string_release(str2);
187183

188184
return rc;
189185
}

ext/intl/intl_common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ END_EXTERN_C()
3838
#define USIZE(data) sizeof((data))/sizeof(UChar)
3939
#define UCHARS(len) ((len) / sizeof(UChar))
4040

41-
#define INTL_Z_STRVAL_P(str) (UChar*) Z_STRVAL_P(str)
42-
#define INTL_Z_STRLEN_P(str) UCHARS( Z_STRLEN_P(str) )
41+
#define INTL_ZSTR_VAL(str) (UChar*) ZSTR_VAL(str)
42+
#define INTL_ZSTR_LEN(str) UCHARS(ZSTR_LEN(str))
4343

4444
BEGIN_EXTERN_C()
4545
extern zend_class_entry *IntlException_ce_ptr;

0 commit comments

Comments
 (0)