intl/icu/source/common/unicode/rep.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 **************************************************************************
michael@0 3 * Copyright (C) 1999-2012, International Business Machines Corporation and
michael@0 4 * others. All Rights Reserved.
michael@0 5 **************************************************************************
michael@0 6 * Date Name Description
michael@0 7 * 11/17/99 aliu Creation. Ported from java. Modified to
michael@0 8 * match current UnicodeString API. Forced
michael@0 9 * to use name "handleReplaceBetween" because
michael@0 10 * of existing methods in UnicodeString.
michael@0 11 **************************************************************************
michael@0 12 */
michael@0 13
michael@0 14 #ifndef REP_H
michael@0 15 #define REP_H
michael@0 16
michael@0 17 #include "unicode/uobject.h"
michael@0 18
michael@0 19 /**
michael@0 20 * \file
michael@0 21 * \brief C++ API: Replaceable String
michael@0 22 */
michael@0 23
michael@0 24 U_NAMESPACE_BEGIN
michael@0 25
michael@0 26 class UnicodeString;
michael@0 27
michael@0 28 /**
michael@0 29 * <code>Replaceable</code> is an abstract base class representing a
michael@0 30 * string of characters that supports the replacement of a range of
michael@0 31 * itself with a new string of characters. It is used by APIs that
michael@0 32 * change a piece of text while retaining metadata. Metadata is data
michael@0 33 * other than the Unicode characters returned by char32At(). One
michael@0 34 * example of metadata is style attributes; another is an edit
michael@0 35 * history, marking each character with an author and revision number.
michael@0 36 *
michael@0 37 * <p>An implicit aspect of the <code>Replaceable</code> API is that
michael@0 38 * during a replace operation, new characters take on the metadata of
michael@0 39 * the old characters. For example, if the string "the <b>bold</b>
michael@0 40 * font" has range (4, 8) replaced with "strong", then it becomes "the
michael@0 41 * <b>strong</b> font".
michael@0 42 *
michael@0 43 * <p><code>Replaceable</code> specifies ranges using a start
michael@0 44 * offset and a limit offset. The range of characters thus specified
michael@0 45 * includes the characters at offset start..limit-1. That is, the
michael@0 46 * start offset is inclusive, and the limit offset is exclusive.
michael@0 47 *
michael@0 48 * <p><code>Replaceable</code> also includes API to access characters
michael@0 49 * in the string: <code>length()</code>, <code>charAt()</code>,
michael@0 50 * <code>char32At()</code>, and <code>extractBetween()</code>.
michael@0 51 *
michael@0 52 * <p>For a subclass to support metadata, typical behavior of
michael@0 53 * <code>replace()</code> is the following:
michael@0 54 * <ul>
michael@0 55 * <li>Set the metadata of the new text to the metadata of the first
michael@0 56 * character replaced</li>
michael@0 57 * <li>If no characters are replaced, use the metadata of the
michael@0 58 * previous character</li>
michael@0 59 * <li>If there is no previous character (i.e. start == 0), use the
michael@0 60 * following character</li>
michael@0 61 * <li>If there is no following character (i.e. the replaceable was
michael@0 62 * empty), use default metadata.<br>
michael@0 63 * <li>If the code point U+FFFF is seen, it should be interpreted as
michael@0 64 * a special marker having no metadata<li>
michael@0 65 * </li>
michael@0 66 * </ul>
michael@0 67 * If this is not the behavior, the subclass should document any differences.
michael@0 68 * @author Alan Liu
michael@0 69 * @stable ICU 2.0
michael@0 70 */
michael@0 71 class U_COMMON_API Replaceable : public UObject {
michael@0 72
michael@0 73 public:
michael@0 74 /**
michael@0 75 * Destructor.
michael@0 76 * @stable ICU 2.0
michael@0 77 */
michael@0 78 virtual ~Replaceable();
michael@0 79
michael@0 80 /**
michael@0 81 * Returns the number of 16-bit code units in the text.
michael@0 82 * @return number of 16-bit code units in text
michael@0 83 * @stable ICU 1.8
michael@0 84 */
michael@0 85 inline int32_t length() const;
michael@0 86
michael@0 87 /**
michael@0 88 * Returns the 16-bit code unit at the given offset into the text.
michael@0 89 * @param offset an integer between 0 and <code>length()</code>-1
michael@0 90 * inclusive
michael@0 91 * @return 16-bit code unit of text at given offset
michael@0 92 * @stable ICU 1.8
michael@0 93 */
michael@0 94 inline UChar charAt(int32_t offset) const;
michael@0 95
michael@0 96 /**
michael@0 97 * Returns the 32-bit code point at the given 16-bit offset into
michael@0 98 * the text. This assumes the text is stored as 16-bit code units
michael@0 99 * with surrogate pairs intermixed. If the offset of a leading or
michael@0 100 * trailing code unit of a surrogate pair is given, return the
michael@0 101 * code point of the surrogate pair.
michael@0 102 *
michael@0 103 * @param offset an integer between 0 and <code>length()</code>-1
michael@0 104 * inclusive
michael@0 105 * @return 32-bit code point of text at given offset
michael@0 106 * @stable ICU 1.8
michael@0 107 */
michael@0 108 inline UChar32 char32At(int32_t offset) const;
michael@0 109
michael@0 110 /**
michael@0 111 * Copies characters in the range [<tt>start</tt>, <tt>limit</tt>)
michael@0 112 * into the UnicodeString <tt>target</tt>.
michael@0 113 * @param start offset of first character which will be copied
michael@0 114 * @param limit offset immediately following the last character to
michael@0 115 * be copied
michael@0 116 * @param target UnicodeString into which to copy characters.
michael@0 117 * @return A reference to <TT>target</TT>
michael@0 118 * @stable ICU 2.1
michael@0 119 */
michael@0 120 virtual void extractBetween(int32_t start,
michael@0 121 int32_t limit,
michael@0 122 UnicodeString& target) const = 0;
michael@0 123
michael@0 124 /**
michael@0 125 * Replaces a substring of this object with the given text. If the
michael@0 126 * characters being replaced have metadata, the new characters
michael@0 127 * that replace them should be given the same metadata.
michael@0 128 *
michael@0 129 * <p>Subclasses must ensure that if the text between start and
michael@0 130 * limit is equal to the replacement text, that replace has no
michael@0 131 * effect. That is, any metadata
michael@0 132 * should be unaffected. In addition, subclasses are encouraged to
michael@0 133 * check for initial and trailing identical characters, and make a
michael@0 134 * smaller replacement if possible. This will preserve as much
michael@0 135 * metadata as possible.
michael@0 136 * @param start the beginning index, inclusive; <code>0 <= start
michael@0 137 * <= limit</code>.
michael@0 138 * @param limit the ending index, exclusive; <code>start <= limit
michael@0 139 * <= length()</code>.
michael@0 140 * @param text the text to replace characters <code>start</code>
michael@0 141 * to <code>limit - 1</code>
michael@0 142 * @stable ICU 2.0
michael@0 143 */
michael@0 144 virtual void handleReplaceBetween(int32_t start,
michael@0 145 int32_t limit,
michael@0 146 const UnicodeString& text) = 0;
michael@0 147 // Note: All other methods in this class take the names of
michael@0 148 // existing UnicodeString methods. This method is the exception.
michael@0 149 // It is named differently because all replace methods of
michael@0 150 // UnicodeString return a UnicodeString&. The 'between' is
michael@0 151 // required in order to conform to the UnicodeString naming
michael@0 152 // convention; API taking start/length are named <operation>, and
michael@0 153 // those taking start/limit are named <operationBetween>. The
michael@0 154 // 'handle' is added because 'replaceBetween' and
michael@0 155 // 'doReplaceBetween' are already taken.
michael@0 156
michael@0 157 /**
michael@0 158 * Copies a substring of this object, retaining metadata.
michael@0 159 * This method is used to duplicate or reorder substrings.
michael@0 160 * The destination index must not overlap the source range.
michael@0 161 *
michael@0 162 * @param start the beginning index, inclusive; <code>0 <= start <=
michael@0 163 * limit</code>.
michael@0 164 * @param limit the ending index, exclusive; <code>start <= limit <=
michael@0 165 * length()</code>.
michael@0 166 * @param dest the destination index. The characters from
michael@0 167 * <code>start..limit-1</code> will be copied to <code>dest</code>.
michael@0 168 * Implementations of this method may assume that <code>dest <= start ||
michael@0 169 * dest >= limit</code>.
michael@0 170 * @stable ICU 2.0
michael@0 171 */
michael@0 172 virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0;
michael@0 173
michael@0 174 /**
michael@0 175 * Returns true if this object contains metadata. If a
michael@0 176 * Replaceable object has metadata, calls to the Replaceable API
michael@0 177 * must be made so as to preserve metadata. If it does not, calls
michael@0 178 * to the Replaceable API may be optimized to improve performance.
michael@0 179 * The default implementation returns true.
michael@0 180 * @return true if this object contains metadata
michael@0 181 * @stable ICU 2.2
michael@0 182 */
michael@0 183 virtual UBool hasMetaData() const;
michael@0 184
michael@0 185 /**
michael@0 186 * Clone this object, an instance of a subclass of Replaceable.
michael@0 187 * Clones can be used concurrently in multiple threads.
michael@0 188 * If a subclass does not implement clone(), or if an error occurs,
michael@0 189 * then NULL is returned.
michael@0 190 * The clone functions in all subclasses return a pointer to a Replaceable
michael@0 191 * because some compilers do not support covariant (same-as-this)
michael@0 192 * return types; cast to the appropriate subclass if necessary.
michael@0 193 * The caller must delete the clone.
michael@0 194 *
michael@0 195 * @return a clone of this object
michael@0 196 *
michael@0 197 * @see getDynamicClassID
michael@0 198 * @stable ICU 2.6
michael@0 199 */
michael@0 200 virtual Replaceable *clone() const;
michael@0 201
michael@0 202 protected:
michael@0 203
michael@0 204 /**
michael@0 205 * Default constructor.
michael@0 206 * @stable ICU 2.4
michael@0 207 */
michael@0 208 inline Replaceable();
michael@0 209
michael@0 210 /*
michael@0 211 * Assignment operator not declared. The compiler will provide one
michael@0 212 * which does nothing since this class does not contain any data members.
michael@0 213 * API/code coverage may show the assignment operator as present and
michael@0 214 * untested - ignore.
michael@0 215 * Subclasses need this assignment operator if they use compiler-provided
michael@0 216 * assignment operators of their own. An alternative to not declaring one
michael@0 217 * here would be to declare and empty-implement a protected or public one.
michael@0 218 Replaceable &Replaceable::operator=(const Replaceable &);
michael@0 219 */
michael@0 220
michael@0 221 /**
michael@0 222 * Virtual version of length().
michael@0 223 * @stable ICU 2.4
michael@0 224 */
michael@0 225 virtual int32_t getLength() const = 0;
michael@0 226
michael@0 227 /**
michael@0 228 * Virtual version of charAt().
michael@0 229 * @stable ICU 2.4
michael@0 230 */
michael@0 231 virtual UChar getCharAt(int32_t offset) const = 0;
michael@0 232
michael@0 233 /**
michael@0 234 * Virtual version of char32At().
michael@0 235 * @stable ICU 2.4
michael@0 236 */
michael@0 237 virtual UChar32 getChar32At(int32_t offset) const = 0;
michael@0 238 };
michael@0 239
michael@0 240 inline Replaceable::Replaceable() {}
michael@0 241
michael@0 242 inline int32_t
michael@0 243 Replaceable::length() const {
michael@0 244 return getLength();
michael@0 245 }
michael@0 246
michael@0 247 inline UChar
michael@0 248 Replaceable::charAt(int32_t offset) const {
michael@0 249 return getCharAt(offset);
michael@0 250 }
michael@0 251
michael@0 252 inline UChar32
michael@0 253 Replaceable::char32At(int32_t offset) const {
michael@0 254 return getChar32At(offset);
michael@0 255 }
michael@0 256
michael@0 257 // There is no rep.cpp, see unistr.cpp for Replaceable function implementations.
michael@0 258
michael@0 259 U_NAMESPACE_END
michael@0 260
michael@0 261 #endif

mercurial