Deprecate ZOOM_resultset_facets_names
[yaz-moved-to-github.git] / src / icu_sortkey.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file
8  * \brief sortkey utility based on ICU Collator
9  */
10
11 #if HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #if YAZ_HAVE_ICU
16 #include <yaz/xmalloc.h>
17
18 #include <yaz/icu_I18N.h>
19
20 #include <yaz/log.h>
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include <unicode/ustring.h>  /* some more string fcns*/
27 #include <unicode/uchar.h>    /* char names           */
28
29 void icu_sortkey8_from_utf16(UCollator *coll,
30                              struct icu_buf_utf8 *dest8,
31                              struct icu_buf_utf16 *src16,
32                              UErrorCode * status)
33 {
34     int32_t sortkey_len = 0;
35     /* we'll fake a capacity of one less, because it turns out
36        that ucol_getSortKey writes ONE character too much */
37     int32_t cap = dest8->utf8_cap ? dest8->utf8_cap - 1 : 0;
38
39     sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
40                                   dest8->utf8, cap);
41
42     /* check for buffer overflow, resize and retry */
43     if (sortkey_len > cap)
44     {
45         icu_buf_utf8_resize(dest8, sortkey_len * 2);
46         sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
47                                       dest8->utf8, dest8->utf8_cap);
48     }
49
50     if (U_SUCCESS(*status)
51         && sortkey_len > 0)
52         dest8->utf8_len = sortkey_len;
53     else
54         icu_buf_utf8_clear(dest8);
55 }
56
57
58 #endif /* YAZ_HAVE_ICU */
59
60 /*
61  * Local variables:
62  * c-basic-offset: 4
63  * c-file-style: "Stroustrup"
64  * indent-tabs-mode: nil
65  * End:
66  * vim: shiftwidth=4 tabstop=8 expandtab
67  */
68