diff options
author | Dmitry Stogov <dmitry@zend.com> | 2015-07-01 13:26:39 +0300 |
---|---|---|
committer | Dmitry Stogov <dmitry@zend.com> | 2015-07-01 13:26:39 +0300 |
commit | f7b6de5b5eea4554a1eb3e7bfc64877567693985 (patch) | |
tree | 0f271798439b3c8c1982f52b5b73073849c7433c /ext/intl/intl_convertcpp.cpp | |
parent | 53403fe56d7c80eff75a3bd1bfd014aa33ac457b (diff) | |
download | php-git-f7b6de5b5eea4554a1eb3e7bfc64877567693985.tar.gz |
Cleanup (avoid string reallocations)
Diffstat (limited to 'ext/intl/intl_convertcpp.cpp')
-rw-r--r-- | ext/intl/intl_convertcpp.cpp | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/ext/intl/intl_convertcpp.cpp b/ext/intl/intl_convertcpp.cpp index e33d845a72..67fb946edd 100644 --- a/ext/intl/intl_convertcpp.cpp +++ b/ext/intl/intl_convertcpp.cpp @@ -53,42 +53,39 @@ int intl_stringFromChar(UnicodeString &ret, char *str, size_t str_len, UErrorCod /* }}} */ /* {{{ intl_charFromString - * faster than doing intl_convert_utf16_to_utf8(&res, &res_len, + * faster than doing intl_convert_utf16_to_utf8( * from.getBuffer(), from.length(), &status), * but consumes more memory */ -int intl_charFromString(const UnicodeString &from, char **res, size_t *res_len, UErrorCode *status) +zend_string* intl_charFromString(const UnicodeString &from, UErrorCode *status) { + zend_string *u8res; + if (from.isBogus()) { - return FAILURE; + return NULL; } //the number of UTF-8 code units is not larger than that of UTF-16 code - //units * 3 + 1 for the terminator - int32_t capacity = from.length() * 3 + 1; + //units * 3 + int32_t capacity = from.length() * 3; if (from.isEmpty()) { - *res = (char*)emalloc(1); - **res = '\0'; - *res_len = 0; - return SUCCESS; + return ZSTR_EMPTY_ALLOC(); } - *res = (char*)emalloc(capacity); - *res_len = 0; //tbd + u8res = zend_string_alloc(capacity, 0); const UChar *utf16buf = from.getBuffer(); int32_t actual_len; - u_strToUTF8WithSub(*res, capacity - 1, &actual_len, utf16buf, from.length(), + u_strToUTF8WithSub(ZSTR_VAL(u8res), capacity, &actual_len, utf16buf, from.length(), U_SENTINEL, NULL, status); if (U_FAILURE(*status)) { - efree(*res); - *res = NULL; - return FAILURE; + zend_string_free(u8res); + return NULL; } - (*res)[actual_len] = '\0'; - *res_len = actual_len; + ZSTR_VAL(u8res)[actual_len] = '\0'; + ZSTR_LEN(u8res) = actual_len; - return SUCCESS; + return u8res; } /* }}} */ |