Replaceable
An implicit aspect of the Replaceable API is that 44 * during a replace operation, new characters take on the metadata of 45 * the old characters. For example, if the string "the bold 46 * font" has range (4, 8) replaced with "strong", then it becomes "the 47 * strong font". 48 * 49 *
Replaceable specifies ranges using a start 50 * offset and a limit offset. The range of characters thus specified 51 * includes the characters at offset start..limit-1. That is, the 52 * start offset is inclusive, and the limit offset is exclusive. 53 * 54 *
Replaceable also includes API to access characters 55 * in the string: length(), charAt(), 56 * char32At(), and extractBetween(). 57 * 58 *
length()
charAt()
char32At()
extractBetween()
For a subclass to support metadata, typical behavior of 59 * replace() is the following: 60 *
replace()
Subclasses must ensure that if the text between start and 136 * limit is equal to the replacement text, that replace has no 137 * effect. That is, any metadata 138 * should be unaffected. In addition, subclasses are encouraged to 139 * check for initial and trailing identical characters, and make a 140 * smaller replacement if possible. This will preserve as much 141 * metadata as possible. 142 * @param start the beginning index, inclusive; 0 <= start 143 * <= limit. 144 * @param limit the ending index, exclusive; start <= limit 145 * <= length(). 146 * @param text the text to replace characters start 147 * to limit - 1 148 * @stable ICU 2.0 149 */ 150 virtual void handleReplaceBetween(int32_t start, 151 int32_t limit, 152 const UnicodeString& text) = 0; 153 // Note: All other methods in this class take the names of 154 // existing UnicodeString methods. This method is the exception. 155 // It is named differently because all replace methods of 156 // UnicodeString return a UnicodeString&. The 'between' is 157 // required in order to conform to the UnicodeString naming 158 // convention; API taking start/length are named , and 159 // those taking start/limit are named . The 160 // 'handle' is added because 'replaceBetween' and 161 // 'doReplaceBetween' are already taken. 162 163 /** 164 * Copies a substring of this object, retaining metadata. 165 * This method is used to duplicate or reorder substrings. 166 * The destination index must not overlap the source range. 167 * 168 * @param start the beginning index, inclusive; 0 <= start <= 169 * limit. 170 * @param limit the ending index, exclusive; start <= limit <= 171 * length(). 172 * @param dest the destination index. The characters from 173 * start..limit-1 will be copied to dest. 174 * Implementations of this method may assume that dest <= start || 175 * dest >= limit. 176 * @stable ICU 2.0 177 */ 178 virtual void copy(int32_t start, int32_t limit, int32_t dest) = 0; 179 180 /** 181 * Returns true if this object contains metadata. If a 182 * Replaceable object has metadata, calls to the Replaceable API 183 * must be made so as to preserve metadata. If it does not, calls 184 * to the Replaceable API may be optimized to improve performance. 185 * The default implementation returns true. 186 * @return true if this object contains metadata 187 * @stable ICU 2.2 188 */ 189 virtual UBool hasMetaData() const; 190 191 /** 192 * Clone this object, an instance of a subclass of Replaceable. 193 * Clones can be used concurrently in multiple threads. 194 * If a subclass does not implement clone(), or if an error occurs, 195 * then nullptr is returned. 196 * The caller must delete the clone. 197 * 198 * @return a clone of this object 199 * 200 * @see getDynamicClassID 201 * @stable ICU 2.6 202 */ 203 virtual Replaceable *clone() const; 204 205 protected: 206 207 /** 208 * Default constructor. 209 * @stable ICU 2.4 210 */ 211 inline Replaceable(); 212 213 /* 214 * Assignment operator not declared. The compiler will provide one 215 * which does nothing since this class does not contain any data members. 216 * API/code coverage may show the assignment operator as present and 217 * untested - ignore. 218 * Subclasses need this assignment operator if they use compiler-provided 219 * assignment operators of their own. An alternative to not declaring one 220 * here would be to declare and empty-implement a protected or public one. 221 Replaceable &Replaceable::operator=(const Replaceable &); 222 */ 223 224 /** 225 * Virtual version of length(). 226 * @stable ICU 2.4 227 */ 228 virtual int32_t getLength() const = 0; 229 230 /** 231 * Virtual version of charAt(). 232 * @stable ICU 2.4 233 */ 234 virtual char16_t getCharAt(int32_t offset) const = 0; 235 236 /** 237 * Virtual version of char32At(). 238 * @stable ICU 2.4 239 */ 240 virtual UChar32 getChar32At(int32_t offset) const = 0; 241 }; 242 243 inline Replaceable::Replaceable() {} 244 245 inline int32_t 246 Replaceable::length() const { 247 return getLength(); 248 } 249 250 inline char16_t 251 Replaceable::charAt(int32_t offset) const { 252 return getCharAt(offset); 253 } 254 255 inline UChar32 256 Replaceable::char32At(int32_t offset) const { 257 return getChar32At(offset); 258 } 259 260 // There is no rep.cpp, see unistr.cpp for Replaceable function implementations. 261 262 U_NAMESPACE_END 263 264 #endif /* U_SHOW_CPLUSPLUS_API */ 265 266 #endif
0 <= start 143 * <= limit
start <= limit 145 * <= length()
start
limit - 1
0 <= start <= 169 * limit
start <= limit <= 171 * length()
start..limit-1
dest
dest <= start || 175 * dest >= limit