From 399eedc05492646adc53a92193652aeae44deb1d Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 2 Feb 2010 13:27:47 +0100 Subject: [PATCH] Introduce icu_iter_first --- include/yaz/icu_I18N.h | 4 ++-- src/icu_chain.c | 46 ++++++++++++++++++++++++---------------------- src/icu_transform.c | 3 +++ test/tst_icu_I18N.c | 42 +++++++++++++++++++++++++++++++++++------- 4 files changed, 64 insertions(+), 31 deletions(-) diff --git a/include/yaz/icu_I18N.h b/include/yaz/icu_I18N.h index c6b8672..932dab6 100644 --- a/include/yaz/icu_I18N.h +++ b/include/yaz/icu_I18N.h @@ -161,8 +161,8 @@ yaz_icu_chain_t icu_chain_create(const char * locale, struct icu_iter; -struct icu_iter *icu_iter_create(struct icu_chain *chain, - const char *src8cstr); +struct icu_iter *icu_iter_create(struct icu_chain *chain); +void icu_iter_first(struct icu_iter *iter, const char *src8cstr); void icu_iter_destroy(struct icu_iter *iter); int icu_iter_next(struct icu_iter *iter, struct icu_buf_utf8 *result); const char *icu_iter_get_sortkey(struct icu_iter *iter); diff --git a/src/icu_chain.c b/src/icu_chain.c index eb52978..4abbf68 100644 --- a/src/icu_chain.c +++ b/src/icu_chain.c @@ -409,28 +409,29 @@ struct icu_buf_utf16 *icu_iter_invoke(struct icu_iter *iter, } } -struct icu_iter *icu_iter_create(struct icu_chain *chain, - const char *src8cstr) +struct icu_iter *icu_iter_create(struct icu_chain *chain) { - if (!src8cstr) - return 0; - else - { - struct icu_iter *iter = xmalloc(sizeof(*iter)); - iter->chain = chain; - iter->status = U_ZERO_ERROR; - iter->display = icu_buf_utf8_create(0); - iter->sort8 = icu_buf_utf8_create(0); - iter->token_count = 0; - iter->last = 0; /* no last returned string (yet) */ - iter->steps = icu_chain_step_clone(chain->csteps); - - /* fill and assign input string.. It will be 0 after - first iteration */ - iter->input = icu_buf_utf16_create(0); - icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status); - return iter; - } + struct icu_iter *iter = xmalloc(sizeof(*iter)); + iter->chain = chain; + iter->status = U_ZERO_ERROR; + iter->display = icu_buf_utf8_create(0); + iter->sort8 = icu_buf_utf8_create(0); + iter->last = 0; /* no last returned string (yet) */ + iter->steps = icu_chain_step_clone(chain->csteps); + iter->input = 0; + + return iter; +} + +void icu_iter_first(struct icu_iter *iter, const char *src8cstr) +{ + if (iter->input) + icu_buf_utf16_destroy(iter->input); + iter->input = icu_buf_utf16_create(0); + iter->token_count = 0; + /* fill and assign input string.. It will be 0 after + first iteration */ + icu_utf16_from_utf8_cstr(iter->input, src8cstr, &iter->status); } void icu_iter_destroy(struct icu_iter *iter) @@ -491,7 +492,8 @@ int icu_chain_assign_cstr(struct icu_chain * chain, const char * src8cstr, { if (chain->iter) icu_iter_destroy(chain->iter); - chain->iter = icu_iter_create(chain, src8cstr); + chain->iter = icu_iter_create(chain); + icu_iter_first(chain->iter, src8cstr); return 1; } diff --git a/src/icu_transform.c b/src/icu_transform.c index 0d75ff7..4e3036d 100644 --- a/src/icu_transform.c +++ b/src/icu_transform.c @@ -60,7 +60,10 @@ struct icu_transform * icu_transform_create(const char *id, char action, transform->trans = 0; if (id) + { icu_utf16_from_utf8_cstr(id16, id, status); + id16->utf16[id16->utf16_len] = 0; + } if (rules) icu_utf16_from_utf8_cstr(rules16, rules, status); diff --git a/test/tst_icu_I18N.c b/test/tst_icu_I18N.c index d4dc7c1..4ffc9de 100644 --- a/test/tst_icu_I18N.c +++ b/test/tst_icu_I18N.c @@ -647,7 +647,8 @@ static void check_icu_iter1(void) xmlFreeDoc(doc); YAZ_CHECK(chain); - iter = icu_iter_create(chain, "a string with 15 tokens and 8 displays"); + iter = icu_iter_create(chain); + icu_iter_first(iter, "a string with 15 tokens and 8 displays"); YAZ_CHECK(iter); if (!iter) return; @@ -665,8 +666,9 @@ static void check_icu_iter1(void) static int test_iter(struct icu_chain *chain, const char *input, const char *expected) { - struct icu_iter *iter = icu_iter_create(chain, input); - WRBUF result; + struct icu_iter *iter = icu_iter_create(chain); + WRBUF result, second; + int success = 1; struct icu_buf_utf8 *token; if (!iter) @@ -676,26 +678,52 @@ static int test_iter(struct icu_chain *chain, const char *input, } token = icu_buf_utf8_create(0); + + if (icu_iter_next(iter, token)) + { + yaz_log(YLOG_WARN, "test_iter: expecting 0 before icu_iter_first"); + return 0; + } + result = wrbuf_alloc(); + icu_iter_first(iter, input); while (icu_iter_next(iter, token)) { wrbuf_puts(result, "["); wrbuf_write(result, (const char *) token->utf8, (int) token->utf8_len); wrbuf_puts(result, "]"); } - icu_buf_utf8_destroy(token); + + second = wrbuf_alloc(); + icu_iter_first(iter, input); + while (icu_iter_next(iter, token)) + { + wrbuf_puts(second, "["); + wrbuf_write(second, (const char *) token->utf8, (int) token->utf8_len); + wrbuf_puts(second, "]"); + } + + icu_buf_utf8_destroy(token); icu_iter_destroy(iter); if (strcmp(expected, wrbuf_cstr(result))) { yaz_log(YLOG_WARN, "test_iter: input=%s expected=%s got=%s", input, expected, wrbuf_cstr(result)); - wrbuf_destroy(result); - return 0; + success = 0; + } + + if (strcmp(expected, wrbuf_cstr(second))) + { + yaz_log(YLOG_WARN, "test_iter: input=%s expected=%s got=%s (2nd)", + input, expected, wrbuf_cstr(second)); + success = 0; } + wrbuf_destroy(result); - return 1; + wrbuf_destroy(second); + return success; } static void *iter_thread(void *p) -- 1.7.10.4