2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: icu_I18N.c,v 1.18 2007-11-08 17:22:49 adam Exp $
14 #include <yaz/timing.h>
17 #include <yaz/xmalloc.h>
20 #include <yaz/icu_I18N.h>
28 #include <unicode/ustring.h> /* some more string fcns*/
29 #include <unicode/uchar.h> /* char names */
32 #include <unicode/ucol.h>
35 int icu_check_status (UErrorCode status)
37 if(U_FAILURE(status)){
39 "ICU: %d %s\n", status, u_errorName(status));
48 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
50 struct icu_buf_utf16 * buf16
51 = (struct icu_buf_utf16 *) xmalloc(sizeof(struct icu_buf_utf16));
58 buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
59 buf16->utf16[0] = (UChar) 0;
60 buf16->utf16_cap = capacity;
65 struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16)
69 buf16->utf16[0] = (UChar) 0;
75 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
82 if (0 == buf16->utf16)
83 buf16->utf16 = (UChar *) xmalloc(sizeof(UChar) * capacity);
86 = (UChar *) xrealloc(buf16->utf16, sizeof(UChar) * capacity);
88 icu_buf_utf16_clear(buf16);
89 buf16->utf16_cap = capacity;
102 struct icu_buf_utf16 * icu_buf_utf16_copy(struct icu_buf_utf16 * dest16,
103 struct icu_buf_utf16 * src16)
109 if (dest16->utf16_cap < src16->utf16_len)
110 icu_buf_utf16_resize(dest16, src16->utf16_len * 2);
112 u_strncpy(dest16->utf16, src16->utf16, src16->utf16_len);
113 dest16->utf16_len = src16->utf16_len;
119 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
128 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
130 struct icu_buf_utf8 * buf8
131 = (struct icu_buf_utf8 *) xmalloc(sizeof(struct icu_buf_utf8));
138 buf8->utf8 = (uint8_t *) xmalloc(sizeof(uint8_t) * capacity);
139 buf8->utf8[0] = (uint8_t) 0;
140 buf8->utf8_cap = capacity;
146 struct icu_buf_utf8 * icu_buf_utf8_clear(struct icu_buf_utf8 * buf8)
150 buf8->utf8[0] = (uint8_t) 0;
157 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
165 buf8->utf8 = (uint8_t *) xmalloc(sizeof(uint8_t) * capacity);
168 = (uint8_t *) xrealloc(buf8->utf8, sizeof(uint8_t) * capacity);
170 icu_buf_utf8_clear(buf8);
171 buf8->utf8_cap = capacity;
184 struct icu_buf_utf8 * icu_buf_utf8_copy(struct icu_buf_utf8 * dest8,
185 struct icu_buf_utf8 * src8)
192 if (dest8->utf8_cap < src8->utf8_len)
193 icu_buf_utf8_resize(dest8, src8->utf8_len * 2);
195 strncpy((char*) dest8->utf8, (char*) src8->utf8, src8->utf8_len);
201 const char *icu_buf_utf8_to_cstr(struct icu_buf_utf8 *src8)
203 if (!src8 || src8->utf8_len == 0)
206 if (src8->utf8_len == src8->utf8_cap)
207 src8 = icu_buf_utf8_resize(src8, src8->utf8_len * 2 + 1);
209 src8->utf8[src8->utf8_len] = '\0';
211 return (const char *) src8->utf8;
215 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
224 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
225 struct icu_buf_utf8 * src8,
228 int32_t utf16_len = 0;
230 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
232 (const char *) src8->utf8, src8->utf8_len, status);
234 /* check for buffer overflow, resize and retry */
235 if (*status == U_BUFFER_OVERFLOW_ERROR)
237 icu_buf_utf16_resize(dest16, utf16_len * 2);
238 *status = U_ZERO_ERROR;
239 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
241 (const char *) src8->utf8, src8->utf8_len, status);
244 if (U_SUCCESS(*status)
245 && utf16_len <= dest16->utf16_cap)
246 dest16->utf16_len = utf16_len;
248 icu_buf_utf16_clear(dest16);
255 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
256 const char * src8cstr,
259 size_t src8cstr_len = 0;
260 int32_t utf16_len = 0;
262 *status = U_ZERO_ERROR;
263 src8cstr_len = strlen(src8cstr);
265 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
267 src8cstr, src8cstr_len, status);
269 /* check for buffer overflow, resize and retry */
270 if (*status == U_BUFFER_OVERFLOW_ERROR)
272 icu_buf_utf16_resize(dest16, utf16_len * 2);
273 *status = U_ZERO_ERROR;
274 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
276 src8cstr, src8cstr_len, status);
279 if (U_SUCCESS(*status)
280 && utf16_len <= dest16->utf16_cap)
281 dest16->utf16_len = utf16_len;
283 icu_buf_utf16_clear(dest16);
291 UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
292 struct icu_buf_utf16 * src16,
295 int32_t utf8_len = 0;
297 u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
299 src16->utf16, src16->utf16_len, status);
301 /* check for buffer overflow, resize and retry */
302 if (*status == U_BUFFER_OVERFLOW_ERROR)
304 icu_buf_utf8_resize(dest8, utf8_len * 2);
305 *status = U_ZERO_ERROR;
306 u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
308 src16->utf16, src16->utf16_len, status);
312 if (U_SUCCESS(*status)
313 && utf8_len <= dest8->utf8_cap)
314 dest8->utf8_len = utf8_len;
316 icu_buf_utf8_clear(dest8);
323 struct icu_casemap * icu_casemap_create(char action, UErrorCode *status)
325 struct icu_casemap * casemap
326 = (struct icu_casemap *) xmalloc(sizeof(struct icu_casemap));
327 casemap->action = action;
329 switch(casemap->action) {
340 icu_casemap_destroy(casemap);
347 void icu_casemap_destroy(struct icu_casemap * casemap)
353 int icu_casemap_casemap(struct icu_casemap * casemap,
354 struct icu_buf_utf16 * dest16,
355 struct icu_buf_utf16 * src16,
362 return icu_utf16_casemap(dest16, src16, locale,
363 casemap->action, status);
367 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
368 struct icu_buf_utf16 * src16,
369 const char *locale, char action,
372 int32_t dest16_len = 0;
375 if (!src16->utf16_len){ //guarding for empty source string
377 dest16->utf16[0] = (UChar) 0;
378 dest16->utf16_len = 0;
386 dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
387 src16->utf16, src16->utf16_len,
392 dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
393 src16->utf16, src16->utf16_len,
398 dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
399 src16->utf16, src16->utf16_len,
404 dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
405 src16->utf16, src16->utf16_len,
406 U_FOLD_CASE_DEFAULT, status);
410 return U_UNSUPPORTED_ERROR;
414 /* check for buffer overflow, resize and retry */
415 if (*status == U_BUFFER_OVERFLOW_ERROR
416 && dest16 != src16 /* do not resize if in-place conversion */
418 icu_buf_utf16_resize(dest16, dest16_len * 2);
419 *status = U_ZERO_ERROR;
425 dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
426 src16->utf16, src16->utf16_len,
431 dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
432 src16->utf16, src16->utf16_len,
437 dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
438 src16->utf16, src16->utf16_len,
443 dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
444 src16->utf16, src16->utf16_len,
445 U_FOLD_CASE_DEFAULT, status);
449 return U_UNSUPPORTED_ERROR;
454 if (U_SUCCESS(*status)
455 && dest16_len <= dest16->utf16_cap)
456 dest16->utf16_len = dest16_len;
459 dest16->utf16[0] = (UChar) 0;
460 dest16->utf16_len = 0;
468 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
469 struct icu_buf_utf8 * dest8,
470 struct icu_buf_utf16 * src16,
474 int32_t sortkey_len = 0;
476 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
477 dest8->utf8, dest8->utf8_cap);
479 /* check for buffer overflow, resize and retry */
480 if (sortkey_len > dest8->utf8_cap) {
481 icu_buf_utf8_resize(dest8, sortkey_len * 2);
482 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
483 dest8->utf8, dest8->utf8_cap);
486 if (U_SUCCESS(*status)
488 dest8->utf8_len = sortkey_len;
490 icu_buf_utf8_clear(dest8);
497 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
500 struct icu_tokenizer * tokenizer
501 = (struct icu_tokenizer *) xmalloc(sizeof(struct icu_tokenizer));
503 tokenizer->action = action;
505 tokenizer->buf16 = 0;
506 tokenizer->token_count = 0;
507 tokenizer->token_id = 0;
508 tokenizer->token_start = 0;
509 tokenizer->token_end = 0;
512 switch(tokenizer->action) {
515 tokenizer->bi = ubrk_open(UBRK_LINE, locale, 0, 0, status);
519 tokenizer->bi = ubrk_open(UBRK_SENTENCE, locale, 0, 0, status);
523 tokenizer->bi = ubrk_open(UBRK_WORD, locale, 0, 0, status);
527 tokenizer->bi = ubrk_open(UBRK_CHARACTER, locale, 0, 0, status);
531 tokenizer->bi = ubrk_open(UBRK_TITLE, locale, 0, 0, status);
534 *status = U_UNSUPPORTED_ERROR;
539 /* ICU error stuff is a very funny business */
540 if (U_SUCCESS(*status))
543 /* freeing if failed */
544 icu_tokenizer_destroy(tokenizer);
548 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
552 ubrk_close(tokenizer->bi);
557 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer,
558 struct icu_buf_utf16 * src16,
561 if (!tokenizer || !tokenizer->bi || !src16)
565 tokenizer->buf16 = src16;
566 tokenizer->token_count = 0;
567 tokenizer->token_id = 0;
568 tokenizer->token_start = 0;
569 tokenizer->token_end = 0;
571 ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
574 if (U_FAILURE(*status))
580 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer,
581 struct icu_buf_utf16 * tkn16,
584 int32_t tkn_start = 0;
589 if (!tokenizer || !tokenizer->bi
590 || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
594 never change tokenizer->buf16 and keep always invariant
595 0 <= tokenizer->token_start
596 <= tokenizer->token_end
597 <= tokenizer->buf16->utf16_len
598 returns length of token
601 if (0 == tokenizer->token_end) /* first call */
602 tkn_start = ubrk_first(tokenizer->bi);
603 else /* successive calls */
604 tkn_start = tokenizer->token_end;
606 /* get next position */
607 tkn_end = ubrk_next(tokenizer->bi);
609 /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
610 if (UBRK_DONE == tkn_end)
611 tkn_end = tokenizer->buf16->utf16_len;
613 /* copy out if everything is well */
614 if(U_FAILURE(*status))
617 /* everything OK, now update internal state */
618 tkn_len = tkn_end - tkn_start;
621 tokenizer->token_count++;
622 tokenizer->token_id++;
624 tokenizer->token_id = 0;
626 tokenizer->token_start = tkn_start;
627 tokenizer->token_end = tkn_end;
630 /* copying into token buffer if it exists */
632 if (tkn16->utf16_cap < tkn_len)
633 icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
635 u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start],
638 tkn16->utf16_len = tkn_len;
645 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
647 return tokenizer->token_id;
650 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer)
652 return tokenizer->token_start;
655 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer)
657 return tokenizer->token_end;
660 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer)
662 return (tokenizer->token_end - tokenizer->token_start);
665 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
667 return tokenizer->token_count;
672 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
676 struct icu_normalizer * normalizer
677 = (struct icu_normalizer *) xmalloc(sizeof(struct icu_normalizer));
679 normalizer->action = action;
680 normalizer->trans = 0;
681 normalizer->rules16 = icu_buf_utf16_create(0);
682 icu_utf16_from_utf8_cstr(normalizer->rules16, rules, status);
684 switch(normalizer->action) {
688 = utrans_openU(normalizer->rules16->utf16,
689 normalizer->rules16->utf16_len,
692 normalizer->parse_error, status);
697 = utrans_openU(normalizer->rules16->utf16,
698 normalizer->rules16->utf16_len,
701 normalizer->parse_error, status);
704 *status = U_UNSUPPORTED_ERROR;
709 if (U_SUCCESS(*status))
712 /* freeing if failed */
713 icu_normalizer_destroy(normalizer);
718 void icu_normalizer_destroy(struct icu_normalizer * normalizer){
720 if (normalizer->rules16)
721 icu_buf_utf16_destroy(normalizer->rules16);
722 if (normalizer->trans)
723 utrans_close(normalizer->trans);
730 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
731 struct icu_buf_utf16 * dest16,
732 struct icu_buf_utf16 * src16,
735 if (!normalizer || !normalizer->trans
740 if (!src16->utf16_len){ //guarding for empty source string
741 icu_buf_utf16_clear(dest16);
745 if (!icu_buf_utf16_copy(dest16, src16))
749 utrans_transUChars (normalizer->trans,
750 dest16->utf16, &(dest16->utf16_len),
752 0, &(src16->utf16_len), status);
754 if (U_FAILURE(*status))
755 icu_buf_utf16_clear(dest16);
757 return dest16->utf16_len;
763 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
764 enum icu_chain_step_type type,
765 const uint8_t * rule,
766 struct icu_buf_utf16 * buf16,
769 struct icu_chain_step * step = 0;
771 if(!chain || !type || !rule)
774 step = (struct icu_chain_step *) xmalloc(sizeof(struct icu_chain_step));
780 /* create auxilary objects */
782 case ICU_chain_step_type_display:
784 case ICU_chain_step_type_casemap:
785 step->u.casemap = icu_casemap_create(rule[0], status);
787 case ICU_chain_step_type_normalize:
788 step->u.normalizer = icu_normalizer_create((char *) rule, 'f', status);
790 case ICU_chain_step_type_tokenize:
791 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
792 (char) rule[0], status);
802 void icu_chain_step_destroy(struct icu_chain_step * step){
807 icu_chain_step_destroy(step->previous);
810 case ICU_chain_step_type_display:
812 case ICU_chain_step_type_casemap:
813 icu_casemap_destroy(step->u.casemap);
814 icu_buf_utf16_destroy(step->buf16);
816 case ICU_chain_step_type_normalize:
817 icu_normalizer_destroy(step->u.normalizer);
818 icu_buf_utf16_destroy(step->buf16);
820 case ICU_chain_step_type_tokenize:
821 icu_tokenizer_destroy(step->u.tokenizer);
822 icu_buf_utf16_destroy(step->buf16);
832 struct icu_chain * icu_chain_create(const char *locale,
837 struct icu_chain * chain
838 = (struct icu_chain *) xmalloc(sizeof(struct icu_chain));
840 *status = U_ZERO_ERROR;
842 chain->locale = xstrdup(locale);
846 chain->coll = ucol_open((const char *) chain->locale, status);
848 if (U_FAILURE(*status))
852 chain->token_count = 0;
856 chain->display8 = icu_buf_utf8_create(0);
857 chain->norm8 = icu_buf_utf8_create(0);
858 chain->sort8 = icu_buf_utf8_create(0);
860 chain->src16 = icu_buf_utf16_create(0);
868 void icu_chain_destroy(struct icu_chain * chain)
873 ucol_close(chain->coll);
875 icu_buf_utf8_destroy(chain->display8);
876 icu_buf_utf8_destroy(chain->norm8);
877 icu_buf_utf8_destroy(chain->sort8);
879 icu_buf_utf16_destroy(chain->src16);
881 icu_chain_step_destroy(chain->steps);
882 xfree(chain->locale);
889 struct icu_chain * icu_chain_xml_config(const xmlNode *xml_node,
894 struct icu_chain * chain = 0;
896 *status = U_ZERO_ERROR;
898 if (!xml_node ||xml_node->type != XML_ELEMENT_NODE)
902 xmlChar * xml_locale = xmlGetProp((xmlNode *) xml_node,
903 (xmlChar *) "locale");
907 chain = icu_chain_create((const char *) xml_locale, sort, status);
915 for (node = xml_node->children; node; node = node->next)
918 struct icu_chain_step * step = 0;
920 if (node->type != XML_ELEMENT_NODE)
923 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
925 if (!strcmp((const char *) node->name,
926 (const char *) "casemap")){
927 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
928 (const uint8_t *) xml_rule, status);
930 else if (!strcmp((const char *) node->name,
931 (const char *) "normalize")){
932 step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
933 (const uint8_t *) xml_rule, status);
935 else if (!strcmp((const char *) node->name,
936 (const char *) "tokenize")){
937 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
938 (const uint8_t *) xml_rule, status);
940 else if (!strcmp((const char *) node->name,
941 (const char *) "display")){
942 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
943 (const uint8_t *) "", status);
946 if (!step || U_FAILURE(*status)){
947 icu_chain_destroy(chain);
959 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
960 enum icu_chain_step_type type,
961 const uint8_t * rule,
964 struct icu_chain_step * step = 0;
965 struct icu_buf_utf16 * src16 = 0;
966 struct icu_buf_utf16 * buf16 = 0;
968 if (!chain || !type || !rule)
971 /* assign utf16 src buffers as needed */
972 if (chain->steps && chain->steps->buf16)
973 src16 = chain->steps->buf16;
974 else if (chain->src16)
975 src16 = chain->src16;
980 /* create utf16 destination buffers as needed, or */
982 case ICU_chain_step_type_display:
985 case ICU_chain_step_type_casemap:
986 buf16 = icu_buf_utf16_create(0);
988 case ICU_chain_step_type_normalize:
989 buf16 = icu_buf_utf16_create(0);
991 case ICU_chain_step_type_tokenize:
992 buf16 = icu_buf_utf16_create(0);
998 /* create actual chain step with this buffer */
999 step = icu_chain_step_create(chain, type, rule, buf16, status);
1001 step->previous = chain->steps;
1002 chain->steps = step;
1008 int icu_chain_step_next_token(struct icu_chain * chain,
1009 struct icu_chain_step * step,
1012 struct icu_buf_utf16 * src16 = 0;
1013 int got_new_token = 0;
1015 if (!chain || !chain->src16 || !step || !step->more_tokens)
1018 /* assign utf16 src buffers as neeed, advance in previous steps
1019 tokens until non-zero token met, and setting stop condition */
1021 if (step->previous){
1022 src16 = step->previous->buf16;
1023 /* tokens might be killed in previous steps, therefore looping */
1025 while (step->need_new_token
1026 && step->previous->more_tokens
1029 = icu_chain_step_next_token(chain, step->previous, status);
1031 else { /* first step can only work once on chain->src16 input buffer */
1032 src16 = chain->src16;
1033 step->more_tokens = 0;
1040 /* stop if nothing to process */
1041 if (step->need_new_token && !got_new_token){
1042 step->more_tokens = 0;
1046 /* either an old token not finished yet, or a new token, thus
1047 perform the work, eventually put this steps output in
1048 step->buf16 or the chains UTF8 output buffers */
1050 switch(step->type) {
1051 case ICU_chain_step_type_display:
1052 icu_utf16_to_utf8(chain->display8, src16, status);
1054 case ICU_chain_step_type_casemap:
1055 icu_casemap_casemap(step->u.casemap,
1056 step->buf16, src16, status,
1059 case ICU_chain_step_type_normalize:
1060 icu_normalizer_normalize(step->u.normalizer,
1061 step->buf16, src16, status);
1063 case ICU_chain_step_type_tokenize:
1064 /* attach to new src16 token only first time during splitting */
1065 if (step->need_new_token){
1066 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1067 step->need_new_token = 0;
1071 /* splitting one src16 token into multiple buf16 tokens */
1073 = icu_tokenizer_next_token(step->u.tokenizer,
1074 step->buf16, status);
1076 /* make sure to get new previous token if this one had been used up
1077 by recursive call to _same_ step */
1079 if (!step->more_tokens){
1080 step->more_tokens = icu_chain_step_next_token(chain, step, status);
1081 return step->more_tokens; // avoid one token count too much!
1090 if (U_FAILURE(*status))
1093 /* if token disappered into thin air, tell caller */
1094 /* if (!step->buf16->utf16_len && !step->more_tokens) */
1101 int icu_chain_assign_cstr(struct icu_chain * chain,
1102 const char * src8cstr,
1105 struct icu_chain_step * stp = 0;
1107 if (!chain || !src8cstr)
1110 chain->src8cstr = src8cstr;
1114 /* clear token count */
1115 chain->token_count = 0;
1117 /* clear all steps stop states */
1119 stp->more_tokens = 1;
1120 stp->need_new_token = 1;
1121 stp = stp->previous;
1124 /* finally convert UTF8 to UTF16 string if needed */
1125 if (chain->steps || chain->sort)
1126 icu_utf16_from_utf8_cstr(chain->src16, chain->src8cstr, status);
1128 if (U_FAILURE(*status))
1136 int icu_chain_next_token(struct icu_chain * chain,
1141 *status = U_ZERO_ERROR;
1146 /* special case with no steps - same as index type binary */
1148 if (chain->token_count)
1151 chain->token_count++;
1154 icu_sortkey8_from_utf16(chain->coll,
1155 chain->sort8, chain->steps->buf16,
1157 return chain->token_count;
1160 /* usual case, one or more icu chain steps existing */
1163 while(!got_token && chain->steps && chain->steps->more_tokens)
1164 got_token = icu_chain_step_next_token(chain, chain->steps, status);
1167 chain->token_count++;
1169 icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
1172 icu_sortkey8_from_utf16(chain->coll,
1173 chain->sort8, chain->steps->buf16,
1176 return chain->token_count;
1183 int icu_chain_token_number(struct icu_chain * chain)
1188 return chain->token_count;
1192 const char * icu_chain_token_display(struct icu_chain * chain)
1194 if (chain->display8)
1195 return icu_buf_utf8_to_cstr(chain->display8);
1200 const char * icu_chain_token_norm(struct icu_chain * chain)
1203 return chain->src8cstr;
1206 return icu_buf_utf8_to_cstr(chain->norm8);
1211 const char * icu_chain_token_sortkey(struct icu_chain * chain)
1214 return icu_buf_utf8_to_cstr(chain->sort8);
1219 const UCollator * icu_chain_get_coll(struct icu_chain * chain)
1225 #endif /* YAZ_HAVE_ICU */
1233 * indent-tabs-mode: nil
1235 * vim: shiftwidth=4 tabstop=8 expandtab