]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/strmatch.cpp
2 **********************************************************************
3 * Copyright (c) 2001-2004, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 07/23/01 aliu Creation.
8 **********************************************************************
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_TRANSLITERATION
18 #include "unicode/uniset.h"
22 static const UChar EMPTY
[] = { 0 }; // empty string: ""
24 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringMatcher
)
26 StringMatcher::StringMatcher(const UnicodeString
& theString
,
30 const TransliterationRuleData
& theData
) :
32 segmentNumber(segmentNum
),
36 theString
.extractBetween(start
, limit
, pattern
);
39 StringMatcher::StringMatcher(const StringMatcher
& o
) :
45 segmentNumber(o
.segmentNumber
),
46 matchStart(o
.matchStart
),
47 matchLimit(o
.matchLimit
)
54 StringMatcher::~StringMatcher() {
58 * Implement UnicodeFunctor
60 UnicodeFunctor
* StringMatcher::clone() const {
61 return new StringMatcher(*this);
65 * UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
66 * and return the pointer.
68 UnicodeMatcher
* StringMatcher::toMatcher() const {
69 return (UnicodeMatcher
*) this;
73 * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
74 * and return the pointer.
76 UnicodeReplacer
* StringMatcher::toReplacer() const {
77 return (UnicodeReplacer
*) this;
81 * Implement UnicodeMatcher
83 UMatchDegree
StringMatcher::matches(const Replaceable
& text
,
88 int32_t cursor
= offset
;
90 // Match in the reverse direction
91 for (i
=pattern
.length()-1; i
>=0; --i
) {
92 UChar keyChar
= pattern
.charAt(i
);
93 UnicodeMatcher
* subm
= data
->lookupMatcher(keyChar
);
96 keyChar
== text
.charAt(cursor
)) {
103 subm
->matches(text
, cursor
, limit
, incremental
);
109 // Record the match position, but adjust for a normal
110 // forward start, limit, and only if a prior match does not
111 // exist -- we want the rightmost match.
112 if (matchStart
< 0) {
113 matchStart
= cursor
+1;
114 matchLimit
= offset
+1;
117 for (i
=0; i
<pattern
.length(); ++i
) {
118 if (incremental
&& cursor
== limit
) {
119 // We've reached the context limit without a mismatch and
120 // without completing our match.
121 return U_PARTIAL_MATCH
;
123 UChar keyChar
= pattern
.charAt(i
);
124 UnicodeMatcher
* subm
= data
->lookupMatcher(keyChar
);
126 // Don't need the cursor < limit check if
127 // incremental is TRUE (because it's done above); do need
129 if (cursor
< limit
&&
130 keyChar
== text
.charAt(cursor
)) {
137 subm
->matches(text
, cursor
, limit
, incremental
);
143 // Record the match position
153 * Implement UnicodeMatcher
155 UnicodeString
& StringMatcher::toPattern(UnicodeString
& result
,
156 UBool escapeUnprintable
) const
159 UnicodeString str
, quoteBuf
;
160 if (segmentNumber
> 0) {
161 result
.append((UChar
)40); /*(*/
163 for (int32_t i
=0; i
<pattern
.length(); ++i
) {
164 UChar keyChar
= pattern
.charAt(i
);
165 const UnicodeMatcher
* m
= data
->lookupMatcher(keyChar
);
167 ICU_Utility::appendToRule(result
, keyChar
, FALSE
, escapeUnprintable
, quoteBuf
);
169 ICU_Utility::appendToRule(result
, m
->toPattern(str
, escapeUnprintable
),
170 TRUE
, escapeUnprintable
, quoteBuf
);
173 if (segmentNumber
> 0) {
174 result
.append((UChar
)41); /*)*/
176 // Flush quoteBuf out to result
177 ICU_Utility::appendToRule(result
, -1,
178 TRUE
, escapeUnprintable
, quoteBuf
);
183 * Implement UnicodeMatcher
185 UBool
StringMatcher::matchesIndexValue(uint8_t v
) const {
186 if (pattern
.length() == 0) {
189 UChar32 c
= pattern
.char32At(0);
190 const UnicodeMatcher
*m
= data
->lookupMatcher(c
);
191 return (m
== 0) ? ((c
& 0xFF) == v
) : m
->matchesIndexValue(v
);
195 * Implement UnicodeMatcher
197 void StringMatcher::addMatchSetTo(UnicodeSet
& toUnionTo
) const {
199 for (int32_t i
=0; i
<pattern
.length(); i
+=UTF_CHAR_LENGTH(ch
)) {
200 ch
= pattern
.char32At(i
);
201 const UnicodeMatcher
* matcher
= data
->lookupMatcher(ch
);
202 if (matcher
== NULL
) {
205 matcher
->addMatchSetTo(toUnionTo
);
211 * UnicodeReplacer API
213 int32_t StringMatcher::replace(Replaceable
& text
,
216 int32_t& /*cursor*/) {
220 // Copy segment with out-of-band data
221 int32_t dest
= limit
;
222 // If there was no match, that means that a quantifier
223 // matched zero-length. E.g., x (a)* y matched "xy".
224 if (matchStart
>= 0) {
225 if (matchStart
!= matchLimit
) {
226 text
.copy(matchStart
, matchLimit
, dest
);
227 outLen
= matchLimit
- matchStart
;
231 text
.handleReplaceBetween(start
, limit
, EMPTY
); // delete original text
237 * UnicodeReplacer API
239 UnicodeString
& StringMatcher::toReplacerPattern(UnicodeString
& rule
,
240 UBool
/*escapeUnprintable*/) const {
241 // assert(segmentNumber > 0);
243 rule
.append((UChar
)0x0024 /*$*/);
244 ICU_Utility::appendNumber(rule
, segmentNumber
, 10, 1);
249 * Remove any match info. This must be called before performing a
250 * set of matches with this segment.
252 void StringMatcher::resetMatch() {
253 matchStart
= matchLimit
= -1;
257 * Union the set of all characters that may output by this object
258 * into the given set.
259 * @param toUnionTo the set into which to union the output characters
261 void StringMatcher::addReplacementSetTo(UnicodeSet
& /*toUnionTo*/) const {
262 // The output of this replacer varies; it is the source text between
263 // matchStart and matchLimit. Since this varies depending on the
264 // input text, we can't compute it here. We can either do nothing
265 // or we can add ALL characters to the set. It's probably more useful
270 * Implement UnicodeFunctor
272 void StringMatcher::setData(const TransliterationRuleData
* d
) {
275 while (i
<pattern
.length()) {
276 UChar32 c
= pattern
.char32At(i
);
277 UnicodeFunctor
* f
= data
->lookup(c
);
281 i
+= UTF_CHAR_LENGTH(c
);
287 #endif /* #if !UCONFIG_NO_TRANSLITERATION */