Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/rep.h
38827 views
/*1**************************************************************************2* Copyright (C) 1999-2012, International Business Machines Corporation and3* others. All Rights Reserved.4**************************************************************************5* Date Name Description6* 11/17/99 aliu Creation. Ported from java. Modified to7* match current UnicodeString API. Forced8* to use name "handleReplaceBetween" because9* of existing methods in UnicodeString.10**************************************************************************11*/1213#ifndef REP_H14#define REP_H1516#include "unicode/uobject.h"1718/**19* \file20* \brief C++ API: Replaceable String21*/2223U_NAMESPACE_BEGIN2425class UnicodeString;2627/**28* <code>Replaceable</code> is an abstract base class representing a29* string of characters that supports the replacement of a range of30* itself with a new string of characters. It is used by APIs that31* change a piece of text while retaining metadata. Metadata is data32* other than the Unicode characters returned by char32At(). One33* example of metadata is style attributes; another is an edit34* history, marking each character with an author and revision number.35*36* <p>An implicit aspect of the <code>Replaceable</code> API is that37* during a replace operation, new characters take on the metadata of38* the old characters. For example, if the string "the <b>bold</b>39* font" has range (4, 8) replaced with "strong", then it becomes "the40* <b>strong</b> font".41*42* <p><code>Replaceable</code> specifies ranges using a start43* offset and a limit offset. The range of characters thus specified44* includes the characters at offset start..limit-1. That is, the45* start offset is inclusive, and the limit offset is exclusive.46*47* <p><code>Replaceable</code> also includes API to access characters48* in the string: <code>length()</code>, <code>charAt()</code>,49* <code>char32At()</code>, and <code>extractBetween()</code>.50*51* <p>For a subclass to support metadata, typical behavior of52* <code>replace()</code> is the following:53* <ul>54* <li>Set the metadata of the new text to the metadata of the first55* character replaced</li>56* <li>If no characters are replaced, use the metadata of the57* previous character</li>58* <li>If there is no previous character (i.e. start == 0), use the59* following character</li>60* <li>If there is no following character (i.e. the replaceable was61* empty), use default metadata.<br>62* <li>If the code point U+FFFF is seen, it should be interpreted as63* a special marker having no metadata<li>64* </li>65* </ul>66* If this is not the behavior, the subclass should document any differences.67* @author Alan Liu68* @stable ICU 2.069*/70class U_COMMON_API Replaceable : public UObject {7172public:73/**74* Destructor.75* @stable ICU 2.076*/77virtual ~Replaceable();7879/**80* Returns the number of 16-bit code units in the text.81* @return number of 16-bit code units in text82* @stable ICU 1.883*/84inline int32_t length() const;8586/**87* Returns the 16-bit code unit at the given offset into the text.88* @param offset an integer between 0 and <code>length()</code>-189* inclusive90* @return 16-bit code unit of text at given offset91* @stable ICU 1.892*/93inline UChar charAt(int32_t offset) const;9495/**96* Returns the 32-bit code point at the given 16-bit offset into97* the text. This assumes the text is stored as 16-bit code units98* with surrogate pairs intermixed. If the offset of a leading or99* trailing code unit of a surrogate pair is given, return the100* code point of the surrogate pair.101*102* @param offset an integer between 0 and <code>length()</code>-1103* inclusive104* @return 32-bit code point of text at given offset105* @stable ICU 1.8106*/107inline UChar32 char32At(int32_t offset) const;108109/**110* Copies characters in the range [<tt>start</tt>, <tt>limit</tt>)111* into the UnicodeString <tt>target</tt>.112* @param start offset of first character which will be copied113* @param limit offset immediately following the last character to114* be copied115* @param target UnicodeString into which to copy characters.116* @return A reference to <TT>target</TT>117* @stable ICU 2.1118*/119virtual void extractBetween(int32_t start,120int32_t limit,121UnicodeString& target) const = 0;122123/**124* Replaces a substring of this object with the given text. If the125* characters being replaced have metadata, the new characters126* that replace them should be given the same metadata.127*128* <p>Subclasses must ensure that if the text between start and129* limit is equal to the replacement text, that replace has no130* effect. That is, any metadata131* should be unaffected. In addition, subclasses are encouraged to132* check for initial and trailing identical characters, and make a133* smaller replacement if possible. This will preserve as much134* metadata as possible.135* @param start the beginning index, inclusive; <code>0 <= start136* <= limit</code>.137* @param limit the ending index, exclusive; <code>start <= limit138* <= length()</code>.139* @param text the text to replace characters <code>start</code>140* to <code>limit - 1</code>141* @stable ICU 2.0142*/143virtual void handleReplaceBetween(int32_t start,144int32_t limit,145const UnicodeString& text) = 0;146// Note: All other methods in this class take the names of147// existing UnicodeString methods. This method is the exception.148// It is named differently because all replace methods of149// UnicodeString return a UnicodeString&. The 'between' is150// required in order to conform to the UnicodeString naming151// convention; API taking start/length are named <operation>, and152// those taking start/limit are named <operationBetween>. The153// 'handle' is added because 'replaceBetween' and154// 'doReplaceBetween' are already taken.155156/**157* Copies a substring of this object, retaining metadata.158* This method is used to duplicate or reorder substrings.159* The destination index must not overlap the source range.160*161* @param start the beginning index, inclusive; <code>0 <= start <=162* limit</code>.163* @param limit the ending index, exclusive; <code>start <= limit <=164* length()</code>.165* @param dest the destination index. The characters from166* <code>start..limit-1</code> will be copied to <code>dest</code>.167* Implementations of this method may assume that <code>dest <= start ||168* dest >= limit</code>.169* @stable ICU 2.0170*/171virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0;172173/**174* Returns true if this object contains metadata. If a175* Replaceable object has metadata, calls to the Replaceable API176* must be made so as to preserve metadata. If it does not, calls177* to the Replaceable API may be optimized to improve performance.178* The default implementation returns true.179* @return true if this object contains metadata180* @stable ICU 2.2181*/182virtual UBool hasMetaData() const;183184/**185* Clone this object, an instance of a subclass of Replaceable.186* Clones can be used concurrently in multiple threads.187* If a subclass does not implement clone(), or if an error occurs,188* then NULL is returned.189* The clone functions in all subclasses return a pointer to a Replaceable190* because some compilers do not support covariant (same-as-this)191* return types; cast to the appropriate subclass if necessary.192* The caller must delete the clone.193*194* @return a clone of this object195*196* @see getDynamicClassID197* @stable ICU 2.6198*/199virtual Replaceable *clone() const;200201protected:202203/**204* Default constructor.205* @stable ICU 2.4206*/207inline Replaceable();208209/*210* Assignment operator not declared. The compiler will provide one211* which does nothing since this class does not contain any data members.212* API/code coverage may show the assignment operator as present and213* untested - ignore.214* Subclasses need this assignment operator if they use compiler-provided215* assignment operators of their own. An alternative to not declaring one216* here would be to declare and empty-implement a protected or public one.217Replaceable &Replaceable::operator=(const Replaceable &);218*/219220/**221* Virtual version of length().222* @stable ICU 2.4223*/224virtual int32_t getLength() const = 0;225226/**227* Virtual version of charAt().228* @stable ICU 2.4229*/230virtual UChar getCharAt(int32_t offset) const = 0;231232/**233* Virtual version of char32At().234* @stable ICU 2.4235*/236virtual UChar32 getChar32At(int32_t offset) const = 0;237};238239inline Replaceable::Replaceable() {}240241inline int32_t242Replaceable::length() const {243return getLength();244}245246inline UChar247Replaceable::charAt(int32_t offset) const {248return getCharAt(offset);249}250251inline UChar32252Replaceable::char32At(int32_t offset) const {253return getChar32At(offset);254}255256// There is no rep.cpp, see unistr.cpp for Replaceable function implementations.257258U_NAMESPACE_END259260#endif261262263