]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/coll/coll.cpp
1 /*************************************************************************
3 * © 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
6 *************************************************************************
7 *************************************************************************
9 * Copyright (C) 2002-2006 IBM, Inc. All Rights Reserved.
11 *************************************************************************/
14 * This program demos string collation
17 const char gHelpString
[] =
18 "usage: coll [options*] -source source_string -target target_string\n"
19 "-help Display this message.\n"
20 "-locale name ICU locale to use. Default is en_US\n"
21 "-rules rule Collation rules file (overrides locale)\n"
22 "-french French accent ordering\n"
23 "-norm Normalizing mode on\n"
24 "-shifted Shifted mode\n"
25 "-lower Lower case first\n"
26 "-upper Upper case first\n"
27 "-case Enable separate case level\n"
28 "-level n Sort level, 1 to 5, for Primary, Secndary, Tertiary, Quaternary, Identical\n"
29 "-source string Source string for comparison\n"
30 "-target string Target string for comparison\n"
31 "Example coll -rules \\u0026b\\u003ca -source a -target b\n"
32 "The format \\uXXXX is supported for the rules and comparison strings\n"
39 #include <unicode/utypes.h>
40 #include <unicode/ucol.h>
41 #include <unicode/ustring.h>
44 * Command line option variables
45 * These global variables are set according to the options specified
46 * on the command line by the user.
48 char * opt_locale
= "en_US";
50 UBool opt_help
= FALSE
;
51 UBool opt_norm
= FALSE
;
52 UBool opt_french
= FALSE
;
53 UBool opt_shifted
= FALSE
;
54 UBool opt_lower
= FALSE
;
55 UBool opt_upper
= FALSE
;
56 UBool opt_case
= FALSE
;
58 char * opt_source
= "abc";
59 char * opt_target
= "abd";
60 UCollator
* collator
= 0;
63 * Definitions for the command line options
67 enum {FLAG
, NUM
, STRING
} type
;
72 {"-locale", OptSpec::STRING
, &opt_locale
},
73 {"-rules", OptSpec::STRING
, &opt_rules
},
74 {"-source", OptSpec::STRING
, &opt_source
},
75 {"-target", OptSpec::STRING
, &opt_target
},
76 {"-norm", OptSpec::FLAG
, &opt_norm
},
77 {"-french", OptSpec::FLAG
, &opt_french
},
78 {"-shifted", OptSpec::FLAG
, &opt_shifted
},
79 {"-lower", OptSpec::FLAG
, &opt_lower
},
80 {"-upper", OptSpec::FLAG
, &opt_upper
},
81 {"-case", OptSpec::FLAG
, &opt_case
},
82 {"-level", OptSpec::NUM
, &opt_level
},
83 {"-help", OptSpec::FLAG
, &opt_help
},
84 {"-?", OptSpec::FLAG
, &opt_help
},
89 * processOptions() Function to read the command line options.
91 UBool
processOptions(int argc
, const char **argv
, OptSpec opts
[])
93 for (int argNum
= 1; argNum
< argc
; argNum
++) {
94 const char *pArgName
= argv
[argNum
];
96 for (pOpt
= opts
; pOpt
->name
!= 0; pOpt
++) {
97 if (strcmp(pOpt
->name
, pArgName
) == 0) {
100 *(UBool
*)(pOpt
->pVar
) = TRUE
;
102 case OptSpec::STRING
:
104 if (argNum
>= argc
) {
105 fprintf(stderr
, "value expected for \"%s\" option.\n",
109 *(const char **)(pOpt
->pVar
) = argv
[argNum
];
113 if (argNum
>= argc
) {
114 fprintf(stderr
, "value expected for \"%s\" option.\n",
119 int i
= strtol(argv
[argNum
], &endp
, 0);
120 if (endp
== argv
[argNum
]) {
122 "integer value expected for \"%s\" option.\n",
126 *(int *)(pOpt
->pVar
) = i
;
133 fprintf(stderr
, "Unrecognized option \"%s\"\n", pArgName
);
141 * ICU string comparison
147 u_unescape(opt_source
, source
, 100);
148 u_unescape(opt_target
, target
, 100);
149 UCollationResult result
= ucol_strcoll(collator
, source
, -1, target
, -1);
150 if (result
== UCOL_LESS
) {
153 else if (result
== UCOL_GREATER
) {
162 UBool
processCollator()
164 // Set up an ICU collator
165 UErrorCode status
= U_ZERO_ERROR
;
168 if (opt_rules
!= 0) {
169 u_unescape(opt_rules
, rules
, 100);
170 collator
= ucol_openRules(rules
, -1, UCOL_OFF
, UCOL_TERTIARY
,
174 collator
= ucol_open(opt_locale
, &status
);
176 if (U_FAILURE(status
)) {
177 fprintf(stderr
, "Collator creation failed.: %d\n", status
);
180 if (status
== U_USING_DEFAULT_WARNING
) {
181 fprintf(stderr
, "Warning, U_USING_DEFAULT_WARNING for %s\n",
184 if (status
== U_USING_FALLBACK_WARNING
) {
185 fprintf(stderr
, "Warning, U_USING_FALLBACK_ERROR for %s\n",
189 ucol_setAttribute(collator
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
192 ucol_setAttribute(collator
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
195 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
,
199 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
,
203 ucol_setAttribute(collator
, UCOL_CASE_LEVEL
, UCOL_ON
, &status
);
206 ucol_setAttribute(collator
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
,
209 if (opt_level
!= 0) {
212 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_PRIMARY
, &status
);
215 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_SECONDARY
,
219 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
222 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_QUATERNARY
,
226 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_IDENTICAL
,
230 fprintf(stderr
, "-level param must be between 1 and 5\n");
234 if (U_FAILURE(status
)) {
235 fprintf(stderr
, "Collator attribute setting failed.: %d\n", status
);
242 * Main -- process command line, read in and pre-process the test file,
243 * call other functions to do the actual tests.
245 int main(int argc
, const char** argv
)
247 if (processOptions(argc
, argv
, opts
) != TRUE
|| opt_help
) {
252 if (processCollator() != TRUE
) {
253 fprintf(stderr
, "Error creating collator for comparison\n");
257 fprintf(stdout
, "Comparing source=%s and target=%s\n", opt_source
,
259 int result
= strcmp();
261 fprintf(stdout
, "source is equals to target\n");
263 else if (result
< 0) {
264 fprintf(stdout
, "source is less than target\n");
267 fprintf(stdout
, "source is greater than target\n");
270 ucol_close(collator
);