]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/strsrch/strsrch.cpp
1 /********************************************************************
3 * Copyright (C) 2002-2006 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
];
101 for (pOpt
= opts
; pOpt
->name
!= 0; pOpt
++) {
102 if (strcmp(pOpt
->name
, pArgName
) == 0) {
103 switch (pOpt
->type
) {
105 *(UBool
*)(pOpt
->pVar
) = TRUE
;
107 case OptSpec::STRING
:
109 if (argNum
>= argc
) {
110 fprintf(stderr
, "value expected for \"%s\" option.\n",
114 *(const char **)(pOpt
->pVar
) = argv
[argNum
];
118 if (argNum
>= argc
) {
119 fprintf(stderr
, "value expected for \"%s\" option.\n",
124 int i
= strtol(argv
[argNum
], &endp
, 0);
125 if (endp
== argv
[argNum
]) {
127 "integer value expected for \"%s\" option.\n",
131 *(int *)(pOpt
->pVar
) = i
;
138 fprintf(stderr
, "Unrecognized option \"%s\"\n", pArgName
);
148 UBool
processCollator()
150 // Set up an ICU collator
151 UErrorCode status
= U_ZERO_ERROR
;
153 if (opt_rules
!= 0) {
154 u_unescape(opt_rules
, rules
, 100);
155 collator
= ucol_openRules(rules
, -1, UCOL_OFF
, UCOL_TERTIARY
,
159 collator
= ucol_open(opt_locale
, &status
);
161 if (U_FAILURE(status
)) {
162 fprintf(stderr
, "Collator creation failed.: %d\n", status
);
165 if (status
== U_USING_DEFAULT_WARNING
) {
166 fprintf(stderr
, "Warning, U_USING_DEFAULT_WARNING for %s\n",
169 if (status
== U_USING_FALLBACK_WARNING
) {
170 fprintf(stderr
, "Warning, U_USING_FALLBACK_ERROR for %s\n",
174 ucol_setAttribute(collator
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
177 ucol_setAttribute(collator
, UCOL_FRENCH_COLLATION
, UCOL_ON
, &status
);
180 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_LOWER_FIRST
,
184 ucol_setAttribute(collator
, UCOL_CASE_FIRST
, UCOL_UPPER_FIRST
,
188 ucol_setAttribute(collator
, UCOL_CASE_LEVEL
, UCOL_ON
, &status
);
191 ucol_setAttribute(collator
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
,
194 if (opt_level
!= 0) {
197 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_PRIMARY
, &status
);
200 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_SECONDARY
,
204 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
207 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_QUATERNARY
,
211 ucol_setAttribute(collator
, UCOL_STRENGTH
, UCOL_IDENTICAL
,
215 fprintf(stderr
, "-level param must be between 1 and 5\n");
219 if (U_FAILURE(status
)) {
220 fprintf(stderr
, "Collator attribute setting failed.: %d\n", status
);
227 * Creates a string search
229 UBool
processStringSearch()
231 u_unescape(opt_source
, source
, 100);
232 u_unescape(opt_pattern
, pattern
, 100);
233 UErrorCode status
= U_ZERO_ERROR
;
234 search
= usearch_openFromCollator(pattern
, -1, source
, -1, collator
, NULL
,
236 if (U_FAILURE(status
)) {
239 if (opt_overlap
== TRUE
) {
240 usearch_setAttribute(search
, USEARCH_OVERLAP
, USEARCH_ON
, &status
);
242 if (opt_canonical
== TRUE
) {
243 usearch_setAttribute(search
, USEARCH_CANONICAL_MATCH
, USEARCH_ON
,
246 if (U_FAILURE(status
)) {
247 fprintf(stderr
, "Error setting search attributes\n");
255 UErrorCode status
= U_ZERO_ERROR
;
256 int32_t offset
= usearch_next(search
, &status
);
257 if (offset
== USEARCH_DONE
) {
258 fprintf(stdout
, "Pattern not found in source\n");
260 while (offset
!= USEARCH_DONE
) {
261 fprintf(stdout
, "Pattern found at offset %d size %d\n", offset
,
262 usearch_getMatchedLength(search
));
263 offset
= usearch_next(search
, &status
);
265 if (U_FAILURE(status
)) {
266 fprintf(stderr
, "Error in searching for pattern %d\n", status
);
269 fprintf(stdout
, "End of search\n");
274 * Main -- process command line, read in and pre-process the test file,
275 * call other functions to do the actual tests.
277 int main(int argc
, const char** argv
)
279 if (processOptions(argc
, argv
, opts
) != TRUE
|| opt_help
) {
284 if (processCollator() != TRUE
) {
285 fprintf(stderr
, "Error creating collator\n");
289 if (processStringSearch() != TRUE
) {
290 fprintf(stderr
, "Error creating string search\n");
294 fprintf(stdout
, "Finding pattern %s in source %s\n", opt_pattern
,
298 ucol_close(collator
);
299 usearch_close(search
);