2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: icu_I18N.c,v 1.3 2007-10-24 07:41:48 marc Exp $
14 #include <yaz/timing.h>
19 #include <yaz/icu_I18N.h>
27 #include <unicode/ustring.h> /* some more string fcns*/
28 #include <unicode/uchar.h> /* char names */
31 #include <unicode/ucol.h>
34 int icu_check_status (UErrorCode status)
36 if(U_FAILURE(status)){
38 "ICU: %d %s\n", status, u_errorName(status));
47 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
49 struct icu_buf_utf16 * buf16
50 = (struct icu_buf_utf16 *) malloc(sizeof(struct icu_buf_utf16));
57 buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
58 buf16->utf16[0] = (UChar) 0;
59 buf16->utf16_cap = capacity;
64 struct icu_buf_utf16 * icu_buf_utf16_clear(struct icu_buf_utf16 * buf16)
68 buf16->utf16[0] = (UChar) 0;
74 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
81 if (0 == buf16->utf16)
82 buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
85 = (UChar *) realloc(buf16->utf16, sizeof(UChar) * capacity);
87 icu_buf_utf16_clear(buf16);
88 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)
133 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
135 struct icu_buf_utf8 * buf8
136 = (struct icu_buf_utf8 *) malloc(sizeof(struct icu_buf_utf8));
143 buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
144 buf8->utf8[0] = (uint8_t) 0;
145 buf8->utf8_cap = capacity;
151 struct icu_buf_utf8 * icu_buf_utf8_clear(struct icu_buf_utf8 * buf8)
155 buf8->utf8[0] = (uint8_t) 0;
162 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
170 buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
173 = (uint8_t *) realloc(buf8->utf8, sizeof(uint8_t) * capacity);
175 icu_buf_utf8_clear(buf8);
176 buf8->utf8_cap = capacity;
190 struct icu_buf_utf8 * icu_buf_utf8_copy(struct icu_buf_utf8 * dest8,
191 struct icu_buf_utf8 * src8)
198 if (dest8->utf8_cap < src8->utf8_len)
199 icu_buf_utf8_resize(dest8, src8->utf8_len * 2);
201 strncpy((char*) dest8->utf8, (char*) src8->utf8, src8->utf8_len);
207 const char *icu_buf_utf8_to_cstr(struct icu_buf_utf8 *src8)
209 if (!src8 || src8->utf8_len == 0)
212 if (src8->utf8_len == src8->utf8_cap)
213 src8 = icu_buf_utf8_resize(src8, src8->utf8_len * 2 + 1);
215 src8->utf8[src8->utf8_len] = '\0';
217 return (const char *) src8->utf8;
221 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
232 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
233 struct icu_buf_utf8 * src8,
236 int32_t utf16_len = 0;
238 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
240 (const char *) src8->utf8, src8->utf8_len, status);
242 /* check for buffer overflow, resize and retry */
243 if (*status == U_BUFFER_OVERFLOW_ERROR)
245 icu_buf_utf16_resize(dest16, utf16_len * 2);
246 *status = U_ZERO_ERROR;
247 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
249 (const char *) src8->utf8, src8->utf8_len, status);
252 if (U_SUCCESS(*status)
253 && utf16_len <= dest16->utf16_cap)
254 dest16->utf16_len = utf16_len;
256 icu_buf_utf16_clear(dest16);
263 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
264 const char * src8cstr,
267 size_t src8cstr_len = 0;
268 int32_t utf16_len = 0;
270 src8cstr_len = strlen(src8cstr);
272 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
274 src8cstr, src8cstr_len, status);
276 /* check for buffer overflow, resize and retry */
277 if (*status == U_BUFFER_OVERFLOW_ERROR)
279 icu_buf_utf16_resize(dest16, utf16_len * 2);
280 *status = U_ZERO_ERROR;
281 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
283 src8cstr, src8cstr_len, status);
286 if (U_SUCCESS(*status)
287 && utf16_len <= dest16->utf16_cap)
288 dest16->utf16_len = utf16_len;
290 icu_buf_utf16_clear(dest16);
298 UErrorCode icu_utf16_to_utf8(struct icu_buf_utf8 * dest8,
299 struct icu_buf_utf16 * src16,
302 int32_t utf8_len = 0;
304 u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
306 src16->utf16, src16->utf16_len, status);
308 /* check for buffer overflow, resize and retry */
309 if (*status == U_BUFFER_OVERFLOW_ERROR)
311 icu_buf_utf8_resize(dest8, utf8_len * 2);
312 *status = U_ZERO_ERROR;
313 u_strToUTF8((char *) dest8->utf8, dest8->utf8_cap,
315 src16->utf16, src16->utf16_len, status);
319 if (U_SUCCESS(*status)
320 && utf8_len <= dest8->utf8_cap)
321 dest8->utf8_len = utf8_len;
323 icu_buf_utf8_clear(dest8);
330 struct icu_casemap * icu_casemap_create(const char *locale, char action,
333 struct icu_casemap * casemap
334 = (struct icu_casemap *) malloc(sizeof(struct icu_casemap));
335 strcpy(casemap->locale, locale);
336 casemap->action = action;
338 switch(casemap->action) {
348 icu_casemap_destroy(casemap);
355 void icu_casemap_destroy(struct icu_casemap * casemap)
362 int icu_casemap_casemap(struct icu_casemap * casemap,
363 struct icu_buf_utf16 * dest16,
364 struct icu_buf_utf16 * src16,
370 return icu_utf16_casemap(dest16, src16,
371 casemap->locale, casemap->action, status);
375 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
376 struct icu_buf_utf16 * src16,
377 const char *locale, char action,
380 int32_t dest16_len = 0;
383 if (!src16->utf16_len){ //guarding for empty source string
385 dest16->utf16[0] = (UChar) 0;
386 dest16->utf16_len = 0;
394 dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
395 src16->utf16, src16->utf16_len,
400 dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
401 src16->utf16, src16->utf16_len,
406 dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
407 src16->utf16, src16->utf16_len,
412 dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
413 src16->utf16, src16->utf16_len,
414 U_FOLD_CASE_DEFAULT, status);
418 return U_UNSUPPORTED_ERROR;
422 /* check for buffer overflow, resize and retry */
423 if (*status == U_BUFFER_OVERFLOW_ERROR
424 && dest16 != src16 /* do not resize if in-place conversion */
426 icu_buf_utf16_resize(dest16, dest16_len * 2);
427 *status = U_ZERO_ERROR;
433 dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
434 src16->utf16, src16->utf16_len,
439 dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
440 src16->utf16, src16->utf16_len,
445 dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
446 src16->utf16, src16->utf16_len,
451 dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
452 src16->utf16, src16->utf16_len,
453 U_FOLD_CASE_DEFAULT, status);
457 return U_UNSUPPORTED_ERROR;
462 if (U_SUCCESS(*status)
463 && dest16_len <= dest16->utf16_cap)
464 dest16->utf16_len = dest16_len;
467 dest16->utf16[0] = (UChar) 0;
468 dest16->utf16_len = 0;
476 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
477 struct icu_buf_utf8 * dest8,
478 struct icu_buf_utf16 * src16,
482 int32_t sortkey_len = 0;
484 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
485 dest8->utf8, dest8->utf8_cap);
487 /* check for buffer overflow, resize and retry */
488 if (sortkey_len > dest8->utf8_cap) {
489 icu_buf_utf8_resize(dest8, sortkey_len * 2);
490 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
491 dest8->utf8, dest8->utf8_cap);
494 if (U_SUCCESS(*status)
496 dest8->utf8_len = sortkey_len;
498 icu_buf_utf8_clear(dest8);
505 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
508 struct icu_tokenizer * tokenizer
509 = (struct icu_tokenizer *) malloc(sizeof(struct icu_tokenizer));
511 strcpy(tokenizer->locale, locale);
512 tokenizer->action = action;
514 tokenizer->buf16 = 0;
515 tokenizer->token_count = 0;
516 tokenizer->token_id = 0;
517 tokenizer->token_start = 0;
518 tokenizer->token_end = 0;
521 switch(tokenizer->action) {
525 = ubrk_open(UBRK_LINE, tokenizer->locale,
531 = ubrk_open(UBRK_SENTENCE, tokenizer->locale,
537 = ubrk_open(UBRK_WORD, tokenizer->locale,
543 = ubrk_open(UBRK_CHARACTER, tokenizer->locale,
549 = ubrk_open(UBRK_TITLE, tokenizer->locale,
553 *status = U_UNSUPPORTED_ERROR;
558 /* ICU error stuff is a very funny business */
559 if (U_SUCCESS(*status))
562 /* freeing if failed */
563 icu_tokenizer_destroy(tokenizer);
567 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
571 ubrk_close(tokenizer->bi);
576 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer,
577 struct icu_buf_utf16 * src16,
580 if (!tokenizer || !tokenizer->bi || !src16)
584 tokenizer->buf16 = src16;
585 tokenizer->token_count = 0;
586 tokenizer->token_id = 0;
587 tokenizer->token_start = 0;
588 tokenizer->token_end = 0;
590 ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
593 if (U_FAILURE(*status))
599 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer,
600 struct icu_buf_utf16 * tkn16,
603 int32_t tkn_start = 0;
608 if (!tokenizer || !tokenizer->bi
609 || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
613 never change tokenizer->buf16 and keep always invariant
614 0 <= tokenizer->token_start
615 <= tokenizer->token_end
616 <= tokenizer->buf16->utf16_len
617 returns length of token
620 if (0 == tokenizer->token_end) /* first call */
621 tkn_start = ubrk_first(tokenizer->bi);
622 else /* successive calls */
623 tkn_start = tokenizer->token_end;
625 /* get next position */
626 tkn_end = ubrk_next(tokenizer->bi);
628 /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
629 if (UBRK_DONE == tkn_end)
630 tkn_end = tokenizer->buf16->utf16_len;
632 /* copy out if everything is well */
633 if(U_FAILURE(*status))
636 /* everything OK, now update internal state */
637 tkn_len = tkn_end - tkn_start;
640 tokenizer->token_count++;
641 tokenizer->token_id++;
643 tokenizer->token_id = 0;
645 tokenizer->token_start = tkn_start;
646 tokenizer->token_end = tkn_end;
649 /* copying into token buffer if it exists */
651 if (tkn16->utf16_cap < tkn_len)
652 icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
654 u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start],
657 tkn16->utf16_len = tkn_len;
664 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
666 return tokenizer->token_id;
669 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer)
671 return tokenizer->token_start;
674 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer)
676 return tokenizer->token_end;
679 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer)
681 return (tokenizer->token_end - tokenizer->token_start);
684 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
686 return tokenizer->token_count;
691 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
695 struct icu_normalizer * normalizer
696 = (struct icu_normalizer *) malloc(sizeof(struct icu_normalizer));
698 normalizer->action = action;
699 normalizer->trans = 0;
700 normalizer->rules16 = icu_buf_utf16_create(0);
701 icu_utf16_from_utf8_cstr(normalizer->rules16, rules, status);
703 switch(normalizer->action) {
707 = utrans_openU(normalizer->rules16->utf16,
708 normalizer->rules16->utf16_len,
711 normalizer->parse_error, status);
716 = utrans_openU(normalizer->rules16->utf16,
717 normalizer->rules16->utf16_len,
720 normalizer->parse_error, status);
723 *status = U_UNSUPPORTED_ERROR;
728 if (U_SUCCESS(*status))
731 /* freeing if failed */
732 icu_normalizer_destroy(normalizer);
737 void icu_normalizer_destroy(struct icu_normalizer * normalizer){
739 if (normalizer->rules16)
740 icu_buf_utf16_destroy(normalizer->rules16);
741 if (normalizer->trans)
742 utrans_close(normalizer->trans);
749 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
750 struct icu_buf_utf16 * dest16,
751 struct icu_buf_utf16 * src16,
754 if (!normalizer || !normalizer->trans
759 if (!src16->utf16_len){ //guarding for empty source string
760 icu_buf_utf16_clear(dest16);
764 if (!icu_buf_utf16_copy(dest16, src16))
768 utrans_transUChars (normalizer->trans,
769 dest16->utf16, &(dest16->utf16_len),
771 0, &(src16->utf16_len), status);
773 if (U_FAILURE(*status))
774 icu_buf_utf16_clear(dest16);
776 return dest16->utf16_len;
782 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
783 enum icu_chain_step_type type,
784 const uint8_t * rule,
785 struct icu_buf_utf16 * buf16,
788 struct icu_chain_step * step = 0;
790 if(!chain || !type || !rule)
793 step = (struct icu_chain_step *) malloc(sizeof(struct icu_chain_step));
799 /* create auxilary objects */
801 case ICU_chain_step_type_display:
803 case ICU_chain_step_type_index:
805 case ICU_chain_step_type_sortkey:
807 case ICU_chain_step_type_casemap:
808 step->u.casemap = icu_casemap_create((char *) chain->locale,
809 (char) rule[0], status);
811 case ICU_chain_step_type_normalize:
812 step->u.normalizer = icu_normalizer_create((char *) rule, 'f', status);
814 case ICU_chain_step_type_tokenize:
815 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
816 (char) rule[0], status);
826 void icu_chain_step_destroy(struct icu_chain_step * step){
831 icu_chain_step_destroy(step->previous);
834 case ICU_chain_step_type_display:
836 case ICU_chain_step_type_index:
838 case ICU_chain_step_type_sortkey:
840 case ICU_chain_step_type_casemap:
841 icu_casemap_destroy(step->u.casemap);
842 icu_buf_utf16_destroy(step->buf16);
844 case ICU_chain_step_type_normalize:
845 icu_normalizer_destroy(step->u.normalizer);
846 icu_buf_utf16_destroy(step->buf16);
848 case ICU_chain_step_type_tokenize:
849 icu_tokenizer_destroy(step->u.tokenizer);
850 icu_buf_utf16_destroy(step->buf16);
860 struct icu_chain * icu_chain_create(const uint8_t * identifier,
861 const uint8_t * locale)
864 struct icu_chain * chain
865 = (struct icu_chain *) malloc(sizeof(struct icu_chain));
867 strncpy((char *) chain->identifier, (const char *) identifier, 128);
868 chain->identifier[128 - 1] = '\0';
869 strncpy((char *) chain->locale, (const char *) locale, 16);
870 chain->locale[16 - 1] = '\0';
872 chain->token_count = 0;
874 chain->display8 = icu_buf_utf8_create(0);
875 chain->norm8 = icu_buf_utf8_create(0);
876 chain->sort8 = icu_buf_utf8_create(0);
878 chain->src16 = icu_buf_utf16_create(0);
886 void icu_chain_destroy(struct icu_chain * chain)
889 icu_buf_utf8_destroy(chain->display8);
890 icu_buf_utf8_destroy(chain->norm8);
891 icu_buf_utf8_destroy(chain->sort8);
893 icu_buf_utf16_destroy(chain->src16);
895 icu_chain_step_destroy(chain->steps);
902 struct icu_chain * icu_chain_xml_config(xmlNode *xml_node,
903 UErrorCode * status){
906 struct icu_chain * chain = 0;
909 ||xml_node->type != XML_ELEMENT_NODE
910 || strcmp((const char *) xml_node->name, "icu_chain"))
915 xmlChar *xml_id = xmlGetProp(xml_node, (xmlChar *) "id");
916 xmlChar *xml_locale = xmlGetProp(xml_node, (xmlChar *) "locale");
918 if (!xml_id || !strlen((const char *) xml_id)
919 || !xml_locale || !strlen((const char *) xml_locale))
922 chain = icu_chain_create((const uint8_t *) xml_id,
923 (const uint8_t *) xml_locale);
931 for (node = xml_node->children; node; node = node->next)
934 struct icu_chain_step * step = 0;
936 if (node->type != XML_ELEMENT_NODE)
939 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
941 if (!strcmp((const char *) node->name,
942 (const char *) "casemap")){
943 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
944 (const uint8_t *) xml_rule, status);
946 else if (!strcmp((const char *) node->name,
947 (const char *) "normalize")){
948 step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
949 (const uint8_t *) xml_rule, status);
951 else if (!strcmp((const char *) node->name,
952 (const char *) "tokenize")){
953 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
954 (const uint8_t *) xml_rule, status);
956 else if (!strcmp((const char *) node->name,
957 (const char *) "display")){
958 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
959 (const uint8_t *) "", status);
961 else if (!strcmp((const char *) node->name,
962 (const char *) "index")){
963 step = icu_chain_insert_step(chain, ICU_chain_step_type_index,
964 (const uint8_t *) "", status);
966 else if (!strcmp((const char *) node->name,
967 (const char *) "sortkey")){
968 step = icu_chain_insert_step(chain, ICU_chain_step_type_sortkey,
969 (const uint8_t *) "", status);
973 if (!step || U_FAILURE(*status)){
974 icu_chain_destroy(chain);
986 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
987 enum icu_chain_step_type type,
988 const uint8_t * rule,
991 struct icu_chain_step * step = 0;
992 struct icu_buf_utf16 * src16 = 0;
993 struct icu_buf_utf16 * buf16 = 0;
995 if (!chain || !type || !rule)
998 /* assign utf16 src buffers as needed */
999 if (chain->steps && chain->steps->buf16)
1000 src16 = chain->steps->buf16;
1001 else if (chain->src16)
1002 src16 = chain->src16;
1007 /* create utf16 destination buffers as needed, or */
1009 case ICU_chain_step_type_display:
1012 case ICU_chain_step_type_index:
1015 case ICU_chain_step_type_sortkey:
1018 case ICU_chain_step_type_casemap:
1019 buf16 = icu_buf_utf16_create(0);
1021 case ICU_chain_step_type_normalize:
1022 buf16 = icu_buf_utf16_create(0);
1024 case ICU_chain_step_type_tokenize:
1025 buf16 = icu_buf_utf16_create(0);
1031 /* create actual chain step with this buffer */
1032 step = icu_chain_step_create(chain, type, rule, buf16, status);
1034 step->previous = chain->steps;
1035 chain->steps = step;
1041 int icu_chain_step_next_token(struct icu_chain * chain,
1042 struct icu_chain_step * step,
1045 struct icu_buf_utf16 * src16 = 0;
1047 if (!chain || !chain->src16 || !step || !step->more_tokens)
1050 /* assign utf16 src buffers as neeed, advance in previous steps
1051 tokens until non-zero token met, and setting stop condition
1053 if (step->previous){
1054 src16 = step->previous->buf16;
1055 if (step->need_new_token)
1057 = icu_chain_step_next_token(chain, step->previous, status);
1059 else { /* first step can only work once on chain->src16 input buffer */
1060 src16 = chain->src16;
1061 step->more_tokens = 1;
1064 /* stop if nothing to process
1065 i.e new token source was not properly assigned
1066 or I did not get any tokens from previous step
1068 if (!step->more_tokens || !src16)
1071 /* perform the work, eventually put this steps output in
1072 step->buf16 or the chains UTF8 output buffers */
1073 switch(step->type) {
1074 case ICU_chain_step_type_display:
1075 icu_utf16_to_utf8(chain->display8, src16, status);
1077 case ICU_chain_step_type_index:
1078 icu_utf16_to_utf8(chain->norm8, src16, status);
1080 case ICU_chain_step_type_sortkey:
1081 icu_utf16_to_utf8(chain->sort8, src16, status);
1083 case ICU_chain_step_type_casemap:
1084 icu_casemap_casemap(step->u.casemap,
1085 step->buf16, src16, status);
1087 case ICU_chain_step_type_normalize:
1088 icu_normalizer_normalize(step->u.normalizer,
1089 step->buf16, src16, status);
1091 case ICU_chain_step_type_tokenize:
1092 /* attach to new src16 token only first time during splitting */
1093 if (step->need_new_token){
1094 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1095 step->need_new_token = 0;
1097 /* splitting one src16 token into multiple buf16 tokens */
1099 = icu_tokenizer_next_token(step->u.tokenizer,
1100 step->buf16, status);
1101 /* make sure to get new previous token if this one had been used up */
1102 if (step->previous && !step->more_tokens){
1103 if (icu_chain_step_next_token(chain, step->previous, status)){
1104 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1105 step->need_new_token = 0;
1107 = icu_tokenizer_next_token(step->u.tokenizer,
1108 step->buf16, status);
1111 if (0 == step->more_tokens)
1119 /* stop further token processing if last step and
1120 new tokens can ot be obtained from last non-existing step
1122 if (!step->previous)
1123 step->more_tokens = 0;
1125 if (U_FAILURE(*status))
1128 // result buf empty, got no token TODO: make while loop around all token
1129 // fetching operations
1130 //if (!step->buf16->utf16_len)
1138 int icu_chain_assign_cstr(struct icu_chain * chain,
1139 const char * src8cstr,
1142 struct icu_chain_step * stp = 0;
1144 if (!chain || !src8cstr)
1149 /* clear token count */
1150 chain->token_count = 0;
1152 /* clear all steps stop states */
1154 stp->more_tokens = 1;
1155 stp->need_new_token = 1;
1156 stp = stp->previous;
1159 /* finally convert UTF8 to UTF16 string */
1160 icu_utf16_from_utf8_cstr(chain->src16, src8cstr, status);
1162 if (U_FAILURE(*status))
1170 int icu_chain_next_token(struct icu_chain * chain,
1175 if (!chain || !chain->steps)
1178 got_token = icu_chain_step_next_token(chain, chain->steps, status);
1181 chain->token_count++;
1182 return chain->token_count;
1188 int icu_chain_get_token_count(struct icu_chain * chain)
1193 return chain->token_count;
1198 const char * icu_chain_get_display(struct icu_chain * chain)
1200 if (chain->display8)
1201 return icu_buf_utf8_to_cstr(chain->display8);
1206 const char * icu_chain_get_norm(struct icu_chain * chain)
1209 return icu_buf_utf8_to_cstr(chain->norm8);
1214 const char * icu_chain_get_sort(struct icu_chain * chain)
1217 return icu_buf_utf8_to_cstr(chain->sort8);
1223 #endif /* HAVE_ICU */
1231 * indent-tabs-mode: nil
1233 * vim: shiftwidth=4 tabstop=8 expandtab