1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
8 * \brief UTF-16 string utilities for ICU
16 #include <yaz/xmalloc.h>
18 #include <yaz/icu_I18N.h>
27 #include <unicode/ustring.h> /* some more string fcns*/
28 #include <unicode/uchar.h> /* char names */
30 struct icu_buf_utf16 *icu_buf_utf16_create(size_t capacity)
32 struct icu_buf_utf16 *buf16
33 = (struct icu_buf_utf16 *) xmalloc(sizeof(struct icu_buf_utf16));
36 buf16->utf16_cap = capacity;
39 buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
40 buf16->utf16[0] = (UChar) 0;
47 struct icu_buf_utf16 *icu_buf_utf16_clear(struct icu_buf_utf16 *buf16)
51 buf16->utf16[0] = (UChar) 0;
56 struct icu_buf_utf16 *icu_buf_utf16_resize(struct icu_buf_utf16 *buf16,
62 if (0 == buf16->utf16)
63 buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
66 = (UChar *) xrealloc(buf16->utf16, sizeof(UChar) * capacity);
67 buf16->utf16_cap = capacity;
73 struct icu_buf_utf16 *icu_buf_utf16_copy(struct icu_buf_utf16 *dest16,
74 const struct icu_buf_utf16 *src16)
76 if (!dest16 || !src16 || dest16 == src16)
79 if (dest16->utf16_cap < src16->utf16_len)
80 icu_buf_utf16_resize(dest16, src16->utf16_len * 2);
82 u_strncpy(dest16->utf16, src16->utf16, src16->utf16_len);
83 dest16->utf16_len = src16->utf16_len;
89 struct icu_buf_utf16 *icu_buf_utf16_append(struct icu_buf_utf16 *dest16,
90 const struct icu_buf_utf16 *src16)
98 if (dest16->utf16_cap <= src16->utf16_len + dest16->utf16_len)
99 icu_buf_utf16_resize(dest16, dest16->utf16_len + src16->utf16_len * 2);
101 u_strncpy(dest16->utf16 + dest16->utf16_len,
102 src16->utf16, src16->utf16_len);
103 dest16->utf16_len += src16->utf16_len;
109 void icu_buf_utf16_destroy(struct icu_buf_utf16 *buf16)
116 void icu_buf_utf16_log(const char *lead, struct icu_buf_utf16 *src16)
120 struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0);
121 UErrorCode status = U_ZERO_ERROR;
122 icu_utf16_to_utf8(dst8, src16, &status);
123 yaz_log(YLOG_LOG, "%s=%s", lead, dst8->utf8);
124 icu_buf_utf8_destroy(dst8);
128 yaz_log(YLOG_LOG, "%s=NULL", lead);
132 #endif /* YAZ_HAVE_ICU */
137 * c-file-style: "Stroustrup"
138 * indent-tabs-mode: nil
140 * vim: shiftwidth=4 tabstop=8 expandtab