2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: icu_I18N.c,v 1.6 2007-10-25 08:32:50 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) {
349 icu_casemap_destroy(casemap);
356 void icu_casemap_destroy(struct icu_casemap * casemap)
363 int icu_casemap_casemap(struct icu_casemap * casemap,
364 struct icu_buf_utf16 * dest16,
365 struct icu_buf_utf16 * src16,
371 return icu_utf16_casemap(dest16, src16,
372 casemap->locale, casemap->action, status);
376 int icu_utf16_casemap(struct icu_buf_utf16 * dest16,
377 struct icu_buf_utf16 * src16,
378 const char *locale, char action,
381 int32_t dest16_len = 0;
384 if (!src16->utf16_len){ //guarding for empty source string
386 dest16->utf16[0] = (UChar) 0;
387 dest16->utf16_len = 0;
395 dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
396 src16->utf16, src16->utf16_len,
401 dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
402 src16->utf16, src16->utf16_len,
407 dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
408 src16->utf16, src16->utf16_len,
413 dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
414 src16->utf16, src16->utf16_len,
415 U_FOLD_CASE_DEFAULT, status);
419 return U_UNSUPPORTED_ERROR;
423 /* check for buffer overflow, resize and retry */
424 if (*status == U_BUFFER_OVERFLOW_ERROR
425 && dest16 != src16 /* do not resize if in-place conversion */
427 icu_buf_utf16_resize(dest16, dest16_len * 2);
428 *status = U_ZERO_ERROR;
434 dest16_len = u_strToLower(dest16->utf16, dest16->utf16_cap,
435 src16->utf16, src16->utf16_len,
440 dest16_len = u_strToUpper(dest16->utf16, dest16->utf16_cap,
441 src16->utf16, src16->utf16_len,
446 dest16_len = u_strToTitle(dest16->utf16, dest16->utf16_cap,
447 src16->utf16, src16->utf16_len,
452 dest16_len = u_strFoldCase(dest16->utf16, dest16->utf16_cap,
453 src16->utf16, src16->utf16_len,
454 U_FOLD_CASE_DEFAULT, status);
458 return U_UNSUPPORTED_ERROR;
463 if (U_SUCCESS(*status)
464 && dest16_len <= dest16->utf16_cap)
465 dest16->utf16_len = dest16_len;
468 dest16->utf16[0] = (UChar) 0;
469 dest16->utf16_len = 0;
477 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
478 struct icu_buf_utf8 * dest8,
479 struct icu_buf_utf16 * src16,
483 int32_t sortkey_len = 0;
485 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
486 dest8->utf8, dest8->utf8_cap);
488 /* check for buffer overflow, resize and retry */
489 if (sortkey_len > dest8->utf8_cap) {
490 icu_buf_utf8_resize(dest8, sortkey_len * 2);
491 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
492 dest8->utf8, dest8->utf8_cap);
495 if (U_SUCCESS(*status)
497 dest8->utf8_len = sortkey_len;
499 icu_buf_utf8_clear(dest8);
506 struct icu_tokenizer * icu_tokenizer_create(const char *locale, char action,
509 struct icu_tokenizer * tokenizer
510 = (struct icu_tokenizer *) malloc(sizeof(struct icu_tokenizer));
512 strcpy(tokenizer->locale, locale);
513 tokenizer->action = action;
515 tokenizer->buf16 = 0;
516 tokenizer->token_count = 0;
517 tokenizer->token_id = 0;
518 tokenizer->token_start = 0;
519 tokenizer->token_end = 0;
522 switch(tokenizer->action) {
526 = ubrk_open(UBRK_LINE, tokenizer->locale,
532 = ubrk_open(UBRK_SENTENCE, tokenizer->locale,
538 = ubrk_open(UBRK_WORD, tokenizer->locale,
544 = ubrk_open(UBRK_CHARACTER, tokenizer->locale,
550 = ubrk_open(UBRK_TITLE, tokenizer->locale,
554 *status = U_UNSUPPORTED_ERROR;
559 /* ICU error stuff is a very funny business */
560 if (U_SUCCESS(*status))
563 /* freeing if failed */
564 icu_tokenizer_destroy(tokenizer);
568 void icu_tokenizer_destroy(struct icu_tokenizer * tokenizer)
572 ubrk_close(tokenizer->bi);
577 int icu_tokenizer_attach(struct icu_tokenizer * tokenizer,
578 struct icu_buf_utf16 * src16,
581 if (!tokenizer || !tokenizer->bi || !src16)
585 tokenizer->buf16 = src16;
586 tokenizer->token_count = 0;
587 tokenizer->token_id = 0;
588 tokenizer->token_start = 0;
589 tokenizer->token_end = 0;
591 ubrk_setText(tokenizer->bi, src16->utf16, src16->utf16_len, status);
594 if (U_FAILURE(*status))
600 int32_t icu_tokenizer_next_token(struct icu_tokenizer * tokenizer,
601 struct icu_buf_utf16 * tkn16,
604 int32_t tkn_start = 0;
609 if (!tokenizer || !tokenizer->bi
610 || !tokenizer->buf16 || !tokenizer->buf16->utf16_len)
614 never change tokenizer->buf16 and keep always invariant
615 0 <= tokenizer->token_start
616 <= tokenizer->token_end
617 <= tokenizer->buf16->utf16_len
618 returns length of token
621 if (0 == tokenizer->token_end) /* first call */
622 tkn_start = ubrk_first(tokenizer->bi);
623 else /* successive calls */
624 tkn_start = tokenizer->token_end;
626 /* get next position */
627 tkn_end = ubrk_next(tokenizer->bi);
629 /* repairing invariant at end of ubrk, which is UBRK_DONE = -1 */
630 if (UBRK_DONE == tkn_end)
631 tkn_end = tokenizer->buf16->utf16_len;
633 /* copy out if everything is well */
634 if(U_FAILURE(*status))
637 /* everything OK, now update internal state */
638 tkn_len = tkn_end - tkn_start;
641 tokenizer->token_count++;
642 tokenizer->token_id++;
644 tokenizer->token_id = 0;
646 tokenizer->token_start = tkn_start;
647 tokenizer->token_end = tkn_end;
650 /* copying into token buffer if it exists */
652 if (tkn16->utf16_cap < tkn_len)
653 icu_buf_utf16_resize(tkn16, (size_t) tkn_len * 2);
655 u_strncpy(tkn16->utf16, &(tokenizer->buf16->utf16)[tkn_start],
658 tkn16->utf16_len = tkn_len;
665 int32_t icu_tokenizer_token_id(struct icu_tokenizer * tokenizer)
667 return tokenizer->token_id;
670 int32_t icu_tokenizer_token_start(struct icu_tokenizer * tokenizer)
672 return tokenizer->token_start;
675 int32_t icu_tokenizer_token_end(struct icu_tokenizer * tokenizer)
677 return tokenizer->token_end;
680 int32_t icu_tokenizer_token_length(struct icu_tokenizer * tokenizer)
682 return (tokenizer->token_end - tokenizer->token_start);
685 int32_t icu_tokenizer_token_count(struct icu_tokenizer * tokenizer)
687 return tokenizer->token_count;
692 struct icu_normalizer * icu_normalizer_create(const char *rules, char action,
696 struct icu_normalizer * normalizer
697 = (struct icu_normalizer *) malloc(sizeof(struct icu_normalizer));
699 normalizer->action = action;
700 normalizer->trans = 0;
701 normalizer->rules16 = icu_buf_utf16_create(0);
702 icu_utf16_from_utf8_cstr(normalizer->rules16, rules, status);
704 switch(normalizer->action) {
708 = utrans_openU(normalizer->rules16->utf16,
709 normalizer->rules16->utf16_len,
712 normalizer->parse_error, status);
717 = utrans_openU(normalizer->rules16->utf16,
718 normalizer->rules16->utf16_len,
721 normalizer->parse_error, status);
724 *status = U_UNSUPPORTED_ERROR;
729 if (U_SUCCESS(*status))
732 /* freeing if failed */
733 icu_normalizer_destroy(normalizer);
738 void icu_normalizer_destroy(struct icu_normalizer * normalizer){
740 if (normalizer->rules16)
741 icu_buf_utf16_destroy(normalizer->rules16);
742 if (normalizer->trans)
743 utrans_close(normalizer->trans);
750 int icu_normalizer_normalize(struct icu_normalizer * normalizer,
751 struct icu_buf_utf16 * dest16,
752 struct icu_buf_utf16 * src16,
755 if (!normalizer || !normalizer->trans
760 if (!src16->utf16_len){ //guarding for empty source string
761 icu_buf_utf16_clear(dest16);
765 if (!icu_buf_utf16_copy(dest16, src16))
769 utrans_transUChars (normalizer->trans,
770 dest16->utf16, &(dest16->utf16_len),
772 0, &(src16->utf16_len), status);
774 if (U_FAILURE(*status))
775 icu_buf_utf16_clear(dest16);
777 return dest16->utf16_len;
783 struct icu_chain_step * icu_chain_step_create(struct icu_chain * chain,
784 enum icu_chain_step_type type,
785 const uint8_t * rule,
786 struct icu_buf_utf16 * buf16,
789 struct icu_chain_step * step = 0;
791 if(!chain || !type || !rule)
794 step = (struct icu_chain_step *) malloc(sizeof(struct icu_chain_step));
800 /* create auxilary objects */
802 case ICU_chain_step_type_display:
804 #ifdef ICU_CHAIN_SORTKEY
805 case ICU_chain_step_type_index:
807 case ICU_chain_step_type_sortkey:
810 case ICU_chain_step_type_casemap:
811 step->u.casemap = icu_casemap_create((char *) chain->locale,
812 (char) rule[0], status);
814 case ICU_chain_step_type_normalize:
815 step->u.normalizer = icu_normalizer_create((char *) rule, 'f', status);
817 case ICU_chain_step_type_tokenize:
818 step->u.tokenizer = icu_tokenizer_create((char *) chain->locale,
819 (char) rule[0], status);
829 void icu_chain_step_destroy(struct icu_chain_step * step){
834 icu_chain_step_destroy(step->previous);
837 case ICU_chain_step_type_display:
839 #ifdef ICU_CHAIN_SORTKEY
840 case ICU_chain_step_type_index:
842 case ICU_chain_step_type_sortkey:
845 case ICU_chain_step_type_casemap:
846 icu_casemap_destroy(step->u.casemap);
847 icu_buf_utf16_destroy(step->buf16);
849 case ICU_chain_step_type_normalize:
850 icu_normalizer_destroy(step->u.normalizer);
851 icu_buf_utf16_destroy(step->buf16);
853 case ICU_chain_step_type_tokenize:
854 icu_tokenizer_destroy(step->u.tokenizer);
855 icu_buf_utf16_destroy(step->buf16);
865 struct icu_chain * icu_chain_create(const uint8_t * locale,
870 struct icu_chain * chain
871 = (struct icu_chain *) malloc(sizeof(struct icu_chain));
873 strncpy((char *) chain->locale, (const char *) locale, 16);
874 chain->locale[16 - 1] = '\0';
878 chain->coll = ucol_open((const char *) chain->locale, status);
880 if (U_FAILURE(*status))
884 chain->token_count = 0;
886 chain->display8 = icu_buf_utf8_create(0);
887 chain->norm8 = icu_buf_utf8_create(0);
888 chain->sort8 = icu_buf_utf8_create(0);
890 chain->src16 = icu_buf_utf16_create(0);
899 void icu_chain_destroy(struct icu_chain * chain)
904 ucol_close(chain->coll);
906 icu_buf_utf8_destroy(chain->display8);
907 icu_buf_utf8_destroy(chain->norm8);
908 icu_buf_utf8_destroy(chain->sort8);
910 icu_buf_utf16_destroy(chain->src16);
912 icu_chain_step_destroy(chain->steps);
919 struct icu_chain * icu_chain_xml_config(xmlNode *xml_node,
920 const uint8_t * locale,
922 UErrorCode * status){
925 struct icu_chain * chain = 0;
928 ||xml_node->type != XML_ELEMENT_NODE
929 // || strcmp((const char *) xml_node->name, "icu_chain")
934 chain = icu_chain_create(locale, sort, status);
940 for (node = xml_node->children; node; node = node->next)
943 struct icu_chain_step * step = 0;
945 if (node->type != XML_ELEMENT_NODE)
948 xml_rule = xmlGetProp(node, (xmlChar *) "rule");
950 if (!strcmp((const char *) node->name,
951 (const char *) "casemap")){
952 step = icu_chain_insert_step(chain, ICU_chain_step_type_casemap,
953 (const uint8_t *) xml_rule, status);
955 else if (!strcmp((const char *) node->name,
956 (const char *) "normalize")){
957 step = icu_chain_insert_step(chain, ICU_chain_step_type_normalize,
958 (const uint8_t *) xml_rule, status);
960 else if (!strcmp((const char *) node->name,
961 (const char *) "tokenize")){
962 step = icu_chain_insert_step(chain, ICU_chain_step_type_tokenize,
963 (const uint8_t *) xml_rule, status);
965 else if (!strcmp((const char *) node->name,
966 (const char *) "display")){
967 step = icu_chain_insert_step(chain, ICU_chain_step_type_display,
968 (const uint8_t *) "", status);
970 #ifdef ICU_CHAIN_SORTKEY
971 else if (!strcmp((const char *) node->name,
972 (const char *) "index")){
973 step = icu_chain_insert_step(chain, ICU_chain_step_type_index,
974 (const uint8_t *) "", status);
976 else if (!strcmp((const char *) node->name,
977 (const char *) "sortkey")){
978 step = icu_chain_insert_step(chain, ICU_chain_step_type_sortkey,
979 (const uint8_t *) "", status);
983 if (!step || U_FAILURE(*status)){
984 icu_chain_destroy(chain);
996 struct icu_chain_step * icu_chain_insert_step(struct icu_chain * chain,
997 enum icu_chain_step_type type,
998 const uint8_t * rule,
1001 struct icu_chain_step * step = 0;
1002 struct icu_buf_utf16 * src16 = 0;
1003 struct icu_buf_utf16 * buf16 = 0;
1005 if (!chain || !type || !rule)
1008 /* assign utf16 src buffers as needed */
1009 if (chain->steps && chain->steps->buf16)
1010 src16 = chain->steps->buf16;
1011 else if (chain->src16)
1012 src16 = chain->src16;
1017 /* create utf16 destination buffers as needed, or */
1019 case ICU_chain_step_type_display:
1022 #ifdef ICU_CHAIN_SORTKEY
1023 case ICU_chain_step_type_index:
1026 case ICU_chain_step_type_sortkey:
1030 case ICU_chain_step_type_casemap:
1031 buf16 = icu_buf_utf16_create(0);
1033 case ICU_chain_step_type_normalize:
1034 buf16 = icu_buf_utf16_create(0);
1036 case ICU_chain_step_type_tokenize:
1037 buf16 = icu_buf_utf16_create(0);
1043 /* create actual chain step with this buffer */
1044 step = icu_chain_step_create(chain, type, rule, buf16, status);
1046 step->previous = chain->steps;
1047 chain->steps = step;
1053 int icu_chain_step_next_token(struct icu_chain * chain,
1054 struct icu_chain_step * step,
1057 struct icu_buf_utf16 * src16 = 0;
1058 int got_new_token = 0;
1060 if (!chain || !chain->src16 || !step || !step->more_tokens)
1063 /* assign utf16 src buffers as neeed, advance in previous steps
1064 tokens until non-zero token met, and setting stop condition */
1066 if (step->previous){
1067 src16 = step->previous->buf16;
1068 /* tokens might be killed in previous steps, therefore looping */
1069 while (step->need_new_token
1070 && step->previous->more_tokens
1073 = icu_chain_step_next_token(chain, step->previous, status);
1075 else { /* first step can only work once on chain->src16 input buffer */
1076 src16 = chain->src16;
1077 step->more_tokens = 0;
1084 /* stop if nothing to process */
1085 if (step->need_new_token && !got_new_token){
1086 step->more_tokens = 0;
1090 /* either an old token not finished yet, or a new token, thus
1091 perform the work, eventually put this steps output in
1092 step->buf16 or the chains UTF8 output buffers */
1094 switch(step->type) {
1095 case ICU_chain_step_type_display:
1096 icu_utf16_to_utf8(chain->display8, src16, status);
1098 #ifdef ICU_CHAIN_SORTKEY
1100 case ICU_chain_step_type_index:
1101 icu_utf16_to_utf8(chain->norm8, src16, status);
1103 case ICU_chain_step_type_sortkey:
1104 icu_utf16_to_utf8(chain->sort8, src16, status);
1105 //UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
1106 // struct icu_buf_utf8 * dest8,
1107 // struct icu_buf_utf16 * src16,
1108 // UErrorCode * status);
1111 case ICU_chain_step_type_casemap:
1112 icu_casemap_casemap(step->u.casemap,
1113 step->buf16, src16, status);
1115 case ICU_chain_step_type_normalize:
1116 icu_normalizer_normalize(step->u.normalizer,
1117 step->buf16, src16, status);
1119 case ICU_chain_step_type_tokenize:
1120 /* attach to new src16 token only first time during splitting */
1121 if (step->need_new_token){
1122 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1123 step->need_new_token = 0;
1127 /* splitting one src16 token into multiple buf16 tokens */
1129 = icu_tokenizer_next_token(step->u.tokenizer,
1130 step->buf16, status);
1132 /* make sure to get new previous token if this one had been used up
1133 by recursive call to _same_ step */
1135 if (!step->more_tokens)
1136 step->more_tokens = icu_chain_step_next_token(chain, step, status);
1138 //if (0 == step->more_tokens)
1146 if (U_FAILURE(*status))
1149 /* if token disappered into thin air, tell caller */
1150 if (!step->buf16->utf16_len)
1153 if (U_FAILURE(*status))
1161 int icu_chain_step_next_token_BAK(struct icu_chain * chain,
1162 struct icu_chain_step * step,
1165 struct icu_buf_utf16 * src16 = 0;
1167 if (!chain || !chain->src16 || !step || !step->more_tokens)
1170 /* assign utf16 src buffers as neeed, advance in previous steps
1171 tokens until non-zero token met, and setting stop condition
1173 if (step->previous){
1174 src16 = step->previous->buf16;
1175 if (step->need_new_token)
1177 = icu_chain_step_next_token(chain, step->previous, status);
1179 else { /* first step can only work once on chain->src16 input buffer */
1180 src16 = chain->src16;
1181 step->more_tokens = 1;
1184 /* stop if nothing to process
1185 i.e new token source was not properly assigned
1186 or I did not get any tokens from previous step
1188 if (!step->more_tokens || !src16)
1191 /* perform the work, eventually put this steps output in
1192 step->buf16 or the chains UTF8 output buffers */
1193 switch(step->type) {
1194 case ICU_chain_step_type_display:
1195 icu_utf16_to_utf8(chain->display8, src16, status);
1197 case ICU_chain_step_type_index:
1198 icu_utf16_to_utf8(chain->norm8, src16, status);
1200 case ICU_chain_step_type_sortkey:
1201 icu_utf16_to_utf8(chain->sort8, src16, status);
1203 case ICU_chain_step_type_casemap:
1204 icu_casemap_casemap(step->u.casemap,
1205 step->buf16, src16, status);
1207 case ICU_chain_step_type_normalize:
1208 icu_normalizer_normalize(step->u.normalizer,
1209 step->buf16, src16, status);
1211 case ICU_chain_step_type_tokenize:
1212 /* attach to new src16 token only first time during splitting */
1213 if (step->need_new_token){
1214 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1215 step->need_new_token = 0;
1217 /* splitting one src16 token into multiple buf16 tokens */
1219 = icu_tokenizer_next_token(step->u.tokenizer,
1220 step->buf16, status);
1221 /* make sure to get new previous token if this one had been used up */
1222 if (step->previous && !step->more_tokens){
1223 if (icu_chain_step_next_token(chain, step->previous, status)){
1224 icu_tokenizer_attach(step->u.tokenizer, src16, status);
1225 step->need_new_token = 0;
1227 = icu_tokenizer_next_token(step->u.tokenizer,
1228 step->buf16, status);
1231 if (0 == step->more_tokens)
1239 /* stop further token processing if last step and
1240 new tokens can ot be obtained from last non-existing step
1242 if (!step->previous)
1243 step->more_tokens = 0;
1245 if (U_FAILURE(*status))
1248 // result buf empty, got no token TODO: make while loop around all token
1249 // fetching operations
1250 //if (!step->buf16->utf16_len)
1258 int icu_chain_assign_cstr(struct icu_chain * chain,
1259 const char * src8cstr,
1262 struct icu_chain_step * stp = 0;
1264 if (!chain || !src8cstr)
1269 /* clear token count */
1270 chain->token_count = 0;
1272 /* clear all steps stop states */
1274 stp->more_tokens = 1;
1275 stp->need_new_token = 1;
1276 stp = stp->previous;
1279 /* finally convert UTF8 to UTF16 string */
1280 icu_utf16_from_utf8_cstr(chain->src16, src8cstr, status);
1282 if (U_FAILURE(*status))
1290 int icu_chain_next_token(struct icu_chain * chain,
1295 if (!chain || !chain->steps)
1298 while(!got_token && chain->steps->more_tokens)
1299 got_token = icu_chain_step_next_token(chain, chain->steps, status);
1302 chain->token_count++;
1304 icu_utf16_to_utf8(chain->norm8, chain->steps->buf16, status);
1306 icu_sortkey8_from_utf16(chain->coll,
1307 chain->sort8, chain->steps->buf16, status);
1309 return chain->token_count;
1315 int icu_chain_get_token_count(struct icu_chain * chain)
1320 return chain->token_count;
1325 const char * icu_chain_get_display(struct icu_chain * chain)
1327 if (chain->display8)
1328 return icu_buf_utf8_to_cstr(chain->display8);
1333 const char * icu_chain_get_norm(struct icu_chain * chain)
1336 return icu_buf_utf8_to_cstr(chain->norm8);
1341 const char * icu_chain_get_sort(struct icu_chain * chain)
1344 return icu_buf_utf8_to_cstr(chain->sort8);
1349 const UCollator * icu_chain_get_coll(struct icu_chain * chain)
1357 #endif /* HAVE_ICU */
1365 * indent-tabs-mode: nil
1367 * vim: shiftwidth=4 tabstop=8 expandtab