2001-07-27 00:18:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001, International Business Machines Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 07/26/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "quant.h"
|
2001-07-30 23:23:16 +00:00
|
|
|
#include "unicode/unistr.h"
|
2001-11-20 00:07:11 +00:00
|
|
|
#include "util.h"
|
2001-07-27 00:18:53 +00:00
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
Quantifier::Quantifier(UnicodeMatcher *adopted,
|
2001-10-15 22:11:00 +00:00
|
|
|
uint32_t _minCount, uint32_t _maxCount) {
|
2001-07-27 00:18:53 +00:00
|
|
|
// assert(adopted != 0);
|
|
|
|
// assert(minCount <= maxCount);
|
|
|
|
matcher = adopted;
|
2001-10-15 22:11:00 +00:00
|
|
|
this->minCount = _minCount;
|
|
|
|
this->maxCount = _maxCount;
|
2001-07-27 00:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Quantifier::Quantifier(const Quantifier& o) :
|
2001-10-09 22:53:02 +00:00
|
|
|
UnicodeMatcher(o),
|
2001-07-27 00:18:53 +00:00
|
|
|
matcher(o.matcher->clone()),
|
|
|
|
minCount(o.minCount),
|
2001-10-09 22:53:02 +00:00
|
|
|
maxCount(o.maxCount)
|
|
|
|
{
|
2001-07-27 00:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Quantifier::~Quantifier() {
|
|
|
|
delete matcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UnicodeMatcher* Quantifier::clone() const {
|
|
|
|
return new Quantifier(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
UMatchDegree Quantifier::matches(const Replaceable& text,
|
|
|
|
int32_t& offset,
|
|
|
|
int32_t limit,
|
2001-10-30 23:55:09 +00:00
|
|
|
UBool incremental) {
|
2001-07-27 00:18:53 +00:00
|
|
|
int32_t start = offset;
|
|
|
|
uint32_t count = 0;
|
|
|
|
while (count < maxCount) {
|
2001-10-17 19:21:12 +00:00
|
|
|
int32_t pos = offset;
|
2001-07-27 00:18:53 +00:00
|
|
|
UMatchDegree m = matcher->matches(text, offset, limit, incremental);
|
|
|
|
if (m == U_MATCH) {
|
|
|
|
++count;
|
2001-10-17 19:21:12 +00:00
|
|
|
if (pos == offset) {
|
|
|
|
// If offset has not moved we have a zero-width match.
|
|
|
|
// Don't keep matching it infinitely.
|
|
|
|
break;
|
|
|
|
}
|
2001-07-27 00:18:53 +00:00
|
|
|
} else if (incremental && m == U_PARTIAL_MATCH) {
|
|
|
|
return U_PARTIAL_MATCH;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (incremental && offset == limit) {
|
|
|
|
return U_PARTIAL_MATCH;
|
|
|
|
}
|
|
|
|
if (count >= minCount) {
|
|
|
|
return U_MATCH;
|
|
|
|
}
|
|
|
|
offset = start;
|
|
|
|
return U_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UnicodeString& Quantifier::toPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const {
|
2001-12-04 05:25:39 +00:00
|
|
|
result.truncate(0);
|
2001-07-30 23:23:16 +00:00
|
|
|
matcher->toPattern(result, escapeUnprintable);
|
|
|
|
if (minCount == 0) {
|
|
|
|
if (maxCount == 1) {
|
|
|
|
return result.append((UChar)63); /*?*/
|
|
|
|
} else if (maxCount == MAX) {
|
|
|
|
return result.append((UChar)42); /***/
|
|
|
|
}
|
|
|
|
// else fall through
|
|
|
|
} else if (minCount == 1 && maxCount == MAX) {
|
|
|
|
return result.append((UChar)43); /*+*/
|
|
|
|
}
|
|
|
|
result.append((UChar)123); /*{*/
|
2001-12-03 20:19:08 +00:00
|
|
|
ICU_Utility::appendNumber(result, minCount);
|
2001-07-30 23:23:16 +00:00
|
|
|
result.append((UChar)44); /*,*/
|
|
|
|
if (maxCount != MAX) {
|
2001-12-03 20:19:08 +00:00
|
|
|
ICU_Utility::appendNumber(result, maxCount);
|
2001-07-30 23:23:16 +00:00
|
|
|
}
|
|
|
|
result.append((UChar)125); /*}*/
|
2001-07-27 00:18:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UBool Quantifier::matchesIndexValue(uint8_t v) const {
|
|
|
|
return (minCount == 0) || matcher->matchesIndexValue(v);
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
//eof
|