1 // Make command on debian 64 bit testing dist
3 gcc -g -Wall `icu-config --cppflags` `icu-config --ldflags` -o icu_bug_2 icu_bug_2.c
4 snatched from http://www.icu-project.org/userguide/Collate_API.html
6 added a struct icu_termmap such that I actually can see the output
13 #include <unicode/ustring.h> /* some more string fcns*/
14 #include <unicode/uchar.h> /* char names */
17 //#include <unicode/ustdio.h>
18 //#include <unicode/utypes.h> /* Basic ICU data types */
19 #include <unicode/ucol.h>
20 //#include <unicode/ucnv.h> /* C Converter API */
21 //#include <unicode/uloc.h>
22 //#include <unicode/ubrk.h>
23 //#include <unicode/unistr.h>
26 #define MAX_KEY_SIZE 256
36 struct icu_buf_utf16 * icu_buf_utf16_create(size_t capacity)
38 struct icu_buf_utf16 * buf16
39 = (struct icu_buf_utf16 *) malloc(sizeof(struct icu_buf_utf16));
46 buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
47 buf16->utf16[0] = (UChar) 0;
48 buf16->utf16_cap = capacity;
54 struct icu_buf_utf16 * icu_buf_utf16_resize(struct icu_buf_utf16 * buf16,
59 if (0 == buf16->utf16)
60 buf16->utf16 = (UChar *) malloc(sizeof(UChar) * capacity);
63 = (UChar *) realloc(buf16->utf16, sizeof(UChar) * capacity);
64 buf16->utf16[0] = (UChar) 0;
66 buf16->utf16_cap = capacity;
81 void icu_buf_utf16_destroy(struct icu_buf_utf16 * buf16)
101 struct icu_buf_utf8 * icu_buf_utf8_create(size_t capacity)
103 struct icu_buf_utf8 * buf8
104 = (struct icu_buf_utf8 *) malloc(sizeof(struct icu_buf_utf8));
111 buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
112 buf8->utf8[0] = (uint8_t) 0;
113 buf8->utf8_cap = capacity;
120 struct icu_buf_utf8 * icu_buf_utf8_resize(struct icu_buf_utf8 * buf8,
126 buf8->utf8 = (uint8_t *) malloc(sizeof(uint8_t) * capacity);
129 = (uint8_t *) realloc(buf8->utf8, sizeof(uint8_t) * capacity);
130 buf8->utf8[0] = (uint8_t) 0;
132 buf8->utf8_cap = capacity;
148 void icu_buf_utf8_destroy(struct icu_buf_utf8 * buf8)
159 UErrorCode icu_utf16_from_utf8(struct icu_buf_utf16 * dest16,
160 struct icu_buf_utf8 * src8,
163 int32_t utf16_len = 0;
165 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
167 (const char *) src8->utf8, src8->utf8_len, status);
169 // check for buffer overflow, resize and retry
170 if (*status == U_BUFFER_OVERFLOW_ERROR
171 //|| dest16->utf16_len > dest16->utf16_cap
173 icu_buf_utf16_resize(dest16, utf16_len * 2);
174 *status = U_ZERO_ERROR;
175 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
177 (const char *) src8->utf8, src8->utf8_len, status);
180 if (*status != U_BUFFER_OVERFLOW_ERROR
181 && utf16_len < dest16->utf16_cap)
182 dest16->utf16_len = utf16_len;
184 dest16->utf16[0] = (UChar) 0;
185 dest16->utf16_len = 0;
193 UErrorCode icu_utf16_from_utf8_cstr(struct icu_buf_utf16 * dest16,
194 const char * src8cstr,
197 size_t src8cstr_len = 0;
198 int32_t utf16_len = 0;
200 src8cstr_len = strlen(src8cstr);
202 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
204 src8cstr, src8cstr_len, status);
206 // check for buffer overflow, resize and retry
207 if (*status == U_BUFFER_OVERFLOW_ERROR
208 //|| dest16->utf16_len > dest16->utf16_cap
210 icu_buf_utf16_resize(dest16, utf16_len * 2);
211 *status = U_ZERO_ERROR;
212 u_strFromUTF8(dest16->utf16, dest16->utf16_cap,
214 src8cstr, src8cstr_len, status);
217 if (*status != U_BUFFER_OVERFLOW_ERROR
218 && utf16_len < dest16->utf16_cap)
219 dest16->utf16_len = utf16_len;
221 dest16->utf16[0] = (UChar) 0;
222 dest16->utf16_len = 0;
229 UErrorCode icu_sortkey8_from_utf16(UCollator *coll,
230 struct icu_buf_utf8 * dest8,
231 struct icu_buf_utf16 * src16,
235 int32_t sortkey_len = 0;
237 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
238 dest8->utf8, dest8->utf8_cap);
240 // check for buffer overflow, resize and retry
241 if (sortkey_len > dest8->utf8_cap) {
242 icu_buf_utf8_resize(dest8, sortkey_len * 2);
243 sortkey_len = ucol_getSortKey(coll, src16->utf16, src16->utf16_len,
244 dest8->utf8, dest8->utf8_cap);
248 dest8->utf8_len = sortkey_len;
258 uint8_t sort_key[MAX_KEY_SIZE]; // standard C string '\0' terminated
259 char disp_term[MAX_KEY_SIZE]; // standard C utf-8 string
264 int icu_termmap_cmp(const void *vp1, const void *vp2)
266 struct icu_termmap *itmp1 = *(struct icu_termmap **) vp1;
267 struct icu_termmap *itmp2 = *(struct icu_termmap **) vp2;
271 cmp = strcmp((const char *)itmp1->sort_key,
272 (const char *)itmp2->sort_key);
277 int icu_check_status(UErrorCode status)
279 if(!U_SUCCESS(status))
280 printf("ICU status: %d %s\n", status, u_errorName(status));
286 int icu_coll_sort(const char * locale, int src_list_len,
287 const char ** src_list, const char ** chk_list)
289 UErrorCode status = U_ZERO_ERROR;
291 struct icu_buf_utf8 * buf8 = icu_buf_utf8_create(0);
292 struct icu_buf_utf16 * buf16 = icu_buf_utf16_create(0);
296 struct icu_termmap * list[src_list_len];
298 UCollator *coll = ucol_open(locale, &status);
299 icu_check_status(status);
301 if(!U_SUCCESS(status))
304 // assigning display terms and sort keys using buf 8 and buf16
305 for( i = 0; i < src_list_len; i++)
308 list[i] = (struct icu_termmap *) malloc(sizeof(struct icu_termmap));
311 strcpy(list[i]->disp_term, src_list[i]);
313 // transforming to UTF16
314 icu_utf16_from_utf8_cstr(buf16, list[i]->disp_term, &status);
315 icu_check_status(status);
317 // computing sortkeys
318 icu_sortkey8_from_utf16(coll, buf8, buf16, &status);
319 icu_check_status(status);
321 // assigning sortkeys
322 memcpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
323 //strncpy(list[i]->sort_key, buf8->utf8, buf8->utf8_len);
324 //strcpy((char *) list[i]->sort_key, (const char *) buf8->utf8);
328 printf("Input str: '%s' : ", locale);
329 for (i = 0; i < src_list_len; i++) {
330 printf(" '%s'", list[i]->disp_term);
335 qsort(list, src_list_len,
336 sizeof(struct icu_termmap *), icu_termmap_cmp);
339 printf("ICU sort: '%s' : ", locale);
340 for (i = 0; i < src_list_len; i++) {
341 printf(" '%s'", list[i]->disp_term);
342 //printf("(%d|%d)", list[i]->sort_key[0],list[i]->sort_key[1]);
348 icu_buf_utf8_destroy(buf8);
349 icu_buf_utf16_destroy(buf16);
355 int main(int argc, char **argv)
359 const char * en_1_src[6] = {"z", "K", "a", "A", "Z", "k"};
360 const char * en_1_cck[6] = {"a", "A", "K", "k", "z", "Z"};
361 icu_coll_sort("en", en_1_len, en_1_src, en_1_cck);
362 icu_coll_sort("en_AU", en_1_len, en_1_src, en_1_cck);
363 icu_coll_sort("en_CA", en_1_len, en_1_src, en_1_cck);
364 icu_coll_sort("en_GB", en_1_len, en_1_src, en_1_cck);
365 icu_coll_sort("en_US", en_1_len, en_1_src, en_1_cck);
369 const char * da_1_src[6] = {"z", "å", "o", "æ", "a", "ø"};
370 const char * da_1_cck[6] = {"a", "o", "z", "æ", "ø", "å"};
371 icu_coll_sort("da", da_1_len, da_1_src, da_1_cck);
372 icu_coll_sort("da_DK", da_1_len, da_1_src, da_1_cck);
376 const char * de_1_src[9] = {"u", "ä", "o", "t", "s", "ß", "ü", "ö", "a"};
377 const char * de_1_cck[9] = {"ä", "a", "o", "ö", "s", "ß", "t", "u", "ü"};
378 icu_coll_sort("de", de_1_len, de_1_src, de_1_cck);
379 icu_coll_sort("de_AT", de_1_len, de_1_src, de_1_cck);
380 icu_coll_sort("de_DE", de_1_len, de_1_src, de_1_cck);