]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/strsrch/strsrch.cpp
1 /********************************************************************
3 * Copyright (C) 2002-2003 IBM, Inc. All Rights Reserved.
5 ********************************************************************/
8 * This program demos string collation
11 const char gHelpString
[] =
12 "usage: strsrch [options*] -source source_string -pattern pattern_string\n"
13 "-help Display this message.\n"
14 "-locale name ICU locale to use. Default is en_US\n"
15 "-rules rule Collation rules file (overrides locale)\n"
16 "-french French accent ordering\n"
17 "-norm Normalizing mode on\n"
18 "-shifted Shifted mode\n"
19 "-lower Lower case first\n"
20 "-upper Upper case first\n"
21 "-case Enable separate case level\n"
22 "-level n Sort level, 1 to 5, for Primary, Secndary, Tertiary, Quaternary, Identical\n"
23 "-source string Source string\n"
24 "-pattern string Pattern string to look for in source\n"
25 "-overlap Enable searching to be done on overlapping patterns\n"
26 "-canonical Enable searching to be done matching canonical equivalent patterns"
27 "Example strsrch -rules \\u0026b\\u003ca -source a\\u0020b\\u0020bc -pattern b\n"
28 "The format \\uXXXX is supported for the rules and comparison strings\n"
35 #include <unicode/utypes.h>
36 #include <unicode/ucol.h>
37 #include <unicode/usearch.h>
38 #include <unicode/ustring.h>
41 * Command line option variables
42 * These global variables are set according to the options specified
43 * on the command line by the user.
45 char * opt_locale
= "en_US";
47 UBool opt_help
= FALSE
;
48 UBool opt_norm
= FALSE
;
49 UBool opt_french
= FALSE
;
50 UBool opt_shifted
= FALSE
;
51 UBool opt_lower
= FALSE
;
52 UBool opt_upper
= FALSE
;
53 UBool opt_case
= FALSE
;
54 UBool opt_overlap
= FALSE
;
55 UBool opt_canonical
= FALSE
;
57 char * opt_source
= "International Components for Unicode";
58 char * opt_pattern
= "Unicode";
59 UCollator
* collator
= 0;
60 UStringSearch
* search
= 0;
66 * Definitions for the command line options
70 enum {FLAG
, NUM
, STRING
} type
;
75 {"-locale", OptSpec::STRING
, &opt_locale
},
76 {"-rules", OptSpec::STRING
, &opt_rules
},
77 {"-source", OptSpec::STRING
, &opt_source
},
78 {"-pattern", OptSpec::STRING
, &opt_pattern
},
79 {"-norm", OptSpec::FLAG
, &opt_norm
},
80 {"-french", OptSpec::FLAG
, &opt_french
},
81 {"-shifted", OptSpec::FLAG
, &opt_shifted
},
82 {"-lower", OptSpec::FLAG
, &opt_lower
},
83 {"-upper", OptSpec::FLAG
, &opt_upper
},
84 {"-case", OptSpec::FLAG
, &opt_case
},
85 {"-level", OptSpec::NUM
, &opt_level
},
86 {"-overlap", OptSpec::FLAG
, &opt_overlap
},
87 {"-canonical", OptSpec::FLAG
, &opt_canonical
},
88 {"-help", OptSpec::FLAG
, &opt_help
},
89 {"-?", OptSpec::FLAG
, &opt_help
},
94 * processOptions() Function to read the command line options.
96 UBool
processOptions(int argc
, const char **argv
, OptSpec opts
[])
98 for (int argNum
= 1; argNum
< argc
; argNum
++) {
99 const char *pArgName
= argv
[argNum
];
100 for (OptSpec
*pOpt
= opts
; pOpt
->name
!= 0; pOpt
++) {
101 if (strcmp(pOpt
->name
, pArgName
) == 0) {
102 switch (pOpt
->type
) {
104 *(UBool
*)(pOpt
->pVar
) = TRUE
;
106 case OptSpec::STRING
:
108 if (argNum
>= argc
) {
109 fprintf(stderr
, "value expected for \"%s\" option.\n",
113 *(const char **)(pOpt
->pVar
) = argv
[argNum
];
117 if (argNum
>= argc
) {
118 fprintf(stderr
, "value expected for \"%s\" option.\n",
123 int i
= strtol(argv
[argNum
], &endp
, 0);
124 if (endp
== argv
[argNum
]) {
126 "integer value expected for \"%s\" option.\n",
130 *(int *)(pOpt
->pVar
) = i
;
137 fprintf(stderr
, "Unrecognized option \"%s\"\n", pArgName
);
147 UBool
processCollator()
149 // Set up an ICU collator
150 UErrorCode status
= U_ZERO_ERROR
;
152 if (opt_rules
!= 0) {
153 u_unescape(opt_rules
, rules
, 100);
154 collator
= ucol_openRules(rules
, -1, UCOL_OFF
, UCOL_TERTIARY
,
158 collator
= ucol_open(opt_locale
, &status
);
160 if (U_FAILURE(status
)) {
161 fprintf(stderr
, "Collator creation failed.: %d\n", status
);
164 if (status
== U_USING_DEFAULT_WARNING
) {
165 fprintf(stderr
, "Warning, U_USING_DEFAULT_WARNING for %s\n",
168 if (status
== U_USING_FALLBACK_WARNING
) {
169 fprintf(stderr
, "Warning, U_USING_FALLBACK_ERROR for %s\n",
173 ucol_setAttribute(collator
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
176 ucol_setAttribute(collator
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
179 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
,
183 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
,
187 ucol_setAttribute(collator
, UCOL_CASE_LEVEL
, UCOL_ON
, &status
);
190 ucol_setAttribute(collator
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
,
193 if (opt_level
!= 0) {
196 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_PRIMARY
, &status
);
199 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_SECONDARY
,
203 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
206 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_QUATERNARY
,
210 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_IDENTICAL
,
214 fprintf(stderr
, "-level param must be between 1 and 5\n");
218 if (U_FAILURE(status
)) {
219 fprintf(stderr
, "Collator attribute setting failed.: %d\n", status
);
226 * Creates a string search
228 UBool
processStringSearch()
230 u_unescape(opt_source
, source
, 100);
231 u_unescape(opt_pattern
, pattern
, 100);
232 UErrorCode status
= U_ZERO_ERROR
;
233 search
= usearch_openFromCollator(pattern
, -1, source
, -1, collator
, NULL
,
235 if (U_FAILURE(status
)) {
238 if (opt_overlap
== TRUE
) {
239 usearch_setAttribute(search
, USEARCH_OVERLAP
, USEARCH_ON
, &status
);
241 if (opt_canonical
== TRUE
) {
242 usearch_setAttribute(search
, USEARCH_CANONICAL_MATCH
, USEARCH_ON
,
245 if (U_FAILURE(status
)) {
246 fprintf(stderr
, "Error setting search attributes\n");
254 UErrorCode status
= U_ZERO_ERROR
;
255 int32_t offset
= usearch_next(search
, &status
);
256 if (offset
== USEARCH_DONE
) {
257 fprintf(stdout
, "Pattern not found in source\n");
259 while (offset
!= USEARCH_DONE
) {
260 fprintf(stdout
, "Pattern found at offset %d size %d\n", offset
,
261 usearch_getMatchedLength(search
));
262 offset
= usearch_next(search
, &status
);
264 if (U_FAILURE(status
)) {
265 fprintf(stderr
, "Error in searching for pattern %d\n", status
);
268 fprintf(stdout
, "End of search\n");
273 * Main -- process command line, read in and pre-process the test file,
274 * call other functions to do the actual tests.
276 int main(int argc
, const char** argv
)
278 if (processOptions(argc
, argv
, opts
) != TRUE
|| opt_help
) {
283 if (processCollator() != TRUE
) {
284 fprintf(stderr
, "Error creating collator\n");
288 if (processStringSearch() != TRUE
) {
289 fprintf(stderr
, "Error creating string search\n");
293 fprintf(stdout
, "Finding pattern %s in source %s\n", opt_pattern
,
297 ucol_close(collator
);
298 usearch_close(search
);