+ // check for buffer overflow, resize and retry
+ if (*status == U_BUFFER_OVERFLOW_ERROR
+ //|| dest16->utf16_len > dest16->utf16_cap
+ ){
+ icu_buf_utf16_resize(dest16, utf16_len * 2);
+ *status = U_ZERO_ERROR;
+ u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
+ &utf16_len,
+ src8cstr, src8cstr_len, status);
+ }
+
+ // if (*status != U_BUFFER_OVERFLOW_ERROR
+ if (U_SUCCESS(*status)
+ && utf16_len < dest16->utf16_cap)
+ dest16->utf16_len = utf16_len;
+ else {
+ dest16->utf16[0] = (UChar) 0;
+ dest16->utf16_len = 0;
+ }
+
+ return *status;
+};
+
+
+
+
+UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
+ struct icu_buf_utf16 * src16,
+ UErrorCode * status)
+{
+ int32_t utf8_len = 0;
+
+ u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
+ &utf8_len,
+ src16->utf16, src16->utf16_len, status);
+
+ // check for buffer overflow, resize and retry
+ if (*status == U_BUFFER_OVERFLOW_ERROR
+ //|| dest8->utf8_len > dest8->utf8_cap
+ ){
+ icu_buf_utf8_resize(dest8, utf8_len * 2);
+ *status = U_ZERO_ERROR;
+ u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
+ &utf8_len,
+ src16->utf16, src16->utf16_len, status);
+
+ }
+
+ //if (*status != U_BUFFER_OVERFLOW_ERROR
+ if (U_SUCCESS(*status)
+ && utf8_len < dest8->utf8_cap)
+ dest8->utf8_len = utf8_len;
+ else {
+ dest8->utf8[0] = (uint8_t) 0;
+ dest8->utf8_len = 0;
+ }
+
+ return *status;
+};
+
+
+
+struct icu_casemap * icu_casemap_create(const char *locale, char action,
+ UErrorCode *status)
+{
+ struct icu_casemap * casemap
+ = (struct icu_casemap *) malloc(sizeof(struct icu_casemap));
+ strcpy(casemap->locale, locale);
+ casemap->action = action;
+
+ switch(casemap->action) {
+ case 'l':
+ break;
+ case 'u':
+ break;
+ case 't':
+ break;
+ case 'f':
+ break;
+ default:
+ icu_casemap_destroy(casemap);
+ return 0;
+ }
+
+ return casemap;
+};
+
+void icu_casemap_destroy(struct icu_casemap * casemap)
+{
+ if (casemap)
+ free(casemap);
+};
+
+
+int icu_casemap_casemap(struct icu_casemap * casemap,
+ struct icu_buf_utf16 * dest16,
+ struct icu_buf_utf16 * src16,
+ UErrorCode *status)
+{
+ if(!casemap)
+ return 0;
+
+ return icu_utf16_casemap(dest16, src16,
+ casemap->locale, casemap->action, status);
+};
+
+
+int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
+ struct icu_buf_utf16 * src16,
+ const char *locale, char action,
+ UErrorCode *status)
+{
+ int32_t dest16_len = 0;
+
+ switch(action) {
+ case 'l':
+ dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ locale, status);
+ break;
+ case 'u':
+ dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ locale, status);
+ break;
+ case 't':
+ dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ 0, locale, status);
+ break;
+ case 'f':
+ dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ U_FOLD_CASE_DEFAULT, status);
+ break;
+
+ default:
+ return U_UNSUPPORTED_ERROR;
+ break;
+ }
+
+ // check for buffer overflow, resize and retry
+ if (*status == U_BUFFER_OVERFLOW_ERROR
+ && dest16 != src16 // do not resize if in-place conversion
+ //|| dest16_len > dest16->utf16_cap
+ ){
+ icu_buf_utf16_resize(dest16, dest16_len * 2);
+ *status = U_ZERO_ERROR;
+
+
+ switch(action) {
+ case 'l':
+ dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ locale, status);
+ break;
+ case 'u':
+ dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ locale, status);
+ break;
+ case 't':
+ dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ 0, locale, status);
+ break;
+ case 'f':
+ dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
+ src16->utf16, src16->utf16_len,
+ U_FOLD_CASE_DEFAULT, status);
+ break;
+
+ default:
+ return U_UNSUPPORTED_ERROR;
+ break;
+ }
+ }
+
+ if (U_SUCCESS(*status)
+ && dest16_len < dest16->utf16_cap)
+ dest16->utf16_len = dest16_len;
+ else {
+ dest16->utf16[0] = (UChar) 0;
+ dest16->utf16_len = 0;
+ }
+
+ return *status;