2001-07-27 00:18:53 +00:00
|
|
|
/*
|
2004-10-29 23:42:49 +00:00
|
|
|
* Copyright (C) 2001-2004, International Business Machines Corporation
|
|
|
|
* and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
|
|
|
* Date Name Description
|
|
|
|
* 07/23/01 aliu Creation.
|
|
|
|
**********************************************************************
|
|
|
|
*/
|
2001-07-27 00:18:53 +00:00
|
|
|
#ifndef STRMATCH_H
|
|
|
|
#define STRMATCH_H
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_TRANSLITERATION
|
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
#include "unicode/unistr.h"
|
2002-02-07 01:07:55 +00:00
|
|
|
#include "unicode/unifunct.h"
|
2001-07-27 00:18:53 +00:00
|
|
|
#include "unicode/unimatch.h"
|
2002-02-07 01:07:55 +00:00
|
|
|
#include "unicode/unirepl.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
|
|
|
class TransliterationRuleData;
|
|
|
|
|
|
|
|
/**
|
2002-02-07 01:07:55 +00:00
|
|
|
* An object that matches a fixed input string, implementing the
|
|
|
|
* UnicodeMatcher API. This object also implements the
|
|
|
|
* UnicodeReplacer API, allowing it to emit the matched text as
|
|
|
|
* output. Since the match text may contain flexible match elements,
|
|
|
|
* such as UnicodeSets, the emitted text is not the match pattern, but
|
|
|
|
* instead a substring of the actual matched text. Following
|
|
|
|
* convention, the output text is the leftmost match seen up to this
|
|
|
|
* point.
|
|
|
|
*
|
|
|
|
* A StringMatcher may represent a segment, in which case it has a
|
|
|
|
* positive segment number. This affects how the matcher converts
|
|
|
|
* itself to a pattern but does not otherwise affect its function.
|
|
|
|
*
|
|
|
|
* A StringMatcher that is not a segment should not be used as a
|
|
|
|
* UnicodeReplacer.
|
2001-07-27 00:18:53 +00:00
|
|
|
*/
|
2002-02-07 01:07:55 +00:00
|
|
|
class StringMatcher : public UnicodeFunctor, public UnicodeMatcher, public UnicodeReplacer {
|
2001-07-27 00:18:53 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Construct a matcher that matches the given pattern string.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param string the pattern to be matched, possibly containing
|
2002-02-07 01:07:55 +00:00
|
|
|
* stand-ins that represent nested UnicodeMatcher objects.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param start inclusive start index of text to be replaced
|
|
|
|
* @param limit exclusive end index of text to be replaced;
|
|
|
|
* must be greater than or equal to start
|
2002-02-07 01:07:55 +00:00
|
|
|
* @param segmentNum the segment number from 1..n, or 0 if this is
|
|
|
|
* not a segment.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param data context object mapping stand-ins to
|
2002-02-07 01:07:55 +00:00
|
|
|
* UnicodeMatcher objects.
|
|
|
|
*/
|
2001-07-27 00:18:53 +00:00
|
|
|
StringMatcher(const UnicodeString& string,
|
|
|
|
int32_t start,
|
|
|
|
int32_t limit,
|
2002-02-07 01:07:55 +00:00
|
|
|
int32_t segmentNum,
|
2001-07-27 00:18:53 +00:00
|
|
|
const TransliterationRuleData& data);
|
|
|
|
|
2002-07-01 11:04:45 +00:00
|
|
|
/**
|
|
|
|
* Copy constructor
|
|
|
|
* @param o the object to be copied.
|
|
|
|
*/
|
2001-07-27 00:18:53 +00:00
|
|
|
StringMatcher(const StringMatcher& o);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor
|
|
|
|
*/
|
|
|
|
virtual ~StringMatcher();
|
|
|
|
|
|
|
|
/**
|
2002-02-07 01:07:55 +00:00
|
|
|
* Implement UnicodeFunctor
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return a copy of the object.
|
2001-07-27 00:18:53 +00:00
|
|
|
*/
|
2002-02-07 01:07:55 +00:00
|
|
|
virtual UnicodeFunctor* clone() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UnicodeFunctor API. Cast 'this' to a UnicodeMatcher* pointer
|
|
|
|
* and return the pointer.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return the UnicodeMatcher point.
|
2002-02-07 01:07:55 +00:00
|
|
|
*/
|
|
|
|
virtual UnicodeMatcher* toMatcher() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UnicodeFunctor API. Cast 'this' to a UnicodeReplacer* pointer
|
|
|
|
* and return the pointer.
|
2002-07-01 11:04:45 +00:00
|
|
|
* @return the UnicodeReplacer pointer.
|
2002-02-07 01:07:55 +00:00
|
|
|
*/
|
|
|
|
virtual UnicodeReplacer* toReplacer() const;
|
2001-07-27 00:18:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param text the text to be matched
|
|
|
|
* @param offset on input, the index into text at which to begin
|
|
|
|
* matching. On output, the limit of the matched text. The
|
|
|
|
* number of matched characters is the output value of offset
|
|
|
|
* minus the input value. Offset should always point to the
|
|
|
|
* HIGH SURROGATE (leading code unit) of a pair of surrogates,
|
|
|
|
* both on entry and upon return.
|
|
|
|
* @param limit the limit index of text to be matched. Greater
|
|
|
|
* than offset for a forward direction match, less than offset for
|
|
|
|
* a backward direction match. The last character to be
|
|
|
|
* considered for matching will be text.charAt(limit-1) in the
|
|
|
|
* forward direction or text.charAt(limit+1) in the backward
|
|
|
|
* direction.
|
|
|
|
* @param incremental if TRUE, then assume further characters may
|
|
|
|
* be inserted at limit and check for partial matching. Otherwise
|
|
|
|
* assume the text as given is complete.
|
|
|
|
* @return a match degree value indicating a full match, a partial
|
|
|
|
* match, or a mismatch. If incremental is FALSE then
|
|
|
|
* U_PARTIAL_MATCH should never be returned.
|
2001-07-27 00:18:53 +00:00
|
|
|
*/
|
|
|
|
virtual UMatchDegree 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
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
2002-07-01 11:04:45 +00:00
|
|
|
* @param result Output param to receive the pattern.
|
|
|
|
* @param escapeUnprintable if True then escape the unprintable characters.
|
|
|
|
* @return A reference to 'result'.
|
2001-07-27 00:18:53 +00:00
|
|
|
*/
|
|
|
|
virtual UnicodeString& toPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable = FALSE) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
2002-07-01 11:04:45 +00:00
|
|
|
* Returns TRUE if this matcher will match a character c, where c
|
|
|
|
* & 0xFF == v, at offset, in the forward direction (with limit >
|
|
|
|
* offset). This is used by <tt>RuleBasedTransliterator</tt> for
|
|
|
|
* indexing.
|
|
|
|
* @param v the given value
|
|
|
|
* @return TRUE if this matcher will match a character c,
|
|
|
|
* where c & 0xFF == v
|
2001-07-27 00:18:53 +00:00
|
|
|
*/
|
|
|
|
virtual UBool matchesIndexValue(uint8_t v) const;
|
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Implement UnicodeMatcher
|
|
|
|
*/
|
|
|
|
virtual void addMatchSetTo(UnicodeSet& toUnionTo) const;
|
|
|
|
|
2002-03-20 00:42:02 +00:00
|
|
|
/**
|
|
|
|
* Implement UnicodeFunctor
|
|
|
|
*/
|
|
|
|
virtual void setData(const TransliterationRuleData*);
|
|
|
|
|
2001-10-30 18:08:53 +00:00
|
|
|
/**
|
2002-02-07 01:07:55 +00:00
|
|
|
* Replace characters in 'text' from 'start' to 'limit' with the
|
|
|
|
* output text of this object. Update the 'cursor' parameter to
|
|
|
|
* give the cursor position and return the length of the
|
|
|
|
* replacement text.
|
|
|
|
*
|
|
|
|
* @param text the text to be matched
|
|
|
|
* @param start inclusive start index of text to be replaced
|
|
|
|
* @param limit exclusive end index of text to be replaced;
|
|
|
|
* must be greater than or equal to start
|
|
|
|
* @param cursor output parameter for the cursor position.
|
|
|
|
* Not all replacer objects will update this, but in a complete
|
|
|
|
* tree of replacer objects, representing the entire output side
|
|
|
|
* of a transliteration rule, at least one must update it.
|
|
|
|
* @return the number of 16-bit code units in the text replacing
|
|
|
|
* the characters at offsets start..(limit-1) in text
|
2001-10-30 18:08:53 +00:00
|
|
|
*/
|
2002-02-07 01:07:55 +00:00
|
|
|
virtual int32_t replace(Replaceable& text,
|
|
|
|
int32_t start,
|
|
|
|
int32_t limit,
|
|
|
|
int32_t& cursor);
|
2001-10-30 18:08:53 +00:00
|
|
|
|
|
|
|
/**
|
2002-02-07 01:07:55 +00:00
|
|
|
* Returns a string representation of this replacer. If the
|
|
|
|
* result of calling this function is passed to the appropriate
|
|
|
|
* parser, typically TransliteratorParser, it will produce another
|
|
|
|
* replacer that is equal to this one.
|
|
|
|
* @param result the string to receive the pattern. Previous
|
|
|
|
* contents will be deleted.
|
|
|
|
* @param escapeUnprintable if TRUE then convert unprintable
|
|
|
|
* character to their hex escape representations, \\uxxxx or
|
|
|
|
* \\Uxxxxxxxx. Unprintable characters are defined by
|
|
|
|
* Utility.isUnprintable().
|
|
|
|
* @return a reference to 'result'.
|
2001-10-30 18:08:53 +00:00
|
|
|
*/
|
2002-02-07 01:07:55 +00:00
|
|
|
virtual UnicodeString& toReplacerPattern(UnicodeString& result,
|
|
|
|
UBool escapeUnprintable) const;
|
2001-10-30 18:08:53 +00:00
|
|
|
|
|
|
|
/**
|
2002-02-07 01:07:55 +00:00
|
|
|
* Remove any match data. This must be called before performing a
|
|
|
|
* set of matches with this segment.
|
2001-10-30 18:08:53 +00:00
|
|
|
*/
|
2002-02-07 01:07:55 +00:00
|
|
|
void resetMatch();
|
2001-10-30 18:08:53 +00:00
|
|
|
|
2002-06-29 00:04:16 +00:00
|
|
|
/**
|
|
|
|
* ICU "poor man's RTTI", returns a UClassID for the actual class.
|
|
|
|
*
|
|
|
|
* @draft ICU 2.2
|
|
|
|
*/
|
2003-08-31 20:53:46 +00:00
|
|
|
virtual UClassID getDynamicClassID() const;
|
2002-06-29 00:04:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ICU "poor man's RTTI", returns a UClassID for this class.
|
|
|
|
*
|
|
|
|
* @draft ICU 2.2
|
|
|
|
*/
|
2004-08-24 17:38:33 +00:00
|
|
|
static UClassID U_EXPORT2 getStaticClassID();
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2002-06-28 21:13:54 +00:00
|
|
|
/**
|
|
|
|
* Union the set of all characters that may output by this object
|
|
|
|
* into the given set.
|
|
|
|
* @param toUnionTo the set into which to union the output characters
|
|
|
|
*/
|
|
|
|
virtual void addReplacementSetTo(UnicodeSet& toUnionTo) const;
|
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
private:
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* The text to be matched.
|
|
|
|
*/
|
2001-07-27 00:18:53 +00:00
|
|
|
UnicodeString pattern;
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Context object that maps stand-ins to matcher and replacer
|
|
|
|
* objects.
|
|
|
|
*/
|
2002-03-20 00:42:02 +00:00
|
|
|
const TransliterationRuleData* data;
|
2001-07-30 23:23:16 +00:00
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* The segment number, 1-based, or 0 if not a segment.
|
|
|
|
*/
|
|
|
|
int32_t segmentNumber;
|
2001-10-30 18:08:53 +00:00
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Start offset, in the match text, of the <em>rightmost</em>
|
|
|
|
* match.
|
|
|
|
*/
|
2001-10-30 18:08:53 +00:00
|
|
|
int32_t matchStart;
|
|
|
|
|
2002-02-07 01:07:55 +00:00
|
|
|
/**
|
|
|
|
* Limit offset, in the match text, of the <em>rightmost</em>
|
|
|
|
* match.
|
|
|
|
*/
|
2001-10-30 18:08:53 +00:00
|
|
|
int32_t matchLimit;
|
2002-06-29 00:04:16 +00:00
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
};
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2002-09-20 01:54:48 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_TRANSLITERATION */
|
|
|
|
|
2001-07-27 00:18:53 +00:00
|
|
|
#endif
|