]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/strmatch.cpp
2 **********************************************************************
3 * Copyright (c) 2001-2011, 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"
19 #include "unicode/utf16.h"
23 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(StringMatcher
)
25 StringMatcher::StringMatcher(const UnicodeString
& theString
,
29 const TransliterationRuleData
& theData
) :
31 segmentNumber(segmentNum
),
35 theString
.extractBetween(start
, limit
, pattern
);
38 StringMatcher::StringMatcher(const StringMatcher
& o
) :
44 segmentNumber(o
.segmentNumber
),
45 matchStart(o
.matchStart
),
46 matchLimit(o
.matchLimit
)
53 StringMatcher::~StringMatcher() {
57 * Implement UnicodeFunctor
59 UnicodeFunctor
* StringMatcher::clone() const {
60 return new StringMatcher(*this);
64 * UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
65 * and return the pointer.
67 UnicodeMatcher
* StringMatcher::toMatcher() const {
68 return (UnicodeMatcher
*) this;
72 * UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
73 * and return the pointer.
75 UnicodeReplacer
* StringMatcher::toReplacer() const {
76 return (UnicodeReplacer
*) this;
80 * Implement UnicodeMatcher
82 UMatchDegree
StringMatcher::matches(const Replaceable
& text
,
87 int32_t cursor
= offset
;
89 // Match in the reverse direction
90 for (i
=pattern
.length()-1; i
>=0; --i
) {
91 UChar keyChar
= pattern
.charAt(i
);
92 UnicodeMatcher
* subm
= data
->lookupMatcher(keyChar
);
95 keyChar
== text
.charAt(cursor
)) {
102 subm
->matches(text
, cursor
, limit
, incremental
);
108 // Record the match position, but adjust for a normal
109 // forward start, limit, and only if a prior match does not
110 // exist -- we want the rightmost match.
111 if (matchStart
< 0) {
112 matchStart
= cursor
+1;
113 matchLimit
= offset
+1;
116 for (i
=0; i
<pattern
.length(); ++i
) {
117 if (incremental
&& cursor
== limit
) {
118 // We've reached the context limit without a mismatch and
119 // without completing our match.
120 return U_PARTIAL_MATCH
;
122 UChar keyChar
= pattern
.charAt(i
);
123 UnicodeMatcher
* subm
= data
->lookupMatcher(keyChar
);
125 // Don't need the cursor < limit check if
126 // incremental is TRUE (because it's done above); do need
128 if (cursor
< limit
&&
129 keyChar
== text
.charAt(cursor
)) {
136 subm
->matches(text
, cursor
, limit
, incremental
);
142 // Record the match position
152 * Implement UnicodeMatcher
154 UnicodeString
& StringMatcher::toPattern(UnicodeString
& result
,
155 UBool escapeUnprintable
) const
158 UnicodeString str
, quoteBuf
;
159 if (segmentNumber
> 0) {
160 result
.append((UChar
)40); /*(*/
162 for (int32_t i
=0; i
<pattern
.length(); ++i
) {
163 UChar keyChar
= pattern
.charAt(i
);
164 const UnicodeMatcher
* m
= data
->lookupMatcher(keyChar
);
166 ICU_Utility::appendToRule(result
, keyChar
, FALSE
, escapeUnprintable
, quoteBuf
);
168 ICU_Utility::appendToRule(result
, m
->toPattern(str
, escapeUnprintable
),
169 TRUE
, escapeUnprintable
, quoteBuf
);
172 if (segmentNumber
> 0) {
173 result
.append((UChar
)41); /*)*/
175 // Flush quoteBuf out to result
176 ICU_Utility::appendToRule(result
, -1,
177 TRUE
, escapeUnprintable
, quoteBuf
);
182 * Implement UnicodeMatcher
184 UBool
StringMatcher::matchesIndexValue(uint8_t v
) const {
185 if (pattern
.length() == 0) {
188 UChar32 c
= pattern
.char32At(0);
189 const UnicodeMatcher
*m
= data
->lookupMatcher(c
);
190 return (m
== 0) ? ((c
& 0xFF) == v
) : m
->matchesIndexValue(v
);
194 * Implement UnicodeMatcher
196 void StringMatcher::addMatchSetTo(UnicodeSet
& toUnionTo
) const {
198 for (int32_t i
=0; i
<pattern
.length(); i
+=U16_LENGTH(ch
)) {
199 ch
= pattern
.char32At(i
);
200 const UnicodeMatcher
* matcher
= data
->lookupMatcher(ch
);
201 if (matcher
== NULL
) {
204 matcher
->addMatchSetTo(toUnionTo
);
210 * UnicodeReplacer API
212 int32_t StringMatcher::replace(Replaceable
& text
,
215 int32_t& /*cursor*/) {
219 // Copy segment with out-of-band data
220 int32_t dest
= limit
;
221 // If there was no match, that means that a quantifier
222 // matched zero-length. E.g., x (a)* y matched "xy".
223 if (matchStart
>= 0) {
224 if (matchStart
!= matchLimit
) {
225 text
.copy(matchStart
, matchLimit
, dest
);
226 outLen
= matchLimit
- matchStart
;
230 text
.handleReplaceBetween(start
, limit
, UnicodeString()); // delete original text
236 * UnicodeReplacer API
238 UnicodeString
& StringMatcher::toReplacerPattern(UnicodeString
& rule
,
239 UBool
/*escapeUnprintable*/) const {
240 // assert(segmentNumber > 0);
242 rule
.append((UChar
)0x0024 /*$*/);
243 ICU_Utility::appendNumber(rule
, segmentNumber
, 10, 1);
248 * Remove any match info. This must be called before performing a
249 * set of matches with this segment.
251 void StringMatcher::resetMatch() {
252 matchStart
= matchLimit
= -1;
256 * Union the set of all characters that may output by this object
257 * into the given set.
258 * @param toUnionTo the set into which to union the output characters
260 void StringMatcher::addReplacementSetTo(UnicodeSet
& /*toUnionTo*/) const {
261 // The output of this replacer varies; it is the source text between
262 // matchStart and matchLimit. Since this varies depending on the
263 // input text, we can't compute it here. We can either do nothing
264 // or we can add ALL characters to the set. It's probably more useful
269 * Implement UnicodeFunctor
271 void StringMatcher::setData(const TransliterationRuleData
* d
) {
274 while (i
<pattern
.length()) {
275 UChar32 c
= pattern
.char32At(i
);
276 UnicodeFunctor
* f
= data
->lookup(c
);
286 #endif /* #if !UCONFIG_NO_TRANSLITERATION */