]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/quant.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 2001-2012, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 * Date Name Description
9 * 07/26/01 aliu Creation.
10 **********************************************************************
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_TRANSLITERATION
18 #include "unicode/unistr.h"
23 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Quantifier
)
25 Quantifier::Quantifier(UnicodeFunctor
*adoptedMatcher
,
26 uint32_t _minCount
, uint32_t _maxCount
) {
27 // assert(adopted != 0);
28 // assert(minCount <= maxCount);
29 matcher
= adoptedMatcher
;
30 this->minCount
= _minCount
;
31 this->maxCount
= _maxCount
;
34 Quantifier::Quantifier(const Quantifier
& o
) :
37 matcher(o
.matcher
->clone()),
43 Quantifier::~Quantifier() {
48 * Implement UnicodeFunctor
50 Quantifier
* Quantifier::clone() const {
51 return new Quantifier(*this);
55 * UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
56 * and return the pointer.
58 UnicodeMatcher
* Quantifier::toMatcher() const {
59 Quantifier
*nonconst_this
= const_cast<Quantifier
*>(this);
60 UnicodeMatcher
*nonconst_base
= static_cast<UnicodeMatcher
*>(nonconst_this
);
65 UMatchDegree
Quantifier::matches(const Replaceable
& text
,
69 int32_t start
= offset
;
71 while (count
< maxCount
) {
73 UMatchDegree m
= matcher
->toMatcher()->matches(text
, offset
, limit
, incremental
);
77 // If offset has not moved we have a zero-width match.
78 // Don't keep matching it infinitely.
81 } else if (incremental
&& m
== U_PARTIAL_MATCH
) {
82 return U_PARTIAL_MATCH
;
87 if (incremental
&& offset
== limit
) {
88 return U_PARTIAL_MATCH
;
90 if (count
>= minCount
) {
98 * Implement UnicodeMatcher
100 UnicodeString
& Quantifier::toPattern(UnicodeString
& result
,
101 UBool escapeUnprintable
) const {
103 matcher
->toMatcher()->toPattern(result
, escapeUnprintable
);
106 return result
.append((UChar
)63); /*?*/
107 } else if (maxCount
== MAX
) {
108 return result
.append((UChar
)42); /***/
111 } else if (minCount
== 1 && maxCount
== MAX
) {
112 return result
.append((UChar
)43); /*+*/
114 result
.append((UChar
)123); /*{*/
115 ICU_Utility::appendNumber(result
, minCount
);
116 result
.append((UChar
)44); /*,*/
117 if (maxCount
!= MAX
) {
118 ICU_Utility::appendNumber(result
, maxCount
);
120 result
.append((UChar
)125); /*}*/
125 * Implement UnicodeMatcher
127 UBool
Quantifier::matchesIndexValue(uint8_t v
) const {
128 return (minCount
== 0) || matcher
->toMatcher()->matchesIndexValue(v
);
132 * Implement UnicodeMatcher
134 void Quantifier::addMatchSetTo(UnicodeSet
& toUnionTo
) const {
136 matcher
->toMatcher()->addMatchSetTo(toUnionTo
);
141 * Implement UnicodeFunctor
143 void Quantifier::setData(const TransliterationRuleData
* d
) {
149 #endif /* #if !UCONFIG_NO_TRANSLITERATION */