1
/**************************************************************************
3
* Copyright (C) 2001, International Business Machines
4
* Corporation and others. All Rights Reserved.
6
***************************************************************************
9
* created on: 2001June8
10
* created by: Helena Shih
12
* Sample code for the ICU Search C++ routines.
15
#include "unicode/utypes.h"
16
#include "unicode/unistr.h"
17
#include "unicode/locid.h"
23
UErrorCode status = U_ZERO_ERROR;
24
UnicodeString target("A quick fox jumped over the lazy dog.", "");
25
UnicodeString easyPatterns[] = {"FoX", "CAT", "jump", "under" };
26
int exactOffsets[] = { -1, -1, 12, -1 };
27
int tertiaryOffsets[] = { 8, -1, 12, -1 };
28
UnicodeString monkeyTarget("abcdefgh");
29
UnicodeString monkeyTarget2("ijklmnop");
33
::StringSearch *searchIter = new ::StringSearch(easyPatterns[0], target, status);
34
fprintf(stdout, "\n");
35
if (U_FAILURE(status))
37
fprintf(stderr, "Failed to create a StringSearch object for the default locale.\n");
39
fprintf(stdout, "Try with default normalization mode and strength.\n");
43
status = U_ZERO_ERROR;
45
pos = searchIter->next();
46
if ( pos != exactOffsets[i] )
47
fprintf(stdout, "Exact match failed at the index %d pattern.\n", i);
54
searchIter->setPattern(easyPatterns[i], status);
55
if (U_FAILURE(status))
57
fprintf(stderr, "Failed to set a pattern for %d element.\n", i);
61
fprintf(stdout, "Try now with strength == primary.\n");
62
status = U_ZERO_ERROR;
63
searchIter->setStrength(Collator::PRIMARY, status);
64
if (U_FAILURE(status))
66
fprintf(stderr, "Failed to set strength of the string search object.\n");
69
searchIter->setPattern(easyPatterns[0], status);
70
if (U_FAILURE(status))
72
fprintf(stderr, "Failed to set a pattern for the first element.\n");
74
pos = searchIter->first();
75
if (pos != tertiaryOffsets[0])
76
fprintf(stdout, "Tertiary match failed at the first pattern.\n");
77
for (i = 1; i < 4; i++)
79
status = U_ZERO_ERROR;
80
searchIter->setPattern(easyPatterns[i], status);
82
pos = searchIter->next();
83
if (pos != tertiaryOffsets[i])
84
fprintf(stdout, "Tertiary match failed at index %d pattern.\n", i);
88
searchIter->setPattern(easyPatterns[--i], status);
89
if (U_FAILURE(status))
91
fprintf(stderr, "Failed to set a pattern for the last element.\n");
93
pos = searchIter->last();
94
if (pos != tertiaryOffsets[i])
95
fprintf(stdout, "Tertiary match failed at the last pattern.\n");
98
status = U_ZERO_ERROR;
99
searchIter->setPattern(easyPatterns[i-1], status);
101
pos = searchIter->previous();
102
if (pos != tertiaryOffsets[i-1])
103
fprintf(stdout, "Walking backwards: tertiary match failed at index %d pattern.\n", i);
105
status = U_ZERO_ERROR;
106
searchIter->setTarget(monkeyTarget);
107
if (U_FAILURE(status))
109
fprintf(stderr, "Failed to set a pattern for the monkey target.\n");
112
searchIter->setStrength(Collator::TERTIARY, status);
113
// change direction again
115
searchIter->setPattern(monkeyTarget, status);
116
if (U_FAILURE(status))
118
fprintf(stderr, "Failed to set a pattern as monkey test itself.\n");
120
pos = searchIter->first();
122
fprintf(stdout, "Matching monkey test itself failed.\n");
123
for (i = 0; i < monkeyTarget.length() - 1; i++)
125
// will always find its substring
126
for (j = i+1; j < monkeyTarget.length(); j++)
129
status = U_ZERO_ERROR;
131
monkeyTarget.extract(i, j, temp);
132
searchIter->setPattern(temp, status);
133
if (U_FAILURE(status))
135
fprintf(stderr, "Failed to set a pattern for the %d -th monkey pattern of length %d.\n", i, j);
138
pos = searchIter->next();
140
fprintf(stdout, "Monkey match failed at index %d in monkey pattern of length %d.\n", i, j);
143
status = U_ZERO_ERROR;
144
searchIter->setTarget(monkeyTarget2);
145
if (U_FAILURE(status))
147
fprintf(stderr, "Failed to set a pattern for the monkey target2.\n");
150
for (i = 0; i < monkeyTarget.length() - 1; i++)
152
// will never find the match
154
status = U_ZERO_ERROR;
155
monkeyTarget.extract(i, monkeyTarget.length(), temp);
157
searchIter->setPattern(temp, status);
158
if (U_FAILURE(status))
160
fprintf(stderr, "Failed to set a pattern for the monkey pattern at offset index %d.\n", i);
163
pos = searchIter->next();
165
fprintf(stdout, "Monkey mismatch failed at index %d in monkey pattern.\n", i);