]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ********************************************************************** | |
2ca993e8 | 5 | * Copyright (C) 1998-2016, International Business Machines |
b75a7d8f A |
6 | * Corporation and others. All Rights Reserved. |
7 | ********************************************************************** | |
8 | * | |
9 | * File unistr.h | |
10 | * | |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 09/25/98 stephen Creation. | |
15 | * 11/11/98 stephen Changed per 11/9 code review. | |
16 | * 04/20/99 stephen Overhauled per 4/16 code review. | |
17 | * 11/18/99 aliu Made to inherit from Replaceable. Added method | |
18 | * handleReplaceBetween(); other methods unchanged. | |
19 | * 06/25/01 grhoten Remove dependency on iostream. | |
20 | ****************************************************************************** | |
21 | */ | |
22 | ||
23 | #ifndef UNISTR_H | |
24 | #define UNISTR_H | |
25 | ||
73c04bcf | 26 | /** |
2ca993e8 A |
27 | * \file |
28 | * \brief C++ API: Unicode String | |
73c04bcf A |
29 | */ |
30 | ||
f3c0d7a5 | 31 | #include <cstddef> |
729e4ab9 | 32 | #include "unicode/utypes.h" |
f3c0d7a5 | 33 | #include "unicode/char16ptr.h" |
b75a7d8f | 34 | #include "unicode/rep.h" |
729e4ab9 A |
35 | #include "unicode/std_string.h" |
36 | #include "unicode/stringpiece.h" | |
37 | #include "unicode/bytestream.h" | |
b75a7d8f A |
38 | |
39 | struct UConverter; // unicode/ucnv.h | |
b75a7d8f | 40 | |
b75a7d8f | 41 | #ifndef USTRING_H |
73c04bcf A |
42 | /** |
43 | * \ingroup ustring_ustrlen | |
44 | */ | |
374ca955 | 45 | U_STABLE int32_t U_EXPORT2 |
b75a7d8f A |
46 | u_strlen(const UChar *s); |
47 | #endif | |
48 | ||
f3c0d7a5 A |
49 | #if U_SHOW_CPLUSPLUS_API |
50 | U_NAMESPACE_BEGIN | |
4388f060 | 51 | |
f3c0d7a5 A |
52 | #if !UCONFIG_NO_BREAK_ITERATION |
53 | class BreakIterator; // unicode/brkiter.h | |
54 | #endif | |
55 | class Edits; | |
56 | ||
57 | U_NAMESPACE_END | |
58 | #endif // U_SHOW_CPLUSPLUS_API | |
59 | ||
60 | #if U_SHOW_CPLUSPLUS_API | |
61 | // Not #ifndef U_HIDE_INTERNAL_API because UnicodeString needs the UStringCaseMapper. | |
4388f060 A |
62 | /** |
63 | * Internal string case mapping function type. | |
f3c0d7a5 A |
64 | * All error checking must be done. |
65 | * src and dest must not overlap. | |
4388f060 A |
66 | * @internal |
67 | */ | |
68 | typedef int32_t U_CALLCONV | |
f3c0d7a5 A |
69 | UStringCaseMapper(int32_t caseLocale, uint32_t options, |
70 | #if !UCONFIG_NO_BREAK_ITERATION | |
71 | icu::BreakIterator *iter, | |
4388f060 | 72 | #endif |
f3c0d7a5 A |
73 | char16_t *dest, int32_t destCapacity, |
74 | const char16_t *src, int32_t srcLength, | |
75 | icu::Edits *edits, | |
76 | UErrorCode &errorCode); | |
77 | #endif // U_SHOW_CPLUSPLUS_API | |
4388f060 | 78 | |
f3c0d7a5 | 79 | #if U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
80 | U_NAMESPACE_BEGIN |
81 | ||
82 | class Locale; // unicode/locid.h | |
b75a7d8f | 83 | class StringCharacterIterator; |
4388f060 | 84 | class UnicodeStringAppendable; // unicode/appendable.h |
b75a7d8f A |
85 | |
86 | /* The <iostream> include has been moved to unicode/ustream.h */ | |
87 | ||
374ca955 A |
88 | /** |
89 | * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor | |
90 | * which constructs a Unicode string from an invariant-character char * string. | |
91 | * About invariant characters see utypes.h. | |
92 | * This constructor has no runtime dependency on conversion code and is | |
93 | * therefore recommended over ones taking a charset name string | |
94 | * (where the empty string "" indicates invariant-character conversion). | |
95 | * | |
73c04bcf | 96 | * @stable ICU 3.2 |
374ca955 | 97 | */ |
4388f060 | 98 | #define US_INV icu::UnicodeString::kInvariant |
374ca955 | 99 | |
b75a7d8f A |
100 | /** |
101 | * Unicode String literals in C++. | |
f3c0d7a5 A |
102 | * |
103 | * Note: these macros are not recommended for new code. | |
104 | * Prior to the availability of C++11 and u"unicode string literals", | |
105 | * these macros were provided for portability and efficiency when | |
106 | * initializing UnicodeStrings from literals. | |
107 | * | |
b75a7d8f A |
108 | * They work only for strings that contain "invariant characters", i.e., |
109 | * only latin letters, digits, and some punctuation. | |
110 | * See utypes.h for details. | |
111 | * | |
112 | * The string parameter must be a C string literal. | |
113 | * The length of the string, not including the terminating | |
114 | * <code>NUL</code>, must be specified as a constant. | |
b75a7d8f A |
115 | * @stable ICU 2.0 |
116 | */ | |
f3c0d7a5 A |
117 | #if !U_CHAR16_IS_TYPEDEF |
118 | # define UNICODE_STRING(cs, _length) icu::UnicodeString(TRUE, u ## cs, _length) | |
b75a7d8f | 119 | #else |
f3c0d7a5 | 120 | # define UNICODE_STRING(cs, _length) icu::UnicodeString(TRUE, (const char16_t*)u ## cs, _length) |
b75a7d8f A |
121 | #endif |
122 | ||
123 | /** | |
124 | * Unicode String literals in C++. | |
125 | * Dependent on the platform properties, different UnicodeString | |
126 | * constructors should be used to create a UnicodeString object from | |
127 | * a string literal. | |
128 | * The macros are defined for improved performance. | |
129 | * They work only for strings that contain "invariant characters", i.e., | |
130 | * only latin letters, digits, and some punctuation. | |
131 | * See utypes.h for details. | |
132 | * | |
133 | * The string parameter must be a C string literal. | |
134 | * @stable ICU 2.0 | |
135 | */ | |
46f4442e | 136 | #define UNICODE_STRING_SIMPLE(cs) UNICODE_STRING(cs, -1) |
b75a7d8f | 137 | |
4388f060 A |
138 | /** |
139 | * \def UNISTR_FROM_CHAR_EXPLICIT | |
140 | * This can be defined to be empty or "explicit". | |
f3c0d7a5 | 141 | * If explicit, then the UnicodeString(char16_t) and UnicodeString(UChar32) |
4388f060 | 142 | * constructors are marked as explicit, preventing their inadvertent use. |
51004dcb | 143 | * @stable ICU 49 |
4388f060 A |
144 | */ |
145 | #ifndef UNISTR_FROM_CHAR_EXPLICIT | |
146 | # if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION) | |
147 | // Auto-"explicit" in ICU library code. | |
148 | # define UNISTR_FROM_CHAR_EXPLICIT explicit | |
149 | # else | |
150 | // Empty by default for source code compatibility. | |
151 | # define UNISTR_FROM_CHAR_EXPLICIT | |
152 | # endif | |
153 | #endif | |
154 | ||
155 | /** | |
156 | * \def UNISTR_FROM_STRING_EXPLICIT | |
157 | * This can be defined to be empty or "explicit". | |
f3c0d7a5 | 158 | * If explicit, then the UnicodeString(const char *) and UnicodeString(const char16_t *) |
4388f060 A |
159 | * constructors are marked as explicit, preventing their inadvertent use. |
160 | * | |
161 | * In particular, this helps prevent accidentally depending on ICU conversion code | |
162 | * by passing a string literal into an API with a const UnicodeString & parameter. | |
51004dcb | 163 | * @stable ICU 49 |
4388f060 A |
164 | */ |
165 | #ifndef UNISTR_FROM_STRING_EXPLICIT | |
166 | # if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION) | |
167 | // Auto-"explicit" in ICU library code. | |
168 | # define UNISTR_FROM_STRING_EXPLICIT explicit | |
169 | # else | |
170 | // Empty by default for source code compatibility. | |
171 | # define UNISTR_FROM_STRING_EXPLICIT | |
172 | # endif | |
173 | #endif | |
174 | ||
2ca993e8 A |
175 | /** |
176 | * \def UNISTR_OBJECT_SIZE | |
177 | * Desired sizeof(UnicodeString) in bytes. | |
178 | * It should be a multiple of sizeof(pointer) to avoid unusable space for padding. | |
179 | * The object size may want to be a multiple of 16 bytes, | |
180 | * which is a common granularity for heap allocation. | |
181 | * | |
182 | * Any space inside the object beyond sizeof(vtable pointer) + 2 | |
183 | * is available for storing short strings inside the object. | |
184 | * The bigger the object, the longer a string that can be stored inside the object, | |
185 | * without additional heap allocation. | |
186 | * | |
187 | * Depending on a platform's pointer size, pointer alignment requirements, | |
188 | * and struct padding, the compiler will usually round up sizeof(UnicodeString) | |
189 | * to 4 * sizeof(pointer) (or 3 * sizeof(pointer) for P128 data models), | |
190 | * to hold the fields for heap-allocated strings. | |
191 | * Such a minimum size also ensures that the object is easily large enough | |
f3c0d7a5 | 192 | * to hold at least 2 char16_ts, for one supplementary code point (U16_MAX_LENGTH). |
2ca993e8 A |
193 | * |
194 | * sizeof(UnicodeString) >= 48 should work for all known platforms. | |
195 | * | |
196 | * For example, on a 64-bit machine where sizeof(vtable pointer) is 8, | |
197 | * sizeof(UnicodeString) = 64 would leave space for | |
198 | * (64 - sizeof(vtable pointer) - 2) / U_SIZEOF_UCHAR = (64 - 8 - 2) / 2 = 27 | |
f3c0d7a5 | 199 | * char16_ts stored inside the object. |
2ca993e8 A |
200 | * |
201 | * The minimum object size on a 64-bit machine would be | |
202 | * 4 * sizeof(pointer) = 4 * 8 = 32 bytes, | |
f3c0d7a5 | 203 | * and the internal buffer would hold up to 11 char16_ts in that case. |
2ca993e8 A |
204 | * |
205 | * @see U16_MAX_LENGTH | |
f3c0d7a5 | 206 | * @stable ICU 56 |
2ca993e8 A |
207 | */ |
208 | #ifndef UNISTR_OBJECT_SIZE | |
209 | # define UNISTR_OBJECT_SIZE 64 | |
210 | #endif | |
211 | ||
b75a7d8f A |
212 | /** |
213 | * UnicodeString is a string class that stores Unicode characters directly and provides | |
2ca993e8 | 214 | * similar functionality as the Java String and StringBuffer/StringBuilder classes. |
b75a7d8f A |
215 | * It is a concrete implementation of the abstract class Replaceable (for transliteration). |
216 | * | |
2ca993e8 A |
217 | * A UnicodeString may also "alias" an external array of characters |
218 | * (that is, point to it, rather than own the array) | |
219 | * whose lifetime must then at least match the lifetime of the aliasing object. | |
220 | * This aliasing may be preserved when returning a UnicodeString by value, | |
221 | * depending on the compiler and the function implementation, | |
222 | * via Return Value Optimization (RVO) or the move assignment operator. | |
223 | * (However, the copy assignment operator does not preserve aliasing.) | |
224 | * For details see the description of storage models at the end of the class API docs | |
225 | * and in the User Guide chapter linked from there. | |
226 | * | |
b75a7d8f A |
227 | * The UnicodeString class is not suitable for subclassing. |
228 | * | |
229 | * <p>For an overview of Unicode strings in C and C++ see the | |
2ca993e8 | 230 | * <a href="http://userguide.icu-project.org/strings#TOC-Strings-in-C-C-">User Guide Strings chapter</a>.</p> |
b75a7d8f A |
231 | * |
232 | * <p>In ICU, a Unicode string consists of 16-bit Unicode <em>code units</em>. | |
73c04bcf A |
233 | * A Unicode character may be stored with either one code unit |
234 | * (the most common case) or with a matched pair of special code units | |
f3c0d7a5 | 235 | * ("surrogates"). The data type for code units is char16_t. |
b75a7d8f A |
236 | * For single-character handling, a Unicode character code <em>point</em> is a value |
237 | * in the range 0..0x10ffff. ICU uses the UChar32 type for code points.</p> | |
238 | * | |
239 | * <p>Indexes and offsets into and lengths of strings always count code units, not code points. | |
240 | * This is the same as with multi-byte char* strings in traditional string handling. | |
241 | * Operations on partial strings typically do not test for code point boundaries. | |
242 | * If necessary, the user needs to take care of such boundaries by testing for the code unit | |
243 | * values or by using functions like | |
244 | * UnicodeString::getChar32Start() and UnicodeString::getChar32Limit() | |
245 | * (or, in C, the equivalent macros U16_SET_CP_START() and U16_SET_CP_LIMIT(), see utf.h).</p> | |
246 | * | |
247 | * UnicodeString methods are more lenient with regard to input parameter values | |
248 | * than other ICU APIs. In particular: | |
249 | * - If indexes are out of bounds for a UnicodeString object | |
250 | * (<0 or >length()) then they are "pinned" to the nearest boundary. | |
f3c0d7a5 | 251 | * - If primitive string pointer values (e.g., const char16_t * or char *) |
b75a7d8f A |
252 | * for input strings are NULL, then those input string parameters are treated |
253 | * as if they pointed to an empty string. | |
254 | * However, this is <em>not</em> the case for char * parameters for charset names | |
255 | * or other IDs. | |
256 | * - Most UnicodeString methods do not take a UErrorCode parameter because | |
257 | * there are usually very few opportunities for failure other than a shortage | |
258 | * of memory, error codes in low-level C++ string methods would be inconvenient, | |
259 | * and the error code as the last parameter (ICU convention) would prevent | |
260 | * the use of default parameter values. | |
261 | * Instead, such methods set the UnicodeString into a "bogus" state | |
262 | * (see isBogus()) if an error occurs. | |
263 | * | |
264 | * In string comparisons, two UnicodeString objects that are both "bogus" | |
265 | * compare equal (to be transitive and prevent endless loops in sorting), | |
266 | * and a "bogus" string compares less than any non-"bogus" one. | |
267 | * | |
268 | * Const UnicodeString methods are thread-safe. Multiple threads can use | |
269 | * const methods on the same UnicodeString object simultaneously, | |
270 | * but non-const methods must not be called concurrently (in multiple threads) | |
271 | * with any other (const or non-const) methods. | |
272 | * | |
273 | * Similarly, const UnicodeString & parameters are thread-safe. | |
274 | * One object may be passed in as such a parameter concurrently in multiple threads. | |
275 | * This includes the const UnicodeString & parameters for | |
276 | * copy construction, assignment, and cloning. | |
277 | * | |
278 | * <p>UnicodeString uses several storage methods. | |
279 | * String contents can be stored inside the UnicodeString object itself, | |
280 | * in an allocated and shared buffer, or in an outside buffer that is "aliased". | |
281 | * Most of this is done transparently, but careful aliasing in particular provides | |
282 | * significant performance improvements. | |
283 | * Also, the internal buffer is accessible via special functions. | |
284 | * For details see the | |
2ca993e8 | 285 | * <a href="http://userguide.icu-project.org/strings#TOC-Maximizing-Performance-with-the-UnicodeString-Storage-Model">User Guide Strings chapter</a>.</p> |
b75a7d8f A |
286 | * |
287 | * @see utf.h | |
288 | * @see CharacterIterator | |
289 | * @stable ICU 2.0 | |
290 | */ | |
291 | class U_COMMON_API UnicodeString : public Replaceable | |
292 | { | |
293 | public: | |
294 | ||
374ca955 A |
295 | /** |
296 | * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor | |
297 | * which constructs a Unicode string from an invariant-character char * string. | |
298 | * Use the macro US_INV instead of the full qualification for this value. | |
299 | * | |
300 | * @see US_INV | |
73c04bcf | 301 | * @stable ICU 3.2 |
374ca955 A |
302 | */ |
303 | enum EInvariant { | |
304 | /** | |
305 | * @see EInvariant | |
73c04bcf | 306 | * @stable ICU 3.2 |
374ca955 A |
307 | */ |
308 | kInvariant | |
309 | }; | |
310 | ||
b75a7d8f A |
311 | //======================================== |
312 | // Read-only operations | |
313 | //======================================== | |
314 | ||
315 | /* Comparison - bitwise only - for international comparison use collation */ | |
316 | ||
317 | /** | |
318 | * Equality operator. Performs only bitwise comparison. | |
319 | * @param text The UnicodeString to compare to this one. | |
320 | * @return TRUE if <TT>text</TT> contains the same characters as this one, | |
321 | * FALSE otherwise. | |
322 | * @stable ICU 2.0 | |
323 | */ | |
324 | inline UBool operator== (const UnicodeString& text) const; | |
325 | ||
326 | /** | |
327 | * Inequality operator. Performs only bitwise comparison. | |
328 | * @param text The UnicodeString to compare to this one. | |
329 | * @return FALSE if <TT>text</TT> contains the same characters as this one, | |
330 | * TRUE otherwise. | |
331 | * @stable ICU 2.0 | |
332 | */ | |
333 | inline UBool operator!= (const UnicodeString& text) const; | |
334 | ||
335 | /** | |
336 | * Greater than operator. Performs only bitwise comparison. | |
337 | * @param text The UnicodeString to compare to this one. | |
374ca955 A |
338 | * @return TRUE if the characters in this are bitwise |
339 | * greater than the characters in <code>text</code>, FALSE otherwise | |
b75a7d8f A |
340 | * @stable ICU 2.0 |
341 | */ | |
342 | inline UBool operator> (const UnicodeString& text) const; | |
343 | ||
344 | /** | |
345 | * Less than operator. Performs only bitwise comparison. | |
346 | * @param text The UnicodeString to compare to this one. | |
374ca955 A |
347 | * @return TRUE if the characters in this are bitwise |
348 | * less than the characters in <code>text</code>, FALSE otherwise | |
b75a7d8f A |
349 | * @stable ICU 2.0 |
350 | */ | |
351 | inline UBool operator< (const UnicodeString& text) const; | |
352 | ||
353 | /** | |
354 | * Greater than or equal operator. Performs only bitwise comparison. | |
355 | * @param text The UnicodeString to compare to this one. | |
374ca955 A |
356 | * @return TRUE if the characters in this are bitwise |
357 | * greater than or equal to the characters in <code>text</code>, FALSE otherwise | |
b75a7d8f A |
358 | * @stable ICU 2.0 |
359 | */ | |
360 | inline UBool operator>= (const UnicodeString& text) const; | |
361 | ||
362 | /** | |
363 | * Less than or equal operator. Performs only bitwise comparison. | |
364 | * @param text The UnicodeString to compare to this one. | |
374ca955 A |
365 | * @return TRUE if the characters in this are bitwise |
366 | * less than or equal to the characters in <code>text</code>, FALSE otherwise | |
b75a7d8f A |
367 | * @stable ICU 2.0 |
368 | */ | |
369 | inline UBool operator<= (const UnicodeString& text) const; | |
370 | ||
371 | /** | |
372 | * Compare the characters bitwise in this UnicodeString to | |
374ca955 | 373 | * the characters in <code>text</code>. |
b75a7d8f | 374 | * @param text The UnicodeString to compare to this one. |
374ca955 A |
375 | * @return The result of bitwise character comparison: 0 if this |
376 | * contains the same characters as <code>text</code>, -1 if the characters in | |
377 | * this are bitwise less than the characters in <code>text</code>, +1 if the | |
378 | * characters in this are bitwise greater than the characters | |
379 | * in <code>text</code>. | |
b75a7d8f A |
380 | * @stable ICU 2.0 |
381 | */ | |
382 | inline int8_t compare(const UnicodeString& text) const; | |
383 | ||
384 | /** | |
374ca955 A |
385 | * Compare the characters bitwise in the range |
386 | * [<TT>start</TT>, <TT>start + length</TT>) with the characters | |
57a6839d A |
387 | * in the <b>entire string</b> <TT>text</TT>. |
388 | * (The parameters "start" and "length" are not applied to the other text "text".) | |
b75a7d8f A |
389 | * @param start the offset at which the compare operation begins |
390 | * @param length the number of characters of text to compare. | |
374ca955 A |
391 | * @param text the other text to be compared against this string. |
392 | * @return The result of bitwise character comparison: 0 if this | |
393 | * contains the same characters as <code>text</code>, -1 if the characters in | |
394 | * this are bitwise less than the characters in <code>text</code>, +1 if the | |
395 | * characters in this are bitwise greater than the characters | |
396 | * in <code>text</code>. | |
b75a7d8f A |
397 | * @stable ICU 2.0 |
398 | */ | |
399 | inline int8_t compare(int32_t start, | |
400 | int32_t length, | |
374ca955 | 401 | const UnicodeString& text) const; |
b75a7d8f A |
402 | |
403 | /** | |
374ca955 A |
404 | * Compare the characters bitwise in the range |
405 | * [<TT>start</TT>, <TT>start + length</TT>) with the characters | |
406 | * in <TT>srcText</TT> in the range | |
407 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
408 | * @param start the offset at which the compare operation begins |
409 | * @param length the number of characters in this to compare. | |
410 | * @param srcText the text to be compared | |
411 | * @param srcStart the offset into <TT>srcText</TT> to start comparison | |
412 | * @param srcLength the number of characters in <TT>src</TT> to compare | |
374ca955 A |
413 | * @return The result of bitwise character comparison: 0 if this |
414 | * contains the same characters as <code>srcText</code>, -1 if the characters in | |
415 | * this are bitwise less than the characters in <code>srcText</code>, +1 if the | |
416 | * characters in this are bitwise greater than the characters | |
417 | * in <code>srcText</code>. | |
b75a7d8f A |
418 | * @stable ICU 2.0 |
419 | */ | |
420 | inline int8_t compare(int32_t start, | |
421 | int32_t length, | |
422 | const UnicodeString& srcText, | |
423 | int32_t srcStart, | |
424 | int32_t srcLength) const; | |
425 | ||
426 | /** | |
374ca955 | 427 | * Compare the characters bitwise in this UnicodeString with the first |
b75a7d8f A |
428 | * <TT>srcLength</TT> characters in <TT>srcChars</TT>. |
429 | * @param srcChars The characters to compare to this UnicodeString. | |
430 | * @param srcLength the number of characters in <TT>srcChars</TT> to compare | |
374ca955 A |
431 | * @return The result of bitwise character comparison: 0 if this |
432 | * contains the same characters as <code>srcChars</code>, -1 if the characters in | |
433 | * this are bitwise less than the characters in <code>srcChars</code>, +1 if the | |
434 | * characters in this are bitwise greater than the characters | |
435 | * in <code>srcChars</code>. | |
b75a7d8f A |
436 | * @stable ICU 2.0 |
437 | */ | |
f3c0d7a5 | 438 | inline int8_t compare(ConstChar16Ptr srcChars, |
b75a7d8f A |
439 | int32_t srcLength) const; |
440 | ||
441 | /** | |
374ca955 A |
442 | * Compare the characters bitwise in the range |
443 | * [<TT>start</TT>, <TT>start + length</TT>) with the first | |
b75a7d8f A |
444 | * <TT>length</TT> characters in <TT>srcChars</TT> |
445 | * @param start the offset at which the compare operation begins | |
446 | * @param length the number of characters to compare. | |
447 | * @param srcChars the characters to be compared | |
374ca955 A |
448 | * @return The result of bitwise character comparison: 0 if this |
449 | * contains the same characters as <code>srcChars</code>, -1 if the characters in | |
450 | * this are bitwise less than the characters in <code>srcChars</code>, +1 if the | |
451 | * characters in this are bitwise greater than the characters | |
452 | * in <code>srcChars</code>. | |
b75a7d8f A |
453 | * @stable ICU 2.0 |
454 | */ | |
455 | inline int8_t compare(int32_t start, | |
456 | int32_t length, | |
f3c0d7a5 | 457 | const char16_t *srcChars) const; |
b75a7d8f A |
458 | |
459 | /** | |
374ca955 A |
460 | * Compare the characters bitwise in the range |
461 | * [<TT>start</TT>, <TT>start + length</TT>) with the characters | |
462 | * in <TT>srcChars</TT> in the range | |
463 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
464 | * @param start the offset at which the compare operation begins |
465 | * @param length the number of characters in this to compare | |
466 | * @param srcChars the characters to be compared | |
467 | * @param srcStart the offset into <TT>srcChars</TT> to start comparison | |
468 | * @param srcLength the number of characters in <TT>srcChars</TT> to compare | |
374ca955 A |
469 | * @return The result of bitwise character comparison: 0 if this |
470 | * contains the same characters as <code>srcChars</code>, -1 if the characters in | |
471 | * this are bitwise less than the characters in <code>srcChars</code>, +1 if the | |
472 | * characters in this are bitwise greater than the characters | |
473 | * in <code>srcChars</code>. | |
b75a7d8f A |
474 | * @stable ICU 2.0 |
475 | */ | |
476 | inline int8_t compare(int32_t start, | |
477 | int32_t length, | |
f3c0d7a5 | 478 | const char16_t *srcChars, |
b75a7d8f A |
479 | int32_t srcStart, |
480 | int32_t srcLength) const; | |
481 | ||
482 | /** | |
374ca955 A |
483 | * Compare the characters bitwise in the range |
484 | * [<TT>start</TT>, <TT>limit</TT>) with the characters | |
485 | * in <TT>srcText</TT> in the range | |
486 | * [<TT>srcStart</TT>, <TT>srcLimit</TT>). | |
b75a7d8f A |
487 | * @param start the offset at which the compare operation begins |
488 | * @param limit the offset immediately following the compare operation | |
489 | * @param srcText the text to be compared | |
490 | * @param srcStart the offset into <TT>srcText</TT> to start comparison | |
491 | * @param srcLimit the offset into <TT>srcText</TT> to limit comparison | |
374ca955 A |
492 | * @return The result of bitwise character comparison: 0 if this |
493 | * contains the same characters as <code>srcText</code>, -1 if the characters in | |
494 | * this are bitwise less than the characters in <code>srcText</code>, +1 if the | |
495 | * characters in this are bitwise greater than the characters | |
496 | * in <code>srcText</code>. | |
b75a7d8f A |
497 | * @stable ICU 2.0 |
498 | */ | |
499 | inline int8_t compareBetween(int32_t start, | |
500 | int32_t limit, | |
501 | const UnicodeString& srcText, | |
502 | int32_t srcStart, | |
503 | int32_t srcLimit) const; | |
504 | ||
505 | /** | |
506 | * Compare two Unicode strings in code point order. | |
46f4442e | 507 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
508 | * if supplementary characters are present: |
509 | * | |
510 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
511 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
512 | * which means that they compare as less than some other BMP characters like U+feff. | |
513 | * This function compares Unicode strings in code point order. | |
514 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
515 | * | |
516 | * @param text Another string to compare this one to. | |
517 | * @return a negative/zero/positive integer corresponding to whether | |
518 | * this string is less than/equal to/greater than the second one | |
519 | * in code point order | |
520 | * @stable ICU 2.0 | |
521 | */ | |
522 | inline int8_t compareCodePointOrder(const UnicodeString& text) const; | |
523 | ||
524 | /** | |
525 | * Compare two Unicode strings in code point order. | |
46f4442e | 526 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
527 | * if supplementary characters are present: |
528 | * | |
529 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
530 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
531 | * which means that they compare as less than some other BMP characters like U+feff. | |
532 | * This function compares Unicode strings in code point order. | |
533 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
534 | * | |
535 | * @param start The start offset in this string at which the compare operation begins. | |
536 | * @param length The number of code units from this string to compare. | |
537 | * @param srcText Another string to compare this one to. | |
538 | * @return a negative/zero/positive integer corresponding to whether | |
539 | * this string is less than/equal to/greater than the second one | |
540 | * in code point order | |
541 | * @stable ICU 2.0 | |
542 | */ | |
543 | inline int8_t compareCodePointOrder(int32_t start, | |
544 | int32_t length, | |
545 | const UnicodeString& srcText) const; | |
546 | ||
547 | /** | |
548 | * Compare two Unicode strings in code point order. | |
46f4442e | 549 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
550 | * if supplementary characters are present: |
551 | * | |
552 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
553 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
554 | * which means that they compare as less than some other BMP characters like U+feff. | |
555 | * This function compares Unicode strings in code point order. | |
556 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
557 | * | |
558 | * @param start The start offset in this string at which the compare operation begins. | |
559 | * @param length The number of code units from this string to compare. | |
560 | * @param srcText Another string to compare this one to. | |
561 | * @param srcStart The start offset in that string at which the compare operation begins. | |
562 | * @param srcLength The number of code units from that string to compare. | |
563 | * @return a negative/zero/positive integer corresponding to whether | |
564 | * this string is less than/equal to/greater than the second one | |
565 | * in code point order | |
566 | * @stable ICU 2.0 | |
567 | */ | |
568 | inline int8_t compareCodePointOrder(int32_t start, | |
569 | int32_t length, | |
570 | const UnicodeString& srcText, | |
571 | int32_t srcStart, | |
572 | int32_t srcLength) const; | |
573 | ||
574 | /** | |
575 | * Compare two Unicode strings in code point order. | |
46f4442e | 576 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
577 | * if supplementary characters are present: |
578 | * | |
579 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
580 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
581 | * which means that they compare as less than some other BMP characters like U+feff. | |
582 | * This function compares Unicode strings in code point order. | |
583 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
584 | * | |
585 | * @param srcChars A pointer to another string to compare this one to. | |
586 | * @param srcLength The number of code units from that string to compare. | |
587 | * @return a negative/zero/positive integer corresponding to whether | |
588 | * this string is less than/equal to/greater than the second one | |
589 | * in code point order | |
590 | * @stable ICU 2.0 | |
591 | */ | |
f3c0d7a5 | 592 | inline int8_t compareCodePointOrder(ConstChar16Ptr srcChars, |
b75a7d8f A |
593 | int32_t srcLength) const; |
594 | ||
595 | /** | |
596 | * Compare two Unicode strings in code point order. | |
46f4442e | 597 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
598 | * if supplementary characters are present: |
599 | * | |
600 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
601 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
602 | * which means that they compare as less than some other BMP characters like U+feff. | |
603 | * This function compares Unicode strings in code point order. | |
604 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
605 | * | |
606 | * @param start The start offset in this string at which the compare operation begins. | |
607 | * @param length The number of code units from this string to compare. | |
608 | * @param srcChars A pointer to another string to compare this one to. | |
609 | * @return a negative/zero/positive integer corresponding to whether | |
610 | * this string is less than/equal to/greater than the second one | |
611 | * in code point order | |
612 | * @stable ICU 2.0 | |
613 | */ | |
614 | inline int8_t compareCodePointOrder(int32_t start, | |
615 | int32_t length, | |
f3c0d7a5 | 616 | const char16_t *srcChars) const; |
b75a7d8f A |
617 | |
618 | /** | |
619 | * Compare two Unicode strings in code point order. | |
46f4442e | 620 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
621 | * if supplementary characters are present: |
622 | * | |
623 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
624 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
625 | * which means that they compare as less than some other BMP characters like U+feff. | |
626 | * This function compares Unicode strings in code point order. | |
627 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
628 | * | |
629 | * @param start The start offset in this string at which the compare operation begins. | |
630 | * @param length The number of code units from this string to compare. | |
631 | * @param srcChars A pointer to another string to compare this one to. | |
632 | * @param srcStart The start offset in that string at which the compare operation begins. | |
633 | * @param srcLength The number of code units from that string to compare. | |
634 | * @return a negative/zero/positive integer corresponding to whether | |
635 | * this string is less than/equal to/greater than the second one | |
636 | * in code point order | |
637 | * @stable ICU 2.0 | |
638 | */ | |
639 | inline int8_t compareCodePointOrder(int32_t start, | |
640 | int32_t length, | |
f3c0d7a5 | 641 | const char16_t *srcChars, |
b75a7d8f A |
642 | int32_t srcStart, |
643 | int32_t srcLength) const; | |
644 | ||
645 | /** | |
646 | * Compare two Unicode strings in code point order. | |
46f4442e | 647 | * The result may be different from the results of compare(), operator<, etc. |
b75a7d8f A |
648 | * if supplementary characters are present: |
649 | * | |
650 | * In UTF-16, supplementary characters (with code points U+10000 and above) are | |
651 | * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff, | |
652 | * which means that they compare as less than some other BMP characters like U+feff. | |
653 | * This function compares Unicode strings in code point order. | |
654 | * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined. | |
655 | * | |
656 | * @param start The start offset in this string at which the compare operation begins. | |
657 | * @param limit The offset after the last code unit from this string to compare. | |
658 | * @param srcText Another string to compare this one to. | |
659 | * @param srcStart The start offset in that string at which the compare operation begins. | |
660 | * @param srcLimit The offset after the last code unit from that string to compare. | |
661 | * @return a negative/zero/positive integer corresponding to whether | |
662 | * this string is less than/equal to/greater than the second one | |
663 | * in code point order | |
664 | * @stable ICU 2.0 | |
665 | */ | |
666 | inline int8_t compareCodePointOrderBetween(int32_t start, | |
667 | int32_t limit, | |
668 | const UnicodeString& srcText, | |
669 | int32_t srcStart, | |
670 | int32_t srcLimit) const; | |
671 | ||
672 | /** | |
673 | * Compare two strings case-insensitively using full case folding. | |
674 | * This is equivalent to this->foldCase(options).compare(text.foldCase(options)). | |
675 | * | |
676 | * @param text Another string to compare this one to. | |
677 | * @param options A bit set of options: | |
678 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
679 | * Comparison in code unit order with default case folding. | |
680 | * | |
681 | * - U_COMPARE_CODE_POINT_ORDER | |
682 | * Set to choose code point order instead of code unit order | |
683 | * (see u_strCompare for details). | |
684 | * | |
685 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
686 | * | |
687 | * @return A negative, zero, or positive integer indicating the comparison result. | |
688 | * @stable ICU 2.0 | |
689 | */ | |
690 | inline int8_t caseCompare(const UnicodeString& text, uint32_t options) const; | |
691 | ||
692 | /** | |
693 | * Compare two strings case-insensitively using full case folding. | |
694 | * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)). | |
695 | * | |
696 | * @param start The start offset in this string at which the compare operation begins. | |
697 | * @param length The number of code units from this string to compare. | |
698 | * @param srcText Another string to compare this one to. | |
699 | * @param options A bit set of options: | |
700 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
701 | * Comparison in code unit order with default case folding. | |
702 | * | |
703 | * - U_COMPARE_CODE_POINT_ORDER | |
704 | * Set to choose code point order instead of code unit order | |
705 | * (see u_strCompare for details). | |
706 | * | |
707 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
708 | * | |
709 | * @return A negative, zero, or positive integer indicating the comparison result. | |
710 | * @stable ICU 2.0 | |
711 | */ | |
712 | inline int8_t caseCompare(int32_t start, | |
713 | int32_t length, | |
714 | const UnicodeString& srcText, | |
715 | uint32_t options) const; | |
716 | ||
717 | /** | |
718 | * Compare two strings case-insensitively using full case folding. | |
719 | * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)). | |
720 | * | |
721 | * @param start The start offset in this string at which the compare operation begins. | |
722 | * @param length The number of code units from this string to compare. | |
723 | * @param srcText Another string to compare this one to. | |
724 | * @param srcStart The start offset in that string at which the compare operation begins. | |
725 | * @param srcLength The number of code units from that string to compare. | |
726 | * @param options A bit set of options: | |
727 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
728 | * Comparison in code unit order with default case folding. | |
729 | * | |
730 | * - U_COMPARE_CODE_POINT_ORDER | |
731 | * Set to choose code point order instead of code unit order | |
732 | * (see u_strCompare for details). | |
733 | * | |
734 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
735 | * | |
736 | * @return A negative, zero, or positive integer indicating the comparison result. | |
737 | * @stable ICU 2.0 | |
738 | */ | |
739 | inline int8_t caseCompare(int32_t start, | |
740 | int32_t length, | |
741 | const UnicodeString& srcText, | |
742 | int32_t srcStart, | |
743 | int32_t srcLength, | |
744 | uint32_t options) const; | |
745 | ||
746 | /** | |
747 | * Compare two strings case-insensitively using full case folding. | |
748 | * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)). | |
749 | * | |
750 | * @param srcChars A pointer to another string to compare this one to. | |
751 | * @param srcLength The number of code units from that string to compare. | |
752 | * @param options A bit set of options: | |
753 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
754 | * Comparison in code unit order with default case folding. | |
755 | * | |
756 | * - U_COMPARE_CODE_POINT_ORDER | |
757 | * Set to choose code point order instead of code unit order | |
758 | * (see u_strCompare for details). | |
759 | * | |
760 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
761 | * | |
762 | * @return A negative, zero, or positive integer indicating the comparison result. | |
763 | * @stable ICU 2.0 | |
764 | */ | |
f3c0d7a5 | 765 | inline int8_t caseCompare(ConstChar16Ptr srcChars, |
b75a7d8f A |
766 | int32_t srcLength, |
767 | uint32_t options) const; | |
768 | ||
769 | /** | |
770 | * Compare two strings case-insensitively using full case folding. | |
771 | * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)). | |
772 | * | |
773 | * @param start The start offset in this string at which the compare operation begins. | |
774 | * @param length The number of code units from this string to compare. | |
775 | * @param srcChars A pointer to another string to compare this one to. | |
776 | * @param options A bit set of options: | |
777 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
778 | * Comparison in code unit order with default case folding. | |
779 | * | |
780 | * - U_COMPARE_CODE_POINT_ORDER | |
781 | * Set to choose code point order instead of code unit order | |
782 | * (see u_strCompare for details). | |
783 | * | |
784 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
785 | * | |
786 | * @return A negative, zero, or positive integer indicating the comparison result. | |
787 | * @stable ICU 2.0 | |
788 | */ | |
789 | inline int8_t caseCompare(int32_t start, | |
790 | int32_t length, | |
f3c0d7a5 | 791 | const char16_t *srcChars, |
b75a7d8f A |
792 | uint32_t options) const; |
793 | ||
794 | /** | |
795 | * Compare two strings case-insensitively using full case folding. | |
796 | * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)). | |
797 | * | |
798 | * @param start The start offset in this string at which the compare operation begins. | |
799 | * @param length The number of code units from this string to compare. | |
800 | * @param srcChars A pointer to another string to compare this one to. | |
801 | * @param srcStart The start offset in that string at which the compare operation begins. | |
802 | * @param srcLength The number of code units from that string to compare. | |
803 | * @param options A bit set of options: | |
804 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
805 | * Comparison in code unit order with default case folding. | |
806 | * | |
807 | * - U_COMPARE_CODE_POINT_ORDER | |
808 | * Set to choose code point order instead of code unit order | |
809 | * (see u_strCompare for details). | |
810 | * | |
811 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
812 | * | |
813 | * @return A negative, zero, or positive integer indicating the comparison result. | |
814 | * @stable ICU 2.0 | |
815 | */ | |
816 | inline int8_t caseCompare(int32_t start, | |
817 | int32_t length, | |
f3c0d7a5 | 818 | const char16_t *srcChars, |
b75a7d8f A |
819 | int32_t srcStart, |
820 | int32_t srcLength, | |
821 | uint32_t options) const; | |
822 | ||
823 | /** | |
824 | * Compare two strings case-insensitively using full case folding. | |
825 | * This is equivalent to this->foldCase(options).compareBetween(text.foldCase(options)). | |
826 | * | |
827 | * @param start The start offset in this string at which the compare operation begins. | |
828 | * @param limit The offset after the last code unit from this string to compare. | |
829 | * @param srcText Another string to compare this one to. | |
830 | * @param srcStart The start offset in that string at which the compare operation begins. | |
831 | * @param srcLimit The offset after the last code unit from that string to compare. | |
832 | * @param options A bit set of options: | |
833 | * - U_FOLD_CASE_DEFAULT or 0 is used for default options: | |
834 | * Comparison in code unit order with default case folding. | |
835 | * | |
836 | * - U_COMPARE_CODE_POINT_ORDER | |
837 | * Set to choose code point order instead of code unit order | |
838 | * (see u_strCompare for details). | |
839 | * | |
840 | * - U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
841 | * | |
842 | * @return A negative, zero, or positive integer indicating the comparison result. | |
843 | * @stable ICU 2.0 | |
844 | */ | |
845 | inline int8_t caseCompareBetween(int32_t start, | |
846 | int32_t limit, | |
847 | const UnicodeString& srcText, | |
848 | int32_t srcStart, | |
849 | int32_t srcLimit, | |
850 | uint32_t options) const; | |
851 | ||
852 | /** | |
853 | * Determine if this starts with the characters in <TT>text</TT> | |
854 | * @param text The text to match. | |
374ca955 | 855 | * @return TRUE if this starts with the characters in <TT>text</TT>, |
b75a7d8f A |
856 | * FALSE otherwise |
857 | * @stable ICU 2.0 | |
858 | */ | |
859 | inline UBool startsWith(const UnicodeString& text) const; | |
860 | ||
861 | /** | |
374ca955 A |
862 | * Determine if this starts with the characters in <TT>srcText</TT> |
863 | * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
864 | * @param srcText The text to match. |
865 | * @param srcStart the offset into <TT>srcText</TT> to start matching | |
866 | * @param srcLength the number of characters in <TT>srcText</TT> to match | |
374ca955 | 867 | * @return TRUE if this starts with the characters in <TT>text</TT>, |
b75a7d8f A |
868 | * FALSE otherwise |
869 | * @stable ICU 2.0 | |
870 | */ | |
871 | inline UBool startsWith(const UnicodeString& srcText, | |
872 | int32_t srcStart, | |
873 | int32_t srcLength) const; | |
874 | ||
875 | /** | |
876 | * Determine if this starts with the characters in <TT>srcChars</TT> | |
877 | * @param srcChars The characters to match. | |
878 | * @param srcLength the number of characters in <TT>srcChars</TT> | |
374ca955 | 879 | * @return TRUE if this starts with the characters in <TT>srcChars</TT>, |
b75a7d8f A |
880 | * FALSE otherwise |
881 | * @stable ICU 2.0 | |
882 | */ | |
f3c0d7a5 | 883 | inline UBool startsWith(ConstChar16Ptr srcChars, |
b75a7d8f A |
884 | int32_t srcLength) const; |
885 | ||
886 | /** | |
374ca955 A |
887 | * Determine if this ends with the characters in <TT>srcChars</TT> |
888 | * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
889 | * @param srcChars The characters to match. |
890 | * @param srcStart the offset into <TT>srcText</TT> to start matching | |
891 | * @param srcLength the number of characters in <TT>srcChars</TT> to match | |
892 | * @return TRUE if this ends with the characters in <TT>srcChars</TT>, FALSE otherwise | |
893 | * @stable ICU 2.0 | |
894 | */ | |
f3c0d7a5 | 895 | inline UBool startsWith(const char16_t *srcChars, |
b75a7d8f A |
896 | int32_t srcStart, |
897 | int32_t srcLength) const; | |
898 | ||
899 | /** | |
900 | * Determine if this ends with the characters in <TT>text</TT> | |
901 | * @param text The text to match. | |
374ca955 | 902 | * @return TRUE if this ends with the characters in <TT>text</TT>, |
b75a7d8f A |
903 | * FALSE otherwise |
904 | * @stable ICU 2.0 | |
905 | */ | |
906 | inline UBool endsWith(const UnicodeString& text) const; | |
907 | ||
908 | /** | |
374ca955 A |
909 | * Determine if this ends with the characters in <TT>srcText</TT> |
910 | * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
911 | * @param srcText The text to match. |
912 | * @param srcStart the offset into <TT>srcText</TT> to start matching | |
913 | * @param srcLength the number of characters in <TT>srcText</TT> to match | |
374ca955 | 914 | * @return TRUE if this ends with the characters in <TT>text</TT>, |
b75a7d8f A |
915 | * FALSE otherwise |
916 | * @stable ICU 2.0 | |
917 | */ | |
918 | inline UBool endsWith(const UnicodeString& srcText, | |
919 | int32_t srcStart, | |
920 | int32_t srcLength) const; | |
921 | ||
922 | /** | |
923 | * Determine if this ends with the characters in <TT>srcChars</TT> | |
924 | * @param srcChars The characters to match. | |
925 | * @param srcLength the number of characters in <TT>srcChars</TT> | |
374ca955 | 926 | * @return TRUE if this ends with the characters in <TT>srcChars</TT>, |
b75a7d8f A |
927 | * FALSE otherwise |
928 | * @stable ICU 2.0 | |
929 | */ | |
f3c0d7a5 | 930 | inline UBool endsWith(ConstChar16Ptr srcChars, |
b75a7d8f A |
931 | int32_t srcLength) const; |
932 | ||
933 | /** | |
374ca955 A |
934 | * Determine if this ends with the characters in <TT>srcChars</TT> |
935 | * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
936 | * @param srcChars The characters to match. |
937 | * @param srcStart the offset into <TT>srcText</TT> to start matching | |
938 | * @param srcLength the number of characters in <TT>srcChars</TT> to match | |
374ca955 | 939 | * @return TRUE if this ends with the characters in <TT>srcChars</TT>, |
b75a7d8f A |
940 | * FALSE otherwise |
941 | * @stable ICU 2.0 | |
942 | */ | |
f3c0d7a5 | 943 | inline UBool endsWith(const char16_t *srcChars, |
b75a7d8f A |
944 | int32_t srcStart, |
945 | int32_t srcLength) const; | |
946 | ||
947 | ||
948 | /* Searching - bitwise only */ | |
949 | ||
950 | /** | |
951 | * Locate in this the first occurrence of the characters in <TT>text</TT>, | |
952 | * using bitwise comparison. | |
953 | * @param text The text to search for. | |
374ca955 | 954 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
955 | * or -1 if not found. |
956 | * @stable ICU 2.0 | |
957 | */ | |
958 | inline int32_t indexOf(const UnicodeString& text) const; | |
959 | ||
960 | /** | |
961 | * Locate in this the first occurrence of the characters in <TT>text</TT> | |
962 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
963 | * @param text The text to search for. | |
964 | * @param start The offset at which searching will start. | |
374ca955 | 965 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
966 | * or -1 if not found. |
967 | * @stable ICU 2.0 | |
968 | */ | |
969 | inline int32_t indexOf(const UnicodeString& text, | |
970 | int32_t start) const; | |
971 | ||
972 | /** | |
973 | * Locate in this the first occurrence in the range | |
374ca955 | 974 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters |
b75a7d8f A |
975 | * in <TT>text</TT>, using bitwise comparison. |
976 | * @param text The text to search for. | |
977 | * @param start The offset at which searching will start. | |
978 | * @param length The number of characters to search | |
374ca955 | 979 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
980 | * or -1 if not found. |
981 | * @stable ICU 2.0 | |
982 | */ | |
983 | inline int32_t indexOf(const UnicodeString& text, | |
984 | int32_t start, | |
985 | int32_t length) const; | |
986 | ||
987 | /** | |
988 | * Locate in this the first occurrence in the range | |
989 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters | |
374ca955 A |
990 | * in <TT>srcText</TT> in the range |
991 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>), | |
992 | * using bitwise comparison. | |
b75a7d8f A |
993 | * @param srcText The text to search for. |
994 | * @param srcStart the offset into <TT>srcText</TT> at which | |
995 | * to start matching | |
996 | * @param srcLength the number of characters in <TT>srcText</TT> to match | |
997 | * @param start the offset into this at which to start matching | |
998 | * @param length the number of characters in this to search | |
374ca955 | 999 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1000 | * or -1 if not found. |
1001 | * @stable ICU 2.0 | |
1002 | */ | |
1003 | inline int32_t indexOf(const UnicodeString& srcText, | |
1004 | int32_t srcStart, | |
1005 | int32_t srcLength, | |
1006 | int32_t start, | |
1007 | int32_t length) const; | |
1008 | ||
1009 | /** | |
1010 | * Locate in this the first occurrence of the characters in | |
374ca955 A |
1011 | * <TT>srcChars</TT> |
1012 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
b75a7d8f A |
1013 | * @param srcChars The text to search for. |
1014 | * @param srcLength the number of characters in <TT>srcChars</TT> to match | |
1015 | * @param start the offset into this at which to start matching | |
374ca955 | 1016 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1017 | * or -1 if not found. |
1018 | * @stable ICU 2.0 | |
1019 | */ | |
f3c0d7a5 | 1020 | inline int32_t indexOf(const char16_t *srcChars, |
b75a7d8f A |
1021 | int32_t srcLength, |
1022 | int32_t start) const; | |
1023 | ||
1024 | /** | |
1025 | * Locate in this the first occurrence in the range | |
374ca955 | 1026 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters |
b75a7d8f A |
1027 | * in <TT>srcChars</TT>, using bitwise comparison. |
1028 | * @param srcChars The text to search for. | |
1029 | * @param srcLength the number of characters in <TT>srcChars</TT> | |
1030 | * @param start The offset at which searching will start. | |
1031 | * @param length The number of characters to search | |
374ca955 | 1032 | * @return The offset into this of the start of <TT>srcChars</TT>, |
b75a7d8f A |
1033 | * or -1 if not found. |
1034 | * @stable ICU 2.0 | |
1035 | */ | |
f3c0d7a5 | 1036 | inline int32_t indexOf(ConstChar16Ptr srcChars, |
b75a7d8f A |
1037 | int32_t srcLength, |
1038 | int32_t start, | |
1039 | int32_t length) const; | |
374ca955 | 1040 | |
b75a7d8f | 1041 | /** |
374ca955 A |
1042 | * Locate in this the first occurrence in the range |
1043 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters | |
1044 | * in <TT>srcChars</TT> in the range | |
1045 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>), | |
1046 | * using bitwise comparison. | |
b75a7d8f | 1047 | * @param srcChars The text to search for. |
374ca955 | 1048 | * @param srcStart the offset into <TT>srcChars</TT> at which |
b75a7d8f A |
1049 | * to start matching |
1050 | * @param srcLength the number of characters in <TT>srcChars</TT> to match | |
1051 | * @param start the offset into this at which to start matching | |
1052 | * @param length the number of characters in this to search | |
374ca955 | 1053 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1054 | * or -1 if not found. |
1055 | * @stable ICU 2.0 | |
1056 | */ | |
f3c0d7a5 | 1057 | int32_t indexOf(const char16_t *srcChars, |
b75a7d8f A |
1058 | int32_t srcStart, |
1059 | int32_t srcLength, | |
1060 | int32_t start, | |
1061 | int32_t length) const; | |
1062 | ||
1063 | /** | |
1064 | * Locate in this the first occurrence of the BMP code point <code>c</code>, | |
1065 | * using bitwise comparison. | |
1066 | * @param c The code unit to search for. | |
1067 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1068 | * @stable ICU 2.0 | |
1069 | */ | |
f3c0d7a5 | 1070 | inline int32_t indexOf(char16_t c) const; |
b75a7d8f A |
1071 | |
1072 | /** | |
374ca955 | 1073 | * Locate in this the first occurrence of the code point <TT>c</TT>, |
b75a7d8f A |
1074 | * using bitwise comparison. |
1075 | * | |
1076 | * @param c The code point to search for. | |
1077 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1078 | * @stable ICU 2.0 | |
1079 | */ | |
1080 | inline int32_t indexOf(UChar32 c) const; | |
1081 | ||
1082 | /** | |
1083 | * Locate in this the first occurrence of the BMP code point <code>c</code>, | |
1084 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
1085 | * @param c The code unit to search for. | |
1086 | * @param start The offset at which searching will start. | |
1087 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1088 | * @stable ICU 2.0 | |
1089 | */ | |
f3c0d7a5 | 1090 | inline int32_t indexOf(char16_t c, |
b75a7d8f A |
1091 | int32_t start) const; |
1092 | ||
1093 | /** | |
1094 | * Locate in this the first occurrence of the code point <TT>c</TT> | |
1095 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
1096 | * | |
1097 | * @param c The code point to search for. | |
1098 | * @param start The offset at which searching will start. | |
1099 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1100 | * @stable ICU 2.0 | |
1101 | */ | |
1102 | inline int32_t indexOf(UChar32 c, | |
1103 | int32_t start) const; | |
1104 | ||
1105 | /** | |
1106 | * Locate in this the first occurrence of the BMP code point <code>c</code> | |
374ca955 A |
1107 | * in the range [<TT>start</TT>, <TT>start + length</TT>), |
1108 | * using bitwise comparison. | |
b75a7d8f A |
1109 | * @param c The code unit to search for. |
1110 | * @param start the offset into this at which to start matching | |
1111 | * @param length the number of characters in this to search | |
1112 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1113 | * @stable ICU 2.0 | |
1114 | */ | |
f3c0d7a5 | 1115 | inline int32_t indexOf(char16_t c, |
b75a7d8f A |
1116 | int32_t start, |
1117 | int32_t length) const; | |
1118 | ||
1119 | /** | |
374ca955 A |
1120 | * Locate in this the first occurrence of the code point <TT>c</TT> |
1121 | * in the range [<TT>start</TT>, <TT>start + length</TT>), | |
1122 | * using bitwise comparison. | |
b75a7d8f A |
1123 | * |
1124 | * @param c The code point to search for. | |
1125 | * @param start the offset into this at which to start matching | |
1126 | * @param length the number of characters in this to search | |
1127 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1128 | * @stable ICU 2.0 | |
1129 | */ | |
1130 | inline int32_t indexOf(UChar32 c, | |
1131 | int32_t start, | |
1132 | int32_t length) const; | |
1133 | ||
1134 | /** | |
374ca955 | 1135 | * Locate in this the last occurrence of the characters in <TT>text</TT>, |
b75a7d8f A |
1136 | * using bitwise comparison. |
1137 | * @param text The text to search for. | |
374ca955 | 1138 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1139 | * or -1 if not found. |
1140 | * @stable ICU 2.0 | |
1141 | */ | |
1142 | inline int32_t lastIndexOf(const UnicodeString& text) const; | |
1143 | ||
1144 | /** | |
1145 | * Locate in this the last occurrence of the characters in <TT>text</TT> | |
1146 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
1147 | * @param text The text to search for. | |
1148 | * @param start The offset at which searching will start. | |
374ca955 | 1149 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1150 | * or -1 if not found. |
1151 | * @stable ICU 2.0 | |
1152 | */ | |
1153 | inline int32_t lastIndexOf(const UnicodeString& text, | |
1154 | int32_t start) const; | |
1155 | ||
1156 | /** | |
374ca955 | 1157 | * Locate in this the last occurrence in the range |
b75a7d8f A |
1158 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters |
1159 | * in <TT>text</TT>, using bitwise comparison. | |
1160 | * @param text The text to search for. | |
1161 | * @param start The offset at which searching will start. | |
1162 | * @param length The number of characters to search | |
374ca955 | 1163 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1164 | * or -1 if not found. |
1165 | * @stable ICU 2.0 | |
1166 | */ | |
1167 | inline int32_t lastIndexOf(const UnicodeString& text, | |
1168 | int32_t start, | |
1169 | int32_t length) const; | |
1170 | ||
1171 | /** | |
374ca955 A |
1172 | * Locate in this the last occurrence in the range |
1173 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters | |
1174 | * in <TT>srcText</TT> in the range | |
1175 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>), | |
1176 | * using bitwise comparison. | |
b75a7d8f | 1177 | * @param srcText The text to search for. |
374ca955 | 1178 | * @param srcStart the offset into <TT>srcText</TT> at which |
b75a7d8f A |
1179 | * to start matching |
1180 | * @param srcLength the number of characters in <TT>srcText</TT> to match | |
1181 | * @param start the offset into this at which to start matching | |
1182 | * @param length the number of characters in this to search | |
374ca955 | 1183 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1184 | * or -1 if not found. |
1185 | * @stable ICU 2.0 | |
1186 | */ | |
1187 | inline int32_t lastIndexOf(const UnicodeString& srcText, | |
1188 | int32_t srcStart, | |
1189 | int32_t srcLength, | |
1190 | int32_t start, | |
1191 | int32_t length) const; | |
1192 | ||
1193 | /** | |
374ca955 A |
1194 | * Locate in this the last occurrence of the characters in <TT>srcChars</TT> |
1195 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
b75a7d8f A |
1196 | * @param srcChars The text to search for. |
1197 | * @param srcLength the number of characters in <TT>srcChars</TT> to match | |
1198 | * @param start the offset into this at which to start matching | |
374ca955 | 1199 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1200 | * or -1 if not found. |
1201 | * @stable ICU 2.0 | |
1202 | */ | |
f3c0d7a5 | 1203 | inline int32_t lastIndexOf(const char16_t *srcChars, |
b75a7d8f A |
1204 | int32_t srcLength, |
1205 | int32_t start) const; | |
1206 | ||
1207 | /** | |
374ca955 A |
1208 | * Locate in this the last occurrence in the range |
1209 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters | |
b75a7d8f A |
1210 | * in <TT>srcChars</TT>, using bitwise comparison. |
1211 | * @param srcChars The text to search for. | |
1212 | * @param srcLength the number of characters in <TT>srcChars</TT> | |
1213 | * @param start The offset at which searching will start. | |
1214 | * @param length The number of characters to search | |
374ca955 | 1215 | * @return The offset into this of the start of <TT>srcChars</TT>, |
b75a7d8f A |
1216 | * or -1 if not found. |
1217 | * @stable ICU 2.0 | |
1218 | */ | |
f3c0d7a5 | 1219 | inline int32_t lastIndexOf(ConstChar16Ptr srcChars, |
b75a7d8f A |
1220 | int32_t srcLength, |
1221 | int32_t start, | |
1222 | int32_t length) const; | |
374ca955 | 1223 | |
b75a7d8f | 1224 | /** |
374ca955 A |
1225 | * Locate in this the last occurrence in the range |
1226 | * [<TT>start</TT>, <TT>start + length</TT>) of the characters | |
1227 | * in <TT>srcChars</TT> in the range | |
1228 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>), | |
1229 | * using bitwise comparison. | |
b75a7d8f A |
1230 | * @param srcChars The text to search for. |
1231 | * @param srcStart the offset into <TT>srcChars</TT> at which | |
1232 | * to start matching | |
1233 | * @param srcLength the number of characters in <TT>srcChars</TT> to match | |
1234 | * @param start the offset into this at which to start matching | |
1235 | * @param length the number of characters in this to search | |
374ca955 | 1236 | * @return The offset into this of the start of <TT>text</TT>, |
b75a7d8f A |
1237 | * or -1 if not found. |
1238 | * @stable ICU 2.0 | |
1239 | */ | |
f3c0d7a5 | 1240 | int32_t lastIndexOf(const char16_t *srcChars, |
b75a7d8f A |
1241 | int32_t srcStart, |
1242 | int32_t srcLength, | |
1243 | int32_t start, | |
1244 | int32_t length) const; | |
1245 | ||
1246 | /** | |
1247 | * Locate in this the last occurrence of the BMP code point <code>c</code>, | |
1248 | * using bitwise comparison. | |
1249 | * @param c The code unit to search for. | |
1250 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1251 | * @stable ICU 2.0 | |
1252 | */ | |
f3c0d7a5 | 1253 | inline int32_t lastIndexOf(char16_t c) const; |
b75a7d8f A |
1254 | |
1255 | /** | |
374ca955 | 1256 | * Locate in this the last occurrence of the code point <TT>c</TT>, |
b75a7d8f A |
1257 | * using bitwise comparison. |
1258 | * | |
1259 | * @param c The code point to search for. | |
1260 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1261 | * @stable ICU 2.0 | |
1262 | */ | |
1263 | inline int32_t lastIndexOf(UChar32 c) const; | |
1264 | ||
1265 | /** | |
1266 | * Locate in this the last occurrence of the BMP code point <code>c</code> | |
1267 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
1268 | * @param c The code unit to search for. | |
1269 | * @param start The offset at which searching will start. | |
1270 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1271 | * @stable ICU 2.0 | |
1272 | */ | |
f3c0d7a5 | 1273 | inline int32_t lastIndexOf(char16_t c, |
b75a7d8f A |
1274 | int32_t start) const; |
1275 | ||
1276 | /** | |
1277 | * Locate in this the last occurrence of the code point <TT>c</TT> | |
1278 | * starting at offset <TT>start</TT>, using bitwise comparison. | |
1279 | * | |
1280 | * @param c The code point to search for. | |
1281 | * @param start The offset at which searching will start. | |
1282 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1283 | * @stable ICU 2.0 | |
1284 | */ | |
1285 | inline int32_t lastIndexOf(UChar32 c, | |
1286 | int32_t start) const; | |
1287 | ||
1288 | /** | |
1289 | * Locate in this the last occurrence of the BMP code point <code>c</code> | |
374ca955 A |
1290 | * in the range [<TT>start</TT>, <TT>start + length</TT>), |
1291 | * using bitwise comparison. | |
b75a7d8f A |
1292 | * @param c The code unit to search for. |
1293 | * @param start the offset into this at which to start matching | |
1294 | * @param length the number of characters in this to search | |
1295 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1296 | * @stable ICU 2.0 | |
1297 | */ | |
f3c0d7a5 | 1298 | inline int32_t lastIndexOf(char16_t c, |
b75a7d8f A |
1299 | int32_t start, |
1300 | int32_t length) const; | |
1301 | ||
1302 | /** | |
374ca955 A |
1303 | * Locate in this the last occurrence of the code point <TT>c</TT> |
1304 | * in the range [<TT>start</TT>, <TT>start + length</TT>), | |
1305 | * using bitwise comparison. | |
b75a7d8f A |
1306 | * |
1307 | * @param c The code point to search for. | |
1308 | * @param start the offset into this at which to start matching | |
1309 | * @param length the number of characters in this to search | |
1310 | * @return The offset into this of <TT>c</TT>, or -1 if not found. | |
1311 | * @stable ICU 2.0 | |
1312 | */ | |
1313 | inline int32_t lastIndexOf(UChar32 c, | |
1314 | int32_t start, | |
1315 | int32_t length) const; | |
1316 | ||
1317 | ||
1318 | /* Character access */ | |
1319 | ||
1320 | /** | |
1321 | * Return the code unit at offset <tt>offset</tt>. | |
1322 | * If the offset is not valid (0..length()-1) then U+ffff is returned. | |
1323 | * @param offset a valid offset into the text | |
1324 | * @return the code unit at offset <tt>offset</tt> | |
1325 | * or 0xffff if the offset is not valid for this string | |
1326 | * @stable ICU 2.0 | |
1327 | */ | |
f3c0d7a5 | 1328 | inline char16_t charAt(int32_t offset) const; |
b75a7d8f A |
1329 | |
1330 | /** | |
1331 | * Return the code unit at offset <tt>offset</tt>. | |
1332 | * If the offset is not valid (0..length()-1) then U+ffff is returned. | |
1333 | * @param offset a valid offset into the text | |
1334 | * @return the code unit at offset <tt>offset</tt> | |
1335 | * @stable ICU 2.0 | |
1336 | */ | |
f3c0d7a5 | 1337 | inline char16_t operator[] (int32_t offset) const; |
b75a7d8f A |
1338 | |
1339 | /** | |
1340 | * Return the code point that contains the code unit | |
1341 | * at offset <tt>offset</tt>. | |
1342 | * If the offset is not valid (0..length()-1) then U+ffff is returned. | |
1343 | * @param offset a valid offset into the text | |
1344 | * that indicates the text offset of any of the code units | |
1345 | * that will be assembled into a code point (21-bit value) and returned | |
1346 | * @return the code point of text at <tt>offset</tt> | |
1347 | * or 0xffff if the offset is not valid for this string | |
1348 | * @stable ICU 2.0 | |
1349 | */ | |
4388f060 | 1350 | UChar32 char32At(int32_t offset) const; |
b75a7d8f A |
1351 | |
1352 | /** | |
1353 | * Adjust a random-access offset so that | |
1354 | * it points to the beginning of a Unicode character. | |
1355 | * The offset that is passed in points to | |
1356 | * any code unit of a code point, | |
1357 | * while the returned offset will point to the first code unit | |
1358 | * of the same code point. | |
1359 | * In UTF-16, if the input offset points to a second surrogate | |
1360 | * of a surrogate pair, then the returned offset will point | |
1361 | * to the first surrogate. | |
1362 | * @param offset a valid offset into one code point of the text | |
1363 | * @return offset of the first code unit of the same code point | |
1364 | * @see U16_SET_CP_START | |
1365 | * @stable ICU 2.0 | |
1366 | */ | |
4388f060 | 1367 | int32_t getChar32Start(int32_t offset) const; |
b75a7d8f A |
1368 | |
1369 | /** | |
1370 | * Adjust a random-access offset so that | |
1371 | * it points behind a Unicode character. | |
1372 | * The offset that is passed in points behind | |
1373 | * any code unit of a code point, | |
1374 | * while the returned offset will point behind the last code unit | |
1375 | * of the same code point. | |
1376 | * In UTF-16, if the input offset points behind the first surrogate | |
1377 | * (i.e., to the second surrogate) | |
1378 | * of a surrogate pair, then the returned offset will point | |
1379 | * behind the second surrogate (i.e., to the first surrogate). | |
1380 | * @param offset a valid offset after any code unit of a code point of the text | |
1381 | * @return offset of the first code unit after the same code point | |
1382 | * @see U16_SET_CP_LIMIT | |
1383 | * @stable ICU 2.0 | |
1384 | */ | |
4388f060 | 1385 | int32_t getChar32Limit(int32_t offset) const; |
b75a7d8f A |
1386 | |
1387 | /** | |
1388 | * Move the code unit index along the string by delta code points. | |
1389 | * Interpret the input index as a code unit-based offset into the string, | |
1390 | * move the index forward or backward by delta code points, and | |
1391 | * return the resulting index. | |
1392 | * The input index should point to the first code unit of a code point, | |
1393 | * if there is more than one. | |
1394 | * | |
1395 | * Both input and output indexes are code unit-based as for all | |
1396 | * string indexes/offsets in ICU (and other libraries, like MBCS char*). | |
1397 | * If delta<0 then the index is moved backward (toward the start of the string). | |
1398 | * If delta>0 then the index is moved forward (toward the end of the string). | |
1399 | * | |
1400 | * This behaves like CharacterIterator::move32(delta, kCurrent). | |
1401 | * | |
1402 | * Behavior for out-of-bounds indexes: | |
1403 | * <code>moveIndex32</code> pins the input index to 0..length(), i.e., | |
1404 | * if the input index<0 then it is pinned to 0; | |
1405 | * if it is index>length() then it is pinned to length(). | |
1406 | * Afterwards, the index is moved by <code>delta</code> code points | |
1407 | * forward or backward, | |
1408 | * but no further backward than to 0 and no further forward than to length(). | |
1409 | * The resulting index return value will be in between 0 and length(), inclusively. | |
1410 | * | |
1411 | * Examples: | |
1412 | * <pre> | |
1413 | * // s has code points 'a' U+10000 'b' U+10ffff U+2029 | |
1414 | * UnicodeString s=UNICODE_STRING("a\\U00010000b\\U0010ffff\\u2029", 31).unescape(); | |
1415 | * | |
1416 | * // initial index: position of U+10000 | |
1417 | * int32_t index=1; | |
1418 | * | |
1419 | * // the following examples will all result in index==4, position of U+10ffff | |
1420 | * | |
1421 | * // skip 2 code points from some position in the string | |
1422 | * index=s.moveIndex32(index, 2); // skips U+10000 and 'b' | |
1423 | * | |
1424 | * // go to the 3rd code point from the start of s (0-based) | |
1425 | * index=s.moveIndex32(0, 3); // skips 'a', U+10000, and 'b' | |
1426 | * | |
1427 | * // go to the next-to-last code point of s | |
1428 | * index=s.moveIndex32(s.length(), -2); // backward-skips U+2029 and U+10ffff | |
1429 | * </pre> | |
1430 | * | |
1431 | * @param index input code unit index | |
1432 | * @param delta (signed) code point count to move the index forward or backward | |
1433 | * in the string | |
1434 | * @return the resulting code unit index | |
1435 | * @stable ICU 2.0 | |
1436 | */ | |
1437 | int32_t moveIndex32(int32_t index, int32_t delta) const; | |
1438 | ||
1439 | /* Substring extraction */ | |
1440 | ||
1441 | /** | |
374ca955 | 1442 | * Copy the characters in the range |
b75a7d8f A |
1443 | * [<tt>start</tt>, <tt>start + length</tt>) into the array <tt>dst</tt>, |
1444 | * beginning at <tt>dstStart</tt>. | |
1445 | * If the string aliases to <code>dst</code> itself as an external buffer, | |
1446 | * then extract() will not copy the contents. | |
1447 | * | |
1448 | * @param start offset of first character which will be copied into the array | |
1449 | * @param length the number of characters to extract | |
1450 | * @param dst array in which to copy characters. The length of <tt>dst</tt> | |
1451 | * must be at least (<tt>dstStart + length</tt>). | |
1452 | * @param dstStart the offset in <TT>dst</TT> where the first character | |
1453 | * will be extracted | |
1454 | * @stable ICU 2.0 | |
1455 | */ | |
374ca955 A |
1456 | inline void extract(int32_t start, |
1457 | int32_t length, | |
f3c0d7a5 | 1458 | Char16Ptr dst, |
b75a7d8f A |
1459 | int32_t dstStart = 0) const; |
1460 | ||
1461 | /** | |
1462 | * Copy the contents of the string into dest. | |
1463 | * This is a convenience function that | |
1464 | * checks if there is enough space in dest, | |
1465 | * extracts the entire string if possible, | |
1466 | * and NUL-terminates dest if possible. | |
1467 | * | |
1468 | * If the string fits into dest but cannot be NUL-terminated | |
1469 | * (length()==destCapacity) then the error code is set to U_STRING_NOT_TERMINATED_WARNING. | |
1470 | * If the string itself does not fit into dest | |
1471 | * (length()>destCapacity) then the error code is set to U_BUFFER_OVERFLOW_ERROR. | |
1472 | * | |
1473 | * If the string aliases to <code>dest</code> itself as an external buffer, | |
1474 | * then extract() will not copy the contents. | |
1475 | * | |
1476 | * @param dest Destination string buffer. | |
f3c0d7a5 | 1477 | * @param destCapacity Number of char16_ts available at dest. |
b75a7d8f A |
1478 | * @param errorCode ICU error code. |
1479 | * @return length() | |
1480 | * @stable ICU 2.0 | |
1481 | */ | |
1482 | int32_t | |
f3c0d7a5 | 1483 | extract(Char16Ptr dest, int32_t destCapacity, |
b75a7d8f A |
1484 | UErrorCode &errorCode) const; |
1485 | ||
1486 | /** | |
374ca955 | 1487 | * Copy the characters in the range |
b75a7d8f A |
1488 | * [<tt>start</tt>, <tt>start + length</tt>) into the UnicodeString |
1489 | * <tt>target</tt>. | |
1490 | * @param start offset of first character which will be copied | |
1491 | * @param length the number of characters to extract | |
1492 | * @param target UnicodeString into which to copy characters. | |
1493 | * @return A reference to <TT>target</TT> | |
1494 | * @stable ICU 2.0 | |
1495 | */ | |
1496 | inline void extract(int32_t start, | |
1497 | int32_t length, | |
1498 | UnicodeString& target) const; | |
1499 | ||
1500 | /** | |
374ca955 | 1501 | * Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>) |
b75a7d8f A |
1502 | * into the array <tt>dst</tt>, beginning at <tt>dstStart</tt>. |
1503 | * @param start offset of first character which will be copied into the array | |
1504 | * @param limit offset immediately following the last character to be copied | |
374ca955 | 1505 | * @param dst array in which to copy characters. The length of <tt>dst</tt> |
b75a7d8f A |
1506 | * must be at least (<tt>dstStart + (limit - start)</tt>). |
1507 | * @param dstStart the offset in <TT>dst</TT> where the first character | |
1508 | * will be extracted | |
1509 | * @stable ICU 2.0 | |
1510 | */ | |
374ca955 A |
1511 | inline void extractBetween(int32_t start, |
1512 | int32_t limit, | |
f3c0d7a5 | 1513 | char16_t *dst, |
b75a7d8f A |
1514 | int32_t dstStart = 0) const; |
1515 | ||
1516 | /** | |
374ca955 | 1517 | * Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>) |
b75a7d8f A |
1518 | * into the UnicodeString <tt>target</tt>. Replaceable API. |
1519 | * @param start offset of first character which will be copied | |
1520 | * @param limit offset immediately following the last character to be copied | |
1521 | * @param target UnicodeString into which to copy characters. | |
1522 | * @return A reference to <TT>target</TT> | |
1523 | * @stable ICU 2.0 | |
1524 | */ | |
1525 | virtual void extractBetween(int32_t start, | |
1526 | int32_t limit, | |
1527 | UnicodeString& target) const; | |
1528 | ||
1529 | /** | |
2ca993e8 A |
1530 | * Copy the characters in the range |
1531 | * [<tt>start</TT>, <tt>start + startLength</TT>) into an array of characters. | |
374ca955 A |
1532 | * All characters must be invariant (see utypes.h). |
1533 | * Use US_INV as the last, signature-distinguishing parameter. | |
1534 | * | |
2ca993e8 | 1535 | * This function does not write any more than <code>targetCapacity</code> |
374ca955 A |
1536 | * characters but returns the length of the entire output string |
1537 | * so that one can allocate a larger buffer and call the function again | |
1538 | * if necessary. | |
1539 | * The output string is NUL-terminated if possible. | |
1540 | * | |
1541 | * @param start offset of first character which will be copied | |
1542 | * @param startLength the number of characters to extract | |
1543 | * @param target the target buffer for extraction, can be NULL | |
1544 | * if targetLength is 0 | |
1545 | * @param targetCapacity the length of the target buffer | |
1546 | * @param inv Signature-distinguishing paramater, use US_INV. | |
1547 | * @return the output string length, not including the terminating NUL | |
73c04bcf | 1548 | * @stable ICU 3.2 |
374ca955 A |
1549 | */ |
1550 | int32_t extract(int32_t start, | |
1551 | int32_t startLength, | |
1552 | char *target, | |
1553 | int32_t targetCapacity, | |
1554 | enum EInvariant inv) const; | |
1555 | ||
729e4ab9 A |
1556 | #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION |
1557 | ||
1558 | /** | |
1559 | * Copy the characters in the range | |
1560 | * [<tt>start</TT>, <tt>start + length</TT>) into an array of characters | |
1561 | * in the platform's default codepage. | |
1562 | * This function does not write any more than <code>targetLength</code> | |
1563 | * characters but returns the length of the entire output string | |
1564 | * so that one can allocate a larger buffer and call the function again | |
1565 | * if necessary. | |
1566 | * The output string is NUL-terminated if possible. | |
1567 | * | |
1568 | * @param start offset of first character which will be copied | |
1569 | * @param startLength the number of characters to extract | |
1570 | * @param target the target buffer for extraction | |
1571 | * @param targetLength the length of the target buffer | |
1572 | * If <TT>target</TT> is NULL, then the number of bytes required for | |
1573 | * <TT>target</TT> is returned. | |
1574 | * @return the output string length, not including the terminating NUL | |
1575 | * @stable ICU 2.0 | |
1576 | */ | |
1577 | int32_t extract(int32_t start, | |
1578 | int32_t startLength, | |
1579 | char *target, | |
1580 | uint32_t targetLength) const; | |
1581 | ||
1582 | #endif | |
1583 | ||
374ca955 A |
1584 | #if !UCONFIG_NO_CONVERSION |
1585 | ||
1586 | /** | |
1587 | * Copy the characters in the range | |
b75a7d8f A |
1588 | * [<tt>start</TT>, <tt>start + length</TT>) into an array of characters |
1589 | * in a specified codepage. | |
1590 | * The output string is NUL-terminated. | |
1591 | * | |
374ca955 A |
1592 | * Recommendation: For invariant-character strings use |
1593 | * extract(int32_t start, int32_t length, char *target, int32_t targetCapacity, enum EInvariant inv) const | |
1594 | * because it avoids object code dependencies of UnicodeString on | |
1595 | * the conversion code. | |
1596 | * | |
b75a7d8f A |
1597 | * @param start offset of first character which will be copied |
1598 | * @param startLength the number of characters to extract | |
1599 | * @param target the target buffer for extraction | |
374ca955 | 1600 | * @param codepage the desired codepage for the characters. 0 has |
b75a7d8f A |
1601 | * the special meaning of the default codepage |
1602 | * If <code>codepage</code> is an empty string (<code>""</code>), | |
1603 | * then a simple conversion is performed on the codepage-invariant | |
1604 | * subset ("invariant characters") of the platform encoding. See utypes.h. | |
1605 | * If <TT>target</TT> is NULL, then the number of bytes required for | |
1606 | * <TT>target</TT> is returned. It is assumed that the target is big enough | |
1607 | * to fit all of the characters. | |
1608 | * @return the output string length, not including the terminating NUL | |
1609 | * @stable ICU 2.0 | |
1610 | */ | |
1611 | inline int32_t extract(int32_t start, | |
1612 | int32_t startLength, | |
1613 | char *target, | |
1614 | const char *codepage = 0) const; | |
1615 | ||
1616 | /** | |
374ca955 | 1617 | * Copy the characters in the range |
b75a7d8f A |
1618 | * [<tt>start</TT>, <tt>start + length</TT>) into an array of characters |
1619 | * in a specified codepage. | |
1620 | * This function does not write any more than <code>targetLength</code> | |
1621 | * characters but returns the length of the entire output string | |
1622 | * so that one can allocate a larger buffer and call the function again | |
1623 | * if necessary. | |
1624 | * The output string is NUL-terminated if possible. | |
1625 | * | |
374ca955 A |
1626 | * Recommendation: For invariant-character strings use |
1627 | * extract(int32_t start, int32_t length, char *target, int32_t targetCapacity, enum EInvariant inv) const | |
1628 | * because it avoids object code dependencies of UnicodeString on | |
1629 | * the conversion code. | |
1630 | * | |
b75a7d8f A |
1631 | * @param start offset of first character which will be copied |
1632 | * @param startLength the number of characters to extract | |
1633 | * @param target the target buffer for extraction | |
1634 | * @param targetLength the length of the target buffer | |
374ca955 | 1635 | * @param codepage the desired codepage for the characters. 0 has |
b75a7d8f A |
1636 | * the special meaning of the default codepage |
1637 | * If <code>codepage</code> is an empty string (<code>""</code>), | |
1638 | * then a simple conversion is performed on the codepage-invariant | |
1639 | * subset ("invariant characters") of the platform encoding. See utypes.h. | |
1640 | * If <TT>target</TT> is NULL, then the number of bytes required for | |
1641 | * <TT>target</TT> is returned. | |
1642 | * @return the output string length, not including the terminating NUL | |
1643 | * @stable ICU 2.0 | |
1644 | */ | |
1645 | int32_t extract(int32_t start, | |
1646 | int32_t startLength, | |
1647 | char *target, | |
1648 | uint32_t targetLength, | |
729e4ab9 | 1649 | const char *codepage) const; |
b75a7d8f A |
1650 | |
1651 | /** | |
1652 | * Convert the UnicodeString into a codepage string using an existing UConverter. | |
1653 | * The output string is NUL-terminated if possible. | |
1654 | * | |
1655 | * This function avoids the overhead of opening and closing a converter if | |
1656 | * multiple strings are extracted. | |
1657 | * | |
1658 | * @param dest destination string buffer, can be NULL if destCapacity==0 | |
1659 | * @param destCapacity the number of chars available at dest | |
1660 | * @param cnv the converter object to be used (ucnv_resetFromUnicode() will be called), | |
1661 | * or NULL for the default converter | |
1662 | * @param errorCode normal ICU error code | |
1663 | * @return the length of the output string, not counting the terminating NUL; | |
1664 | * if the length is greater than destCapacity, then the string will not fit | |
1665 | * and a buffer of the indicated length would need to be passed in | |
1666 | * @stable ICU 2.0 | |
1667 | */ | |
1668 | int32_t extract(char *dest, int32_t destCapacity, | |
1669 | UConverter *cnv, | |
1670 | UErrorCode &errorCode) const; | |
1671 | ||
374ca955 A |
1672 | #endif |
1673 | ||
729e4ab9 A |
1674 | /** |
1675 | * Create a temporary substring for the specified range. | |
1676 | * Unlike the substring constructor and setTo() functions, | |
1677 | * the object returned here will be a read-only alias (using getBuffer()) | |
1678 | * rather than copying the text. | |
1679 | * As a result, this substring operation is much faster but requires | |
1680 | * that the original string not be modified or deleted during the lifetime | |
1681 | * of the returned substring object. | |
1682 | * @param start offset of the first character visible in the substring | |
1683 | * @param length length of the substring | |
1684 | * @return a read-only alias UnicodeString object for the substring | |
1685 | * @stable ICU 4.4 | |
1686 | */ | |
1687 | UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const; | |
1688 | ||
1689 | /** | |
1690 | * Create a temporary substring for the specified range. | |
1691 | * Same as tempSubString(start, length) except that the substring range | |
1692 | * is specified as a (start, limit) pair (with an exclusive limit index) | |
1693 | * rather than a (start, length) pair. | |
1694 | * @param start offset of the first character visible in the substring | |
1695 | * @param limit offset immediately following the last character visible in the substring | |
1696 | * @return a read-only alias UnicodeString object for the substring | |
1697 | * @stable ICU 4.4 | |
1698 | */ | |
1699 | inline UnicodeString tempSubStringBetween(int32_t start, int32_t limit=INT32_MAX) const; | |
1700 | ||
1701 | /** | |
1702 | * Convert the UnicodeString to UTF-8 and write the result | |
1703 | * to a ByteSink. This is called by toUTF8String(). | |
1704 | * Unpaired surrogates are replaced with U+FFFD. | |
1705 | * Calls u_strToUTF8WithSub(). | |
1706 | * | |
1707 | * @param sink A ByteSink to which the UTF-8 version of the string is written. | |
1708 | * sink.Flush() is called at the end. | |
1709 | * @stable ICU 4.2 | |
1710 | * @see toUTF8String | |
1711 | */ | |
1712 | void toUTF8(ByteSink &sink) const; | |
1713 | ||
729e4ab9 A |
1714 | /** |
1715 | * Convert the UnicodeString to UTF-8 and append the result | |
1716 | * to a standard string. | |
1717 | * Unpaired surrogates are replaced with U+FFFD. | |
1718 | * Calls toUTF8(). | |
1719 | * | |
1720 | * @param result A standard string (or a compatible object) | |
1721 | * to which the UTF-8 version of the string is appended. | |
1722 | * @return The string object. | |
1723 | * @stable ICU 4.2 | |
1724 | * @see toUTF8 | |
1725 | */ | |
1726 | template<typename StringClass> | |
1727 | StringClass &toUTF8String(StringClass &result) const { | |
0f5d89e8 | 1728 | StringByteSink<StringClass> sbs(&result, length()); |
729e4ab9 A |
1729 | toUTF8(sbs); |
1730 | return result; | |
1731 | } | |
1732 | ||
729e4ab9 A |
1733 | /** |
1734 | * Convert the UnicodeString to UTF-32. | |
1735 | * Unpaired surrogates are replaced with U+FFFD. | |
1736 | * Calls u_strToUTF32WithSub(). | |
1737 | * | |
1738 | * @param utf32 destination string buffer, can be NULL if capacity==0 | |
1739 | * @param capacity the number of UChar32s available at utf32 | |
1740 | * @param errorCode Standard ICU error code. Its input value must | |
1741 | * pass the U_SUCCESS() test, or else the function returns | |
1742 | * immediately. Check for U_FAILURE() on output or use with | |
1743 | * function chaining. (See User Guide for details.) | |
1744 | * @return The length of the UTF-32 string. | |
1745 | * @see fromUTF32 | |
1746 | * @stable ICU 4.2 | |
1747 | */ | |
1748 | int32_t toUTF32(UChar32 *utf32, int32_t capacity, UErrorCode &errorCode) const; | |
1749 | ||
b75a7d8f A |
1750 | /* Length operations */ |
1751 | ||
1752 | /** | |
374ca955 | 1753 | * Return the length of the UnicodeString object. |
f3c0d7a5 | 1754 | * The length is the number of char16_t code units are in the UnicodeString. |
374ca955 | 1755 | * If you want the number of code points, please use countChar32(). |
b75a7d8f | 1756 | * @return the length of the UnicodeString object |
374ca955 | 1757 | * @see countChar32 |
b75a7d8f A |
1758 | * @stable ICU 2.0 |
1759 | */ | |
1760 | inline int32_t length(void) const; | |
1761 | ||
1762 | /** | |
f3c0d7a5 A |
1763 | * Count Unicode code points in the length char16_t code units of the string. |
1764 | * A code point may occupy either one or two char16_t code units. | |
b75a7d8f A |
1765 | * Counting code points involves reading all code units. |
1766 | * | |
1767 | * This functions is basically the inverse of moveIndex32(). | |
1768 | * | |
1769 | * @param start the index of the first code unit to check | |
f3c0d7a5 | 1770 | * @param length the number of char16_t code units to check |
b75a7d8f | 1771 | * @return the number of code points in the specified code units |
374ca955 | 1772 | * @see length |
b75a7d8f A |
1773 | * @stable ICU 2.0 |
1774 | */ | |
1775 | int32_t | |
1776 | countChar32(int32_t start=0, int32_t length=INT32_MAX) const; | |
1777 | ||
1778 | /** | |
f3c0d7a5 | 1779 | * Check if the length char16_t code units of the string |
b75a7d8f A |
1780 | * contain more Unicode code points than a certain number. |
1781 | * This is more efficient than counting all code points in this part of the string | |
1782 | * and comparing that number with a threshold. | |
1783 | * This function may not need to scan the string at all if the length | |
1784 | * falls within a certain range, and | |
1785 | * never needs to count more than 'number+1' code points. | |
1786 | * Logically equivalent to (countChar32(start, length)>number). | |
f3c0d7a5 | 1787 | * A Unicode code point may occupy either one or two char16_t code units. |
b75a7d8f A |
1788 | * |
1789 | * @param start the index of the first code unit to check (0 for the entire string) | |
f3c0d7a5 | 1790 | * @param length the number of char16_t code units to check |
b75a7d8f A |
1791 | * (use INT32_MAX for the entire string; remember that start/length |
1792 | * values are pinned) | |
1793 | * @param number The number of code points in the (sub)string is compared against | |
1794 | * the 'number' parameter. | |
1795 | * @return Boolean value for whether the string contains more Unicode code points | |
1796 | * than 'number'. Same as (u_countChar32(s, length)>number). | |
1797 | * @see countChar32 | |
1798 | * @see u_strHasMoreChar32Than | |
374ca955 | 1799 | * @stable ICU 2.4 |
b75a7d8f A |
1800 | */ |
1801 | UBool | |
1802 | hasMoreChar32Than(int32_t start, int32_t length, int32_t number) const; | |
1803 | ||
1804 | /** | |
1805 | * Determine if this string is empty. | |
1806 | * @return TRUE if this string contains 0 characters, FALSE otherwise. | |
1807 | * @stable ICU 2.0 | |
1808 | */ | |
1809 | inline UBool isEmpty(void) const; | |
1810 | ||
1811 | /** | |
1812 | * Return the capacity of the internal buffer of the UnicodeString object. | |
1813 | * This is useful together with the getBuffer functions. | |
1814 | * See there for details. | |
1815 | * | |
f3c0d7a5 | 1816 | * @return the number of char16_ts available in the internal buffer |
b75a7d8f A |
1817 | * @see getBuffer |
1818 | * @stable ICU 2.0 | |
1819 | */ | |
1820 | inline int32_t getCapacity(void) const; | |
1821 | ||
1822 | /* Other operations */ | |
1823 | ||
1824 | /** | |
1825 | * Generate a hash code for this object. | |
1826 | * @return The hash code of this UnicodeString. | |
1827 | * @stable ICU 2.0 | |
1828 | */ | |
1829 | inline int32_t hashCode(void) const; | |
1830 | ||
1831 | /** | |
1832 | * Determine if this object contains a valid string. | |
729e4ab9 A |
1833 | * A bogus string has no value. It is different from an empty string, |
1834 | * although in both cases isEmpty() returns TRUE and length() returns 0. | |
1835 | * setToBogus() and isBogus() can be used to indicate that no string value is available. | |
1836 | * For a bogus string, getBuffer() and getTerminatedBuffer() return NULL, and | |
b75a7d8f A |
1837 | * length() returns 0. |
1838 | * | |
57a6839d | 1839 | * @return TRUE if the string is bogus/invalid, FALSE otherwise |
b75a7d8f A |
1840 | * @see setToBogus() |
1841 | * @stable ICU 2.0 | |
1842 | */ | |
1843 | inline UBool isBogus(void) const; | |
1844 | ||
1845 | ||
1846 | //======================================== | |
1847 | // Write operations | |
1848 | //======================================== | |
1849 | ||
1850 | /* Assignment operations */ | |
1851 | ||
1852 | /** | |
1853 | * Assignment operator. Replace the characters in this UnicodeString | |
1854 | * with the characters from <TT>srcText</TT>. | |
2ca993e8 A |
1855 | * |
1856 | * Starting with ICU 2.4, the assignment operator and the copy constructor | |
1857 | * allocate a new buffer and copy the buffer contents even for readonly aliases. | |
1858 | * By contrast, the fastCopyFrom() function implements the old, | |
1859 | * more efficient but less safe behavior | |
1860 | * of making this string also a readonly alias to the same buffer. | |
1861 | * | |
1862 | * If the source object has an "open" buffer from getBuffer(minCapacity), | |
1863 | * then the copy is an empty string. | |
1864 | * | |
b75a7d8f A |
1865 | * @param srcText The text containing the characters to replace |
1866 | * @return a reference to this | |
1867 | * @stable ICU 2.0 | |
2ca993e8 | 1868 | * @see fastCopyFrom |
b75a7d8f A |
1869 | */ |
1870 | UnicodeString &operator=(const UnicodeString &srcText); | |
1871 | ||
1872 | /** | |
1873 | * Almost the same as the assignment operator. | |
1874 | * Replace the characters in this UnicodeString | |
1875 | * with the characters from <code>srcText</code>. | |
1876 | * | |
51004dcb A |
1877 | * This function works the same as the assignment operator |
1878 | * for all strings except for ones that are readonly aliases. | |
1879 | * | |
b75a7d8f A |
1880 | * Starting with ICU 2.4, the assignment operator and the copy constructor |
1881 | * allocate a new buffer and copy the buffer contents even for readonly aliases. | |
1882 | * This function implements the old, more efficient but less safe behavior | |
1883 | * of making this string also a readonly alias to the same buffer. | |
51004dcb | 1884 | * |
b75a7d8f | 1885 | * The fastCopyFrom function must be used only if it is known that the lifetime of |
51004dcb | 1886 | * this UnicodeString does not exceed the lifetime of the aliased buffer |
b75a7d8f | 1887 | * including its contents, for example for strings from resource bundles |
51004dcb | 1888 | * or aliases to string constants. |
b75a7d8f | 1889 | * |
2ca993e8 A |
1890 | * If the source object has an "open" buffer from getBuffer(minCapacity), |
1891 | * then the copy is an empty string. | |
1892 | * | |
b75a7d8f A |
1893 | * @param src The text containing the characters to replace. |
1894 | * @return a reference to this | |
374ca955 | 1895 | * @stable ICU 2.4 |
b75a7d8f A |
1896 | */ |
1897 | UnicodeString &fastCopyFrom(const UnicodeString &src); | |
1898 | ||
2ca993e8 | 1899 | /** |
0f5d89e8 | 1900 | * Move assignment operator; might leave src in bogus state. |
2ca993e8 A |
1901 | * This string will have the same contents and state that the source string had. |
1902 | * The behavior is undefined if *this and src are the same object. | |
1903 | * @param src source string | |
1904 | * @return *this | |
f3c0d7a5 | 1905 | * @stable ICU 56 |
2ca993e8 A |
1906 | */ |
1907 | UnicodeString &operator=(UnicodeString &&src) U_NOEXCEPT { | |
1908 | return moveFrom(src); | |
1909 | } | |
0f5d89e8 | 1910 | |
f3c0d7a5 | 1911 | // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API |
2ca993e8 | 1912 | /** |
0f5d89e8 | 1913 | * Move assignment; might leave src in bogus state. |
2ca993e8 A |
1914 | * This string will have the same contents and state that the source string had. |
1915 | * The behavior is undefined if *this and src are the same object. | |
1916 | * | |
1917 | * Can be called explicitly, does not need C++11 support. | |
1918 | * @param src source string | |
1919 | * @return *this | |
1920 | * @draft ICU 56 | |
1921 | */ | |
1922 | UnicodeString &moveFrom(UnicodeString &src) U_NOEXCEPT; | |
1923 | ||
1924 | /** | |
1925 | * Swap strings. | |
1926 | * @param other other string | |
f3c0d7a5 | 1927 | * @stable ICU 56 |
2ca993e8 A |
1928 | */ |
1929 | void swap(UnicodeString &other) U_NOEXCEPT; | |
2ca993e8 A |
1930 | |
1931 | /** | |
1932 | * Non-member UnicodeString swap function. | |
1933 | * @param s1 will get s2's contents and state | |
1934 | * @param s2 will get s1's contents and state | |
f3c0d7a5 | 1935 | * @stable ICU 56 |
2ca993e8 A |
1936 | */ |
1937 | friend U_COMMON_API inline void U_EXPORT2 | |
1938 | swap(UnicodeString &s1, UnicodeString &s2) U_NOEXCEPT { | |
1939 | s1.swap(s2); | |
1940 | } | |
1941 | ||
b75a7d8f A |
1942 | /** |
1943 | * Assignment operator. Replace the characters in this UnicodeString | |
1944 | * with the code unit <TT>ch</TT>. | |
1945 | * @param ch the code unit to replace | |
1946 | * @return a reference to this | |
1947 | * @stable ICU 2.0 | |
1948 | */ | |
f3c0d7a5 | 1949 | inline UnicodeString& operator= (char16_t ch); |
b75a7d8f A |
1950 | |
1951 | /** | |
1952 | * Assignment operator. Replace the characters in this UnicodeString | |
1953 | * with the code point <TT>ch</TT>. | |
1954 | * @param ch the code point to replace | |
1955 | * @return a reference to this | |
1956 | * @stable ICU 2.0 | |
1957 | */ | |
1958 | inline UnicodeString& operator= (UChar32 ch); | |
1959 | ||
1960 | /** | |
1961 | * Set the text in the UnicodeString object to the characters | |
374ca955 | 1962 | * in <TT>srcText</TT> in the range |
b75a7d8f A |
1963 | * [<TT>srcStart</TT>, <TT>srcText.length()</TT>). |
1964 | * <TT>srcText</TT> is not modified. | |
1965 | * @param srcText the source for the new characters | |
1966 | * @param srcStart the offset into <TT>srcText</TT> where new characters | |
1967 | * will be obtained | |
1968 | * @return a reference to this | |
374ca955 | 1969 | * @stable ICU 2.2 |
b75a7d8f | 1970 | */ |
374ca955 | 1971 | inline UnicodeString& setTo(const UnicodeString& srcText, |
b75a7d8f A |
1972 | int32_t srcStart); |
1973 | ||
1974 | /** | |
1975 | * Set the text in the UnicodeString object to the characters | |
374ca955 | 1976 | * in <TT>srcText</TT> in the range |
b75a7d8f A |
1977 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). |
1978 | * <TT>srcText</TT> is not modified. | |
1979 | * @param srcText the source for the new characters | |
1980 | * @param srcStart the offset into <TT>srcText</TT> where new characters | |
1981 | * will be obtained | |
1982 | * @param srcLength the number of characters in <TT>srcText</TT> in the | |
1983 | * replace string. | |
1984 | * @return a reference to this | |
1985 | * @stable ICU 2.0 | |
1986 | */ | |
374ca955 A |
1987 | inline UnicodeString& setTo(const UnicodeString& srcText, |
1988 | int32_t srcStart, | |
b75a7d8f A |
1989 | int32_t srcLength); |
1990 | ||
1991 | /** | |
374ca955 A |
1992 | * Set the text in the UnicodeString object to the characters in |
1993 | * <TT>srcText</TT>. | |
b75a7d8f A |
1994 | * <TT>srcText</TT> is not modified. |
1995 | * @param srcText the source for the new characters | |
1996 | * @return a reference to this | |
1997 | * @stable ICU 2.0 | |
1998 | */ | |
1999 | inline UnicodeString& setTo(const UnicodeString& srcText); | |
2000 | ||
2001 | /** | |
2002 | * Set the characters in the UnicodeString object to the characters | |
2003 | * in <TT>srcChars</TT>. <TT>srcChars</TT> is not modified. | |
2004 | * @param srcChars the source for the new characters | |
2005 | * @param srcLength the number of Unicode characters in srcChars. | |
2006 | * @return a reference to this | |
2007 | * @stable ICU 2.0 | |
2008 | */ | |
f3c0d7a5 | 2009 | inline UnicodeString& setTo(const char16_t *srcChars, |
b75a7d8f A |
2010 | int32_t srcLength); |
2011 | ||
2012 | /** | |
2013 | * Set the characters in the UnicodeString object to the code unit | |
2014 | * <TT>srcChar</TT>. | |
374ca955 | 2015 | * @param srcChar the code unit which becomes the UnicodeString's character |
b75a7d8f A |
2016 | * content |
2017 | * @return a reference to this | |
2018 | * @stable ICU 2.0 | |
2019 | */ | |
f3c0d7a5 | 2020 | UnicodeString& setTo(char16_t srcChar); |
b75a7d8f A |
2021 | |
2022 | /** | |
2023 | * Set the characters in the UnicodeString object to the code point | |
2024 | * <TT>srcChar</TT>. | |
374ca955 | 2025 | * @param srcChar the code point which becomes the UnicodeString's character |
b75a7d8f A |
2026 | * content |
2027 | * @return a reference to this | |
2028 | * @stable ICU 2.0 | |
2029 | */ | |
2030 | UnicodeString& setTo(UChar32 srcChar); | |
2031 | ||
2032 | /** | |
f3c0d7a5 | 2033 | * Aliasing setTo() function, analogous to the readonly-aliasing char16_t* constructor. |
b75a7d8f A |
2034 | * The text will be used for the UnicodeString object, but |
2035 | * it will not be released when the UnicodeString is destroyed. | |
2036 | * This has copy-on-write semantics: | |
2037 | * When the string is modified, then the buffer is first copied into | |
2038 | * newly allocated memory. | |
2039 | * The aliased buffer is never modified. | |
51004dcb A |
2040 | * |
2041 | * In an assignment to another UnicodeString, when using the copy constructor | |
2042 | * or the assignment operator, the text will be copied. | |
2043 | * When using fastCopyFrom(), the text will be aliased again, | |
b75a7d8f A |
2044 | * so that both strings then alias the same readonly-text. |
2045 | * | |
2046 | * @param isTerminated specifies if <code>text</code> is <code>NUL</code>-terminated. | |
2047 | * This must be true if <code>textLength==-1</code>. | |
2048 | * @param text The characters to alias for the UnicodeString. | |
2049 | * @param textLength The number of Unicode characters in <code>text</code> to alias. | |
2050 | * If -1, then this constructor will determine the length | |
2051 | * by calling <code>u_strlen()</code>. | |
2052 | * @return a reference to this | |
2053 | * @stable ICU 2.0 | |
2054 | */ | |
2055 | UnicodeString &setTo(UBool isTerminated, | |
f3c0d7a5 | 2056 | ConstChar16Ptr text, |
b75a7d8f A |
2057 | int32_t textLength); |
2058 | ||
2059 | /** | |
f3c0d7a5 | 2060 | * Aliasing setTo() function, analogous to the writable-aliasing char16_t* constructor. |
b75a7d8f A |
2061 | * The text will be used for the UnicodeString object, but |
2062 | * it will not be released when the UnicodeString is destroyed. | |
2063 | * This has write-through semantics: | |
2064 | * For as long as the capacity of the buffer is sufficient, write operations | |
2065 | * will directly affect the buffer. When more capacity is necessary, then | |
2066 | * a new buffer will be allocated and the contents copied as with regularly | |
2067 | * constructed strings. | |
2068 | * In an assignment to another UnicodeString, the buffer will be copied. | |
f3c0d7a5 | 2069 | * The extract(Char16Ptr dst) function detects whether the dst pointer is the same |
b75a7d8f A |
2070 | * as the string buffer itself and will in this case not copy the contents. |
2071 | * | |
2072 | * @param buffer The characters to alias for the UnicodeString. | |
2073 | * @param buffLength The number of Unicode characters in <code>buffer</code> to alias. | |
f3c0d7a5 | 2074 | * @param buffCapacity The size of <code>buffer</code> in char16_ts. |
b75a7d8f A |
2075 | * @return a reference to this |
2076 | * @stable ICU 2.0 | |
2077 | */ | |
f3c0d7a5 | 2078 | UnicodeString &setTo(char16_t *buffer, |
b75a7d8f A |
2079 | int32_t buffLength, |
2080 | int32_t buffCapacity); | |
2081 | ||
2082 | /** | |
2083 | * Make this UnicodeString object invalid. | |
2084 | * The string will test TRUE with isBogus(). | |
2085 | * | |
2086 | * A bogus string has no value. It is different from an empty string. | |
2087 | * It can be used to indicate that no string value is available. | |
2088 | * getBuffer() and getTerminatedBuffer() return NULL, and | |
2089 | * length() returns 0. | |
2090 | * | |
2091 | * This utility function is used throughout the UnicodeString | |
2092 | * implementation to indicate that a UnicodeString operation failed, | |
2093 | * and may be used in other functions, | |
2094 | * especially but not exclusively when such functions do not | |
2095 | * take a UErrorCode for simplicity. | |
2096 | * | |
2097 | * The following methods, and no others, will clear a string object's bogus flag: | |
2098 | * - remove() | |
2099 | * - remove(0, INT32_MAX) | |
2100 | * - truncate(0) | |
2101 | * - operator=() (assignment operator) | |
2102 | * - setTo(...) | |
2103 | * | |
2104 | * The simplest ways to turn a bogus string into an empty one | |
2105 | * is to use the remove() function. | |
2106 | * Examples for other functions that are equivalent to "set to empty string": | |
2107 | * \code | |
2108 | * if(s.isBogus()) { | |
2109 | * s.remove(); // set to an empty string (remove all), or | |
2110 | * s.remove(0, INT32_MAX); // set to an empty string (remove all), or | |
2111 | * s.truncate(0); // set to an empty string (complete truncation), or | |
2112 | * s=UnicodeString(); // assign an empty string, or | |
2113 | * s.setTo((UChar32)-1); // set to a pseudo code point that is out of range, or | |
f3c0d7a5 | 2114 | * static const char16_t nul=0; |
b75a7d8f A |
2115 | * s.setTo(&nul, 0); // set to an empty C Unicode string |
2116 | * } | |
2117 | * \endcode | |
2118 | * | |
2119 | * @see isBogus() | |
2120 | * @stable ICU 2.0 | |
2121 | */ | |
2122 | void setToBogus(); | |
2123 | ||
2124 | /** | |
2125 | * Set the character at the specified offset to the specified character. | |
2126 | * @param offset A valid offset into the text of the character to set | |
2127 | * @param ch The new character | |
2128 | * @return A reference to this | |
2129 | * @stable ICU 2.0 | |
2130 | */ | |
374ca955 | 2131 | UnicodeString& setCharAt(int32_t offset, |
f3c0d7a5 | 2132 | char16_t ch); |
b75a7d8f A |
2133 | |
2134 | ||
2135 | /* Append operations */ | |
2136 | ||
2137 | /** | |
2138 | * Append operator. Append the code unit <TT>ch</TT> to the UnicodeString | |
2139 | * object. | |
2140 | * @param ch the code unit to be appended | |
2141 | * @return a reference to this | |
2142 | * @stable ICU 2.0 | |
2143 | */ | |
f3c0d7a5 | 2144 | inline UnicodeString& operator+= (char16_t ch); |
b75a7d8f A |
2145 | |
2146 | /** | |
2147 | * Append operator. Append the code point <TT>ch</TT> to the UnicodeString | |
2148 | * object. | |
2149 | * @param ch the code point to be appended | |
2150 | * @return a reference to this | |
2151 | * @stable ICU 2.0 | |
2152 | */ | |
2153 | inline UnicodeString& operator+= (UChar32 ch); | |
2154 | ||
2155 | /** | |
2156 | * Append operator. Append the characters in <TT>srcText</TT> to the | |
4388f060 | 2157 | * UnicodeString object. <TT>srcText</TT> is not modified. |
b75a7d8f A |
2158 | * @param srcText the source for the new characters |
2159 | * @return a reference to this | |
2160 | * @stable ICU 2.0 | |
2161 | */ | |
2162 | inline UnicodeString& operator+= (const UnicodeString& srcText); | |
2163 | ||
2164 | /** | |
2165 | * Append the characters | |
374ca955 A |
2166 | * in <TT>srcText</TT> in the range |
2167 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) to the | |
2168 | * UnicodeString object at offset <TT>start</TT>. <TT>srcText</TT> | |
b75a7d8f A |
2169 | * is not modified. |
2170 | * @param srcText the source for the new characters | |
374ca955 | 2171 | * @param srcStart the offset into <TT>srcText</TT> where new characters |
b75a7d8f | 2172 | * will be obtained |
374ca955 | 2173 | * @param srcLength the number of characters in <TT>srcText</TT> in |
b75a7d8f A |
2174 | * the append string |
2175 | * @return a reference to this | |
2176 | * @stable ICU 2.0 | |
2177 | */ | |
374ca955 A |
2178 | inline UnicodeString& append(const UnicodeString& srcText, |
2179 | int32_t srcStart, | |
b75a7d8f A |
2180 | int32_t srcLength); |
2181 | ||
2182 | /** | |
4388f060 A |
2183 | * Append the characters in <TT>srcText</TT> to the UnicodeString object. |
2184 | * <TT>srcText</TT> is not modified. | |
b75a7d8f A |
2185 | * @param srcText the source for the new characters |
2186 | * @return a reference to this | |
2187 | * @stable ICU 2.0 | |
2188 | */ | |
2189 | inline UnicodeString& append(const UnicodeString& srcText); | |
2190 | ||
2191 | /** | |
374ca955 A |
2192 | * Append the characters in <TT>srcChars</TT> in the range |
2193 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) to the UnicodeString | |
2194 | * object at offset | |
b75a7d8f A |
2195 | * <TT>start</TT>. <TT>srcChars</TT> is not modified. |
2196 | * @param srcChars the source for the new characters | |
374ca955 | 2197 | * @param srcStart the offset into <TT>srcChars</TT> where new characters |
b75a7d8f | 2198 | * will be obtained |
374ca955 | 2199 | * @param srcLength the number of characters in <TT>srcChars</TT> in |
4388f060 | 2200 | * the append string; can be -1 if <TT>srcChars</TT> is NUL-terminated |
b75a7d8f A |
2201 | * @return a reference to this |
2202 | * @stable ICU 2.0 | |
2203 | */ | |
f3c0d7a5 | 2204 | inline UnicodeString& append(const char16_t *srcChars, |
374ca955 | 2205 | int32_t srcStart, |
b75a7d8f A |
2206 | int32_t srcLength); |
2207 | ||
2208 | /** | |
374ca955 | 2209 | * Append the characters in <TT>srcChars</TT> to the UnicodeString object |
b75a7d8f A |
2210 | * at offset <TT>start</TT>. <TT>srcChars</TT> is not modified. |
2211 | * @param srcChars the source for the new characters | |
4388f060 A |
2212 | * @param srcLength the number of Unicode characters in <TT>srcChars</TT>; |
2213 | * can be -1 if <TT>srcChars</TT> is NUL-terminated | |
b75a7d8f A |
2214 | * @return a reference to this |
2215 | * @stable ICU 2.0 | |
2216 | */ | |
f3c0d7a5 | 2217 | inline UnicodeString& append(ConstChar16Ptr srcChars, |
b75a7d8f A |
2218 | int32_t srcLength); |
2219 | ||
2220 | /** | |
2221 | * Append the code unit <TT>srcChar</TT> to the UnicodeString object. | |
2222 | * @param srcChar the code unit to append | |
2223 | * @return a reference to this | |
2224 | * @stable ICU 2.0 | |
2225 | */ | |
f3c0d7a5 | 2226 | inline UnicodeString& append(char16_t srcChar); |
b75a7d8f A |
2227 | |
2228 | /** | |
2229 | * Append the code point <TT>srcChar</TT> to the UnicodeString object. | |
2230 | * @param srcChar the code point to append | |
2231 | * @return a reference to this | |
2232 | * @stable ICU 2.0 | |
2233 | */ | |
4388f060 | 2234 | UnicodeString& append(UChar32 srcChar); |
b75a7d8f A |
2235 | |
2236 | ||
2237 | /* Insert operations */ | |
2238 | ||
2239 | /** | |
374ca955 A |
2240 | * Insert the characters in <TT>srcText</TT> in the range |
2241 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) into the UnicodeString | |
b75a7d8f A |
2242 | * object at offset <TT>start</TT>. <TT>srcText</TT> is not modified. |
2243 | * @param start the offset where the insertion begins | |
374ca955 A |
2244 | * @param srcText the source for the new characters |
2245 | * @param srcStart the offset into <TT>srcText</TT> where new characters | |
b75a7d8f | 2246 | * will be obtained |
374ca955 | 2247 | * @param srcLength the number of characters in <TT>srcText</TT> in |
b75a7d8f A |
2248 | * the insert string |
2249 | * @return a reference to this | |
2250 | * @stable ICU 2.0 | |
2251 | */ | |
374ca955 A |
2252 | inline UnicodeString& insert(int32_t start, |
2253 | const UnicodeString& srcText, | |
2254 | int32_t srcStart, | |
b75a7d8f A |
2255 | int32_t srcLength); |
2256 | ||
2257 | /** | |
2258 | * Insert the characters in <TT>srcText</TT> into the UnicodeString object | |
2259 | * at offset <TT>start</TT>. <TT>srcText</TT> is not modified. | |
2260 | * @param start the offset where the insertion begins | |
374ca955 | 2261 | * @param srcText the source for the new characters |
b75a7d8f A |
2262 | * @return a reference to this |
2263 | * @stable ICU 2.0 | |
2264 | */ | |
374ca955 | 2265 | inline UnicodeString& insert(int32_t start, |
b75a7d8f A |
2266 | const UnicodeString& srcText); |
2267 | ||
2268 | /** | |
374ca955 | 2269 | * Insert the characters in <TT>srcChars</TT> in the range |
b75a7d8f A |
2270 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) into the UnicodeString |
2271 | * object at offset <TT>start</TT>. <TT>srcChars</TT> is not modified. | |
2272 | * @param start the offset at which the insertion begins | |
2273 | * @param srcChars the source for the new characters | |
374ca955 | 2274 | * @param srcStart the offset into <TT>srcChars</TT> where new characters |
b75a7d8f | 2275 | * will be obtained |
374ca955 | 2276 | * @param srcLength the number of characters in <TT>srcChars</TT> |
b75a7d8f A |
2277 | * in the insert string |
2278 | * @return a reference to this | |
2279 | * @stable ICU 2.0 | |
2280 | */ | |
374ca955 | 2281 | inline UnicodeString& insert(int32_t start, |
f3c0d7a5 | 2282 | const char16_t *srcChars, |
374ca955 | 2283 | int32_t srcStart, |
b75a7d8f A |
2284 | int32_t srcLength); |
2285 | ||
2286 | /** | |
374ca955 | 2287 | * Insert the characters in <TT>srcChars</TT> into the UnicodeString object |
b75a7d8f A |
2288 | * at offset <TT>start</TT>. <TT>srcChars</TT> is not modified. |
2289 | * @param start the offset where the insertion begins | |
2290 | * @param srcChars the source for the new characters | |
2291 | * @param srcLength the number of Unicode characters in srcChars. | |
2292 | * @return a reference to this | |
2293 | * @stable ICU 2.0 | |
2294 | */ | |
374ca955 | 2295 | inline UnicodeString& insert(int32_t start, |
f3c0d7a5 | 2296 | ConstChar16Ptr srcChars, |
b75a7d8f A |
2297 | int32_t srcLength); |
2298 | ||
2299 | /** | |
374ca955 | 2300 | * Insert the code unit <TT>srcChar</TT> into the UnicodeString object at |
b75a7d8f A |
2301 | * offset <TT>start</TT>. |
2302 | * @param start the offset at which the insertion occurs | |
2303 | * @param srcChar the code unit to insert | |
2304 | * @return a reference to this | |
2305 | * @stable ICU 2.0 | |
2306 | */ | |
374ca955 | 2307 | inline UnicodeString& insert(int32_t start, |
f3c0d7a5 | 2308 | char16_t srcChar); |
b75a7d8f A |
2309 | |
2310 | /** | |
374ca955 | 2311 | * Insert the code point <TT>srcChar</TT> into the UnicodeString object at |
b75a7d8f A |
2312 | * offset <TT>start</TT>. |
2313 | * @param start the offset at which the insertion occurs | |
2314 | * @param srcChar the code point to insert | |
2315 | * @return a reference to this | |
2316 | * @stable ICU 2.0 | |
2317 | */ | |
374ca955 | 2318 | inline UnicodeString& insert(int32_t start, |
b75a7d8f A |
2319 | UChar32 srcChar); |
2320 | ||
2321 | ||
2322 | /* Replace operations */ | |
2323 | ||
2324 | /** | |
374ca955 A |
2325 | * Replace the characters in the range |
2326 | * [<TT>start</TT>, <TT>start + length</TT>) with the characters in | |
2327 | * <TT>srcText</TT> in the range | |
2328 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). | |
b75a7d8f A |
2329 | * <TT>srcText</TT> is not modified. |
2330 | * @param start the offset at which the replace operation begins | |
374ca955 | 2331 | * @param length the number of characters to replace. The character at |
b75a7d8f A |
2332 | * <TT>start + length</TT> is not modified. |
2333 | * @param srcText the source for the new characters | |
374ca955 | 2334 | * @param srcStart the offset into <TT>srcText</TT> where new characters |
b75a7d8f | 2335 | * will be obtained |
374ca955 | 2336 | * @param srcLength the number of characters in <TT>srcText</TT> in |
b75a7d8f A |
2337 | * the replace string |
2338 | * @return a reference to this | |
2339 | * @stable ICU 2.0 | |
2340 | */ | |
374ca955 A |
2341 | UnicodeString& replace(int32_t start, |
2342 | int32_t length, | |
2343 | const UnicodeString& srcText, | |
2344 | int32_t srcStart, | |
b75a7d8f A |
2345 | int32_t srcLength); |
2346 | ||
2347 | /** | |
374ca955 A |
2348 | * Replace the characters in the range |
2349 | * [<TT>start</TT>, <TT>start + length</TT>) | |
b75a7d8f A |
2350 | * with the characters in <TT>srcText</TT>. <TT>srcText</TT> is |
2351 | * not modified. | |
2352 | * @param start the offset at which the replace operation begins | |
2353 | * @param length the number of characters to replace. The character at | |
2354 | * <TT>start + length</TT> is not modified. | |
2355 | * @param srcText the source for the new characters | |
2356 | * @return a reference to this | |
2357 | * @stable ICU 2.0 | |
2358 | */ | |
374ca955 A |
2359 | UnicodeString& replace(int32_t start, |
2360 | int32_t length, | |
b75a7d8f A |
2361 | const UnicodeString& srcText); |
2362 | ||
2363 | /** | |
374ca955 A |
2364 | * Replace the characters in the range |
2365 | * [<TT>start</TT>, <TT>start + length</TT>) with the characters in | |
2366 | * <TT>srcChars</TT> in the range | |
2367 | * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). <TT>srcChars</TT> | |
b75a7d8f A |
2368 | * is not modified. |
2369 | * @param start the offset at which the replace operation begins | |
2370 | * @param length the number of characters to replace. The character at | |
2371 | * <TT>start + length</TT> is not modified. | |
2372 | * @param srcChars the source for the new characters | |
2373 | * @param srcStart the offset into <TT>srcChars</TT> where new characters | |
2374 | * will be obtained | |
374ca955 | 2375 | * @param srcLength the number of characters in <TT>srcChars</TT> |
b75a7d8f A |
2376 | * in the replace string |
2377 | * @return a reference to this | |
2378 | * @stable ICU 2.0 | |
2379 | */ | |
374ca955 A |
2380 | UnicodeString& replace(int32_t start, |
2381 | int32_t length, | |
f3c0d7a5 | 2382 | const char16_t *srcChars, |
374ca955 | 2383 | int32_t srcStart, |
b75a7d8f A |
2384 | int32_t srcLength); |
2385 | ||
2386 | /** | |
374ca955 | 2387 | * Replace the characters in the range |
b75a7d8f A |
2388 | * [<TT>start</TT>, <TT>start + length</TT>) with the characters in |
2389 | * <TT>srcChars</TT>. <TT>srcChars</TT> is not modified. | |
2390 | * @param start the offset at which the replace operation begins | |
2391 | * @param length number of characters to replace. The character at | |
2392 | * <TT>start + length</TT> is not modified. | |
2393 | * @param srcChars the source for the new characters | |
2394 | * @param srcLength the number of Unicode characters in srcChars | |
2395 | * @return a reference to this | |
2396 | * @stable ICU 2.0 | |
2397 | */ | |
374ca955 A |
2398 | inline UnicodeString& replace(int32_t start, |
2399 | int32_t length, | |
f3c0d7a5 | 2400 | ConstChar16Ptr srcChars, |
b75a7d8f A |
2401 | int32_t srcLength); |
2402 | ||
2403 | /** | |
374ca955 | 2404 | * Replace the characters in the range |
b75a7d8f A |
2405 | * [<TT>start</TT>, <TT>start + length</TT>) with the code unit |
2406 | * <TT>srcChar</TT>. | |
2407 | * @param start the offset at which the replace operation begins | |
2408 | * @param length the number of characters to replace. The character at | |
2409 | * <TT>start + length</TT> is not modified. | |
2410 | * @param srcChar the new code unit | |
2411 | * @return a reference to this | |
2412 | * @stable ICU 2.0 | |
2413 | */ | |
374ca955 A |
2414 | inline UnicodeString& replace(int32_t start, |
2415 | int32_t length, | |
f3c0d7a5 | 2416 | char16_t srcChar); |
b75a7d8f A |
2417 | |
2418 | /** | |
374ca955 | 2419 | * Replace the characters in the range |
b75a7d8f A |
2420 | * [<TT>start</TT>, <TT>start + length</TT>) with the code point |
2421 | * <TT>srcChar</TT>. | |
2422 | * @param start the offset at which the replace operation begins | |
2423 | * @param length the number of characters to replace. The character at | |
2424 | * <TT>start + length</TT> is not modified. | |
2425 | * @param srcChar the new code point | |
2426 | * @return a reference to this | |
2427 | * @stable ICU 2.0 | |
2428 | */ | |
4388f060 | 2429 | UnicodeString& replace(int32_t start, int32_t length, UChar32 srcChar); |
b75a7d8f A |
2430 | |
2431 | /** | |
374ca955 | 2432 | * Replace the characters in the range [<TT>start</TT>, <TT>limit</TT>) |
b75a7d8f A |
2433 | * with the characters in <TT>srcText</TT>. <TT>srcText</TT> is not modified. |
2434 | * @param start the offset at which the replace operation begins | |
2435 | * @param limit the offset immediately following the replace range | |
2436 | * @param srcText the source for the new characters | |
2437 | * @return a reference to this | |
2438 | * @stable ICU 2.0 | |
2439 | */ | |
374ca955 A |
2440 | inline UnicodeString& replaceBetween(int32_t start, |
2441 | int32_t limit, | |
b75a7d8f A |
2442 | const UnicodeString& srcText); |
2443 | ||
2444 | /** | |
374ca955 A |
2445 | * Replace the characters in the range [<TT>start</TT>, <TT>limit</TT>) |
2446 | * with the characters in <TT>srcText</TT> in the range | |
b75a7d8f A |
2447 | * [<TT>srcStart</TT>, <TT>srcLimit</TT>). <TT>srcText</TT> is not modified. |
2448 | * @param start the offset at which the replace operation begins | |
2449 | * @param limit the offset immediately following the replace range | |
2450 | * @param srcText the source for the new characters | |
374ca955 | 2451 | * @param srcStart the offset into <TT>srcChars</TT> where new characters |
b75a7d8f | 2452 | * will be obtained |
374ca955 | 2453 | * @param srcLimit the offset immediately following the range to copy |
b75a7d8f A |
2454 | * in <TT>srcText</TT> |
2455 | * @return a reference to this | |
2456 | * @stable ICU 2.0 | |
2457 | */ | |
374ca955 A |
2458 | inline UnicodeString& replaceBetween(int32_t start, |
2459 | int32_t limit, | |
2460 | const UnicodeString& srcText, | |
2461 | int32_t srcStart, | |
b75a7d8f A |
2462 | int32_t srcLimit); |
2463 | ||
2464 | /** | |
2465 | * Replace a substring of this object with the given text. | |
2466 | * @param start the beginning index, inclusive; <code>0 <= start | |
2467 | * <= limit</code>. | |
2468 | * @param limit the ending index, exclusive; <code>start <= limit | |
2469 | * <= length()</code>. | |
2470 | * @param text the text to replace characters <code>start</code> | |
2471 | * to <code>limit - 1</code> | |
2472 | * @stable ICU 2.0 | |
2473 | */ | |
2474 | virtual void handleReplaceBetween(int32_t start, | |
2475 | int32_t limit, | |
2476 | const UnicodeString& text); | |
2477 | ||
2478 | /** | |
2479 | * Replaceable API | |
2480 | * @return TRUE if it has MetaData | |
374ca955 | 2481 | * @stable ICU 2.4 |
b75a7d8f A |
2482 | */ |
2483 | virtual UBool hasMetaData() const; | |
374ca955 | 2484 | |
b75a7d8f A |
2485 | /** |
2486 | * Copy a substring of this object, retaining attribute (out-of-band) | |
2487 | * information. This method is used to duplicate or reorder substrings. | |
2488 | * The destination index must not overlap the source range. | |
374ca955 | 2489 | * |
b75a7d8f A |
2490 | * @param start the beginning index, inclusive; <code>0 <= start <= |
2491 | * limit</code>. | |
2492 | * @param limit the ending index, exclusive; <code>start <= limit <= | |
2493 | * length()</code>. | |
2494 | * @param dest the destination index. The characters from | |
2495 | * <code>start..limit-1</code> will be copied to <code>dest</code>. | |
2496 | * Implementations of this method may assume that <code>dest <= start || | |
2497 | * dest >= limit</code>. | |
2498 | * @stable ICU 2.0 | |
2499 | */ | |
2500 | virtual void copy(int32_t start, int32_t limit, int32_t dest); | |
2501 | ||
2502 | /* Search and replace operations */ | |
2503 | ||
2504 | /** | |
374ca955 | 2505 | * Replace all occurrences of characters in oldText with the characters |
b75a7d8f A |
2506 | * in newText |
2507 | * @param oldText the text containing the search text | |
2508 | * @param newText the text containing the replacement text | |
2509 | * @return a reference to this | |
2510 | * @stable ICU 2.0 | |
2511 | */ | |
2512 | inline UnicodeString& findAndReplace(const UnicodeString& oldText, | |
2513 | const UnicodeString& newText); | |
2514 | ||
2515 | /** | |
374ca955 | 2516 | * Replace all occurrences of characters in oldText with characters |
b75a7d8f A |
2517 | * in newText |
2518 | * in the range [<TT>start</TT>, <TT>start + length</TT>). | |
2519 | * @param start the start of the range in which replace will performed | |
2520 | * @param length the length of the range in which replace will be performed | |
2521 | * @param oldText the text containing the search text | |
2522 | * @param newText the text containing the replacement text | |
2523 | * @return a reference to this | |
2524 | * @stable ICU 2.0 | |
2525 | */ | |
2526 | inline UnicodeString& findAndReplace(int32_t start, | |
2527 | int32_t length, | |
2528 | const UnicodeString& oldText, | |
2529 | const UnicodeString& newText); | |
2530 | ||
2531 | /** | |
374ca955 A |
2532 | * Replace all occurrences of characters in oldText in the range |
2533 | * [<TT>oldStart</TT>, <TT>oldStart + oldLength</TT>) with the characters | |
2534 | * in newText in the range | |
2535 | * [<TT>newStart</TT>, <TT>newStart + newLength</TT>) | |
b75a7d8f A |
2536 | * in the range [<TT>start</TT>, <TT>start + length</TT>). |
2537 | * @param start the start of the range in which replace will performed | |
2538 | * @param length the length of the range in which replace will be performed | |
2539 | * @param oldText the text containing the search text | |
2540 | * @param oldStart the start of the search range in <TT>oldText</TT> | |
2541 | * @param oldLength the length of the search range in <TT>oldText</TT> | |
2542 | * @param newText the text containing the replacement text | |
2543 | * @param newStart the start of the replacement range in <TT>newText</TT> | |
2544 | * @param newLength the length of the replacement range in <TT>newText</TT> | |
2545 | * @return a reference to this | |
2546 | * @stable ICU 2.0 | |
2547 | */ | |
2548 | UnicodeString& findAndReplace(int32_t start, | |
2549 | int32_t length, | |
2550 | const UnicodeString& oldText, | |
2551 | int32_t oldStart, | |
2552 | int32_t oldLength, | |
2553 | const UnicodeString& newText, | |
2554 | int32_t newStart, | |
2555 | int32_t newLength); | |
2556 | ||
2557 | ||
2558 | /* Remove operations */ | |
2559 | ||
2560 | /** | |
2561 | * Remove all characters from the UnicodeString object. | |
2562 | * @return a reference to this | |
2563 | * @stable ICU 2.0 | |
2564 | */ | |
2565 | inline UnicodeString& remove(void); | |
2566 | ||
2567 | /** | |
374ca955 | 2568 | * Remove the characters in the range |
b75a7d8f A |
2569 | * [<TT>start</TT>, <TT>start + length</TT>) from the UnicodeString object. |
2570 | * @param start the offset of the first character to remove | |
2571 | * @param length the number of characters to remove | |
2572 | * @return a reference to this | |
2573 | * @stable ICU 2.0 | |
2574 | */ | |
374ca955 | 2575 | inline UnicodeString& remove(int32_t start, |
b75a7d8f A |
2576 | int32_t length = (int32_t)INT32_MAX); |
2577 | ||
2578 | /** | |
374ca955 | 2579 | * Remove the characters in the range |
b75a7d8f A |
2580 | * [<TT>start</TT>, <TT>limit</TT>) from the UnicodeString object. |
2581 | * @param start the offset of the first character to remove | |
2582 | * @param limit the offset immediately following the range to remove | |
2583 | * @return a reference to this | |
2584 | * @stable ICU 2.0 | |
2585 | */ | |
2586 | inline UnicodeString& removeBetween(int32_t start, | |
2587 | int32_t limit = (int32_t)INT32_MAX); | |
2588 | ||
729e4ab9 A |
2589 | /** |
2590 | * Retain only the characters in the range | |
2591 | * [<code>start</code>, <code>limit</code>) from the UnicodeString object. | |
2592 | * Removes characters before <code>start</code> and at and after <code>limit</code>. | |
2593 | * @param start the offset of the first character to retain | |
2594 | * @param limit the offset immediately following the range to retain | |
2595 | * @return a reference to this | |
2596 | * @stable ICU 4.4 | |
2597 | */ | |
2598 | inline UnicodeString &retainBetween(int32_t start, int32_t limit = INT32_MAX); | |
b75a7d8f A |
2599 | |
2600 | /* Length operations */ | |
2601 | ||
2602 | /** | |
374ca955 A |
2603 | * Pad the start of this UnicodeString with the character <TT>padChar</TT>. |
2604 | * If the length of this UnicodeString is less than targetLength, | |
b75a7d8f A |
2605 | * length() - targetLength copies of padChar will be added to the |
2606 | * beginning of this UnicodeString. | |
2607 | * @param targetLength the desired length of the string | |
374ca955 | 2608 | * @param padChar the character to use for padding. Defaults to |
b75a7d8f A |
2609 | * space (U+0020) |
2610 | * @return TRUE if the text was padded, FALSE otherwise. | |
2611 | * @stable ICU 2.0 | |
2612 | */ | |
2613 | UBool padLeading(int32_t targetLength, | |
f3c0d7a5 | 2614 | char16_t padChar = 0x0020); |
b75a7d8f A |
2615 | |
2616 | /** | |
374ca955 A |
2617 | * Pad the end of this UnicodeString with the character <TT>padChar</TT>. |
2618 | * If the length of this UnicodeString is less than targetLength, | |
b75a7d8f A |
2619 | * length() - targetLength copies of padChar will be added to the |
2620 | * end of this UnicodeString. | |
2621 | * @param targetLength the desired length of the string | |
374ca955 | 2622 | * @param padChar the character to use for padding. Defaults to |
b75a7d8f A |
2623 | * space (U+0020) |
2624 | * @return TRUE if the text was padded, FALSE otherwise. | |
2625 | * @stable ICU 2.0 | |
2626 | */ | |
2627 | UBool padTrailing(int32_t targetLength, | |
f3c0d7a5 | 2628 | char16_t padChar = 0x0020); |
b75a7d8f A |
2629 | |
2630 | /** | |
2631 | * Truncate this UnicodeString to the <TT>targetLength</TT>. | |
2632 | * @param targetLength the desired length of this UnicodeString. | |
2633 | * @return TRUE if the text was truncated, FALSE otherwise | |
2634 | * @stable ICU 2.0 | |
2635 | */ | |
2636 | inline UBool truncate(int32_t targetLength); | |
2637 | ||
2638 | /** | |
2639 | * Trims leading and trailing whitespace from this UnicodeString. | |
2640 | * @return a reference to this | |
2641 | * @stable ICU 2.0 | |
2642 | */ | |
2643 | UnicodeString& trim(void); | |
2644 | ||
2645 | ||
2646 | /* Miscellaneous operations */ | |
2647 | ||
2648 | /** | |
2649 | * Reverse this UnicodeString in place. | |
2650 | * @return a reference to this | |
2651 | * @stable ICU 2.0 | |
2652 | */ | |
2653 | inline UnicodeString& reverse(void); | |
2654 | ||
2655 | /** | |
2656 | * Reverse the range [<TT>start</TT>, <TT>start + length</TT>) in | |
2657 | * this UnicodeString. | |
2658 | * @param start the start of the range to reverse | |
2659 | * @param length the number of characters to to reverse | |
2660 | * @return a reference to this | |
2661 | * @stable ICU 2.0 | |
2662 | */ | |
2663 | inline UnicodeString& reverse(int32_t start, | |
2664 | int32_t length); | |
2665 | ||
2666 | /** | |
2667 | * Convert the characters in this to UPPER CASE following the conventions of | |
2668 | * the default locale. | |
2669 | * @return A reference to this. | |
2670 | * @stable ICU 2.0 | |
2671 | */ | |
2672 | UnicodeString& toUpper(void); | |
2673 | ||
2674 | /** | |
2675 | * Convert the characters in this to UPPER CASE following the conventions of | |
2676 | * a specific locale. | |
2677 | * @param locale The locale containing the conventions to use. | |
2678 | * @return A reference to this. | |
2679 | * @stable ICU 2.0 | |
2680 | */ | |
2681 | UnicodeString& toUpper(const Locale& locale); | |
2682 | ||
2683 | /** | |
2684 | * Convert the characters in this to lower case following the conventions of | |
2685 | * the default locale. | |
2686 | * @return A reference to this. | |
2687 | * @stable ICU 2.0 | |
2688 | */ | |
2689 | UnicodeString& toLower(void); | |
2690 | ||
2691 | /** | |
2692 | * Convert the characters in this to lower case following the conventions of | |
2693 | * a specific locale. | |
2694 | * @param locale The locale containing the conventions to use. | |
2695 | * @return A reference to this. | |
2696 | * @stable ICU 2.0 | |
2697 | */ | |
2698 | UnicodeString& toLower(const Locale& locale); | |
2699 | ||
2700 | #if !UCONFIG_NO_BREAK_ITERATION | |
2701 | ||
2702 | /** | |
2703 | * Titlecase this string, convenience function using the default locale. | |
2704 | * | |
2705 | * Casing is locale-dependent and context-sensitive. | |
2706 | * Titlecasing uses a break iterator to find the first characters of words | |
2707 | * that are to be titlecased. It titlecases those characters and lowercases | |
2708 | * all others. | |
2709 | * | |
2710 | * The titlecase break iterator can be provided to customize for arbitrary | |
2711 | * styles, using rules and dictionaries beyond the standard iterators. | |
2712 | * It may be more efficient to always provide an iterator to avoid | |
2713 | * opening and closing one for each string. | |
2714 | * The standard titlecase iterator for the root locale implements the | |
2715 | * algorithm of Unicode TR 21. | |
2716 | * | |
46f4442e | 2717 | * This function uses only the setText(), first() and next() methods of the |
b75a7d8f A |
2718 | * provided break iterator. |
2719 | * | |
2720 | * @param titleIter A break iterator to find the first characters of words | |
2721 | * that are to be titlecased. | |
2722 | * If none is provided (0), then a standard titlecase | |
2723 | * break iterator is opened. | |
374ca955 | 2724 | * Otherwise the provided iterator is set to the string's text. |
b75a7d8f A |
2725 | * @return A reference to this. |
2726 | * @stable ICU 2.1 | |
2727 | */ | |
2728 | UnicodeString &toTitle(BreakIterator *titleIter); | |
2729 | ||
2730 | /** | |
2731 | * Titlecase this string. | |
2732 | * | |
2733 | * Casing is locale-dependent and context-sensitive. | |
2734 | * Titlecasing uses a break iterator to find the first characters of words | |
2735 | * that are to be titlecased. It titlecases those characters and lowercases | |
2736 | * all others. | |
2737 | * | |
2738 | * The titlecase break iterator can be provided to customize for arbitrary | |
2739 | * styles, using rules and dictionaries beyond the standard iterators. | |
2740 | * It may be more efficient to always provide an iterator to avoid | |
2741 | * opening and closing one for each string. | |
2742 | * The standard titlecase iterator for the root locale implements the | |
2743 | * algorithm of Unicode TR 21. | |
2744 | * | |
46f4442e | 2745 | * This function uses only the setText(), first() and next() methods of the |
b75a7d8f A |
2746 | * provided break iterator. |
2747 | * | |
2748 | * @param titleIter A break iterator to find the first characters of words | |
2749 | * that are to be titlecased. | |
2750 | * If none is provided (0), then a standard titlecase | |
2751 | * break iterator is opened. | |
374ca955 | 2752 | * Otherwise the provided iterator is set to the string's text. |
b75a7d8f A |
2753 | * @param locale The locale to consider. |
2754 | * @return A reference to this. | |
2755 | * @stable ICU 2.1 | |
2756 | */ | |
2757 | UnicodeString &toTitle(BreakIterator *titleIter, const Locale &locale); | |
2758 | ||
46f4442e A |
2759 | /** |
2760 | * Titlecase this string, with options. | |
2761 | * | |
2762 | * Casing is locale-dependent and context-sensitive. | |
2763 | * Titlecasing uses a break iterator to find the first characters of words | |
2764 | * that are to be titlecased. It titlecases those characters and lowercases | |
2765 | * all others. (This can be modified with options.) | |
2766 | * | |
2767 | * The titlecase break iterator can be provided to customize for arbitrary | |
2768 | * styles, using rules and dictionaries beyond the standard iterators. | |
2769 | * It may be more efficient to always provide an iterator to avoid | |
2770 | * opening and closing one for each string. | |
2771 | * The standard titlecase iterator for the root locale implements the | |
2772 | * algorithm of Unicode TR 21. | |
2773 | * | |
2774 | * This function uses only the setText(), first() and next() methods of the | |
2775 | * provided break iterator. | |
2776 | * | |
2777 | * @param titleIter A break iterator to find the first characters of words | |
2778 | * that are to be titlecased. | |
2779 | * If none is provided (0), then a standard titlecase | |
2780 | * break iterator is opened. | |
2781 | * Otherwise the provided iterator is set to the string's text. | |
2782 | * @param locale The locale to consider. | |
0f5d89e8 A |
2783 | * @param options Options bit set, usually 0. See U_TITLECASE_NO_LOWERCASE, |
2784 | * U_TITLECASE_NO_BREAK_ADJUSTMENT, U_TITLECASE_ADJUST_TO_CASED, | |
2785 | * U_TITLECASE_WHOLE_STRING, U_TITLECASE_SENTENCES. | |
46f4442e A |
2786 | * @param options Options bit set, see ucasemap_open(). |
2787 | * @return A reference to this. | |
729e4ab9 | 2788 | * @stable ICU 3.8 |
46f4442e A |
2789 | */ |
2790 | UnicodeString &toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options); | |
2791 | ||
b75a7d8f A |
2792 | #endif |
2793 | ||
2794 | /** | |
51004dcb A |
2795 | * Case-folds the characters in this string. |
2796 | * | |
b75a7d8f A |
2797 | * Case-folding is locale-independent and not context-sensitive, |
2798 | * but there is an option for whether to include or exclude mappings for dotted I | |
51004dcb A |
2799 | * and dotless i that are marked with 'T' in CaseFolding.txt. |
2800 | * | |
b75a7d8f A |
2801 | * The result may be longer or shorter than the original. |
2802 | * | |
2803 | * @param options Either U_FOLD_CASE_DEFAULT or U_FOLD_CASE_EXCLUDE_SPECIAL_I | |
2804 | * @return A reference to this. | |
2805 | * @stable ICU 2.0 | |
2806 | */ | |
2807 | UnicodeString &foldCase(uint32_t options=0 /*U_FOLD_CASE_DEFAULT*/); | |
2808 | ||
2809 | //======================================== | |
2810 | // Access to the internal buffer | |
2811 | //======================================== | |
2812 | ||
2813 | /** | |
2814 | * Get a read/write pointer to the internal buffer. | |
f3c0d7a5 | 2815 | * The buffer is guaranteed to be large enough for at least minCapacity char16_ts, |
b75a7d8f A |
2816 | * writable, and is still owned by the UnicodeString object. |
2817 | * Calls to getBuffer(minCapacity) must not be nested, and | |
2818 | * must be matched with calls to releaseBuffer(newLength). | |
2819 | * If the string buffer was read-only or shared, | |
2820 | * then it will be reallocated and copied. | |
2821 | * | |
2822 | * An attempted nested call will return 0, and will not further modify the | |
2823 | * state of the UnicodeString object. | |
2824 | * It also returns 0 if the string is bogus. | |
2825 | * | |
2826 | * The actual capacity of the string buffer may be larger than minCapacity. | |
2827 | * getCapacity() returns the actual capacity. | |
2828 | * For many operations, the full capacity should be used to avoid reallocations. | |
2829 | * | |
2830 | * While the buffer is "open" between getBuffer(minCapacity) | |
2831 | * and releaseBuffer(newLength), the following applies: | |
2832 | * - The string length is set to 0. | |
2833 | * - Any read API call on the UnicodeString object will behave like on a 0-length string. | |
2834 | * - Any write API call on the UnicodeString object is disallowed and will have no effect. | |
2835 | * - You can read from and write to the returned buffer. | |
2836 | * - The previous string contents will still be in the buffer; | |
2837 | * if you want to use it, then you need to call length() before getBuffer(minCapacity). | |
2838 | * If the length() was greater than minCapacity, then any contents after minCapacity | |
2839 | * may be lost. | |
2840 | * The buffer contents is not NUL-terminated by getBuffer(). | |
2841 | * If length()<getCapacity() then you can terminate it by writing a NUL | |
2842 | * at index length(). | |
2843 | * - You must call releaseBuffer(newLength) before and in order to | |
2844 | * return to normal UnicodeString operation. | |
2845 | * | |
f3c0d7a5 | 2846 | * @param minCapacity the minimum number of char16_ts that are to be available |
b75a7d8f A |
2847 | * in the buffer, starting at the returned pointer; |
2848 | * default to the current string capacity if minCapacity==-1 | |
2849 | * @return a writable pointer to the internal string buffer, | |
f3c0d7a5 | 2850 | * or nullptr if an error occurs (nested calls, out of memory) |
b75a7d8f A |
2851 | * |
2852 | * @see releaseBuffer | |
2853 | * @see getTerminatedBuffer() | |
2854 | * @stable ICU 2.0 | |
2855 | */ | |
f3c0d7a5 | 2856 | char16_t *getBuffer(int32_t minCapacity); |
b75a7d8f A |
2857 | |
2858 | /** | |
2859 | * Release a read/write buffer on a UnicodeString object with an | |
2860 | * "open" getBuffer(minCapacity). | |
2861 | * This function must be called in a matched pair with getBuffer(minCapacity). | |
2862 | * releaseBuffer(newLength) must be called if and only if a getBuffer(minCapacity) is "open". | |
2863 | * | |
2864 | * It will set the string length to newLength, at most to the current capacity. | |
2865 | * If newLength==-1 then it will set the length according to the | |
2866 | * first NUL in the buffer, or to the capacity if there is no NUL. | |
2867 | * | |
2868 | * After calling releaseBuffer(newLength) the UnicodeString is back to normal operation. | |
2869 | * | |
2870 | * @param newLength the new length of the UnicodeString object; | |
2871 | * defaults to the current capacity if newLength is greater than that; | |
2872 | * if newLength==-1, it defaults to u_strlen(buffer) but not more than | |
2873 | * the current capacity of the string | |
2874 | * | |
2875 | * @see getBuffer(int32_t minCapacity) | |
2876 | * @stable ICU 2.0 | |
2877 | */ | |
2878 | void releaseBuffer(int32_t newLength=-1); | |
2879 | ||
2880 | /** | |
2881 | * Get a read-only pointer to the internal buffer. | |
2882 | * This can be called at any time on a valid UnicodeString. | |
2883 | * | |
2884 | * It returns 0 if the string is bogus, or | |
2885 | * during an "open" getBuffer(minCapacity). | |
2886 | * | |
2887 | * It can be called as many times as desired. | |
2888 | * The pointer that it returns will remain valid until the UnicodeString object is modified, | |
2889 | * at which time the pointer is semantically invalidated and must not be used any more. | |
2890 | * | |
2891 | * The capacity of the buffer can be determined with getCapacity(). | |
2892 | * The part after length() may or may not be initialized and valid, | |
2893 | * depending on the history of the UnicodeString object. | |
2894 | * | |
2895 | * The buffer contents is (probably) not NUL-terminated. | |
2896 | * You can check if it is with | |
2897 | * <code>(s.length()<s.getCapacity() && buffer[s.length()]==0)</code>. | |
2898 | * (See getTerminatedBuffer().) | |
2899 | * | |
2900 | * The buffer may reside in read-only memory. Its contents must not | |
2901 | * be modified. | |
2902 | * | |
2903 | * @return a read-only pointer to the internal string buffer, | |
f3c0d7a5 | 2904 | * or nullptr if the string is empty or bogus |
b75a7d8f A |
2905 | * |
2906 | * @see getBuffer(int32_t minCapacity) | |
2907 | * @see getTerminatedBuffer() | |
2908 | * @stable ICU 2.0 | |
2909 | */ | |
f3c0d7a5 | 2910 | inline const char16_t *getBuffer() const; |
b75a7d8f A |
2911 | |
2912 | /** | |
2913 | * Get a read-only pointer to the internal buffer, | |
2914 | * making sure that it is NUL-terminated. | |
2915 | * This can be called at any time on a valid UnicodeString. | |
2916 | * | |
2917 | * It returns 0 if the string is bogus, or | |
2918 | * during an "open" getBuffer(minCapacity), or if the buffer cannot | |
2919 | * be NUL-terminated (because memory allocation failed). | |
2920 | * | |
2921 | * It can be called as many times as desired. | |
2922 | * The pointer that it returns will remain valid until the UnicodeString object is modified, | |
2923 | * at which time the pointer is semantically invalidated and must not be used any more. | |
2924 | * | |
2925 | * The capacity of the buffer can be determined with getCapacity(). | |
2926 | * The part after length()+1 may or may not be initialized and valid, | |
2927 | * depending on the history of the UnicodeString object. | |
2928 | * | |
2929 | * The buffer contents is guaranteed to be NUL-terminated. | |
2930 | * getTerminatedBuffer() may reallocate the buffer if a terminating NUL | |
2931 | * is written. | |
2932 | * For this reason, this function is not const, unlike getBuffer(). | |
2933 | * Note that a UnicodeString may also contain NUL characters as part of its contents. | |
2934 | * | |
2935 | * The buffer may reside in read-only memory. Its contents must not | |
2936 | * be modified. | |
2937 | * | |
2938 | * @return a read-only pointer to the internal string buffer, | |
2939 | * or 0 if the string is empty or bogus | |
2940 | * | |
2941 | * @see getBuffer(int32_t minCapacity) | |
2942 | * @see getBuffer() | |
374ca955 | 2943 | * @stable ICU 2.2 |
b75a7d8f | 2944 | */ |
f3c0d7a5 | 2945 | const char16_t *getTerminatedBuffer(); |
b75a7d8f A |
2946 | |
2947 | //======================================== | |
2948 | // Constructors | |
2949 | //======================================== | |
2950 | ||
374ca955 | 2951 | /** Construct an empty UnicodeString. |
b75a7d8f A |
2952 | * @stable ICU 2.0 |
2953 | */ | |
51004dcb | 2954 | inline UnicodeString(); |
b75a7d8f A |
2955 | |
2956 | /** | |
f3c0d7a5 A |
2957 | * Construct a UnicodeString with capacity to hold <TT>capacity</TT> char16_ts |
2958 | * @param capacity the number of char16_ts this UnicodeString should hold | |
b75a7d8f A |
2959 | * before a resize is necessary; if count is greater than 0 and count |
2960 | * code points c take up more space than capacity, then capacity is adjusted | |
2961 | * accordingly. | |
2962 | * @param c is used to initially fill the string | |
2963 | * @param count specifies how many code points c are to be written in the | |
2964 | * string | |
2965 | * @stable ICU 2.0 | |
2966 | */ | |
2967 | UnicodeString(int32_t capacity, UChar32 c, int32_t count); | |
2968 | ||
2969 | /** | |
f3c0d7a5 | 2970 | * Single char16_t (code unit) constructor. |
4388f060 A |
2971 | * |
2972 | * It is recommended to mark this constructor "explicit" by | |
2973 | * <code>-DUNISTR_FROM_CHAR_EXPLICIT=explicit</code> | |
2974 | * on the compiler command line or similar. | |
b75a7d8f A |
2975 | * @param ch the character to place in the UnicodeString |
2976 | * @stable ICU 2.0 | |
2977 | */ | |
f3c0d7a5 | 2978 | UNISTR_FROM_CHAR_EXPLICIT UnicodeString(char16_t ch); |
b75a7d8f A |
2979 | |
2980 | /** | |
2981 | * Single UChar32 (code point) constructor. | |
4388f060 A |
2982 | * |
2983 | * It is recommended to mark this constructor "explicit" by | |
2984 | * <code>-DUNISTR_FROM_CHAR_EXPLICIT=explicit</code> | |
2985 | * on the compiler command line or similar. | |
b75a7d8f A |
2986 | * @param ch the character to place in the UnicodeString |
2987 | * @stable ICU 2.0 | |
2988 | */ | |
4388f060 | 2989 | UNISTR_FROM_CHAR_EXPLICIT UnicodeString(UChar32 ch); |
b75a7d8f A |
2990 | |
2991 | /** | |
f3c0d7a5 | 2992 | * char16_t* constructor. |
4388f060 A |
2993 | * |
2994 | * It is recommended to mark this constructor "explicit" by | |
2995 | * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code> | |
2996 | * on the compiler command line or similar. | |
b75a7d8f A |
2997 | * @param text The characters to place in the UnicodeString. <TT>text</TT> |
2998 | * must be NULL (U+0000) terminated. | |
2999 | * @stable ICU 2.0 | |
3000 | */ | |
f3c0d7a5 A |
3001 | UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char16_t *text); |
3002 | ||
f3c0d7a5 A |
3003 | #if !U_CHAR16_IS_TYPEDEF |
3004 | /** | |
3005 | * uint16_t * constructor. | |
3006 | * Delegates to UnicodeString(const char16_t *). | |
3007 | * | |
3008 | * It is recommended to mark this constructor "explicit" by | |
3009 | * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code> | |
3010 | * on the compiler command line or similar. | |
3011 | * @param text NUL-terminated UTF-16 string | |
0f5d89e8 | 3012 | * @stable ICU 59 |
f3c0d7a5 A |
3013 | */ |
3014 | UNISTR_FROM_STRING_EXPLICIT UnicodeString(const uint16_t *text) : | |
3015 | UnicodeString(ConstChar16Ptr(text)) {} | |
3016 | #endif | |
3017 | ||
f3c0d7a5 A |
3018 | #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) |
3019 | /** | |
3020 | * wchar_t * constructor. | |
3021 | * (Only defined if U_SIZEOF_WCHAR_T==2.) | |
3022 | * Delegates to UnicodeString(const char16_t *). | |
3023 | * | |
3024 | * It is recommended to mark this constructor "explicit" by | |
3025 | * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code> | |
3026 | * on the compiler command line or similar. | |
3027 | * @param text NUL-terminated UTF-16 string | |
0f5d89e8 | 3028 | * @stable ICU 59 |
f3c0d7a5 A |
3029 | */ |
3030 | UNISTR_FROM_STRING_EXPLICIT UnicodeString(const wchar_t *text) : | |
3031 | UnicodeString(ConstChar16Ptr(text)) {} | |
3032 | #endif | |
3033 | ||
f3c0d7a5 A |
3034 | /** |
3035 | * nullptr_t constructor. | |
3036 | * Effectively the same as the default constructor, makes an empty string object. | |
3037 | * | |
3038 | * It is recommended to mark this constructor "explicit" by | |
3039 | * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code> | |
3040 | * on the compiler command line or similar. | |
3041 | * @param text nullptr | |
0f5d89e8 | 3042 | * @stable ICU 59 |
f3c0d7a5 A |
3043 | */ |
3044 | UNISTR_FROM_STRING_EXPLICIT inline UnicodeString(const std::nullptr_t text); | |
b75a7d8f A |
3045 | |
3046 | /** | |
f3c0d7a5 | 3047 | * char16_t* constructor. |
b75a7d8f A |
3048 | * @param text The characters to place in the UnicodeString. |
3049 | * @param textLength The number of Unicode characters in <TT>text</TT> | |
3050 | * to copy. | |
3051 | * @stable ICU 2.0 | |
3052 | */ | |
f3c0d7a5 | 3053 | UnicodeString(const char16_t *text, |
b75a7d8f A |
3054 | int32_t textLength); |
3055 | ||
f3c0d7a5 | 3056 | #if !U_CHAR16_IS_TYPEDEF |
b75a7d8f | 3057 | /** |
f3c0d7a5 A |
3058 | * uint16_t * constructor. |
3059 | * Delegates to UnicodeString(const char16_t *, int32_t). | |
3060 | * @param text UTF-16 string | |
3061 | * @param length string length | |
0f5d89e8 | 3062 | * @stable ICU 59 |
f3c0d7a5 A |
3063 | */ |
3064 | UnicodeString(const uint16_t *text, int32_t length) : | |
3065 | UnicodeString(ConstChar16Ptr(text), length) {} | |
3066 | #endif | |
3067 | ||
f3c0d7a5 A |
3068 | #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) |
3069 | /** | |
3070 | * wchar_t * constructor. | |
3071 | * (Only defined if U_SIZEOF_WCHAR_T==2.) | |
3072 | * Delegates to UnicodeString(const char16_t *, int32_t). | |
3073 | * @param text NUL-terminated UTF-16 string | |
3074 | * @param length string length | |
0f5d89e8 | 3075 | * @stable ICU 59 |
f3c0d7a5 A |
3076 | */ |
3077 | UnicodeString(const wchar_t *text, int32_t length) : | |
3078 | UnicodeString(ConstChar16Ptr(text), length) {} | |
3079 | #endif | |
3080 | ||
f3c0d7a5 A |
3081 | /** |
3082 | * nullptr_t constructor. | |
3083 | * Effectively the same as the default constructor, makes an empty string object. | |
3084 | * @param text nullptr | |
3085 | * @param length ignored | |
0f5d89e8 | 3086 | * @stable ICU 59 |
f3c0d7a5 A |
3087 | */ |
3088 | inline UnicodeString(const std::nullptr_t text, int32_t length); | |
3089 | ||
3090 | /** | |
3091 | * Readonly-aliasing char16_t* constructor. | |
b75a7d8f A |
3092 | * The text will be used for the UnicodeString object, but |
3093 | * it will not be released when the UnicodeString is destroyed. | |
3094 | * This has copy-on-write semantics: | |
3095 | * When the string is modified, then the buffer is first copied into | |
3096 | * newly allocated memory. | |
3097 | * The aliased buffer is never modified. | |
51004dcb A |
3098 | * |
3099 | * In an assignment to another UnicodeString, when using the copy constructor | |
3100 | * or the assignment operator, the text will be copied. | |
3101 | * When using fastCopyFrom(), the text will be aliased again, | |
b75a7d8f A |
3102 | * so that both strings then alias the same readonly-text. |
3103 | * | |
3104 | * @param isTerminated specifies if <code>text</code> is <code>NUL</code>-terminated. | |
3105 | * This must be true if <code>textLength==-1</code>. | |
3106 | * @param text The characters to alias for the UnicodeString. | |
3107 | * @param textLength The number of Unicode characters in <code>text</code> to alias. | |
3108 | * If -1, then this constructor will determine the length | |
3109 | * by calling <code>u_strlen()</code>. | |
3110 | * @stable ICU 2.0 | |
3111 | */ | |
3112 | UnicodeString(UBool isTerminated, | |
f3c0d7a5 | 3113 | ConstChar16Ptr text, |
b75a7d8f A |
3114 | int32_t textLength); |
3115 | ||
3116 | /** | |
f3c0d7a5 | 3117 | * Writable-aliasing char16_t* constructor. |
b75a7d8f A |
3118 | * The text will be used for the UnicodeString object, but |
3119 | * it will not be released when the UnicodeString is destroyed. | |
3120 | * This has write-through semantics: | |
3121 | * For as long as the capacity of the buffer is sufficient, write operations | |
3122 | * will directly affect the buffer. When more capacity is necessary, then | |
3123 | * a new buffer will be allocated and the contents copied as with regularly | |
3124 | * constructed strings. | |
3125 | * In an assignment to another UnicodeString, the buffer will be copied. | |
f3c0d7a5 | 3126 | * The extract(Char16Ptr dst) function detects whether the dst pointer is the same |
b75a7d8f A |
3127 | * as the string buffer itself and will in this case not copy the contents. |
3128 | * | |
3129 | * @param buffer The characters to alias for the UnicodeString. | |
3130 | * @param buffLength The number of Unicode characters in <code>buffer</code> to alias. | |
f3c0d7a5 | 3131 | * @param buffCapacity The size of <code>buffer</code> in char16_ts. |
b75a7d8f A |
3132 | * @stable ICU 2.0 |
3133 | */ | |
f3c0d7a5 A |
3134 | UnicodeString(char16_t *buffer, int32_t buffLength, int32_t buffCapacity); |
3135 | ||
f3c0d7a5 A |
3136 | #if !U_CHAR16_IS_TYPEDEF |
3137 | /** | |
3138 | * Writable-aliasing uint16_t * constructor. | |
3139 | * Delegates to UnicodeString(const char16_t *, int32_t, int32_t). | |
3140 | * @param buffer writable buffer of/for UTF-16 text | |
3141 | * @param buffLength length of the current buffer contents | |
3142 | * @param buffCapacity buffer capacity | |
0f5d89e8 | 3143 | * @stable ICU 59 |
f3c0d7a5 A |
3144 | */ |
3145 | UnicodeString(uint16_t *buffer, int32_t buffLength, int32_t buffCapacity) : | |
3146 | UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {} | |
3147 | #endif | |
3148 | ||
f3c0d7a5 A |
3149 | #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN) |
3150 | /** | |
3151 | * Writable-aliasing wchar_t * constructor. | |
3152 | * (Only defined if U_SIZEOF_WCHAR_T==2.) | |
3153 | * Delegates to UnicodeString(const char16_t *, int32_t, int32_t). | |
3154 | * @param buffer writable buffer of/for UTF-16 text | |
3155 | * @param buffLength length of the current buffer contents | |
3156 | * @param buffCapacity buffer capacity | |
0f5d89e8 | 3157 | * @stable ICU 59 |
f3c0d7a5 A |
3158 | */ |
3159 | UnicodeString(wchar_t *buffer, int32_t buffLength, int32_t buffCapacity) : | |
3160 | UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {} | |
3161 | #endif | |
3162 | ||
f3c0d7a5 A |
3163 | /** |
3164 | * Writable-aliasing nullptr_t constructor. | |
3165 | * Effectively the same as the default constructor, makes an empty string object. | |
3166 | * @param buffer nullptr | |
3167 | * @param buffLength ignored | |
3168 | * @param buffCapacity ignored | |
0f5d89e8 | 3169 | * @stable ICU 59 |
f3c0d7a5 A |
3170 | */ |
3171 | inline UnicodeString(std::nullptr_t buffer, int32_t buffLength, int32_t buffCapacity); | |
b75a7d8f | 3172 | |
729e4ab9 A |
3173 | #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION |
3174 | ||
3175 | /** | |
3176 | * char* constructor. | |
4388f060 A |
3177 | * Uses the default converter (and thus depends on the ICU conversion code) |
3178 | * unless U_CHARSET_IS_UTF8 is set to 1. | |
3179 | * | |
3180 | * For ASCII (really "invariant character") strings it is more efficient to use | |
3181 | * the constructor that takes a US_INV (for its enum EInvariant). | |
3182 | * For ASCII (invariant-character) string literals, see UNICODE_STRING and | |
3183 | * UNICODE_STRING_SIMPLE. | |
3184 | * | |
3185 | * It is recommended to mark this constructor "explicit" by | |
3186 | * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code> | |
3187 | * on the compiler command line or similar. | |
729e4ab9 A |
3188 | * @param codepageData an array of bytes, null-terminated, |
3189 | * in the platform's default codepage. | |
3190 | * @stable ICU 2.0 | |
4388f060 A |
3191 | * @see UNICODE_STRING |
3192 | * @see UNICODE_STRING_SIMPLE | |
729e4ab9 | 3193 | */ |
4388f060 | 3194 | UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char *codepageData); |
729e4ab9 A |
3195 | |
3196 | /** | |
3197 | * char* constructor. | |
4388f060 A |
3198 | * Uses the default converter (and thus depends on the ICU conversion code) |
3199 | * unless U_CHARSET_IS_UTF8 is set to 1. | |
729e4ab9 A |
3200 | * @param codepageData an array of bytes in the platform's default codepage. |
3201 | * @param dataLength The number of bytes in <TT>codepageData</TT>. | |
3202 | * @stable ICU 2.0 | |
3203 | */ | |
3204 | UnicodeString(const char *codepageData, int32_t dataLength); | |
3205 | ||
3206 | #endif | |
3207 | ||
374ca955 A |
3208 | #if !UCONFIG_NO_CONVERSION |
3209 | ||
b75a7d8f A |
3210 | /** |
3211 | * char* constructor. | |
3212 | * @param codepageData an array of bytes, null-terminated | |
3213 | * @param codepage the encoding of <TT>codepageData</TT>. The special | |
374ca955 | 3214 | * value 0 for <TT>codepage</TT> indicates that the text is in the |
b75a7d8f | 3215 | * platform's default codepage. |
374ca955 | 3216 | * |
b75a7d8f A |
3217 | * If <code>codepage</code> is an empty string (<code>""</code>), |
3218 | * then a simple conversion is performed on the codepage-invariant | |
3219 | * subset ("invariant characters") of the platform encoding. See utypes.h. | |
374ca955 A |
3220 | * Recommendation: For invariant-character strings use the constructor |
3221 | * UnicodeString(const char *src, int32_t length, enum EInvariant inv) | |
3222 | * because it avoids object code dependencies of UnicodeString on | |
3223 | * the conversion code. | |
3224 | * | |
b75a7d8f A |
3225 | * @stable ICU 2.0 |
3226 | */ | |
729e4ab9 | 3227 | UnicodeString(const char *codepageData, const char *codepage); |
b75a7d8f A |
3228 | |
3229 | /** | |
3230 | * char* constructor. | |
3231 | * @param codepageData an array of bytes. | |
3232 | * @param dataLength The number of bytes in <TT>codepageData</TT>. | |
3233 | * @param codepage the encoding of <TT>codepageData</TT>. The special | |
374ca955 | 3234 | * value 0 for <TT>codepage</TT> indicates that the text is in the |
b75a7d8f A |
3235 | * platform's default codepage. |
3236 | * If <code>codepage</code> is an empty string (<code>""</code>), | |
3237 | * then a simple conversion is performed on the codepage-invariant | |
3238 | * subset ("invariant characters") of the platform encoding. See utypes.h. | |
374ca955 A |
3239 | * Recommendation: For invariant-character strings use the constructor |
3240 | * UnicodeString(const char *src, int32_t length, enum EInvariant inv) | |
3241 | * because it avoids object code dependencies of UnicodeString on | |
3242 | * the conversion code. | |
3243 | * | |
b75a7d8f A |
3244 | * @stable ICU 2.0 |
3245 | */ | |
729e4ab9 | 3246 | UnicodeString(const char *codepageData, int32_t dataLength, const char *codepage); |
b75a7d8f A |
3247 | |
3248 | /** | |
3249 | * char * / UConverter constructor. | |
3250 | * This constructor uses an existing UConverter object to | |
3251 | * convert the codepage string to Unicode and construct a UnicodeString | |
3252 | * from that. | |
3253 | * | |
3254 | * The converter is reset at first. | |
3255 | * If the error code indicates a failure before this constructor is called, | |
3256 | * or if an error occurs during conversion or construction, | |
3257 | * then the string will be bogus. | |
3258 | * | |
3259 | * This function avoids the overhead of opening and closing a converter if | |
3260 | * multiple strings are constructed. | |
3261 | * | |
3262 | * @param src input codepage string | |
3263 | * @param srcLength length of the input string, can be -1 for NUL-terminated strings | |
3264 | * @param cnv converter object (ucnv_resetToUnicode() will be called), | |
3265 | * can be NULL for the default converter | |
3266 | * @param errorCode normal ICU error code | |
3267 | * @stable ICU 2.0 | |
3268 | */ | |
3269 | UnicodeString( | |
3270 | const char *src, int32_t srcLength, | |
3271 | UConverter *cnv, | |
3272 | UErrorCode &errorCode); | |
3273 | ||
374ca955 A |
3274 | #endif |
3275 | ||
3276 | /** | |
3277 | * Constructs a Unicode string from an invariant-character char * string. | |
3278 | * About invariant characters see utypes.h. | |
3279 | * This constructor has no runtime dependency on conversion code and is | |
3280 | * therefore recommended over ones taking a charset name string | |
3281 | * (where the empty string "" indicates invariant-character conversion). | |
3282 | * | |
3283 | * Use the macro US_INV as the third, signature-distinguishing parameter. | |
3284 | * | |
3285 | * For example: | |
3286 | * \code | |
3287 | * void fn(const char *s) { | |
3288 | * UnicodeString ustr(s, -1, US_INV); | |
3289 | * // use ustr ... | |
3290 | * } | |
3291 | * \endcode | |
3292 | * | |
3293 | * @param src String using only invariant characters. | |
3294 | * @param length Length of src, or -1 if NUL-terminated. | |
3295 | * @param inv Signature-distinguishing paramater, use US_INV. | |
3296 | * | |
3297 | * @see US_INV | |
73c04bcf | 3298 | * @stable ICU 3.2 |
374ca955 A |
3299 | */ |
3300 | UnicodeString(const char *src, int32_t length, enum EInvariant inv); | |
3301 | ||
b75a7d8f A |
3302 | |
3303 | /** | |
3304 | * Copy constructor. | |
2ca993e8 A |
3305 | * |
3306 | * Starting with ICU 2.4, the assignment operator and the copy constructor | |
3307 | * allocate a new buffer and copy the buffer contents even for readonly aliases. | |
3308 | * By contrast, the fastCopyFrom() function implements the old, | |
3309 | * more efficient but less safe behavior | |
3310 | * of making this string also a readonly alias to the same buffer. | |
3311 | * | |
3312 | * If the source object has an "open" buffer from getBuffer(minCapacity), | |
3313 | * then the copy is an empty string. | |
3314 | * | |
b75a7d8f A |
3315 | * @param that The UnicodeString object to copy. |
3316 | * @stable ICU 2.0 | |
2ca993e8 | 3317 | * @see fastCopyFrom |
b75a7d8f A |
3318 | */ |
3319 | UnicodeString(const UnicodeString& that); | |
3320 | ||
2ca993e8 | 3321 | /** |
0f5d89e8 | 3322 | * Move constructor; might leave src in bogus state. |
2ca993e8 A |
3323 | * This string will have the same contents and state that the source string had. |
3324 | * @param src source string | |
f3c0d7a5 | 3325 | * @stable ICU 56 |
2ca993e8 A |
3326 | */ |
3327 | UnicodeString(UnicodeString &&src) U_NOEXCEPT; | |
2ca993e8 | 3328 | |
b75a7d8f A |
3329 | /** |
3330 | * 'Substring' constructor from tail of source string. | |
3331 | * @param src The UnicodeString object to copy. | |
3332 | * @param srcStart The offset into <tt>src</tt> at which to start copying. | |
374ca955 | 3333 | * @stable ICU 2.2 |
b75a7d8f A |
3334 | */ |
3335 | UnicodeString(const UnicodeString& src, int32_t srcStart); | |
3336 | ||
3337 | /** | |
3338 | * 'Substring' constructor from subrange of source string. | |
3339 | * @param src The UnicodeString object to copy. | |
3340 | * @param srcStart The offset into <tt>src</tt> at which to start copying. | |
3341 | * @param srcLength The number of characters from <tt>src</tt> to copy. | |
374ca955 | 3342 | * @stable ICU 2.2 |
b75a7d8f A |
3343 | */ |
3344 | UnicodeString(const UnicodeString& src, int32_t srcStart, int32_t srcLength); | |
3345 | ||
3346 | /** | |
3347 | * Clone this object, an instance of a subclass of Replaceable. | |
3348 | * Clones can be used concurrently in multiple threads. | |
3349 | * If a subclass does not implement clone(), or if an error occurs, | |
3350 | * then NULL is returned. | |
3351 | * The clone functions in all subclasses return a pointer to a Replaceable | |
3352 | * because some compilers do not support covariant (same-as-this) | |
3353 | * return types; cast to the appropriate subclass if necessary. | |
3354 | * The caller must delete the clone. | |
3355 | * | |
3356 | * @return a clone of this object | |
3357 | * | |
3358 | * @see Replaceable::clone | |
3359 | * @see getDynamicClassID | |
374ca955 | 3360 | * @stable ICU 2.6 |
b75a7d8f A |
3361 | */ |
3362 | virtual Replaceable *clone() const; | |
3363 | ||
374ca955 | 3364 | /** Destructor. |
b75a7d8f A |
3365 | * @stable ICU 2.0 |
3366 | */ | |
374ca955 | 3367 | virtual ~UnicodeString(); |
b75a7d8f | 3368 | |
729e4ab9 A |
3369 | /** |
3370 | * Create a UnicodeString from a UTF-8 string. | |
3371 | * Illegal input is replaced with U+FFFD. Otherwise, errors result in a bogus string. | |
3372 | * Calls u_strFromUTF8WithSub(). | |
3373 | * | |
3374 | * @param utf8 UTF-8 input string. | |
3375 | * Note that a StringPiece can be implicitly constructed | |
3376 | * from a std::string or a NUL-terminated const char * string. | |
3377 | * @return A UnicodeString with equivalent UTF-16 contents. | |
3378 | * @see toUTF8 | |
3379 | * @see toUTF8String | |
3380 | * @stable ICU 4.2 | |
3381 | */ | |
f3c0d7a5 | 3382 | static UnicodeString fromUTF8(StringPiece utf8); |
729e4ab9 A |
3383 | |
3384 | /** | |
3385 | * Create a UnicodeString from a UTF-32 string. | |
3386 | * Illegal input is replaced with U+FFFD. Otherwise, errors result in a bogus string. | |
3387 | * Calls u_strFromUTF32WithSub(). | |
3388 | * | |
3389 | * @param utf32 UTF-32 input string. Must not be NULL. | |
3390 | * @param length Length of the input string, or -1 if NUL-terminated. | |
3391 | * @return A UnicodeString with equivalent UTF-16 contents. | |
3392 | * @see toUTF32 | |
3393 | * @stable ICU 4.2 | |
3394 | */ | |
3395 | static UnicodeString fromUTF32(const UChar32 *utf32, int32_t length); | |
b75a7d8f A |
3396 | |
3397 | /* Miscellaneous operations */ | |
3398 | ||
3399 | /** | |
3400 | * Unescape a string of characters and return a string containing | |
3401 | * the result. The following escape sequences are recognized: | |
3402 | * | |
374ca955 A |
3403 | * \\uhhhh 4 hex digits; h in [0-9A-Fa-f] |
3404 | * \\Uhhhhhhhh 8 hex digits | |
3405 | * \\xhh 1-2 hex digits | |
3406 | * \\ooo 1-3 octal digits; o in [0-7] | |
3407 | * \\cX control-X; X is masked with 0x1F | |
b75a7d8f A |
3408 | * |
3409 | * as well as the standard ANSI C escapes: | |
3410 | * | |
374ca955 A |
3411 | * \\a => U+0007, \\b => U+0008, \\t => U+0009, \\n => U+000A, |
3412 | * \\v => U+000B, \\f => U+000C, \\r => U+000D, \\e => U+001B, | |
729e4ab9 | 3413 | * \\" => U+0022, \\' => U+0027, \\? => U+003F, \\\\ => U+005C |
b75a7d8f A |
3414 | * |
3415 | * Anything else following a backslash is generically escaped. For | |
374ca955 | 3416 | * example, "[a\\-z]" returns "[a-z]". |
b75a7d8f A |
3417 | * |
3418 | * If an escape sequence is ill-formed, this method returns an empty | |
374ca955 | 3419 | * string. An example of an ill-formed sequence is "\\u" followed by |
b75a7d8f A |
3420 | * fewer than 4 hex digits. |
3421 | * | |
3422 | * This function is similar to u_unescape() but not identical to it. | |
3423 | * The latter takes a source char*, so it does escape recognition | |
3424 | * and also invariant conversion. | |
3425 | * | |
3426 | * @return a string with backslash escapes interpreted, or an | |
3427 | * empty string on error. | |
3428 | * @see UnicodeString#unescapeAt() | |
3429 | * @see u_unescape() | |
3430 | * @see u_unescapeAt() | |
3431 | * @stable ICU 2.0 | |
3432 | */ | |
3433 | UnicodeString unescape() const; | |
3434 | ||
3435 | /** | |
3436 | * Unescape a single escape sequence and return the represented | |
3437 | * character. See unescape() for a listing of the recognized escape | |
3438 | * sequences. The character at offset-1 is assumed (without | |
3439 | * checking) to be a backslash. If the escape sequence is | |
51004dcb | 3440 | * ill-formed, or the offset is out of range, U_SENTINEL=-1 is |
b75a7d8f A |
3441 | * returned. |
3442 | * | |
3443 | * @param offset an input output parameter. On input, it is the | |
3444 | * offset into this string where the escape sequence is located, | |
3445 | * after the initial backslash. On output, it is advanced after the | |
3446 | * last character parsed. On error, it is not advanced at all. | |
3447 | * @return the character represented by the escape sequence at | |
51004dcb | 3448 | * offset, or U_SENTINEL=-1 on error. |
b75a7d8f A |
3449 | * @see UnicodeString#unescape() |
3450 | * @see u_unescape() | |
3451 | * @see u_unescapeAt() | |
3452 | * @stable ICU 2.0 | |
3453 | */ | |
3454 | UChar32 unescapeAt(int32_t &offset) const; | |
3455 | ||
3456 | /** | |
374ca955 | 3457 | * ICU "poor man's RTTI", returns a UClassID for this class. |
b75a7d8f | 3458 | * |
374ca955 | 3459 | * @stable ICU 2.2 |
b75a7d8f | 3460 | */ |
374ca955 | 3461 | static UClassID U_EXPORT2 getStaticClassID(); |
b75a7d8f A |
3462 | |
3463 | /** | |
374ca955 | 3464 | * ICU "poor man's RTTI", returns a UClassID for the actual class. |
b75a7d8f | 3465 | * |
374ca955 | 3466 | * @stable ICU 2.2 |
b75a7d8f | 3467 | */ |
374ca955 | 3468 | virtual UClassID getDynamicClassID() const; |
b75a7d8f A |
3469 | |
3470 | //======================================== | |
3471 | // Implementation methods | |
3472 | //======================================== | |
3473 | ||
3474 | protected: | |
3475 | /** | |
3476 | * Implement Replaceable::getLength() (see jitterbug 1027). | |
374ca955 | 3477 | * @stable ICU 2.4 |
b75a7d8f A |
3478 | */ |
3479 | virtual int32_t getLength() const; | |
3480 | ||
3481 | /** | |
3482 | * The change in Replaceable to use virtual getCharAt() allows | |
3483 | * UnicodeString::charAt() to be inline again (see jitterbug 709). | |
374ca955 | 3484 | * @stable ICU 2.4 |
b75a7d8f | 3485 | */ |
f3c0d7a5 | 3486 | virtual char16_t getCharAt(int32_t offset) const; |
b75a7d8f A |
3487 | |
3488 | /** | |
3489 | * The change in Replaceable to use virtual getChar32At() allows | |
3490 | * UnicodeString::char32At() to be inline again (see jitterbug 709). | |
374ca955 | 3491 | * @stable ICU 2.4 |
b75a7d8f A |
3492 | */ |
3493 | virtual UChar32 getChar32At(int32_t offset) const; | |
3494 | ||
3495 | private: | |
729e4ab9 | 3496 | // For char* constructors. Could be made public. |
f3c0d7a5 | 3497 | UnicodeString &setToUTF8(StringPiece utf8); |
729e4ab9 A |
3498 | // For extract(char*). |
3499 | // We could make a toUTF8(target, capacity, errorCode) public but not | |
3500 | // this version: New API will be cleaner if we make callers create substrings | |
3501 | // rather than having start+length on every method, | |
3502 | // and it should take a UErrorCode&. | |
3503 | int32_t | |
3504 | toUTF8(int32_t start, int32_t len, | |
3505 | char *target, int32_t capacity) const; | |
3506 | ||
51004dcb A |
3507 | /** |
3508 | * Internal string contents comparison, called by operator==. | |
3509 | * Requires: this & text not bogus and have same lengths. | |
3510 | */ | |
3511 | UBool doEquals(const UnicodeString &text, int32_t len) const; | |
b75a7d8f A |
3512 | |
3513 | inline int8_t | |
3514 | doCompare(int32_t start, | |
3515 | int32_t length, | |
3516 | const UnicodeString& srcText, | |
3517 | int32_t srcStart, | |
3518 | int32_t srcLength) const; | |
3519 | ||
3520 | int8_t doCompare(int32_t start, | |
3521 | int32_t length, | |
f3c0d7a5 | 3522 | const char16_t *srcChars, |
b75a7d8f A |
3523 | int32_t srcStart, |
3524 | int32_t srcLength) const; | |
3525 | ||
3526 | inline int8_t | |
3527 | doCompareCodePointOrder(int32_t start, | |
3528 | int32_t length, | |
3529 | const UnicodeString& srcText, | |
3530 | int32_t srcStart, | |
3531 | int32_t srcLength) const; | |
3532 | ||
3533 | int8_t doCompareCodePointOrder(int32_t start, | |
3534 | int32_t length, | |
f3c0d7a5 | 3535 | const char16_t *srcChars, |
b75a7d8f A |
3536 | int32_t srcStart, |
3537 | int32_t srcLength) const; | |
3538 | ||
3539 | inline int8_t | |
3540 | doCaseCompare(int32_t start, | |
3541 | int32_t length, | |
3542 | const UnicodeString &srcText, | |
3543 | int32_t srcStart, | |
3544 | int32_t srcLength, | |
3545 | uint32_t options) const; | |
3546 | ||
3547 | int8_t | |
3548 | doCaseCompare(int32_t start, | |
3549 | int32_t length, | |
f3c0d7a5 | 3550 | const char16_t *srcChars, |
b75a7d8f A |
3551 | int32_t srcStart, |
3552 | int32_t srcLength, | |
3553 | uint32_t options) const; | |
3554 | ||
f3c0d7a5 | 3555 | int32_t doIndexOf(char16_t c, |
b75a7d8f A |
3556 | int32_t start, |
3557 | int32_t length) const; | |
3558 | ||
3559 | int32_t doIndexOf(UChar32 c, | |
3560 | int32_t start, | |
3561 | int32_t length) const; | |
3562 | ||
f3c0d7a5 | 3563 | int32_t doLastIndexOf(char16_t c, |
b75a7d8f A |
3564 | int32_t start, |
3565 | int32_t length) const; | |
3566 | ||
3567 | int32_t doLastIndexOf(UChar32 c, | |
3568 | int32_t start, | |
3569 | int32_t length) const; | |
3570 | ||
374ca955 A |
3571 | void doExtract(int32_t start, |
3572 | int32_t length, | |
f3c0d7a5 | 3573 | char16_t *dst, |
b75a7d8f A |
3574 | int32_t dstStart) const; |
3575 | ||
3576 | inline void doExtract(int32_t start, | |
3577 | int32_t length, | |
3578 | UnicodeString& target) const; | |
3579 | ||
f3c0d7a5 | 3580 | inline char16_t doCharAt(int32_t offset) const; |
b75a7d8f | 3581 | |
374ca955 A |
3582 | UnicodeString& doReplace(int32_t start, |
3583 | int32_t length, | |
3584 | const UnicodeString& srcText, | |
3585 | int32_t srcStart, | |
b75a7d8f A |
3586 | int32_t srcLength); |
3587 | ||
374ca955 A |
3588 | UnicodeString& doReplace(int32_t start, |
3589 | int32_t length, | |
f3c0d7a5 | 3590 | const char16_t *srcChars, |
374ca955 | 3591 | int32_t srcStart, |
b75a7d8f A |
3592 | int32_t srcLength); |
3593 | ||
2ca993e8 | 3594 | UnicodeString& doAppend(const UnicodeString& src, int32_t srcStart, int32_t srcLength); |
f3c0d7a5 | 3595 | UnicodeString& doAppend(const char16_t *srcChars, int32_t srcStart, int32_t srcLength); |
2ca993e8 | 3596 | |
b75a7d8f A |
3597 | UnicodeString& doReverse(int32_t start, |
3598 | int32_t length); | |
3599 | ||
3600 | // calculate hash code | |
3601 | int32_t doHashCode(void) const; | |
3602 | ||
3603 | // get pointer to start of array | |
46f4442e | 3604 | // these do not check for kOpenGetBuffer, unlike the public getBuffer() function |
f3c0d7a5 A |
3605 | inline char16_t* getArrayStart(void); |
3606 | inline const char16_t* getArrayStart(void) const; | |
b75a7d8f | 3607 | |
b331163b A |
3608 | inline UBool hasShortLength() const; |
3609 | inline int32_t getShortLength() const; | |
3610 | ||
46f4442e A |
3611 | // A UnicodeString object (not necessarily its current buffer) |
3612 | // is writable unless it isBogus() or it has an "open" getBuffer(minCapacity). | |
3613 | inline UBool isWritable() const; | |
3614 | ||
3615 | // Is the current buffer writable? | |
3616 | inline UBool isBufferWritable() const; | |
3617 | ||
3618 | // None of the following does releaseArray(). | |
b331163b A |
3619 | inline void setZeroLength(); |
3620 | inline void setShortLength(int32_t len); | |
3621 | inline void setLength(int32_t len); | |
3622 | inline void setToEmpty(); | |
f3c0d7a5 | 3623 | inline void setArray(char16_t *array, int32_t len, int32_t capacity); // sets length but not flags |
46f4442e | 3624 | |
b331163b | 3625 | // allocate the array; result may be the stack buffer |
b75a7d8f | 3626 | // sets refCount to 1 if appropriate |
b331163b A |
3627 | // sets fArray, fCapacity, and flags |
3628 | // sets length to 0 | |
b75a7d8f A |
3629 | // returns boolean for success or failure |
3630 | UBool allocate(int32_t capacity); | |
3631 | ||
3632 | // release the array if owned | |
3633 | void releaseArray(void); | |
3634 | ||
3635 | // turn a bogus string into an empty one | |
3636 | void unBogus(); | |
3637 | ||
3638 | // implements assigment operator, copy constructor, and fastCopyFrom() | |
3639 | UnicodeString ©From(const UnicodeString &src, UBool fastCopy=FALSE); | |
3640 | ||
2ca993e8 A |
3641 | // Copies just the fields without memory management. |
3642 | void copyFieldsFrom(UnicodeString &src, UBool setSrcToBogus) U_NOEXCEPT; | |
3643 | ||
b75a7d8f A |
3644 | // Pin start and limit to acceptable values. |
3645 | inline void pinIndex(int32_t& start) const; | |
3646 | inline void pinIndices(int32_t& start, | |
3647 | int32_t& length) const; | |
3648 | ||
374ca955 A |
3649 | #if !UCONFIG_NO_CONVERSION |
3650 | ||
b75a7d8f A |
3651 | /* Internal extract() using UConverter. */ |
3652 | int32_t doExtract(int32_t start, int32_t length, | |
3653 | char *dest, int32_t destCapacity, | |
3654 | UConverter *cnv, | |
3655 | UErrorCode &errorCode) const; | |
3656 | ||
3657 | /* | |
3658 | * Real constructor for converting from codepage data. | |
3659 | * It assumes that it is called with !fRefCounted. | |
3660 | * | |
3661 | * If <code>codepage==0</code>, then the default converter | |
3662 | * is used for the platform encoding. | |
3663 | * If <code>codepage</code> is an empty string (<code>""</code>), | |
3664 | * then a simple conversion is performed on the codepage-invariant | |
3665 | * subset ("invariant characters") of the platform encoding. See utypes.h. | |
3666 | */ | |
3667 | void doCodepageCreate(const char *codepageData, | |
3668 | int32_t dataLength, | |
3669 | const char *codepage); | |
3670 | ||
3671 | /* | |
3672 | * Worker function for creating a UnicodeString from | |
3673 | * a codepage string using a UConverter. | |
3674 | */ | |
3675 | void | |
3676 | doCodepageCreate(const char *codepageData, | |
3677 | int32_t dataLength, | |
3678 | UConverter *converter, | |
3679 | UErrorCode &status); | |
374ca955 A |
3680 | |
3681 | #endif | |
3682 | ||
b75a7d8f A |
3683 | /* |
3684 | * This function is called when write access to the array | |
3685 | * is necessary. | |
3686 | * | |
3687 | * We need to make a copy of the array if | |
3688 | * the buffer is read-only, or | |
3689 | * the buffer is refCounted (shared), and refCount>1, or | |
3690 | * the buffer is too small. | |
3691 | * | |
3692 | * Return FALSE if memory could not be allocated. | |
3693 | */ | |
3694 | UBool cloneArrayIfNeeded(int32_t newCapacity = -1, | |
3695 | int32_t growCapacity = -1, | |
3696 | UBool doCopyArray = TRUE, | |
3697 | int32_t **pBufferToDelete = 0, | |
3698 | UBool forceClone = FALSE); | |
3699 | ||
4388f060 A |
3700 | /** |
3701 | * Common function for UnicodeString case mappings. | |
3702 | * The stringCaseMapper has the same type UStringCaseMapper | |
3703 | * as in ustr_imp.h for ustrcase_map(). | |
3704 | */ | |
b75a7d8f | 3705 | UnicodeString & |
f3c0d7a5 A |
3706 | caseMap(int32_t caseLocale, uint32_t options, |
3707 | #if !UCONFIG_NO_BREAK_ITERATION | |
3708 | BreakIterator *iter, | |
3709 | #endif | |
3710 | UStringCaseMapper *stringCaseMapper); | |
b75a7d8f A |
3711 | |
3712 | // ref counting | |
3713 | void addRef(void); | |
3714 | int32_t removeRef(void); | |
3715 | int32_t refCount(void) const; | |
3716 | ||
3717 | // constants | |
3718 | enum { | |
2ca993e8 A |
3719 | /** |
3720 | * Size of stack buffer for short strings. | |
3721 | * Must be at least U16_MAX_LENGTH for the single-code point constructor to work. | |
3722 | * @see UNISTR_OBJECT_SIZE | |
3723 | */ | |
3724 | US_STACKBUF_SIZE=(int32_t)(UNISTR_OBJECT_SIZE-sizeof(void *)-2)/U_SIZEOF_UCHAR, | |
b331163b | 3725 | kInvalidUChar=0xffff, // U+FFFF returned by charAt(invalid index) |
b75a7d8f A |
3726 | kInvalidHashCode=0, // invalid hash code |
3727 | kEmptyHashCode=1, // hash code for empty string | |
3728 | ||
b331163b | 3729 | // bit flag values for fLengthAndFlags |
b75a7d8f | 3730 | kIsBogus=1, // this string is bogus, i.e., not valid or NULL |
b331163b | 3731 | kUsingStackBuffer=2,// using fUnion.fStackFields instead of fUnion.fFields |
b75a7d8f A |
3732 | kRefCounted=4, // there is a refCount field before the characters in fArray |
3733 | kBufferIsReadonly=8,// do not write to this buffer | |
3734 | kOpenGetBuffer=16, // getBuffer(minCapacity) was called (is "open"), | |
3735 | // and releaseBuffer(newLength) must be called | |
b331163b A |
3736 | kAllStorageFlags=0x1f, |
3737 | ||
3738 | kLengthShift=5, // remaining 11 bits for non-negative short length, or negative if long | |
3739 | kLength1=1<<kLengthShift, | |
3740 | kMaxShortLength=0x3ff, // max non-negative short length (leaves top bit 0) | |
3741 | kLengthIsLarge=0xffe0, // short length < 0, real length is in fUnion.fFields.fLength | |
b75a7d8f A |
3742 | |
3743 | // combined values for convenience | |
3744 | kShortString=kUsingStackBuffer, | |
3745 | kLongString=kRefCounted, | |
3746 | kReadonlyAlias=kBufferIsReadonly, | |
3747 | kWritableAlias=0 | |
3748 | }; | |
3749 | ||
4388f060 | 3750 | friend class UnicodeStringAppendable; |
b75a7d8f | 3751 | |
46f4442e A |
3752 | union StackBufferOrFields; // forward declaration necessary before friend declaration |
3753 | friend union StackBufferOrFields; // make US_STACKBUF_SIZE visible inside fUnion | |
3754 | ||
b75a7d8f A |
3755 | /* |
3756 | * The following are all the class fields that are stored | |
3757 | * in each UnicodeString object. | |
3758 | * Note that UnicodeString has virtual functions, | |
3759 | * therefore there is an implicit vtable pointer | |
3760 | * as the first real field. | |
4388f060 | 3761 | * The fields should be aligned such that no padding is necessary. |
b75a7d8f A |
3762 | * On 32-bit machines, the size should be 32 bytes, |
3763 | * on 64-bit machines (8-byte pointers), it should be 40 bytes. | |
4388f060 A |
3764 | * |
3765 | * We use a hack to achieve this. | |
3766 | * | |
3767 | * With at least some compilers, each of the following is forced to | |
3768 | * a multiple of sizeof(pointer) [the largest field base unit here is a data pointer], | |
3769 | * rounded up with additional padding if the fields do not already fit that requirement: | |
3770 | * - sizeof(class UnicodeString) | |
3771 | * - offsetof(UnicodeString, fUnion) | |
3772 | * - sizeof(fUnion) | |
b331163b | 3773 | * - sizeof(fStackFields) |
4388f060 | 3774 | * |
b331163b A |
3775 | * We optimize for the longest possible internal buffer for short strings. |
3776 | * fUnion.fStackFields begins with 2 bytes for storage flags | |
3777 | * and the length of relatively short strings, | |
3778 | * followed by the buffer for short string contents. | |
3779 | * There is no padding inside fStackFields. | |
4388f060 | 3780 | * |
b331163b A |
3781 | * Heap-allocated and aliased strings use fUnion.fFields. |
3782 | * Both fStackFields and fFields must begin with the same fields for flags and short length, | |
3783 | * that is, those must have the same memory offsets inside the object, | |
3784 | * because the flags must be inspected in order to decide which half of fUnion is being used. | |
3785 | * We assume that the compiler does not reorder the fields. | |
4388f060 | 3786 | * |
b331163b A |
3787 | * (Padding at the end of fFields is ok: |
3788 | * As long as it is no larger than fStackFields, it is not wasted space.) | |
3789 | * | |
2ca993e8 A |
3790 | * For some of the history of the UnicodeString class fields layout, see |
3791 | * - ICU ticket #11551 "longer UnicodeString contents in stack buffer" | |
3792 | * - ICU ticket #11336 "UnicodeString: recombine stack buffer arrays" | |
3793 | * - ICU ticket #8322 "why is sizeof(UnicodeString)==48?" | |
b75a7d8f A |
3794 | */ |
3795 | // (implicit) *vtable; | |
46f4442e | 3796 | union StackBufferOrFields { |
b331163b A |
3797 | // fStackFields is used iff (fLengthAndFlags&kUsingStackBuffer) else fFields is used. |
3798 | // Each struct of the union must begin with fLengthAndFlags. | |
3799 | struct { | |
3800 | int16_t fLengthAndFlags; // bit fields: see constants above | |
f3c0d7a5 | 3801 | char16_t fBuffer[US_STACKBUF_SIZE]; // buffer for short strings |
b331163b | 3802 | } fStackFields; |
46f4442e | 3803 | struct { |
b331163b | 3804 | int16_t fLengthAndFlags; // bit fields: see constants above |
4388f060 | 3805 | int32_t fLength; // number of characters in fArray if >127; else undefined |
f3c0d7a5 | 3806 | int32_t fCapacity; // capacity of fArray (in char16_ts) |
2ca993e8 A |
3807 | // array pointer last to minimize padding for machines with P128 data model |
3808 | // or pointer sizes that are not a power of 2 | |
f3c0d7a5 | 3809 | char16_t *fArray; // the Unicode data |
46f4442e A |
3810 | } fFields; |
3811 | } fUnion; | |
b75a7d8f A |
3812 | }; |
3813 | ||
374ca955 A |
3814 | /** |
3815 | * Create a new UnicodeString with the concatenation of two others. | |
3816 | * | |
3817 | * @param s1 The first string to be copied to the new one. | |
3818 | * @param s2 The second string to be copied to the new one, after s1. | |
3819 | * @return UnicodeString(s1).append(s2) | |
73c04bcf | 3820 | * @stable ICU 2.8 |
374ca955 A |
3821 | */ |
3822 | U_COMMON_API UnicodeString U_EXPORT2 | |
3823 | operator+ (const UnicodeString &s1, const UnicodeString &s2); | |
3824 | ||
b75a7d8f A |
3825 | //======================================== |
3826 | // Inline members | |
3827 | //======================================== | |
3828 | ||
3829 | //======================================== | |
3830 | // Privates | |
3831 | //======================================== | |
3832 | ||
3833 | inline void | |
3834 | UnicodeString::pinIndex(int32_t& start) const | |
3835 | { | |
3836 | // pin index | |
3837 | if(start < 0) { | |
3838 | start = 0; | |
46f4442e A |
3839 | } else if(start > length()) { |
3840 | start = length(); | |
b75a7d8f A |
3841 | } |
3842 | } | |
3843 | ||
3844 | inline void | |
3845 | UnicodeString::pinIndices(int32_t& start, | |
3846 | int32_t& _length) const | |
3847 | { | |
3848 | // pin indices | |
46f4442e | 3849 | int32_t len = length(); |
b75a7d8f A |
3850 | if(start < 0) { |
3851 | start = 0; | |
46f4442e A |
3852 | } else if(start > len) { |
3853 | start = len; | |
b75a7d8f A |
3854 | } |
3855 | if(_length < 0) { | |
3856 | _length = 0; | |
46f4442e A |
3857 | } else if(_length > (len - start)) { |
3858 | _length = (len - start); | |
b75a7d8f A |
3859 | } |
3860 | } | |
3861 | ||
f3c0d7a5 | 3862 | inline char16_t* |
b331163b A |
3863 | UnicodeString::getArrayStart() { |
3864 | return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ? | |
3865 | fUnion.fStackFields.fBuffer : fUnion.fFields.fArray; | |
3866 | } | |
b75a7d8f | 3867 | |
f3c0d7a5 | 3868 | inline const char16_t* |
b331163b A |
3869 | UnicodeString::getArrayStart() const { |
3870 | return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ? | |
3871 | fUnion.fStackFields.fBuffer : fUnion.fFields.fArray; | |
3872 | } | |
b75a7d8f | 3873 | |
51004dcb A |
3874 | //======================================== |
3875 | // Default constructor | |
3876 | //======================================== | |
3877 | ||
3878 | inline | |
b331163b A |
3879 | UnicodeString::UnicodeString() { |
3880 | fUnion.fStackFields.fLengthAndFlags=kShortString; | |
3881 | } | |
51004dcb | 3882 | |
f3c0d7a5 A |
3883 | inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/) { |
3884 | fUnion.fStackFields.fLengthAndFlags=kShortString; | |
3885 | } | |
3886 | ||
3887 | inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/, int32_t /*length*/) { | |
3888 | fUnion.fStackFields.fLengthAndFlags=kShortString; | |
3889 | } | |
3890 | ||
3891 | inline UnicodeString::UnicodeString(std::nullptr_t /*buffer*/, int32_t /*buffLength*/, int32_t /*buffCapacity*/) { | |
3892 | fUnion.fStackFields.fLengthAndFlags=kShortString; | |
3893 | } | |
3894 | ||
b75a7d8f A |
3895 | //======================================== |
3896 | // Read-only implementation methods | |
3897 | //======================================== | |
b331163b A |
3898 | inline UBool |
3899 | UnicodeString::hasShortLength() const { | |
3900 | return fUnion.fFields.fLengthAndFlags>=0; | |
3901 | } | |
3902 | ||
374ca955 | 3903 | inline int32_t |
b331163b A |
3904 | UnicodeString::getShortLength() const { |
3905 | // fLengthAndFlags must be non-negative -> short length >= 0 | |
3906 | // and arithmetic or logical shift does not matter. | |
3907 | return fUnion.fFields.fLengthAndFlags>>kLengthShift; | |
3908 | } | |
3909 | ||
3910 | inline int32_t | |
3911 | UnicodeString::length() const { | |
3912 | return hasShortLength() ? getShortLength() : fUnion.fFields.fLength; | |
3913 | } | |
b75a7d8f | 3914 | |
374ca955 | 3915 | inline int32_t |
b331163b A |
3916 | UnicodeString::getCapacity() const { |
3917 | return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ? | |
3918 | US_STACKBUF_SIZE : fUnion.fFields.fCapacity; | |
3919 | } | |
b75a7d8f | 3920 | |
374ca955 | 3921 | inline int32_t |
b75a7d8f A |
3922 | UnicodeString::hashCode() const |
3923 | { return doHashCode(); } | |
3924 | ||
374ca955 | 3925 | inline UBool |
b75a7d8f | 3926 | UnicodeString::isBogus() const |
b331163b | 3927 | { return (UBool)(fUnion.fFields.fLengthAndFlags & kIsBogus); } |
b75a7d8f | 3928 | |
46f4442e A |
3929 | inline UBool |
3930 | UnicodeString::isWritable() const | |
b331163b | 3931 | { return (UBool)!(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus)); } |
46f4442e A |
3932 | |
3933 | inline UBool | |
3934 | UnicodeString::isBufferWritable() const | |
3935 | { | |
3936 | return (UBool)( | |
b331163b A |
3937 | !(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus|kBufferIsReadonly)) && |
3938 | (!(fUnion.fFields.fLengthAndFlags&kRefCounted) || refCount()==1)); | |
46f4442e A |
3939 | } |
3940 | ||
f3c0d7a5 | 3941 | inline const char16_t * |
b75a7d8f | 3942 | UnicodeString::getBuffer() const { |
b331163b | 3943 | if(fUnion.fFields.fLengthAndFlags&(kIsBogus|kOpenGetBuffer)) { |
f3c0d7a5 | 3944 | return nullptr; |
b331163b A |
3945 | } else if(fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) { |
3946 | return fUnion.fStackFields.fBuffer; | |
46f4442e A |
3947 | } else { |
3948 | return fUnion.fFields.fArray; | |
b75a7d8f A |
3949 | } |
3950 | } | |
3951 | ||
3952 | //======================================== | |
3953 | // Read-only alias methods | |
3954 | //======================================== | |
3955 | inline int8_t | |
3956 | UnicodeString::doCompare(int32_t start, | |
46f4442e | 3957 | int32_t thisLength, |
b75a7d8f A |
3958 | const UnicodeString& srcText, |
3959 | int32_t srcStart, | |
3960 | int32_t srcLength) const | |
3961 | { | |
3962 | if(srcText.isBogus()) { | |
3963 | return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise | |
3964 | } else { | |
3965 | srcText.pinIndices(srcStart, srcLength); | |
46f4442e | 3966 | return doCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength); |
b75a7d8f A |
3967 | } |
3968 | } | |
3969 | ||
3970 | inline UBool | |
3971 | UnicodeString::operator== (const UnicodeString& text) const | |
3972 | { | |
3973 | if(isBogus()) { | |
3974 | return text.isBogus(); | |
3975 | } else { | |
46f4442e | 3976 | int32_t len = length(), textLength = text.length(); |
51004dcb | 3977 | return !text.isBogus() && len == textLength && doEquals(text, len); |
b75a7d8f A |
3978 | } |
3979 | } | |
3980 | ||
3981 | inline UBool | |
3982 | UnicodeString::operator!= (const UnicodeString& text) const | |
3983 | { return (! operator==(text)); } | |
3984 | ||
3985 | inline UBool | |
3986 | UnicodeString::operator> (const UnicodeString& text) const | |
46f4442e | 3987 | { return doCompare(0, length(), text, 0, text.length()) == 1; } |
b75a7d8f A |
3988 | |
3989 | inline UBool | |
3990 | UnicodeString::operator< (const UnicodeString& text) const | |
46f4442e | 3991 | { return doCompare(0, length(), text, 0, text.length()) == -1; } |
b75a7d8f A |
3992 | |
3993 | inline UBool | |
3994 | UnicodeString::operator>= (const UnicodeString& text) const | |
46f4442e | 3995 | { return doCompare(0, length(), text, 0, text.length()) != -1; } |
b75a7d8f A |
3996 | |
3997 | inline UBool | |
3998 | UnicodeString::operator<= (const UnicodeString& text) const | |
46f4442e | 3999 | { return doCompare(0, length(), text, 0, text.length()) != 1; } |
b75a7d8f | 4000 | |
374ca955 | 4001 | inline int8_t |
b75a7d8f | 4002 | UnicodeString::compare(const UnicodeString& text) const |
46f4442e | 4003 | { return doCompare(0, length(), text, 0, text.length()); } |
b75a7d8f | 4004 | |
374ca955 | 4005 | inline int8_t |
b75a7d8f A |
4006 | UnicodeString::compare(int32_t start, |
4007 | int32_t _length, | |
4008 | const UnicodeString& srcText) const | |
46f4442e | 4009 | { return doCompare(start, _length, srcText, 0, srcText.length()); } |
b75a7d8f | 4010 | |
374ca955 | 4011 | inline int8_t |
f3c0d7a5 | 4012 | UnicodeString::compare(ConstChar16Ptr srcChars, |
b75a7d8f | 4013 | int32_t srcLength) const |
46f4442e | 4014 | { return doCompare(0, length(), srcChars, 0, srcLength); } |
b75a7d8f | 4015 | |
374ca955 | 4016 | inline int8_t |
b75a7d8f A |
4017 | UnicodeString::compare(int32_t start, |
4018 | int32_t _length, | |
4019 | const UnicodeString& srcText, | |
4020 | int32_t srcStart, | |
4021 | int32_t srcLength) const | |
4022 | { return doCompare(start, _length, srcText, srcStart, srcLength); } | |
4023 | ||
4024 | inline int8_t | |
4025 | UnicodeString::compare(int32_t start, | |
4026 | int32_t _length, | |
f3c0d7a5 | 4027 | const char16_t *srcChars) const |
b75a7d8f A |
4028 | { return doCompare(start, _length, srcChars, 0, _length); } |
4029 | ||
374ca955 | 4030 | inline int8_t |
b75a7d8f A |
4031 | UnicodeString::compare(int32_t start, |
4032 | int32_t _length, | |
f3c0d7a5 | 4033 | const char16_t *srcChars, |
b75a7d8f A |
4034 | int32_t srcStart, |
4035 | int32_t srcLength) const | |
4036 | { return doCompare(start, _length, srcChars, srcStart, srcLength); } | |
4037 | ||
4038 | inline int8_t | |
4039 | UnicodeString::compareBetween(int32_t start, | |
4040 | int32_t limit, | |
4041 | const UnicodeString& srcText, | |
4042 | int32_t srcStart, | |
4043 | int32_t srcLimit) const | |
374ca955 | 4044 | { return doCompare(start, limit - start, |
b75a7d8f A |
4045 | srcText, srcStart, srcLimit - srcStart); } |
4046 | ||
4047 | inline int8_t | |
4048 | UnicodeString::doCompareCodePointOrder(int32_t start, | |
46f4442e | 4049 | int32_t thisLength, |
b75a7d8f A |
4050 | const UnicodeString& srcText, |
4051 | int32_t srcStart, | |
4052 | int32_t srcLength) const | |
4053 | { | |
4054 | if(srcText.isBogus()) { | |
4055 | return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise | |
4056 | } else { | |
4057 | srcText.pinIndices(srcStart, srcLength); | |
46f4442e | 4058 | return doCompareCodePointOrder(start, thisLength, srcText.getArrayStart(), srcStart, srcLength); |
b75a7d8f A |
4059 | } |
4060 | } | |
4061 | ||
374ca955 | 4062 | inline int8_t |
b75a7d8f | 4063 | UnicodeString::compareCodePointOrder(const UnicodeString& text) const |
46f4442e | 4064 | { return doCompareCodePointOrder(0, length(), text, 0, text.length()); } |
b75a7d8f | 4065 | |
374ca955 | 4066 | inline int8_t |
b75a7d8f A |
4067 | UnicodeString::compareCodePointOrder(int32_t start, |
4068 | int32_t _length, | |
4069 | const UnicodeString& srcText) const | |
46f4442e | 4070 | { return doCompareCodePointOrder(start, _length, srcText, 0, srcText.length()); } |
b75a7d8f | 4071 | |
374ca955 | 4072 | inline int8_t |
f3c0d7a5 | 4073 | UnicodeString::compareCodePointOrder(ConstChar16Ptr srcChars, |
b75a7d8f | 4074 | int32_t srcLength) const |
46f4442e | 4075 | { return doCompareCodePointOrder(0, length(), srcChars, 0, srcLength); } |
b75a7d8f | 4076 | |
374ca955 | 4077 | inline int8_t |
b75a7d8f A |
4078 | UnicodeString::compareCodePointOrder(int32_t start, |
4079 | int32_t _length, | |
4080 | const UnicodeString& srcText, | |
4081 | int32_t srcStart, | |
4082 | int32_t srcLength) const | |
4083 | { return doCompareCodePointOrder(start, _length, srcText, srcStart, srcLength); } | |
4084 | ||
4085 | inline int8_t | |
4086 | UnicodeString::compareCodePointOrder(int32_t start, | |
4087 | int32_t _length, | |
f3c0d7a5 | 4088 | const char16_t *srcChars) const |
b75a7d8f A |
4089 | { return doCompareCodePointOrder(start, _length, srcChars, 0, _length); } |
4090 | ||
374ca955 | 4091 | inline int8_t |
b75a7d8f A |
4092 | UnicodeString::compareCodePointOrder(int32_t start, |
4093 | int32_t _length, | |
f3c0d7a5 | 4094 | const char16_t *srcChars, |
b75a7d8f A |
4095 | int32_t srcStart, |
4096 | int32_t srcLength) const | |
4097 | { return doCompareCodePointOrder(start, _length, srcChars, srcStart, srcLength); } | |
4098 | ||
4099 | inline int8_t | |
4100 | UnicodeString::compareCodePointOrderBetween(int32_t start, | |
4101 | int32_t limit, | |
4102 | const UnicodeString& srcText, | |
4103 | int32_t srcStart, | |
4104 | int32_t srcLimit) const | |
374ca955 | 4105 | { return doCompareCodePointOrder(start, limit - start, |
b75a7d8f A |
4106 | srcText, srcStart, srcLimit - srcStart); } |
4107 | ||
4108 | inline int8_t | |
4109 | UnicodeString::doCaseCompare(int32_t start, | |
46f4442e | 4110 | int32_t thisLength, |
b75a7d8f A |
4111 | const UnicodeString &srcText, |
4112 | int32_t srcStart, | |
4113 | int32_t srcLength, | |
4114 | uint32_t options) const | |
4115 | { | |
4116 | if(srcText.isBogus()) { | |
4117 | return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise | |
4118 | } else { | |
4119 | srcText.pinIndices(srcStart, srcLength); | |
46f4442e | 4120 | return doCaseCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength, options); |
b75a7d8f A |
4121 | } |
4122 | } | |
4123 | ||
374ca955 | 4124 | inline int8_t |
b75a7d8f | 4125 | UnicodeString::caseCompare(const UnicodeString &text, uint32_t options) const { |
46f4442e | 4126 | return doCaseCompare(0, length(), text, 0, text.length(), options); |
b75a7d8f A |
4127 | } |
4128 | ||
374ca955 | 4129 | inline int8_t |
b75a7d8f A |
4130 | UnicodeString::caseCompare(int32_t start, |
4131 | int32_t _length, | |
4132 | const UnicodeString &srcText, | |
4133 | uint32_t options) const { | |
46f4442e | 4134 | return doCaseCompare(start, _length, srcText, 0, srcText.length(), options); |
b75a7d8f A |
4135 | } |
4136 | ||
374ca955 | 4137 | inline int8_t |
f3c0d7a5 | 4138 | UnicodeString::caseCompare(ConstChar16Ptr srcChars, |
b75a7d8f A |
4139 | int32_t srcLength, |
4140 | uint32_t options) const { | |
46f4442e | 4141 | return doCaseCompare(0, length(), srcChars, 0, srcLength, options); |
b75a7d8f A |
4142 | } |
4143 | ||
374ca955 | 4144 | inline int8_t |
b75a7d8f A |
4145 | UnicodeString::caseCompare(int32_t start, |
4146 | int32_t _length, | |
4147 | const UnicodeString &srcText, | |
4148 | int32_t srcStart, | |
4149 | int32_t srcLength, | |
4150 | uint32_t options) const { | |
4151 | return doCaseCompare(start, _length, srcText, srcStart, srcLength, options); | |
4152 | } | |
4153 | ||
4154 | inline int8_t | |
4155 | UnicodeString::caseCompare(int32_t start, | |
4156 | int32_t _length, | |
f3c0d7a5 | 4157 | const char16_t *srcChars, |
b75a7d8f A |
4158 | uint32_t options) const { |
4159 | return doCaseCompare(start, _length, srcChars, 0, _length, options); | |
4160 | } | |
4161 | ||
374ca955 | 4162 | inline int8_t |
b75a7d8f A |
4163 | UnicodeString::caseCompare(int32_t start, |
4164 | int32_t _length, | |
f3c0d7a5 | 4165 | const char16_t *srcChars, |
b75a7d8f A |
4166 | int32_t srcStart, |
4167 | int32_t srcLength, | |
4168 | uint32_t options) const { | |
4169 | return doCaseCompare(start, _length, srcChars, srcStart, srcLength, options); | |
4170 | } | |
4171 | ||
4172 | inline int8_t | |
4173 | UnicodeString::caseCompareBetween(int32_t start, | |
4174 | int32_t limit, | |
4175 | const UnicodeString &srcText, | |
4176 | int32_t srcStart, | |
4177 | int32_t srcLimit, | |
4178 | uint32_t options) const { | |
4179 | return doCaseCompare(start, limit - start, srcText, srcStart, srcLimit - srcStart, options); | |
4180 | } | |
4181 | ||
374ca955 | 4182 | inline int32_t |
b75a7d8f A |
4183 | UnicodeString::indexOf(const UnicodeString& srcText, |
4184 | int32_t srcStart, | |
4185 | int32_t srcLength, | |
4186 | int32_t start, | |
4187 | int32_t _length) const | |
4188 | { | |
4189 | if(!srcText.isBogus()) { | |
4190 | srcText.pinIndices(srcStart, srcLength); | |
4191 | if(srcLength > 0) { | |
4192 | return indexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length); | |
4193 | } | |
4194 | } | |
4195 | return -1; | |
4196 | } | |
4197 | ||
374ca955 | 4198 | inline int32_t |
b75a7d8f | 4199 | UnicodeString::indexOf(const UnicodeString& text) const |
46f4442e | 4200 | { return indexOf(text, 0, text.length(), 0, length()); } |
b75a7d8f | 4201 | |
374ca955 | 4202 | inline int32_t |
b75a7d8f A |
4203 | UnicodeString::indexOf(const UnicodeString& text, |
4204 | int32_t start) const { | |
4205 | pinIndex(start); | |
46f4442e | 4206 | return indexOf(text, 0, text.length(), start, length() - start); |
b75a7d8f A |
4207 | } |
4208 | ||
374ca955 | 4209 | inline int32_t |
b75a7d8f A |
4210 | UnicodeString::indexOf(const UnicodeString& text, |
4211 | int32_t start, | |
4212 | int32_t _length) const | |
46f4442e | 4213 | { return indexOf(text, 0, text.length(), start, _length); } |
b75a7d8f | 4214 | |
374ca955 | 4215 | inline int32_t |
f3c0d7a5 | 4216 | UnicodeString::indexOf(const char16_t *srcChars, |
b75a7d8f A |
4217 | int32_t srcLength, |
4218 | int32_t start) const { | |
4219 | pinIndex(start); | |
46f4442e | 4220 | return indexOf(srcChars, 0, srcLength, start, length() - start); |
b75a7d8f A |
4221 | } |
4222 | ||
374ca955 | 4223 | inline int32_t |
f3c0d7a5 | 4224 | UnicodeString::indexOf(ConstChar16Ptr srcChars, |
b75a7d8f A |
4225 | int32_t srcLength, |
4226 | int32_t start, | |
4227 | int32_t _length) const | |
4228 | { return indexOf(srcChars, 0, srcLength, start, _length); } | |
4229 | ||
374ca955 | 4230 | inline int32_t |
f3c0d7a5 | 4231 | UnicodeString::indexOf(char16_t c, |
b75a7d8f A |
4232 | int32_t start, |
4233 | int32_t _length) const | |
4234 | { return doIndexOf(c, start, _length); } | |
4235 | ||
374ca955 | 4236 | inline int32_t |
b75a7d8f A |
4237 | UnicodeString::indexOf(UChar32 c, |
4238 | int32_t start, | |
4239 | int32_t _length) const | |
4240 | { return doIndexOf(c, start, _length); } | |
4241 | ||
374ca955 | 4242 | inline int32_t |
f3c0d7a5 | 4243 | UnicodeString::indexOf(char16_t c) const |
46f4442e | 4244 | { return doIndexOf(c, 0, length()); } |
b75a7d8f | 4245 | |
374ca955 | 4246 | inline int32_t |
b75a7d8f | 4247 | UnicodeString::indexOf(UChar32 c) const |
46f4442e | 4248 | { return indexOf(c, 0, length()); } |
b75a7d8f | 4249 | |
374ca955 | 4250 | inline int32_t |
f3c0d7a5 | 4251 | UnicodeString::indexOf(char16_t c, |
b75a7d8f A |
4252 | int32_t start) const { |
4253 | pinIndex(start); | |
46f4442e | 4254 | return doIndexOf(c, start, length() - start); |
b75a7d8f A |
4255 | } |
4256 | ||
374ca955 | 4257 | inline int32_t |
b75a7d8f A |
4258 | UnicodeString::indexOf(UChar32 c, |
4259 | int32_t start) const { | |
4260 | pinIndex(start); | |
46f4442e | 4261 | return indexOf(c, start, length() - start); |
b75a7d8f A |
4262 | } |
4263 | ||
374ca955 | 4264 | inline int32_t |
f3c0d7a5 | 4265 | UnicodeString::lastIndexOf(ConstChar16Ptr srcChars, |
b75a7d8f A |
4266 | int32_t srcLength, |
4267 | int32_t start, | |
4268 | int32_t _length) const | |
4269 | { return lastIndexOf(srcChars, 0, srcLength, start, _length); } | |
4270 | ||
374ca955 | 4271 | inline int32_t |
f3c0d7a5 | 4272 | UnicodeString::lastIndexOf(const char16_t *srcChars, |
b75a7d8f A |
4273 | int32_t srcLength, |
4274 | int32_t start) const { | |
4275 | pinIndex(start); | |
46f4442e | 4276 | return lastIndexOf(srcChars, 0, srcLength, start, length() - start); |
b75a7d8f A |
4277 | } |
4278 | ||
374ca955 | 4279 | inline int32_t |
b75a7d8f A |
4280 | UnicodeString::lastIndexOf(const UnicodeString& srcText, |
4281 | int32_t srcStart, | |
4282 | int32_t srcLength, | |
4283 | int32_t start, | |
4284 | int32_t _length) const | |
4285 | { | |
4286 | if(!srcText.isBogus()) { | |
4287 | srcText.pinIndices(srcStart, srcLength); | |
4288 | if(srcLength > 0) { | |
4289 | return lastIndexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length); | |
4290 | } | |
4291 | } | |
4292 | return -1; | |
4293 | } | |
4294 | ||
374ca955 | 4295 | inline int32_t |
b75a7d8f A |
4296 | UnicodeString::lastIndexOf(const UnicodeString& text, |
4297 | int32_t start, | |
4298 | int32_t _length) const | |
46f4442e | 4299 | { return lastIndexOf(text, 0, text.length(), start, _length); } |
b75a7d8f | 4300 | |
374ca955 | 4301 | inline int32_t |
b75a7d8f A |
4302 | UnicodeString::lastIndexOf(const UnicodeString& text, |
4303 | int32_t start) const { | |
4304 | pinIndex(start); | |
46f4442e | 4305 | return lastIndexOf(text, 0, text.length(), start, length() - start); |
b75a7d8f A |
4306 | } |
4307 | ||
374ca955 | 4308 | inline int32_t |
b75a7d8f | 4309 | UnicodeString::lastIndexOf(const UnicodeString& text) const |
46f4442e | 4310 | { return lastIndexOf(text, 0, text.length(), 0, length()); } |
b75a7d8f | 4311 | |
374ca955 | 4312 | inline int32_t |
f3c0d7a5 | 4313 | UnicodeString::lastIndexOf(char16_t c, |
b75a7d8f A |
4314 | int32_t start, |
4315 | int32_t _length) const | |
4316 | { return doLastIndexOf(c, start, _length); } | |
4317 | ||
374ca955 | 4318 | inline int32_t |
b75a7d8f A |
4319 | UnicodeString::lastIndexOf(UChar32 c, |
4320 | int32_t start, | |
4321 | int32_t _length) const { | |
4322 | return doLastIndexOf(c, start, _length); | |
4323 | } | |
4324 | ||
374ca955 | 4325 | inline int32_t |
f3c0d7a5 | 4326 | UnicodeString::lastIndexOf(char16_t c) const |
46f4442e | 4327 | { return doLastIndexOf(c, 0, length()); } |
b75a7d8f | 4328 | |
374ca955 | 4329 | inline int32_t |
b75a7d8f | 4330 | UnicodeString::lastIndexOf(UChar32 c) const { |
46f4442e | 4331 | return lastIndexOf(c, 0, length()); |
b75a7d8f A |
4332 | } |
4333 | ||
374ca955 | 4334 | inline int32_t |
f3c0d7a5 | 4335 | UnicodeString::lastIndexOf(char16_t c, |
b75a7d8f A |
4336 | int32_t start) const { |
4337 | pinIndex(start); | |
46f4442e | 4338 | return doLastIndexOf(c, start, length() - start); |
b75a7d8f A |
4339 | } |
4340 | ||
374ca955 | 4341 | inline int32_t |
b75a7d8f A |
4342 | UnicodeString::lastIndexOf(UChar32 c, |
4343 | int32_t start) const { | |
4344 | pinIndex(start); | |
46f4442e | 4345 | return lastIndexOf(c, start, length() - start); |
b75a7d8f A |
4346 | } |
4347 | ||
374ca955 | 4348 | inline UBool |
b75a7d8f | 4349 | UnicodeString::startsWith(const UnicodeString& text) const |
46f4442e | 4350 | { return compare(0, text.length(), text, 0, text.length()) == 0; } |
b75a7d8f | 4351 | |
374ca955 | 4352 | inline UBool |
b75a7d8f A |
4353 | UnicodeString::startsWith(const UnicodeString& srcText, |
4354 | int32_t srcStart, | |
4355 | int32_t srcLength) const | |
4356 | { return doCompare(0, srcLength, srcText, srcStart, srcLength) == 0; } | |
4357 | ||
374ca955 | 4358 | inline UBool |
f3c0d7a5 | 4359 | UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const { |
4388f060 | 4360 | if(srcLength < 0) { |
f3c0d7a5 | 4361 | srcLength = u_strlen(toUCharPtr(srcChars)); |
4388f060 A |
4362 | } |
4363 | return doCompare(0, srcLength, srcChars, 0, srcLength) == 0; | |
4364 | } | |
b75a7d8f | 4365 | |
374ca955 | 4366 | inline UBool |
f3c0d7a5 | 4367 | UnicodeString::startsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const { |
4388f060 | 4368 | if(srcLength < 0) { |
f3c0d7a5 | 4369 | srcLength = u_strlen(toUCharPtr(srcChars)); |
4388f060 A |
4370 | } |
4371 | return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0; | |
4372 | } | |
b75a7d8f | 4373 | |
374ca955 | 4374 | inline UBool |
b75a7d8f | 4375 | UnicodeString::endsWith(const UnicodeString& text) const |
46f4442e A |
4376 | { return doCompare(length() - text.length(), text.length(), |
4377 | text, 0, text.length()) == 0; } | |
b75a7d8f | 4378 | |
374ca955 | 4379 | inline UBool |
b75a7d8f A |
4380 | UnicodeString::endsWith(const UnicodeString& srcText, |
4381 | int32_t srcStart, | |
4382 | int32_t srcLength) const { | |
4383 | srcText.pinIndices(srcStart, srcLength); | |
46f4442e | 4384 | return doCompare(length() - srcLength, srcLength, |
b75a7d8f A |
4385 | srcText, srcStart, srcLength) == 0; |
4386 | } | |
4387 | ||
374ca955 | 4388 | inline UBool |
f3c0d7a5 | 4389 | UnicodeString::endsWith(ConstChar16Ptr srcChars, |
b75a7d8f A |
4390 | int32_t srcLength) const { |
4391 | if(srcLength < 0) { | |
f3c0d7a5 | 4392 | srcLength = u_strlen(toUCharPtr(srcChars)); |
b75a7d8f | 4393 | } |
46f4442e | 4394 | return doCompare(length() - srcLength, srcLength, |
b75a7d8f A |
4395 | srcChars, 0, srcLength) == 0; |
4396 | } | |
4397 | ||
374ca955 | 4398 | inline UBool |
f3c0d7a5 | 4399 | UnicodeString::endsWith(const char16_t *srcChars, |
b75a7d8f A |
4400 | int32_t srcStart, |
4401 | int32_t srcLength) const { | |
4402 | if(srcLength < 0) { | |
f3c0d7a5 | 4403 | srcLength = u_strlen(toUCharPtr(srcChars + srcStart)); |
b75a7d8f | 4404 | } |
46f4442e | 4405 | return doCompare(length() - srcLength, srcLength, |
b75a7d8f A |
4406 | srcChars, srcStart, srcLength) == 0; |
4407 | } | |
4408 | ||
4409 | //======================================== | |
4410 | // replace | |
4411 | //======================================== | |
374ca955 A |
4412 | inline UnicodeString& |
4413 | UnicodeString::replace(int32_t start, | |
4414 | int32_t _length, | |
4415 | const UnicodeString& srcText) | |
46f4442e | 4416 | { return doReplace(start, _length, srcText, 0, srcText.length()); } |
b75a7d8f | 4417 | |
374ca955 A |
4418 | inline UnicodeString& |
4419 | UnicodeString::replace(int32_t start, | |
4420 | int32_t _length, | |
4421 | const UnicodeString& srcText, | |
4422 | int32_t srcStart, | |
b75a7d8f A |
4423 | int32_t srcLength) |
4424 | { return doReplace(start, _length, srcText, srcStart, srcLength); } | |
4425 | ||
374ca955 A |
4426 | inline UnicodeString& |
4427 | UnicodeString::replace(int32_t start, | |
4428 | int32_t _length, | |
f3c0d7a5 | 4429 | ConstChar16Ptr srcChars, |
b75a7d8f A |
4430 | int32_t srcLength) |
4431 | { return doReplace(start, _length, srcChars, 0, srcLength); } | |
4432 | ||
374ca955 A |
4433 | inline UnicodeString& |
4434 | UnicodeString::replace(int32_t start, | |
4435 | int32_t _length, | |
f3c0d7a5 | 4436 | const char16_t *srcChars, |
374ca955 | 4437 | int32_t srcStart, |
b75a7d8f A |
4438 | int32_t srcLength) |
4439 | { return doReplace(start, _length, srcChars, srcStart, srcLength); } | |
4440 | ||
374ca955 A |
4441 | inline UnicodeString& |
4442 | UnicodeString::replace(int32_t start, | |
4443 | int32_t _length, | |
f3c0d7a5 | 4444 | char16_t srcChar) |
b75a7d8f A |
4445 | { return doReplace(start, _length, &srcChar, 0, 1); } |
4446 | ||
374ca955 A |
4447 | inline UnicodeString& |
4448 | UnicodeString::replaceBetween(int32_t start, | |
4449 | int32_t limit, | |
b75a7d8f | 4450 | const UnicodeString& srcText) |
46f4442e | 4451 | { return doReplace(start, limit - start, srcText, 0, srcText.length()); } |
b75a7d8f A |
4452 | |
4453 | inline UnicodeString& | |
374ca955 A |
4454 | UnicodeString::replaceBetween(int32_t start, |
4455 | int32_t limit, | |
4456 | const UnicodeString& srcText, | |
4457 | int32_t srcStart, | |
b75a7d8f A |
4458 | int32_t srcLimit) |
4459 | { return doReplace(start, limit - start, srcText, srcStart, srcLimit - srcStart); } | |
4460 | ||
374ca955 | 4461 | inline UnicodeString& |
b75a7d8f A |
4462 | UnicodeString::findAndReplace(const UnicodeString& oldText, |
4463 | const UnicodeString& newText) | |
46f4442e A |
4464 | { return findAndReplace(0, length(), oldText, 0, oldText.length(), |
4465 | newText, 0, newText.length()); } | |
b75a7d8f | 4466 | |
374ca955 | 4467 | inline UnicodeString& |
b75a7d8f A |
4468 | UnicodeString::findAndReplace(int32_t start, |
4469 | int32_t _length, | |
4470 | const UnicodeString& oldText, | |
4471 | const UnicodeString& newText) | |
46f4442e A |
4472 | { return findAndReplace(start, _length, oldText, 0, oldText.length(), |
4473 | newText, 0, newText.length()); } | |
b75a7d8f A |
4474 | |
4475 | // ============================ | |
4476 | // extract | |
4477 | // ============================ | |
4478 | inline void | |
4479 | UnicodeString::doExtract(int32_t start, | |
4480 | int32_t _length, | |
4481 | UnicodeString& target) const | |
46f4442e | 4482 | { target.replace(0, target.length(), *this, start, _length); } |
b75a7d8f | 4483 | |
374ca955 A |
4484 | inline void |
4485 | UnicodeString::extract(int32_t start, | |
4486 | int32_t _length, | |
f3c0d7a5 | 4487 | Char16Ptr target, |
b75a7d8f A |
4488 | int32_t targetStart) const |
4489 | { doExtract(start, _length, target, targetStart); } | |
4490 | ||
374ca955 | 4491 | inline void |
b75a7d8f A |
4492 | UnicodeString::extract(int32_t start, |
4493 | int32_t _length, | |
4494 | UnicodeString& target) const | |
4495 | { doExtract(start, _length, target); } | |
4496 | ||
374ca955 A |
4497 | #if !UCONFIG_NO_CONVERSION |
4498 | ||
b75a7d8f A |
4499 | inline int32_t |
4500 | UnicodeString::extract(int32_t start, | |
4501 | int32_t _length, | |
4502 | char *dst, | |
4503 | const char *codepage) const | |
4504 | ||
4505 | { | |
4506 | // This dstSize value will be checked explicitly | |
4507 | return extract(start, _length, dst, dst!=0 ? 0xffffffff : 0, codepage); | |
4508 | } | |
4509 | ||
374ca955 A |
4510 | #endif |
4511 | ||
4512 | inline void | |
4513 | UnicodeString::extractBetween(int32_t start, | |
4514 | int32_t limit, | |
f3c0d7a5 | 4515 | char16_t *dst, |
374ca955 A |
4516 | int32_t dstStart) const { |
4517 | pinIndex(start); | |
4518 | pinIndex(limit); | |
4519 | doExtract(start, limit - start, dst, dstStart); | |
4520 | } | |
b75a7d8f | 4521 | |
729e4ab9 A |
4522 | inline UnicodeString |
4523 | UnicodeString::tempSubStringBetween(int32_t start, int32_t limit) const { | |
4524 | return tempSubString(start, limit - start); | |
4525 | } | |
4526 | ||
f3c0d7a5 | 4527 | inline char16_t |
b75a7d8f A |
4528 | UnicodeString::doCharAt(int32_t offset) const |
4529 | { | |
46f4442e A |
4530 | if((uint32_t)offset < (uint32_t)length()) { |
4531 | return getArrayStart()[offset]; | |
b75a7d8f A |
4532 | } else { |
4533 | return kInvalidUChar; | |
4534 | } | |
4535 | } | |
4536 | ||
f3c0d7a5 | 4537 | inline char16_t |
b75a7d8f A |
4538 | UnicodeString::charAt(int32_t offset) const |
4539 | { return doCharAt(offset); } | |
4540 | ||
f3c0d7a5 | 4541 | inline char16_t |
b75a7d8f A |
4542 | UnicodeString::operator[] (int32_t offset) const |
4543 | { return doCharAt(offset); } | |
4544 | ||
b75a7d8f A |
4545 | inline UBool |
4546 | UnicodeString::isEmpty() const { | |
b331163b A |
4547 | // Arithmetic or logical right shift does not matter: only testing for 0. |
4548 | return (fUnion.fFields.fLengthAndFlags>>kLengthShift) == 0; | |
b75a7d8f A |
4549 | } |
4550 | ||
4551 | //======================================== | |
4552 | // Write implementation methods | |
4553 | //======================================== | |
b331163b A |
4554 | inline void |
4555 | UnicodeString::setZeroLength() { | |
4556 | fUnion.fFields.fLengthAndFlags &= kAllStorageFlags; | |
4557 | } | |
4558 | ||
4559 | inline void | |
4560 | UnicodeString::setShortLength(int32_t len) { | |
4561 | // requires 0 <= len <= kMaxShortLength | |
4562 | fUnion.fFields.fLengthAndFlags = | |
4563 | (int16_t)((fUnion.fFields.fLengthAndFlags & kAllStorageFlags) | (len << kLengthShift)); | |
4564 | } | |
4565 | ||
46f4442e A |
4566 | inline void |
4567 | UnicodeString::setLength(int32_t len) { | |
b331163b A |
4568 | if(len <= kMaxShortLength) { |
4569 | setShortLength(len); | |
46f4442e | 4570 | } else { |
b331163b | 4571 | fUnion.fFields.fLengthAndFlags |= kLengthIsLarge; |
46f4442e A |
4572 | fUnion.fFields.fLength = len; |
4573 | } | |
4574 | } | |
4575 | ||
4576 | inline void | |
4577 | UnicodeString::setToEmpty() { | |
b331163b | 4578 | fUnion.fFields.fLengthAndFlags = kShortString; |
46f4442e A |
4579 | } |
4580 | ||
46f4442e | 4581 | inline void |
f3c0d7a5 | 4582 | UnicodeString::setArray(char16_t *array, int32_t len, int32_t capacity) { |
46f4442e A |
4583 | setLength(len); |
4584 | fUnion.fFields.fArray = array; | |
4585 | fUnion.fFields.fCapacity = capacity; | |
4586 | } | |
4587 | ||
374ca955 | 4588 | inline UnicodeString& |
f3c0d7a5 | 4589 | UnicodeString::operator= (char16_t ch) |
46f4442e | 4590 | { return doReplace(0, length(), &ch, 0, 1); } |
b75a7d8f | 4591 | |
374ca955 A |
4592 | inline UnicodeString& |
4593 | UnicodeString::operator= (UChar32 ch) | |
46f4442e | 4594 | { return replace(0, length(), ch); } |
b75a7d8f | 4595 | |
374ca955 A |
4596 | inline UnicodeString& |
4597 | UnicodeString::setTo(const UnicodeString& srcText, | |
4598 | int32_t srcStart, | |
b75a7d8f A |
4599 | int32_t srcLength) |
4600 | { | |
4601 | unBogus(); | |
46f4442e | 4602 | return doReplace(0, length(), srcText, srcStart, srcLength); |
b75a7d8f A |
4603 | } |
4604 | ||
374ca955 A |
4605 | inline UnicodeString& |
4606 | UnicodeString::setTo(const UnicodeString& srcText, | |
b75a7d8f A |
4607 | int32_t srcStart) |
4608 | { | |
4609 | unBogus(); | |
4610 | srcText.pinIndex(srcStart); | |
46f4442e | 4611 | return doReplace(0, length(), srcText, srcStart, srcText.length() - srcStart); |
b75a7d8f A |
4612 | } |
4613 | ||
374ca955 | 4614 | inline UnicodeString& |
b75a7d8f A |
4615 | UnicodeString::setTo(const UnicodeString& srcText) |
4616 | { | |
4388f060 | 4617 | return copyFrom(srcText); |
b75a7d8f A |
4618 | } |
4619 | ||
374ca955 | 4620 | inline UnicodeString& |
f3c0d7a5 | 4621 | UnicodeString::setTo(const char16_t *srcChars, |
b75a7d8f A |
4622 | int32_t srcLength) |
4623 | { | |
4624 | unBogus(); | |
46f4442e | 4625 | return doReplace(0, length(), srcChars, 0, srcLength); |
b75a7d8f A |
4626 | } |
4627 | ||
374ca955 | 4628 | inline UnicodeString& |
f3c0d7a5 | 4629 | UnicodeString::setTo(char16_t srcChar) |
b75a7d8f A |
4630 | { |
4631 | unBogus(); | |
46f4442e | 4632 | return doReplace(0, length(), &srcChar, 0, 1); |
b75a7d8f A |
4633 | } |
4634 | ||
374ca955 | 4635 | inline UnicodeString& |
b75a7d8f A |
4636 | UnicodeString::setTo(UChar32 srcChar) |
4637 | { | |
4638 | unBogus(); | |
46f4442e | 4639 | return replace(0, length(), srcChar); |
b75a7d8f A |
4640 | } |
4641 | ||
374ca955 A |
4642 | inline UnicodeString& |
4643 | UnicodeString::append(const UnicodeString& srcText, | |
4644 | int32_t srcStart, | |
b75a7d8f | 4645 | int32_t srcLength) |
2ca993e8 | 4646 | { return doAppend(srcText, srcStart, srcLength); } |
b75a7d8f | 4647 | |
374ca955 | 4648 | inline UnicodeString& |
b75a7d8f | 4649 | UnicodeString::append(const UnicodeString& srcText) |
2ca993e8 | 4650 | { return doAppend(srcText, 0, srcText.length()); } |
b75a7d8f | 4651 | |
374ca955 | 4652 | inline UnicodeString& |
f3c0d7a5 | 4653 | UnicodeString::append(const char16_t *srcChars, |
374ca955 | 4654 | int32_t srcStart, |
b75a7d8f | 4655 | int32_t srcLength) |
2ca993e8 | 4656 | { return doAppend(srcChars, srcStart, srcLength); } |
b75a7d8f | 4657 | |
374ca955 | 4658 | inline UnicodeString& |
f3c0d7a5 | 4659 | UnicodeString::append(ConstChar16Ptr srcChars, |
b75a7d8f | 4660 | int32_t srcLength) |
2ca993e8 | 4661 | { return doAppend(srcChars, 0, srcLength); } |
b75a7d8f | 4662 | |
374ca955 | 4663 | inline UnicodeString& |
f3c0d7a5 | 4664 | UnicodeString::append(char16_t srcChar) |
2ca993e8 | 4665 | { return doAppend(&srcChar, 0, 1); } |
b75a7d8f | 4666 | |
374ca955 | 4667 | inline UnicodeString& |
f3c0d7a5 | 4668 | UnicodeString::operator+= (char16_t ch) |
2ca993e8 | 4669 | { return doAppend(&ch, 0, 1); } |
73c04bcf A |
4670 | |
4671 | inline UnicodeString& | |
4672 | UnicodeString::operator+= (UChar32 ch) { | |
4673 | return append(ch); | |
4674 | } | |
4675 | ||
4676 | inline UnicodeString& | |
4677 | UnicodeString::operator+= (const UnicodeString& srcText) | |
2ca993e8 | 4678 | { return doAppend(srcText, 0, srcText.length()); } |
73c04bcf | 4679 | |
374ca955 A |
4680 | inline UnicodeString& |
4681 | UnicodeString::insert(int32_t start, | |
4682 | const UnicodeString& srcText, | |
4683 | int32_t srcStart, | |
b75a7d8f A |
4684 | int32_t srcLength) |
4685 | { return doReplace(start, 0, srcText, srcStart, srcLength); } | |
4686 | ||
374ca955 A |
4687 | inline UnicodeString& |
4688 | UnicodeString::insert(int32_t start, | |
b75a7d8f | 4689 | const UnicodeString& srcText) |
46f4442e | 4690 | { return doReplace(start, 0, srcText, 0, srcText.length()); } |
b75a7d8f | 4691 | |
374ca955 A |
4692 | inline UnicodeString& |
4693 | UnicodeString::insert(int32_t start, | |
f3c0d7a5 | 4694 | const char16_t *srcChars, |
374ca955 | 4695 | int32_t srcStart, |
b75a7d8f A |
4696 | int32_t srcLength) |
4697 | { return doReplace(start, 0, srcChars, srcStart, srcLength); } | |
4698 | ||
374ca955 A |
4699 | inline UnicodeString& |
4700 | UnicodeString::insert(int32_t start, | |
f3c0d7a5 | 4701 | ConstChar16Ptr srcChars, |
b75a7d8f A |
4702 | int32_t srcLength) |
4703 | { return doReplace(start, 0, srcChars, 0, srcLength); } | |
4704 | ||
374ca955 A |
4705 | inline UnicodeString& |
4706 | UnicodeString::insert(int32_t start, | |
f3c0d7a5 | 4707 | char16_t srcChar) |
b75a7d8f A |
4708 | { return doReplace(start, 0, &srcChar, 0, 1); } |
4709 | ||
374ca955 A |
4710 | inline UnicodeString& |
4711 | UnicodeString::insert(int32_t start, | |
b75a7d8f A |
4712 | UChar32 srcChar) |
4713 | { return replace(start, 0, srcChar); } | |
4714 | ||
4715 | ||
374ca955 | 4716 | inline UnicodeString& |
b75a7d8f A |
4717 | UnicodeString::remove() |
4718 | { | |
4719 | // remove() of a bogus string makes the string empty and non-bogus | |
57a6839d | 4720 | if(isBogus()) { |
729e4ab9 | 4721 | setToEmpty(); |
b75a7d8f | 4722 | } else { |
b331163b | 4723 | setZeroLength(); |
b75a7d8f A |
4724 | } |
4725 | return *this; | |
4726 | } | |
4727 | ||
374ca955 A |
4728 | inline UnicodeString& |
4729 | UnicodeString::remove(int32_t start, | |
b75a7d8f A |
4730 | int32_t _length) |
4731 | { | |
73c04bcf A |
4732 | if(start <= 0 && _length == INT32_MAX) { |
4733 | // remove(guaranteed everything) of a bogus string makes the string empty and non-bogus | |
4734 | return remove(); | |
4735 | } | |
b75a7d8f | 4736 | return doReplace(start, _length, NULL, 0, 0); |
b75a7d8f A |
4737 | } |
4738 | ||
374ca955 | 4739 | inline UnicodeString& |
b75a7d8f A |
4740 | UnicodeString::removeBetween(int32_t start, |
4741 | int32_t limit) | |
4742 | { return doReplace(start, limit - start, NULL, 0, 0); } | |
4743 | ||
729e4ab9 A |
4744 | inline UnicodeString & |
4745 | UnicodeString::retainBetween(int32_t start, int32_t limit) { | |
4746 | truncate(limit); | |
4747 | return doReplace(0, start, NULL, 0, 0); | |
4748 | } | |
4749 | ||
374ca955 | 4750 | inline UBool |
b75a7d8f A |
4751 | UnicodeString::truncate(int32_t targetLength) |
4752 | { | |
4753 | if(isBogus() && targetLength == 0) { | |
4754 | // truncate(0) of a bogus string makes the string empty and non-bogus | |
4755 | unBogus(); | |
4756 | return FALSE; | |
46f4442e A |
4757 | } else if((uint32_t)targetLength < (uint32_t)length()) { |
4758 | setLength(targetLength); | |
b75a7d8f A |
4759 | return TRUE; |
4760 | } else { | |
4761 | return FALSE; | |
4762 | } | |
4763 | } | |
4764 | ||
374ca955 | 4765 | inline UnicodeString& |
b75a7d8f | 4766 | UnicodeString::reverse() |
46f4442e | 4767 | { return doReverse(0, length()); } |
b75a7d8f | 4768 | |
374ca955 | 4769 | inline UnicodeString& |
b75a7d8f A |
4770 | UnicodeString::reverse(int32_t start, |
4771 | int32_t _length) | |
4772 | { return doReverse(start, _length); } | |
4773 | ||
b75a7d8f | 4774 | U_NAMESPACE_END |
f3c0d7a5 | 4775 | #endif // U_SHOW_CPLUSPLUS_API |
b75a7d8f A |
4776 | |
4777 | #endif |