Skip to content

Commit f7b6de5

Browse files
committed
Cleanup (avoid string reallocations)
1 parent 53403fe commit f7b6de5

23 files changed

+197
-294
lines changed

Diff for: ext/intl/breakiterator/rulebasedbreakiterator_methods.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,18 @@ U_CFUNC PHP_FUNCTION(rbbi_get_rules)
114114

115115
BREAKITER_METHOD_FETCH_OBJECT;
116116

117-
char *str;
118-
size_t str_len;
117+
zend_string *u8str;
119118
const UnicodeString rules = fetch_rbbi(bio)->getRules();
120119

121-
if (intl_charFromString(rules, &str, &str_len, BREAKITER_ERROR_CODE_P(bio)) == FAILURE)
120+
u8str = intl_charFromString(rules, BREAKITER_ERROR_CODE_P(bio));
121+
if (!u8str)
122122
{
123123
intl_errors_set(BREAKITER_ERROR_P(bio), BREAKITER_ERROR_CODE(bio),
124124
"rbbi_hash_code: Error converting result to UTF-8 string",
125125
0);
126126
RETURN_FALSE;
127127
}
128-
RETVAL_STRINGL(str, str_len);
129-
//???
130-
efree(str);
128+
RETVAL_STR(u8str);
131129
}
132130

133131
U_CFUNC PHP_FUNCTION(rbbi_get_rule_status)

Diff for: ext/intl/collator/collator_convert.c

+12-19
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ static void collator_convert_hash_item_from_utf16_to_utf8(
8787
{
8888
const char* old_val;
8989
size_t old_val_len;
90-
char* new_val = NULL;
91-
size_t new_val_len = 0;
90+
zend_string* u8str;
9291
zval znew_val;
9392

9493
/* Process string values only. */
@@ -99,15 +98,13 @@ static void collator_convert_hash_item_from_utf16_to_utf8(
9998
old_val_len = Z_STRLEN_P( hashData );
10099

101100
/* Convert it from UTF-16LE to UTF-8 and save the result to new_val[_len]. */
102-
intl_convert_utf16_to_utf8( &new_val, &new_val_len,
101+
u8str = intl_convert_utf16_to_utf8(
103102
(UChar*)old_val, UCHARS(old_val_len), status );
104-
if( U_FAILURE( *status ) )
103+
if( !u8str )
105104
return;
106105

107106
/* Update current hash item with the converted value. */
108-
ZVAL_STRINGL( &znew_val, new_val, new_val_len);
109-
//???
110-
efree(new_val);
107+
ZVAL_NEW_STR( &znew_val, u8str);
111108

112109
if( hashKey )
113110
{
@@ -169,23 +166,19 @@ void collator_convert_hash_from_utf16_to_utf8( HashTable* hash, UErrorCode* stat
169166
*/
170167
zval* collator_convert_zstr_utf16_to_utf8( zval* utf16_zval, zval *rv )
171168
{
172-
zval* utf8_zval = NULL;
173-
char* str = NULL;
174-
size_t str_len = 0;
169+
zend_string* u8str;
175170
UErrorCode status = U_ZERO_ERROR;
176171

177172
/* Convert to utf8 then. */
178-
intl_convert_utf16_to_utf8( &str, &str_len,
173+
u8str = intl_convert_utf16_to_utf8(
179174
(UChar*) Z_STRVAL_P(utf16_zval), UCHARS( Z_STRLEN_P(utf16_zval) ), &status );
180-
if( U_FAILURE( status ) )
175+
if( !u8str ) {
181176
php_error( E_WARNING, "Error converting utf16 to utf8 in collator_convert_zval_utf16_to_utf8()" );
182-
183-
utf8_zval = rv;
184-
ZVAL_STRINGL( utf8_zval, str, str_len);
185-
//???
186-
efree(str);
187-
188-
return utf8_zval;
177+
ZVAL_EMPTY_STRING( rv );
178+
} else {
179+
ZVAL_NEW_STR( rv, u8str );
180+
}
181+
return rv;
189182
}
190183
/* }}} */
191184

Diff for: ext/intl/collator/collator_sort.c

+6-7
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,8 @@ PHP_FUNCTION( collator_get_sort_key )
549549
size_t str_len = 0;
550550
UChar* ustr = NULL;
551551
int32_t ustr_len = 0;
552-
uint8_t* key = NULL;
553552
int key_len = 0;
553+
zend_string* key_str;
554554

555555
COLLATOR_METHOD_INIT_VARS
556556

@@ -597,20 +597,19 @@ PHP_FUNCTION( collator_get_sort_key )
597597

598598
/* ucol_getSortKey is exception in that the key length includes the
599599
* NUL terminator*/
600-
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, 0);
600+
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, NULL, 0);
601601
if(!key_len) {
602602
efree( ustr );
603603
RETURN_FALSE;
604604
}
605-
key = emalloc(key_len);
606-
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, key, key_len);
605+
key_str = zend_string_alloc(key_len, 0);
606+
key_len = ucol_getSortKey(co->ucoll, ustr, ustr_len, (uint8_t*)ZSTR_VAL(key_str), key_len);
607607
efree( ustr );
608608
if(!key_len) {
609609
RETURN_FALSE;
610610
}
611-
RETVAL_STRINGL((char *)key, key_len - 1);
612-
//????
613-
efree(key);
611+
ZSTR_LEN(key_str) = key_len - 1;
612+
RETVAL_NEW_STR(key_str);
614613
}
615614
/* }}} */
616615

Diff for: ext/intl/converter/converter.c

+27-40
Original file line numberDiff line numberDiff line change
@@ -652,27 +652,26 @@ static PHP_METHOD(UConverter, getSubstChars) {
652652
/* }}} */
653653

654654
/* {{{ php_converter_do_convert */
655-
static zend_bool php_converter_do_convert(UConverter *dest_cnv, char **pdest, int32_t *pdest_len,
656-
UConverter *src_cnv, const char *src, int32_t src_len,
657-
php_converter_object *objval
658-
) {
655+
static zend_string* php_converter_do_convert(UConverter *dest_cnv,
656+
UConverter *src_cnv, const char *src, int32_t src_len,
657+
php_converter_object *objval
658+
) {
659659
UErrorCode error = U_ZERO_ERROR;
660-
int32_t dest_len,
661-
temp_len;
662-
char *dest;
660+
int32_t temp_len, ret_len;
661+
zend_string *ret;
663662
UChar *temp;
664663

665664
if (!src_cnv || !dest_cnv) {
666665
php_converter_throw_failure(objval, U_INVALID_STATE_ERROR,
667666
"Internal converters not initialized");
668-
return 0;
667+
return NULL;
669668
}
670669

671670
/* Get necessary buffer size first */
672671
temp_len = 1 + ucnv_toUChars(src_cnv, NULL, 0, src, src_len, &error);
673672
if (U_FAILURE(error) && error != U_BUFFER_OVERFLOW_ERROR) {
674673
THROW_UFAILURE(objval, "ucnv_toUChars", error);
675-
return 0;
674+
return NULL;
676675
}
677676
temp = safe_emalloc(sizeof(UChar), temp_len, sizeof(UChar));
678677

@@ -682,36 +681,31 @@ static zend_bool php_converter_do_convert(UConverter *dest_cnv, char **pdest, in
682681
if (U_FAILURE(error)) {
683682
THROW_UFAILURE(objval, "ucnv_toUChars", error);
684683
efree(temp);
685-
return 0;
684+
return NULL;
686685
}
687686
temp[temp_len] = 0;
688687

689688
/* Get necessary output buffer size */
690-
dest_len = 1 + ucnv_fromUChars(dest_cnv, NULL, 0, temp, temp_len, &error);
689+
ret_len = ucnv_fromUChars(dest_cnv, NULL, 0, temp, temp_len, &error);
691690
if (U_FAILURE(error) && error != U_BUFFER_OVERFLOW_ERROR) {
692691
THROW_UFAILURE(objval, "ucnv_fromUChars", error);
693692
efree(temp);
694-
return 0;
693+
return NULL;
695694
}
696695

697-
dest = safe_emalloc(sizeof(char), dest_len, sizeof(char));
696+
ret = zend_string_alloc(ret_len, 0);
698697

699698
/* Convert to final encoding */
700699
error = U_ZERO_ERROR;
701-
dest_len = ucnv_fromUChars(dest_cnv, dest, dest_len, temp, temp_len, &error);
700+
ZSTR_LEN(ret) = ucnv_fromUChars(dest_cnv, ZSTR_VAL(ret), ret_len+1, temp, temp_len, &error);
702701
efree(temp);
703702
if (U_FAILURE(error)) {
704703
THROW_UFAILURE(objval, "ucnv_fromUChars", error);
705-
efree(dest);
706-
return 0;
704+
zend_string_free(ret);
705+
return NULL;
707706
}
708707

709-
*pdest = dest;
710-
if (pdest_len) {
711-
*pdest_len = dest_len;
712-
}
713-
714-
return 1;
708+
return ret;
715709
}
716710
/* }}} */
717711

@@ -752,9 +746,9 @@ ZEND_END_ARG_INFO();
752746

753747
static PHP_METHOD(UConverter, convert) {
754748
php_converter_object *objval = CONV_GET(getThis());
755-
char *str, *dest;
749+
char *str;
756750
size_t str_len;
757-
int32_t dest_len;
751+
zend_string *ret;
758752
zend_bool reverse = 0;
759753

760754
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b",
@@ -765,15 +759,12 @@ static PHP_METHOD(UConverter, convert) {
765759
}
766760
intl_errors_reset(&objval->error);
767761

768-
if (php_converter_do_convert(reverse ? objval->src : objval->dest,
769-
&dest, &dest_len,
770-
reverse ? objval->dest : objval->src,
771-
str, str_len,
772-
objval)) {
773-
RETVAL_STRINGL(dest, dest_len);
774-
//???
775-
efree(dest);
776-
return;
762+
ret = php_converter_do_convert(reverse ? objval->src : objval->dest,
763+
reverse ? objval->dest : objval->src,
764+
str, str_len,
765+
objval);
766+
if (ret) {
767+
RETURN_NEW_STR(ret);
777768
} else {
778769
RETURN_FALSE;
779770
}
@@ -804,8 +795,7 @@ static PHP_METHOD(UConverter, transcode) {
804795

805796
if (php_converter_set_encoding(NULL, &src_cnv, src, src_len) &&
806797
php_converter_set_encoding(NULL, &dest_cnv, dest, dest_len)) {
807-
char *out = NULL;
808-
int out_len = 0;
798+
zend_string *ret;
809799
UErrorCode error = U_ZERO_ERROR;
810800

811801
if (options && zend_hash_num_elements(Z_ARRVAL_P(options))) {
@@ -826,11 +816,8 @@ static PHP_METHOD(UConverter, transcode) {
826816
}
827817

828818
if (U_SUCCESS(error) &&
829-
php_converter_do_convert(dest_cnv, &out, &out_len, src_cnv, str, str_len, NULL)) {
830-
RETVAL_STRINGL(out, out_len);
831-
//???
832-
efree(out);
833-
return;
819+
(ret = php_converter_do_convert(dest_cnv, src_cnv, str, str_len, NULL)) != NULL) {
820+
RETURN_NEW_STR(ret);
834821
}
835822

836823
if (U_FAILURE(error)) {

Diff for: ext/intl/dateformat/dateformat_attrcpp.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ static inline DateFormat *fetch_datefmt(IntlDateFormatter_object *dfo) {
4444
*/
4545
U_CFUNC PHP_FUNCTION(datefmt_get_timezone_id)
4646
{
47-
char *str;
48-
size_t str_len;
47+
zend_string *u8str;
4948
DATE_FORMAT_METHOD_INIT_VARS;
5049

5150
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
@@ -59,12 +58,10 @@ U_CFUNC PHP_FUNCTION(datefmt_get_timezone_id)
5958

6059
UnicodeString res = UnicodeString();
6160
fetch_datefmt(dfo)->getTimeZone().getID(res);
62-
intl_charFromString(res, &str, &str_len, &INTL_DATA_ERROR_CODE(dfo));
61+
u8str = intl_charFromString(res, &INTL_DATA_ERROR_CODE(dfo));
6362
INTL_METHOD_CHECK_STATUS(dfo, "Could not convert time zone id to UTF-8");
6463

65-
RETVAL_STRINGL(str, str_len);
66-
//????
67-
efree(str);
64+
RETVAL_STR(u8str);
6865
}
6966

7067
/* {{{ proto IntlTimeZone IntlDateFormatter::getTimeZone()

Diff for: ext/intl/dateformat/dateformat_format_object.cpp

+4-6
Original file line numberDiff line numberDiff line change
@@ -221,21 +221,19 @@ U_CFUNC PHP_FUNCTION(datefmt_format_object)
221221
timeZone = NULL;
222222

223223
{
224-
char *ret_str;
225-
size_t ret_str_len;
224+
zend_string *u8str;
226225
UnicodeString result = UnicodeString();
227226
df->format(date, result);
228227

229-
if (intl_charFromString(result, &ret_str, &ret_str_len, &status) == FAILURE) {
228+
u8str = intl_charFromString(result, &status);
229+
if (!u8str) {
230230
intl_error_set(NULL, status,
231231
"datefmt_format_object: error converting result to UTF-8",
232232
0);
233233
RETVAL_FALSE;
234234
goto cleanup;
235235
}
236-
RETVAL_STRINGL(ret_str, ret_str_len);
237-
//???
238-
efree(ret_str);
236+
RETVAL_STR(u8str);
239237
}
240238

241239

Diff for: ext/intl/formatter/formatter_parse.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ PHP_FUNCTION( numfmt_parse_currency )
129129
UChar currency[5] = {0};
130130
UChar* sstr = NULL;
131131
int32_t sstr_len = 0;
132-
char *currency_str = NULL;
133-
size_t currency_len = 0;
132+
zend_string *u8str;
134133
char *str;
135134
size_t str_len;
136135
int32_t* position_p = NULL;
@@ -173,12 +172,10 @@ PHP_FUNCTION( numfmt_parse_currency )
173172
INTL_METHOD_CHECK_STATUS( nfo, "Number parsing failed" );
174173

175174
/* Convert parsed currency to UTF-8 and pass it back to caller. */
176-
intl_convert_utf16_to_utf8(&currency_str, &currency_len, currency, u_strlen(currency), &INTL_DATA_ERROR_CODE(nfo));
175+
u8str = intl_convert_utf16_to_utf8(currency, u_strlen(currency), &INTL_DATA_ERROR_CODE(nfo));
177176
INTL_METHOD_CHECK_STATUS( nfo, "Currency conversion to UTF-8 failed" );
178177
zval_dtor( zcurrency );
179-
ZVAL_STRINGL(zcurrency, currency_str, currency_len);
180-
//????
181-
efree(currency_str);
178+
ZVAL_NEW_STR(zcurrency, u8str);
182179

183180
RETVAL_DOUBLE( number );
184181
}

0 commit comments

Comments
 (0)