]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/legacy/newcol.cpp
2 *******************************************************************************
4 * © 2016 and later: Unicode, Inc. and others.
5 * License & terms of use: http://www.unicode.org/copyright.html#License
7 *******************************************************************************
8 *******************************************************************************
10 * Copyright (C) 2001 - 2005, International Business Machines
11 * Corporation and others. All Rights Reserved.
13 *******************************************************************************
14 * file name: newcol.cpp
16 * tab size: 8 (not used)
19 * created on: 2001jul24
20 * created by: Vladimir Weinstein
23 /******************************************************************************
24 * This is the module that uses new collation
25 ******************************************************************************/
29 #include "unicode/ucol.h"
31 // Very simple example code - sticks a sortkey in the buffer
32 // Not much error checking
33 int32_t getSortKey_current(const char *locale
, const UChar
*string
, int32_t sLen
, uint8_t *buffer
, int32_t bLen
) {
34 UErrorCode status
= U_ZERO_ERROR
;
35 UCollator
*coll
= ucol_open(locale
, &status
);
36 if(U_FAILURE(status
)) {
39 int32_t result
= ucol_getSortKey(coll
, string
, sLen
, buffer
, bLen
);
44 // This one can be used for passing to qsort function
45 // Not thread safe or anything
46 static UCollator
*compareCollator
= NULL
;
48 int compare_current(const void *string1
, const void *string2
) {
49 if(compareCollator
!= NULL
) {
50 UCollationResult res
= ucol_strcoll(compareCollator
, (UChar
*) string1
, -1, (UChar
*) string2
, -1);
51 if(res
== UCOL_LESS
) {
53 } else if(res
== UCOL_GREATER
) {
63 void initCollator_current(const char *locale
) {
64 UErrorCode status
= U_ZERO_ERROR
;
65 compareCollator
= ucol_open(locale
, &status
);
68 void closeCollator_current(void) {
69 ucol_close(compareCollator
);
70 compareCollator
= NULL
;
74 extern "C" void test_current(UChar data
[][5], uint32_t size
, uint32_t maxlen
, uint8_t keys
[][32]) {
80 fprintf(stderr
, "Entered current, version: [%d.%d.%d.%d]\nMoving to sortkeys\n", uvi
[0], uvi
[1], uvi
[2], uvi
[3]);
82 for(i
= 0; i
<size
; i
++) {
83 keySize
= getSortKey_current("ja", data
[i
], -1, keys
[i
], 32);
84 fprintf(stderr
, "For i=%d, size of sortkey is %d\n", i
, keySize
);
87 fprintf(stderr
, "Done sortkeys, doing qsort test\n");
89 initCollator_current("ja");
90 qsort(data
, size
, maxlen
*sizeof(UChar
), compare_current
);
91 closeCollator_current();
93 fprintf(stderr
, "Done current!\n");