1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
8 * \brief ICU character case (u_strToUpper, etc)
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
26 #include <unicode/ustring.h>
33 struct icu_casemap *icu_casemap_create(char action, UErrorCode *status)
35 struct icu_casemap *casemap
36 = (struct icu_casemap *) xmalloc(sizeof(struct icu_casemap));
37 casemap->action = action;
39 switch (casemap->action)
51 icu_casemap_destroy(casemap);
57 struct icu_casemap *icu_casemap_clone(struct icu_casemap *old)
59 struct icu_casemap * casemap
60 = (struct icu_casemap *) xmalloc(sizeof(struct icu_casemap));
61 casemap->action = old->action;
65 void icu_casemap_destroy(struct icu_casemap * casemap)
70 int icu_casemap_casemap(struct icu_casemap *casemap,
71 struct icu_buf_utf16 *dest16,
72 struct icu_buf_utf16 *src16,
79 return icu_utf16_casemap(dest16, src16, locale,
80 casemap->action, status);
84 static uint32_t icu_utf16_sub(struct icu_buf_utf16 *dest16,
85 struct icu_buf_utf16 *src16,
86 const char *locale, char action,
93 return u_strToLower(dest16->utf16, dest16->utf16_cap,
94 src16->utf16, src16->utf16_len,
98 return u_strToUpper(dest16->utf16, dest16->utf16_cap,
99 src16->utf16, src16->utf16_len,
104 return u_strToTitle(dest16->utf16, dest16->utf16_cap,
105 src16->utf16, src16->utf16_len,
110 return u_strFoldCase(dest16->utf16, dest16->utf16_cap,
111 src16->utf16, src16->utf16_len,
112 U_FOLD_CASE_DEFAULT, status);
115 *status = U_UNSUPPORTED_ERROR;
122 int icu_utf16_casemap(struct icu_buf_utf16 *dest16,
123 struct icu_buf_utf16 *src16,
124 const char *locale, char action,
127 int32_t dest16_len = 0;
129 if (!src16->utf16_len)
130 { /* guarding for empty source string */
132 dest16->utf16[0] = (UChar) 0;
133 dest16->utf16_len = 0;
137 dest16_len = icu_utf16_sub(dest16, src16, locale, action, status);
139 /* check for buffer overflow, resize and retry */
140 if (*status == U_BUFFER_OVERFLOW_ERROR
141 && dest16 != src16) /* do not resize if in-place conversion */
143 icu_buf_utf16_resize(dest16, dest16_len * 2);
144 *status = U_ZERO_ERROR;
146 icu_utf16_sub(dest16, src16, locale, action, status);
149 if (U_SUCCESS(*status) && dest16_len <= dest16->utf16_cap)
150 dest16->utf16_len = dest16_len;
154 dest16->utf16[0] = (UChar) 0;
155 dest16->utf16_len = 0;
162 #endif /* YAZ_HAVE_ICU */
167 * c-file-style: "Stroustrup"
168 * indent-tabs-mode: nil
170 * vim: shiftwidth=4 tabstop=8 expandtab