sl@0: /* sl@0: ************************************************************************** sl@0: * Copyright (C) 1999-2005, International Business Machines Corporation and sl@0: * others. All Rights Reserved. sl@0: ************************************************************************** sl@0: * Date Name Description sl@0: * 11/17/99 aliu Creation. Ported from java. Modified to sl@0: * match current UnicodeString API. Forced sl@0: * to use name "handleReplaceBetween" because sl@0: * of existing methods in UnicodeString. sl@0: ************************************************************************** sl@0: */ sl@0: sl@0: #ifndef REP_H sl@0: #define REP_H sl@0: sl@0: #include "unicode/uobject.h" sl@0: sl@0: /** sl@0: * \file sl@0: * \brief C++ API: Replaceable String sl@0: */ sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: class UnicodeString; sl@0: sl@0: /** sl@0: * Replaceable is an abstract base class representing a sl@0: * string of characters that supports the replacement of a range of sl@0: * itself with a new string of characters. It is used by APIs that sl@0: * change a piece of text while retaining metadata. Metadata is data sl@0: * other than the Unicode characters returned by char32At(). One sl@0: * example of metadata is style attributes; another is an edit sl@0: * history, marking each character with an author and revision number. sl@0: * sl@0: *

An implicit aspect of the Replaceable API is that sl@0: * during a replace operation, new characters take on the metadata of sl@0: * the old characters. For example, if the string "the bold sl@0: * font" has range (4, 8) replaced with "strong", then it becomes "the sl@0: * strong font". sl@0: * sl@0: *

Replaceable specifies ranges using a start sl@0: * offset and a limit offset. The range of characters thus specified sl@0: * includes the characters at offset start..limit-1. That is, the sl@0: * start offset is inclusive, and the limit offset is exclusive. sl@0: * sl@0: *

Replaceable also includes API to access characters sl@0: * in the string: length(), charAt(), sl@0: * char32At(), and extractBetween(). sl@0: * sl@0: *

For a subclass to support metadata, typical behavior of sl@0: * replace() is the following: sl@0: *

sl@0: * If this is not the behavior, the subclass should document any differences. sl@0: * @author Alan Liu sl@0: * @stable ICU 2.0 sl@0: */ sl@0: class U_COMMON_API Replaceable : public UObject { sl@0: sl@0: public: sl@0: /** sl@0: * Destructor. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual ~Replaceable(); sl@0: sl@0: /** sl@0: * Returns the number of 16-bit code units in the text. sl@0: * @return number of 16-bit code units in text sl@0: * @stable ICU 1.8 sl@0: */ sl@0: inline int32_t length() const; sl@0: sl@0: /** sl@0: * Returns the 16-bit code unit at the given offset into the text. sl@0: * @param offset an integer between 0 and length()-1 sl@0: * inclusive sl@0: * @return 16-bit code unit of text at given offset sl@0: * @stable ICU 1.8 sl@0: */ sl@0: inline UChar charAt(int32_t offset) const; sl@0: sl@0: /** sl@0: * Returns the 32-bit code point at the given 16-bit offset into sl@0: * the text. This assumes the text is stored as 16-bit code units sl@0: * with surrogate pairs intermixed. If the offset of a leading or sl@0: * trailing code unit of a surrogate pair is given, return the sl@0: * code point of the surrogate pair. sl@0: * sl@0: * @param offset an integer between 0 and length()-1 sl@0: * inclusive sl@0: * @return 32-bit code point of text at given offset sl@0: * @stable ICU 1.8 sl@0: */ sl@0: inline UChar32 char32At(int32_t offset) const; sl@0: sl@0: /** sl@0: * Copies characters in the range [start, limit) sl@0: * into the UnicodeString target. sl@0: * @param start offset of first character which will be copied sl@0: * @param limit offset immediately following the last character to sl@0: * be copied sl@0: * @param target UnicodeString into which to copy characters. sl@0: * @return A reference to target sl@0: * @stable ICU 2.1 sl@0: */ sl@0: virtual void extractBetween(int32_t start, sl@0: int32_t limit, sl@0: UnicodeString& target) const = 0; sl@0: sl@0: /** sl@0: * Replaces a substring of this object with the given text. If the sl@0: * characters being replaced have metadata, the new characters sl@0: * that replace them should be given the same metadata. sl@0: * sl@0: *

Subclasses must ensure that if the text between start and sl@0: * limit is equal to the replacement text, that replace has no sl@0: * effect. That is, any metadata sl@0: * should be unaffected. In addition, subclasses are encouraged to sl@0: * check for initial and trailing identical characters, and make a sl@0: * smaller replacement if possible. This will preserve as much sl@0: * metadata as possible. sl@0: * @param start the beginning index, inclusive; 0 <= start sl@0: * <= limit. sl@0: * @param limit the ending index, exclusive; start <= limit sl@0: * <= length(). sl@0: * @param text the text to replace characters start sl@0: * to limit - 1 sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual void handleReplaceBetween(int32_t start, sl@0: int32_t limit, sl@0: const UnicodeString& text) = 0; sl@0: // Note: All other methods in this class take the names of sl@0: // existing UnicodeString methods. This method is the exception. sl@0: // It is named differently because all replace methods of sl@0: // UnicodeString return a UnicodeString&. The 'between' is sl@0: // required in order to conform to the UnicodeString naming sl@0: // convention; API taking start/length are named , and sl@0: // those taking start/limit are named . The sl@0: // 'handle' is added because 'replaceBetween' and sl@0: // 'doReplaceBetween' are already taken. sl@0: sl@0: /** sl@0: * Copies a substring of this object, retaining metadata. sl@0: * This method is used to duplicate or reorder substrings. sl@0: * The destination index must not overlap the source range. sl@0: * sl@0: * @param start the beginning index, inclusive; 0 <= start <= sl@0: * limit. sl@0: * @param limit the ending index, exclusive; start <= limit <= sl@0: * length(). sl@0: * @param dest the destination index. The characters from sl@0: * start..limit-1 will be copied to dest. sl@0: * Implementations of this method may assume that dest <= start || sl@0: * dest >= limit. sl@0: * @stable ICU 2.0 sl@0: */ sl@0: virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0; sl@0: sl@0: /** sl@0: * Returns true if this object contains metadata. If a sl@0: * Replaceable object has metadata, calls to the Replaceable API sl@0: * must be made so as to preserve metadata. If it does not, calls sl@0: * to the Replaceable API may be optimized to improve performance. sl@0: * The default implementation returns true. sl@0: * @return true if this object contains metadata sl@0: * @stable ICU 2.2 sl@0: */ sl@0: virtual UBool hasMetaData() const; sl@0: sl@0: /** sl@0: * Clone this object, an instance of a subclass of Replaceable. sl@0: * Clones can be used concurrently in multiple threads. sl@0: * If a subclass does not implement clone(), or if an error occurs, sl@0: * then NULL is returned. sl@0: * The clone functions in all subclasses return a pointer to a Replaceable sl@0: * because some compilers do not support covariant (same-as-this) sl@0: * return types; cast to the appropriate subclass if necessary. sl@0: * The caller must delete the clone. sl@0: * sl@0: * @return a clone of this object sl@0: * sl@0: * @see getDynamicClassID sl@0: * @stable ICU 2.6 sl@0: */ sl@0: virtual Replaceable *clone() const; sl@0: sl@0: protected: sl@0: sl@0: /** sl@0: * Default constructor. sl@0: * @stable ICU 2.4 sl@0: */ sl@0: Replaceable(); sl@0: sl@0: /* sl@0: * Assignment operator not declared. The compiler will provide one sl@0: * which does nothing since this class does not contain any data members. sl@0: * API/code coverage may show the assignment operator as present and sl@0: * untested - ignore. sl@0: * Subclasses need this assignment operator if they use compiler-provided sl@0: * assignment operators of their own. An alternative to not declaring one sl@0: * here would be to declare and empty-implement a protected or public one. sl@0: Replaceable &Replaceable::operator=(const Replaceable &); sl@0: */ sl@0: sl@0: /** sl@0: * Virtual version of length(). sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual int32_t getLength() const = 0; sl@0: sl@0: /** sl@0: * Virtual version of charAt(). sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual UChar getCharAt(int32_t offset) const = 0; sl@0: sl@0: /** sl@0: * Virtual version of char32At(). sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual UChar32 getChar32At(int32_t offset) const = 0; sl@0: }; sl@0: sl@0: inline int32_t sl@0: Replaceable::length() const { sl@0: return getLength(); sl@0: } sl@0: sl@0: inline UChar sl@0: Replaceable::charAt(int32_t offset) const { sl@0: return getCharAt(offset); sl@0: } sl@0: sl@0: inline UChar32 sl@0: Replaceable::char32At(int32_t offset) const { sl@0: return getChar32At(offset); sl@0: } sl@0: sl@0: // There is no rep.cpp, see unistr.cpp for Replaceable function implementations. sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #endif