intl_convertcpp.cpp [plain text]
#include "intl_cppshims.h"
#include "intl_convertcpp.h"
#include <unicode/ustring.h>
extern "C" {
#include <php.h>
}
int intl_stringFromChar(UnicodeString &ret, char *str, size_t str_len, UErrorCode *status)
{
if(str_len > INT32_MAX) {
*status = U_BUFFER_OVERFLOW_ERROR;
ret.setToBogus();
return FAILURE;
}
int32_t capacity = (int32_t)str_len + 1;
UChar *utf16 = ret.getBuffer(capacity);
int32_t utf16_len = 0;
*status = U_ZERO_ERROR;
u_strFromUTF8WithSub(utf16, ret.getCapacity(), &utf16_len,
str, str_len, U_SENTINEL , NULL,
status);
ret.releaseBuffer(utf16_len);
if (U_FAILURE(*status)) {
ret.setToBogus();
return FAILURE;
}
return SUCCESS;
}
zend_string* intl_charFromString(const UnicodeString &from, UErrorCode *status)
{
zend_string *u8res;
if (from.isBogus()) {
return NULL;
}
int32_t capacity = from.length() * 3;
if (from.isEmpty()) {
return ZSTR_EMPTY_ALLOC();
}
u8res = zend_string_alloc(capacity, 0);
const UChar *utf16buf = from.getBuffer();
int32_t actual_len;
u_strToUTF8WithSub(ZSTR_VAL(u8res), capacity, &actual_len, utf16buf, from.length(),
U_SENTINEL, NULL, status);
if (U_FAILURE(*status)) {
zend_string_free(u8res);
return NULL;
}
ZSTR_VAL(u8res)[actual_len] = '\0';
ZSTR_LEN(u8res) = actual_len;
return u8res;
}