Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/rep.h
48697 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3**************************************************************************4* Copyright (C) 1999-2012, International Business Machines Corporation and5* others. All Rights Reserved.6**************************************************************************7* Date Name Description8* 11/17/99 aliu Creation. Ported from java. Modified to9* match current UnicodeString API. Forced10* to use name "handleReplaceBetween" because11* of existing methods in UnicodeString.12**************************************************************************13*/1415#ifndef REP_H16#define REP_H1718#include "unicode/uobject.h"1920/**21* \file22* \brief C++ API: Replaceable String23*/2425U_NAMESPACE_BEGIN2627class UnicodeString;2829/**30* <code>Replaceable</code> is an abstract base class representing a31* string of characters that supports the replacement of a range of32* itself with a new string of characters. It is used by APIs that33* change a piece of text while retaining metadata. Metadata is data34* other than the Unicode characters returned by char32At(). One35* example of metadata is style attributes; another is an edit36* history, marking each character with an author and revision number.37*38* <p>An implicit aspect of the <code>Replaceable</code> API is that39* during a replace operation, new characters take on the metadata of40* the old characters. For example, if the string "the <b>bold</b>41* font" has range (4, 8) replaced with "strong", then it becomes "the42* <b>strong</b> font".43*44* <p><code>Replaceable</code> specifies ranges using a start45* offset and a limit offset. The range of characters thus specified46* includes the characters at offset start..limit-1. That is, the47* start offset is inclusive, and the limit offset is exclusive.48*49* <p><code>Replaceable</code> also includes API to access characters50* in the string: <code>length()</code>, <code>charAt()</code>,51* <code>char32At()</code>, and <code>extractBetween()</code>.52*53* <p>For a subclass to support metadata, typical behavior of54* <code>replace()</code> is the following:55* <ul>56* <li>Set the metadata of the new text to the metadata of the first57* character replaced</li>58* <li>If no characters are replaced, use the metadata of the59* previous character</li>60* <li>If there is no previous character (i.e. start == 0), use the61* following character</li>62* <li>If there is no following character (i.e. the replaceable was63* empty), use default metadata.<br>64* <li>If the code point U+FFFF is seen, it should be interpreted as65* a special marker having no metadata<li>66* </li>67* </ul>68* If this is not the behavior, the subclass should document any differences.69* @author Alan Liu70* @stable ICU 2.071*/72class U_COMMON_API Replaceable : public UObject {7374public:75/**76* Destructor.77* @stable ICU 2.078*/79virtual ~Replaceable();8081/**82* Returns the number of 16-bit code units in the text.83* @return number of 16-bit code units in text84* @stable ICU 1.885*/86inline int32_t length() const;8788/**89* Returns the 16-bit code unit at the given offset into the text.90* @param offset an integer between 0 and <code>length()</code>-191* inclusive92* @return 16-bit code unit of text at given offset93* @stable ICU 1.894*/95inline char16_t charAt(int32_t offset) const;9697/**98* Returns the 32-bit code point at the given 16-bit offset into99* the text. This assumes the text is stored as 16-bit code units100* with surrogate pairs intermixed. If the offset of a leading or101* trailing code unit of a surrogate pair is given, return the102* code point of the surrogate pair.103*104* @param offset an integer between 0 and <code>length()</code>-1105* inclusive106* @return 32-bit code point of text at given offset107* @stable ICU 1.8108*/109inline UChar32 char32At(int32_t offset) const;110111/**112* Copies characters in the range [<tt>start</tt>, <tt>limit</tt>)113* into the UnicodeString <tt>target</tt>.114* @param start offset of first character which will be copied115* @param limit offset immediately following the last character to116* be copied117* @param target UnicodeString into which to copy characters.118* @return A reference to <TT>target</TT>119* @stable ICU 2.1120*/121virtual void extractBetween(int32_t start,122int32_t limit,123UnicodeString& target) const = 0;124125/**126* Replaces a substring of this object with the given text. If the127* characters being replaced have metadata, the new characters128* that replace them should be given the same metadata.129*130* <p>Subclasses must ensure that if the text between start and131* limit is equal to the replacement text, that replace has no132* effect. That is, any metadata133* should be unaffected. In addition, subclasses are encouraged to134* check for initial and trailing identical characters, and make a135* smaller replacement if possible. This will preserve as much136* metadata as possible.137* @param start the beginning index, inclusive; <code>0 <= start138* <= limit</code>.139* @param limit the ending index, exclusive; <code>start <= limit140* <= length()</code>.141* @param text the text to replace characters <code>start</code>142* to <code>limit - 1</code>143* @stable ICU 2.0144*/145virtual void handleReplaceBetween(int32_t start,146int32_t limit,147const UnicodeString& text) = 0;148// Note: All other methods in this class take the names of149// existing UnicodeString methods. This method is the exception.150// It is named differently because all replace methods of151// UnicodeString return a UnicodeString&. The 'between' is152// required in order to conform to the UnicodeString naming153// convention; API taking start/length are named <operation>, and154// those taking start/limit are named <operationBetween>. The155// 'handle' is added because 'replaceBetween' and156// 'doReplaceBetween' are already taken.157158/**159* Copies a substring of this object, retaining metadata.160* This method is used to duplicate or reorder substrings.161* The destination index must not overlap the source range.162*163* @param start the beginning index, inclusive; <code>0 <= start <=164* limit</code>.165* @param limit the ending index, exclusive; <code>start <= limit <=166* length()</code>.167* @param dest the destination index. The characters from168* <code>start..limit-1</code> will be copied to <code>dest</code>.169* Implementations of this method may assume that <code>dest <= start ||170* dest >= limit</code>.171* @stable ICU 2.0172*/173virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0;174175/**176* Returns true if this object contains metadata. If a177* Replaceable object has metadata, calls to the Replaceable API178* must be made so as to preserve metadata. If it does not, calls179* to the Replaceable API may be optimized to improve performance.180* The default implementation returns true.181* @return true if this object contains metadata182* @stable ICU 2.2183*/184virtual UBool hasMetaData() const;185186/**187* Clone this object, an instance of a subclass of Replaceable.188* Clones can be used concurrently in multiple threads.189* If a subclass does not implement clone(), or if an error occurs,190* then NULL is returned.191* The clone functions in all subclasses return a pointer to a Replaceable192* because some compilers do not support covariant (same-as-this)193* return types; cast to the appropriate subclass if necessary.194* The caller must delete the clone.195*196* @return a clone of this object197*198* @see getDynamicClassID199* @stable ICU 2.6200*/201virtual Replaceable *clone() const;202203protected:204205/**206* Default constructor.207* @stable ICU 2.4208*/209inline Replaceable();210211/*212* Assignment operator not declared. The compiler will provide one213* which does nothing since this class does not contain any data members.214* API/code coverage may show the assignment operator as present and215* untested - ignore.216* Subclasses need this assignment operator if they use compiler-provided217* assignment operators of their own. An alternative to not declaring one218* here would be to declare and empty-implement a protected or public one.219Replaceable &Replaceable::operator=(const Replaceable &);220*/221222/**223* Virtual version of length().224* @stable ICU 2.4225*/226virtual int32_t getLength() const = 0;227228/**229* Virtual version of charAt().230* @stable ICU 2.4231*/232virtual char16_t getCharAt(int32_t offset) const = 0;233234/**235* Virtual version of char32At().236* @stable ICU 2.4237*/238virtual UChar32 getChar32At(int32_t offset) const = 0;239};240241inline Replaceable::Replaceable() {}242243inline int32_t244Replaceable::length() const {245return getLength();246}247248inline char16_t249Replaceable::charAt(int32_t offset) const {250return getCharAt(offset);251}252253inline UChar32254Replaceable::char32At(int32_t offset) const {255return getChar32At(offset);256}257258// There is no rep.cpp, see unistr.cpp for Replaceable function implementations.259260U_NAMESPACE_END261262#endif263264265