2001-07-27 00:18:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001, International Business Machines Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 07/23/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "strmatch.h"
|
|
|
|
#include "rbt_data.h"
|
2001-07-30 23:23:16 +00:00
|
|
|
#include "rbt_rule.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
|
|
|
StringMatcher::StringMatcher(const UnicodeString& theString,
|
|
|
|
int32_t start,
|
|
|
|
int32_t limit,
|
2001-07-30 23:23:16 +00:00
|
|
|
UBool isSeg,
|
2001-07-27 00:18:53 +00:00
|
|
|
const TransliterationRuleData& theData) :
|
2001-07-30 23:23:16 +00:00
|
|
|
data(theData),
|
2001-10-30 18:08:53 +00:00
|
|
|
isSegment(isSeg),
|
|
|
|
matchStart(-1),
|
|
|
|
matchLimit(-1)
|
2001-10-09 22:21:01 +00:00
|
|
|
{
|
2001-07-27 00:18:53 +00:00
|
|
|
theString.extractBetween(start, limit, pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringMatcher::StringMatcher(const StringMatcher& o) :
|
2001-10-09 22:21:01 +00:00
|
|
|
UnicodeMatcher(o),
|
2001-07-27 00:18:53 +00:00
|
|
|
pattern(o.pattern),
|
2001-10-09 22:21:01 +00:00
|
|
|
data(o.data),
|
2001-10-30 18:08:53 +00:00
|
|
|
isSegment(o.isSegment),
|
|
|
|
matchStart(o.matchStart),
|
|
|
|
matchLimit(o.matchStart)
|
2001-10-09 22:21:01 +00:00
|
|
|
{
|
2001-07-27 00:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
StringMatcher::~StringMatcher() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UnicodeMatcher* StringMatcher::clone() const {
|
|
|
|
return new StringMatcher(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UMatchDegree StringMatcher::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 i;
|
|
|
|
int32_t cursor = offset;
|
|
|
|
if (limit < cursor) {
|
2001-10-30 18:08:53 +00:00
|
|
|
// Match in the reverse direction
|
2001-07-27 00:18:53 +00:00
|
|
|
for (i=pattern.length()-1; i>=0; --i) {
|
|
|
|
UChar keyChar = pattern.charAt(i);
|
2001-10-30 23:55:09 +00:00
|
|
|
UnicodeMatcher* subm = data.lookup(keyChar);
|
2001-07-27 00:18:53 +00:00
|
|
|
if (subm == 0) {
|
|
|
|
if (cursor >= limit &&
|
|
|
|
keyChar == text.charAt(cursor)) {
|
|
|
|
--cursor;
|
|
|
|
} else {
|
|
|
|
return U_MISMATCH;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UMatchDegree m =
|
|
|
|
subm->matches(text, cursor, limit, incremental);
|
|
|
|
if (m != U_MATCH) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-30 18:08:53 +00:00
|
|
|
// Record the match position, but adjust for a normal
|
|
|
|
// forward start, limit, and only if a prior match does not
|
|
|
|
// exist -- we want the rightmost match.
|
|
|
|
if (matchStart < 0) {
|
2001-10-30 23:55:09 +00:00
|
|
|
matchStart = cursor+1;
|
|
|
|
matchLimit = offset+1;
|
2001-10-30 18:08:53 +00:00
|
|
|
}
|
2001-07-27 00:18:53 +00:00
|
|
|
} else {
|
|
|
|
for (i=0; i<pattern.length(); ++i) {
|
|
|
|
if (incremental && cursor == limit) {
|
|
|
|
// We've reached the context limit without a mismatch and
|
|
|
|
// without completing our match.
|
|
|
|
return U_PARTIAL_MATCH;
|
|
|
|
}
|
|
|
|
UChar keyChar = pattern.charAt(i);
|
2001-10-30 23:55:09 +00:00
|
|
|
UnicodeMatcher* subm = data.lookup(keyChar);
|
2001-07-27 00:18:53 +00:00
|
|
|
if (subm == 0) {
|
|
|
|
// Don't need the cursor < limit check if
|
|
|
|
// incremental is TRUE (because it's done above); do need
|
|
|
|
// it otherwise.
|
|
|
|
if (cursor < limit &&
|
|
|
|
keyChar == text.charAt(cursor)) {
|
|
|
|
++cursor;
|
|
|
|
} else {
|
|
|
|
return U_MISMATCH;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
UMatchDegree m =
|
|
|
|
subm->matches(text, cursor, limit, incremental);
|
|
|
|
if (m != U_MATCH) {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-10-30 18:08:53 +00:00
|
|
|
// Record the match position
|
2001-10-30 23:55:09 +00:00
|
|
|
matchStart = offset;
|
|
|
|
matchLimit = cursor;
|
2001-07-27 00:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
offset = cursor;
|
|
|
|
return U_MATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UnicodeString& StringMatcher::toPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const {
|
2001-07-30 23:23:16 +00:00
|
|
|
UnicodeString str, quoteBuf;
|
|
|
|
if (isSegment) {
|
|
|
|
result.append((UChar)40); /*(*/
|
|
|
|
}
|
2001-07-27 00:18:53 +00:00
|
|
|
for (int32_t i=0; i<pattern.length(); ++i) {
|
2001-07-30 23:23:16 +00:00
|
|
|
UChar keyChar = pattern.charAt(i);
|
|
|
|
const UnicodeMatcher* m = data.lookup(keyChar);
|
|
|
|
if (m == 0) {
|
|
|
|
TransliterationRule::appendToRule(result, keyChar, FALSE, escapeUnprintable, quoteBuf);
|
|
|
|
} else {
|
|
|
|
TransliterationRule::appendToRule(result, m->toPattern(str, escapeUnprintable),
|
|
|
|
TRUE, escapeUnprintable, quoteBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (isSegment) {
|
|
|
|
result.append((UChar)41); /*)*/
|
2001-07-27 00:18:53 +00:00
|
|
|
}
|
2001-07-30 23:23:16 +00:00
|
|
|
// Flush quoteBuf out to result
|
2001-10-30 18:08:53 +00:00
|
|
|
TransliterationRule::appendToRule(result, -1,
|
|
|
|
TRUE, escapeUnprintable, quoteBuf);
|
2001-07-27 00:18:53 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
UBool StringMatcher::matchesIndexValue(uint8_t v) const {
|
|
|
|
if (pattern.length() == 0) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
UChar32 c = pattern.char32At(0);
|
|
|
|
const UnicodeMatcher *m = data.lookup(c);
|
|
|
|
return (m == 0) ? ((c & 0xFF) == v) : m->matchesIndexValue(v);
|
|
|
|
}
|
|
|
|
|
2001-10-30 18:08:53 +00:00
|
|
|
/**
|
|
|
|
* Remove any match data. This must be called before performing a
|
|
|
|
* set of matches with this segment.
|
|
|
|
*/
|
|
|
|
void StringMatcher::resetMatch() {
|
|
|
|
matchStart = matchLimit = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the start offset, in the match text, of the <em>rightmost</em>
|
|
|
|
* match. This method may get moved up into the UnicodeMatcher if
|
|
|
|
* it turns out to be useful to generalize this.
|
|
|
|
*/
|
|
|
|
int32_t StringMatcher::getMatchStart() const {
|
|
|
|
return matchStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the limit offset, in the match text, of the <em>rightmost</em>
|
|
|
|
* match. This method may get moved up into the UnicodeMatcher if
|
|
|
|
* it turns out to be useful to generalize this.
|
|
|
|
*/
|
|
|
|
int32_t StringMatcher::getMatchLimit() const {
|
|
|
|
return matchLimit;
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
//eof
|
2001-10-08 23:26:58 +00:00
|
|
|
|