intl/icu/source/i18n/quant.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 **********************************************************************
michael@0 3 * Copyright (C) 2001-2012, International Business Machines
michael@0 4 * Corporation and others. All Rights Reserved.
michael@0 5 **********************************************************************
michael@0 6 * Date Name Description
michael@0 7 * 07/26/01 aliu Creation.
michael@0 8 **********************************************************************
michael@0 9 */
michael@0 10
michael@0 11 #include "unicode/utypes.h"
michael@0 12
michael@0 13 #if !UCONFIG_NO_TRANSLITERATION
michael@0 14
michael@0 15 #include "quant.h"
michael@0 16 #include "unicode/unistr.h"
michael@0 17 #include "util.h"
michael@0 18
michael@0 19 U_NAMESPACE_BEGIN
michael@0 20
michael@0 21 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(Quantifier)
michael@0 22
michael@0 23 Quantifier::Quantifier(UnicodeFunctor *adoptedMatcher,
michael@0 24 uint32_t _minCount, uint32_t _maxCount) {
michael@0 25 // assert(adopted != 0);
michael@0 26 // assert(minCount <= maxCount);
michael@0 27 matcher = adoptedMatcher;
michael@0 28 this->minCount = _minCount;
michael@0 29 this->maxCount = _maxCount;
michael@0 30 }
michael@0 31
michael@0 32 Quantifier::Quantifier(const Quantifier& o) :
michael@0 33 UnicodeFunctor(o),
michael@0 34 UnicodeMatcher(o),
michael@0 35 matcher(o.matcher->clone()),
michael@0 36 minCount(o.minCount),
michael@0 37 maxCount(o.maxCount)
michael@0 38 {
michael@0 39 }
michael@0 40
michael@0 41 Quantifier::~Quantifier() {
michael@0 42 delete matcher;
michael@0 43 }
michael@0 44
michael@0 45 /**
michael@0 46 * Implement UnicodeFunctor
michael@0 47 */
michael@0 48 UnicodeFunctor* Quantifier::clone() const {
michael@0 49 return new Quantifier(*this);
michael@0 50 }
michael@0 51
michael@0 52 /**
michael@0 53 * UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
michael@0 54 * and return the pointer.
michael@0 55 */
michael@0 56 UnicodeMatcher* Quantifier::toMatcher() const {
michael@0 57 Quantifier *nonconst_this = const_cast<Quantifier *>(this);
michael@0 58 UnicodeMatcher *nonconst_base = static_cast<UnicodeMatcher *>(nonconst_this);
michael@0 59
michael@0 60 return nonconst_base;
michael@0 61 }
michael@0 62
michael@0 63 UMatchDegree Quantifier::matches(const Replaceable& text,
michael@0 64 int32_t& offset,
michael@0 65 int32_t limit,
michael@0 66 UBool incremental) {
michael@0 67 int32_t start = offset;
michael@0 68 uint32_t count = 0;
michael@0 69 while (count < maxCount) {
michael@0 70 int32_t pos = offset;
michael@0 71 UMatchDegree m = matcher->toMatcher()->matches(text, offset, limit, incremental);
michael@0 72 if (m == U_MATCH) {
michael@0 73 ++count;
michael@0 74 if (pos == offset) {
michael@0 75 // If offset has not moved we have a zero-width match.
michael@0 76 // Don't keep matching it infinitely.
michael@0 77 break;
michael@0 78 }
michael@0 79 } else if (incremental && m == U_PARTIAL_MATCH) {
michael@0 80 return U_PARTIAL_MATCH;
michael@0 81 } else {
michael@0 82 break;
michael@0 83 }
michael@0 84 }
michael@0 85 if (incremental && offset == limit) {
michael@0 86 return U_PARTIAL_MATCH;
michael@0 87 }
michael@0 88 if (count >= minCount) {
michael@0 89 return U_MATCH;
michael@0 90 }
michael@0 91 offset = start;
michael@0 92 return U_MISMATCH;
michael@0 93 }
michael@0 94
michael@0 95 /**
michael@0 96 * Implement UnicodeMatcher
michael@0 97 */
michael@0 98 UnicodeString& Quantifier::toPattern(UnicodeString& result,
michael@0 99 UBool escapeUnprintable) const {
michael@0 100 result.truncate(0);
michael@0 101 matcher->toMatcher()->toPattern(result, escapeUnprintable);
michael@0 102 if (minCount == 0) {
michael@0 103 if (maxCount == 1) {
michael@0 104 return result.append((UChar)63); /*?*/
michael@0 105 } else if (maxCount == MAX) {
michael@0 106 return result.append((UChar)42); /***/
michael@0 107 }
michael@0 108 // else fall through
michael@0 109 } else if (minCount == 1 && maxCount == MAX) {
michael@0 110 return result.append((UChar)43); /*+*/
michael@0 111 }
michael@0 112 result.append((UChar)123); /*{*/
michael@0 113 ICU_Utility::appendNumber(result, minCount);
michael@0 114 result.append((UChar)44); /*,*/
michael@0 115 if (maxCount != MAX) {
michael@0 116 ICU_Utility::appendNumber(result, maxCount);
michael@0 117 }
michael@0 118 result.append((UChar)125); /*}*/
michael@0 119 return result;
michael@0 120 }
michael@0 121
michael@0 122 /**
michael@0 123 * Implement UnicodeMatcher
michael@0 124 */
michael@0 125 UBool Quantifier::matchesIndexValue(uint8_t v) const {
michael@0 126 return (minCount == 0) || matcher->toMatcher()->matchesIndexValue(v);
michael@0 127 }
michael@0 128
michael@0 129 /**
michael@0 130 * Implement UnicodeMatcher
michael@0 131 */
michael@0 132 void Quantifier::addMatchSetTo(UnicodeSet& toUnionTo) const {
michael@0 133 if (maxCount > 0) {
michael@0 134 matcher->toMatcher()->addMatchSetTo(toUnionTo);
michael@0 135 }
michael@0 136 }
michael@0 137
michael@0 138 /**
michael@0 139 * Implement UnicodeFunctor
michael@0 140 */
michael@0 141 void Quantifier::setData(const TransliterationRuleData* d) {
michael@0 142 matcher->setData(d);
michael@0 143 }
michael@0 144
michael@0 145 U_NAMESPACE_END
michael@0 146
michael@0 147 #endif /* #if !UCONFIG_NO_TRANSLITERATION */
michael@0 148
michael@0 149 //eof

mercurial