1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- #include "intl_cppshims.h"
- #include "intl_convertcpp.h"
- #include <unicode/ustring.h>
- extern "C" {
- #include <php.h>
- }
- int intl_stringFromChar(UnicodeString &ret, char *str, int32_t str_len, UErrorCode *status)
- {
-
-
- int32_t capacity = 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;
- }
- int intl_charFromString(const UnicodeString &from, char **res, int *res_len, UErrorCode *status)
- {
- if (from.isBogus()) {
- return FAILURE;
- }
-
-
- int32_t capacity = from.length() * 3 + 1;
- if (from.isEmpty()) {
- *res = (char*)emalloc(1);
- **res = '\0';
- *res_len = 0;
- return SUCCESS;
- }
- *res = (char*)emalloc(capacity);
- *res_len = 0;
- const UChar *utf16buf = from.getBuffer();
- int32_t actual_len;
- u_strToUTF8WithSub(*res, capacity - 1, &actual_len, utf16buf, from.length(),
- U_SENTINEL, NULL, status);
- if (U_FAILURE(*status)) {
- efree(*res);
- *res = NULL;
- return FAILURE;
- }
- (*res)[actual_len] = '\0';
- *res_len = (int)actual_len;
-
- return SUCCESS;
- }
|