]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unifilt.cpp
ICU-400.42.tar.gz
[apple/icu.git] / icuSources / common / unifilt.cpp
1 /*
2 **********************************************************************
3 * Copyright (c) 2001-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
6 * Date Name Description
7 * 07/18/01 aliu Creation.
8 **********************************************************************
9 */
10
11 #include "unicode/unifilt.h"
12 #include "unicode/rep.h"
13
14 U_NAMESPACE_BEGIN
15 UOBJECT_DEFINE_ABSTRACT_RTTI_IMPLEMENTATION(UnicodeFilter)
16
17
18 /* Define this here due to the lack of another file.
19 It can't be defined in the header */
20 UnicodeMatcher::~UnicodeMatcher() {}
21
22 UnicodeFilter::~UnicodeFilter() {}
23
24 /**
25 * UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
26 * and return the pointer.
27 */
28 UnicodeMatcher* UnicodeFilter::toMatcher() const {
29 return (UnicodeMatcher*) this;
30 }
31
32 void UnicodeFilter::setData(const TransliterationRuleData*) {}
33
34 /**
35 * Default implementation of UnicodeMatcher::matches() for Unicode
36 * filters. Matches a single code point at offset (either one or
37 * two 16-bit code units).
38 */
39 UMatchDegree UnicodeFilter::matches(const Replaceable& text,
40 int32_t& offset,
41 int32_t limit,
42 UBool incremental) {
43 UChar32 c;
44 if (offset < limit &&
45 contains(c = text.char32At(offset))) {
46 offset += UTF_CHAR_LENGTH(c);
47 return U_MATCH;
48 }
49 if (offset > limit &&
50 contains(c = text.char32At(offset))) {
51 // Backup offset by 1, unless the preceding character is a
52 // surrogate pair -- then backup by 2 (keep offset pointing at
53 // the lead surrogate).
54 --offset;
55 if (offset >= 0) {
56 offset -= UTF_CHAR_LENGTH(text.char32At(offset)) - 1;
57 }
58 return U_MATCH;
59 }
60 if (incremental && offset == limit) {
61 return U_PARTIAL_MATCH;
62 }
63 return U_MISMATCH;
64 }
65
66 U_NAMESPACE_END
67
68 //eof