X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Ficu_utf16.c;h=962e90c7dd4856324fb34becd3a0391b93e50ca2;hp=a5bfcb68ca33ed0ad10a2cbe8afd93fb2340f41d;hb=cf91f233d0e16ed6920d84ce2d871eed90d3491b;hpb=689388c889a644a40ca1f447cc862da009049836 diff --git a/src/icu_utf16.c b/src/icu_utf16.c index a5bfcb6..962e90c 100644 --- a/src/icu_utf16.c +++ b/src/icu_utf16.c @@ -1,5 +1,5 @@ /* This file is part of the YAZ toolkit. - * Copyright (C) 1995-2011 Index Data + * Copyright (C) Index Data * See the file LICENSE for details. */ @@ -22,69 +22,56 @@ #include #include #include +#include #include /* some more string fcns*/ #include /* char names */ -struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity) +struct icu_buf_utf16 *icu_buf_utf16_create(size_t capacity) { - struct icu_buf_utf16 * buf16 + struct icu_buf_utf16 *buf16 = (struct icu_buf_utf16 *) xmalloc(sizeof(struct icu_buf_utf16)); - buf16->utf16 = 0; buf16->utf16_len = 0; - buf16->utf16_cap = 0; - + buf16->utf16_cap = capacity; if (capacity > 0) { buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity); buf16->utf16[0] = (UChar) 0; - buf16->utf16_cap = capacity; } + else + buf16->utf16 = 0; return buf16; } -struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16) +struct icu_buf_utf16 *icu_buf_utf16_clear(struct icu_buf_utf16 *buf16) { - if (buf16) - { - if (buf16->utf16) - buf16->utf16[0] = (UChar) 0; - buf16->utf16_len = 0; - } + assert(buf16); + if (buf16->utf16) + buf16->utf16[0] = (UChar) 0; + buf16->utf16_len = 0; return buf16; } -struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16, - size_t capacity) +struct icu_buf_utf16 *icu_buf_utf16_resize(struct icu_buf_utf16 *buf16, + size_t capacity) { - if (!buf16) - return 0; - + assert(buf16); if (capacity > 0) { if (0 == buf16->utf16) buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity); else - buf16->utf16 + buf16->utf16 = (UChar *) xrealloc(buf16->utf16, sizeof(UChar) * capacity); - - icu_buf_utf16_clear(buf16); buf16->utf16_cap = capacity; - } - else - { - xfree(buf16->utf16); - buf16->utf16 = 0; - buf16->utf16_len = 0; - buf16->utf16_cap = 0; } return buf16; } -struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16, - const struct icu_buf_utf16 * src16) +struct icu_buf_utf16 *icu_buf_utf16_copy(struct icu_buf_utf16 *dest16, + const struct icu_buf_utf16 *src16) { if (!dest16 || !src16 || dest16 == src16) return 0; @@ -98,13 +85,49 @@ struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16, return dest16; } -void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16) + +struct icu_buf_utf16 *icu_buf_utf16_append(struct icu_buf_utf16 *dest16, + const struct icu_buf_utf16 *src16) +{ + assert(dest16); + if (!src16) + return dest16; + if (dest16 == src16) + return 0; + + if (dest16->utf16_cap <= src16->utf16_len + dest16->utf16_len) + icu_buf_utf16_resize(dest16, dest16->utf16_len + src16->utf16_len * 2); + + u_strncpy(dest16->utf16 + dest16->utf16_len, + src16->utf16, src16->utf16_len); + dest16->utf16_len += src16->utf16_len; + + return dest16; +} + + +void icu_buf_utf16_destroy(struct icu_buf_utf16 *buf16) { if (buf16) xfree(buf16->utf16); xfree(buf16); } +void icu_buf_utf16_log(const char *lead, struct icu_buf_utf16 *src16) +{ + if (src16) + { + struct icu_buf_utf8 *dst8 = icu_buf_utf8_create(0); + UErrorCode status = U_ZERO_ERROR; + icu_utf16_to_utf8(dst8, src16, &status); + yaz_log(YLOG_LOG, "%s=%s", lead, dst8->utf8); + icu_buf_utf8_destroy(dst8); + } + else + { + yaz_log(YLOG_LOG, "%s=NULL", lead); + } +} #endif /* YAZ_HAVE_ICU */