From 6c5f1e84b0917856d2bb726ee34bd982c9b91aa1 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 15 Nov 2011 13:45:48 +0100 Subject: [PATCH] Minor refactor of ICU casemap --- src/icu_casemap.c | 106 +++++++++++++++++++++-------------------------------- 1 file changed, 42 insertions(+), 64 deletions(-) diff --git a/src/icu_casemap.c b/src/icu_casemap.c index 6453229..f75381e 100644 --- a/src/icu_casemap.c +++ b/src/icu_casemap.c @@ -80,95 +80,73 @@ int icu_casemap_casemap(struct icu_casemap * casemap, 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; - if (!src16->utf16_len) - { /* guarding for empty source string */ - if (dest16->utf16) - dest16->utf16[0] = (UChar) 0; - dest16->utf16_len = 0; - return U_ZERO_ERROR; - } - +static uint32_t icu_utf16_sub(struct icu_buf_utf16 * dest16, + struct icu_buf_utf16 * src16, + const char *locale, char action, + UErrorCode *status) +{ switch (action) { case 'l': case 'L': - dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; + return u_strToLower(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + locale, status); case 'u': case 'U': - dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); + return u_strToUpper(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + locale, status); break; case 't': case 'T': - dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - 0, locale, status); + return u_strToTitle(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + 0, locale, status); break; case 'f': case 'F': - dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - U_FOLD_CASE_DEFAULT, status); + return u_strFoldCase(dest16->utf16, dest16->utf16_cap, + src16->utf16, src16->utf16_len, + U_FOLD_CASE_DEFAULT, status); break; - default: - return U_UNSUPPORTED_ERROR; + *status = U_UNSUPPORTED_ERROR; break; } + return 0; +} + + +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; + + if (!src16->utf16_len) + { /* guarding for empty source string */ + if (dest16->utf16) + dest16->utf16[0] = (UChar) 0; + dest16->utf16_len = 0; + return U_ZERO_ERROR; + } + + dest16_len = icu_utf16_sub(dest16, src16, locale, action, status); /* check for buffer overflow, resize and retry */ if (*status == U_BUFFER_OVERFLOW_ERROR - && dest16 != src16 /* do not resize if in-place conversion */ - ) + && dest16 != src16) /* do not resize if in-place conversion */ { icu_buf_utf16_resize(dest16, dest16_len * 2); *status = U_ZERO_ERROR; - switch (action) { - case 'l': - case 'L': - dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; - case 'u': - case 'U': - dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - locale, status); - break; - case 't': - case 'T': - dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap, - src16->utf16, src16->utf16_len, - 0, locale, status); - break; - case 'f': - 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; - } + icu_utf16_sub(dest16, src16, locale, action, status); } - - if (U_SUCCESS(*status) - && dest16_len <= dest16->utf16_cap) + + if (U_SUCCESS(*status) && dest16_len <= dest16->utf16_cap) dest16->utf16_len = dest16_len; else { -- 1.7.10.4