1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 **********************************************************************
5 * Copyright (C) 1998-2016, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
11 * Modification History:
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 ******************************************************************************
28 * \brief C++ API: Unicode String
32 #include "unicode/utypes.h"
33 #include "unicode/char16ptr.h"
34 #include "unicode/rep.h"
35 #include "unicode/std_string.h"
36 #include "unicode/stringpiece.h"
37 #include "unicode/bytestream.h"
39 struct UConverter
; // unicode/ucnv.h
43 * \ingroup ustring_ustrlen
45 U_STABLE
int32_t U_EXPORT2
46 u_strlen(const UChar
*s
);
49 #if U_SHOW_CPLUSPLUS_API
52 #if !UCONFIG_NO_BREAK_ITERATION
53 class BreakIterator
; // unicode/brkiter.h
58 #endif // U_SHOW_CPLUSPLUS_API
60 #if U_SHOW_CPLUSPLUS_API
61 // Not #ifndef U_HIDE_INTERNAL_API because UnicodeString needs the UStringCaseMapper.
63 * Internal string case mapping function type.
64 * All error checking must be done.
65 * src and dest must not overlap.
68 typedef int32_t U_CALLCONV
69 UStringCaseMapper(int32_t caseLocale
, uint32_t options
,
70 #if !UCONFIG_NO_BREAK_ITERATION
71 icu::BreakIterator
*iter
,
73 char16_t *dest
, int32_t destCapacity
,
74 const char16_t *src
, int32_t srcLength
,
76 UErrorCode
&errorCode
);
77 #endif // U_SHOW_CPLUSPLUS_API
79 #if U_SHOW_CPLUSPLUS_API
82 class Locale
; // unicode/locid.h
83 class StringCharacterIterator
;
84 class UnicodeStringAppendable
; // unicode/appendable.h
86 /* The <iostream> include has been moved to unicode/ustream.h */
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).
98 #define US_INV icu::UnicodeString::kInvariant
101 * Unicode String literals in C++.
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.
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.
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.
117 #if !U_CHAR16_IS_TYPEDEF
118 # define UNICODE_STRING(cs, _length) icu::UnicodeString(TRUE, u ## cs, _length)
120 # define UNICODE_STRING(cs, _length) icu::UnicodeString(TRUE, (const char16_t*)u ## cs, _length)
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
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.
133 * The string parameter must be a C string literal.
136 #define UNICODE_STRING_SIMPLE(cs) UNICODE_STRING(cs, -1)
139 * \def UNISTR_FROM_CHAR_EXPLICIT
140 * This can be defined to be empty or "explicit".
141 * If explicit, then the UnicodeString(char16_t) and UnicodeString(UChar32)
142 * constructors are marked as explicit, preventing their inadvertent use.
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
150 // Empty by default for source code compatibility.
151 # define UNISTR_FROM_CHAR_EXPLICIT
156 * \def UNISTR_FROM_STRING_EXPLICIT
157 * This can be defined to be empty or "explicit".
158 * If explicit, then the UnicodeString(const char *) and UnicodeString(const char16_t *)
159 * constructors are marked as explicit, preventing their inadvertent use.
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.
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
170 // Empty by default for source code compatibility.
171 # define UNISTR_FROM_STRING_EXPLICIT
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.
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.
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
192 * to hold at least 2 char16_ts, for one supplementary code point (U16_MAX_LENGTH).
194 * sizeof(UnicodeString) >= 48 should work for all known platforms.
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
199 * char16_ts stored inside the object.
201 * The minimum object size on a 64-bit machine would be
202 * 4 * sizeof(pointer) = 4 * 8 = 32 bytes,
203 * and the internal buffer would hold up to 11 char16_ts in that case.
205 * @see U16_MAX_LENGTH
208 #ifndef UNISTR_OBJECT_SIZE
209 # define UNISTR_OBJECT_SIZE 64
213 * UnicodeString is a string class that stores Unicode characters directly and provides
214 * similar functionality as the Java String and StringBuffer/StringBuilder classes.
215 * It is a concrete implementation of the abstract class Replaceable (for transliteration).
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.
227 * The UnicodeString class is not suitable for subclassing.
229 * <p>For an overview of Unicode strings in C and C++ see the
230 * <a href="http://userguide.icu-project.org/strings#TOC-Strings-in-C-C-">User Guide Strings chapter</a>.</p>
232 * <p>In ICU, a Unicode string consists of 16-bit Unicode <em>code units</em>.
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
235 * ("surrogates"). The data type for code units is char16_t.
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>
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>
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.
251 * - If primitive string pointer values (e.g., const char16_t * or char *)
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
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.
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.
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.
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.
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
285 * <a href="http://userguide.icu-project.org/strings#TOC-Maximizing-Performance-with-the-UnicodeString-Storage-Model">User Guide Strings chapter</a>.</p>
288 * @see CharacterIterator
291 class U_COMMON_API UnicodeString
: public Replaceable
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.
311 //========================================
312 // Read-only operations
313 //========================================
315 /* Comparison - bitwise only - for international comparison use collation */
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,
324 inline UBool
operator== (const UnicodeString
& text
) const;
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,
333 inline UBool
operator!= (const UnicodeString
& text
) const;
336 * Greater than operator. Performs only bitwise comparison.
337 * @param text The UnicodeString to compare to this one.
338 * @return TRUE if the characters in this are bitwise
339 * greater than the characters in <code>text</code>, FALSE otherwise
342 inline UBool
operator> (const UnicodeString
& text
) const;
345 * Less than operator. Performs only bitwise comparison.
346 * @param text The UnicodeString to compare to this one.
347 * @return TRUE if the characters in this are bitwise
348 * less than the characters in <code>text</code>, FALSE otherwise
351 inline UBool
operator< (const UnicodeString
& text
) const;
354 * Greater than or equal operator. Performs only bitwise comparison.
355 * @param text The UnicodeString to compare to this one.
356 * @return TRUE if the characters in this are bitwise
357 * greater than or equal to the characters in <code>text</code>, FALSE otherwise
360 inline UBool
operator>= (const UnicodeString
& text
) const;
363 * Less than or equal operator. Performs only bitwise comparison.
364 * @param text The UnicodeString to compare to this one.
365 * @return TRUE if the characters in this are bitwise
366 * less than or equal to the characters in <code>text</code>, FALSE otherwise
369 inline UBool
operator<= (const UnicodeString
& text
) const;
372 * Compare the characters bitwise in this UnicodeString to
373 * the characters in <code>text</code>.
374 * @param text The UnicodeString to compare to this one.
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>.
382 inline int8_t compare(const UnicodeString
& text
) const;
385 * Compare the characters bitwise in the range
386 * [<TT>start</TT>, <TT>start + length</TT>) with the characters
387 * in the <b>entire string</b> <TT>text</TT>.
388 * (The parameters "start" and "length" are not applied to the other text "text".)
389 * @param start the offset at which the compare operation begins
390 * @param length the number of characters of text to compare.
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>.
399 inline int8_t compare(int32_t start
,
401 const UnicodeString
& text
) const;
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>).
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
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>.
420 inline int8_t compare(int32_t start
,
422 const UnicodeString
& srcText
,
424 int32_t srcLength
) const;
427 * Compare the characters bitwise in this UnicodeString with the first
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
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>.
438 inline int8_t compare(ConstChar16Ptr srcChars
,
439 int32_t srcLength
) const;
442 * Compare the characters bitwise in the range
443 * [<TT>start</TT>, <TT>start + length</TT>) with the first
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
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>.
455 inline int8_t compare(int32_t start
,
457 const char16_t *srcChars
) const;
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>).
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
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>.
476 inline int8_t compare(int32_t start
,
478 const char16_t *srcChars
,
480 int32_t srcLength
) const;
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>).
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
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>.
499 inline int8_t compareBetween(int32_t start
,
501 const UnicodeString
& srcText
,
503 int32_t srcLimit
) const;
506 * Compare two Unicode strings in code point order.
507 * The result may be different from the results of compare(), operator<, etc.
508 * if supplementary characters are present:
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.
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
522 inline int8_t compareCodePointOrder(const UnicodeString
& text
) const;
525 * Compare two Unicode strings in code point order.
526 * The result may be different from the results of compare(), operator<, etc.
527 * if supplementary characters are present:
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.
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
543 inline int8_t compareCodePointOrder(int32_t start
,
545 const UnicodeString
& srcText
) const;
548 * Compare two Unicode strings in code point order.
549 * The result may be different from the results of compare(), operator<, etc.
550 * if supplementary characters are present:
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.
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
568 inline int8_t compareCodePointOrder(int32_t start
,
570 const UnicodeString
& srcText
,
572 int32_t srcLength
) const;
575 * Compare two Unicode strings in code point order.
576 * The result may be different from the results of compare(), operator<, etc.
577 * if supplementary characters are present:
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.
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
592 inline int8_t compareCodePointOrder(ConstChar16Ptr srcChars
,
593 int32_t srcLength
) const;
596 * Compare two Unicode strings in code point order.
597 * The result may be different from the results of compare(), operator<, etc.
598 * if supplementary characters are present:
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.
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
614 inline int8_t compareCodePointOrder(int32_t start
,
616 const char16_t *srcChars
) const;
619 * Compare two Unicode strings in code point order.
620 * The result may be different from the results of compare(), operator<, etc.
621 * if supplementary characters are present:
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.
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
639 inline int8_t compareCodePointOrder(int32_t start
,
641 const char16_t *srcChars
,
643 int32_t srcLength
) const;
646 * Compare two Unicode strings in code point order.
647 * The result may be different from the results of compare(), operator<, etc.
648 * if supplementary characters are present:
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.
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
666 inline int8_t compareCodePointOrderBetween(int32_t start
,
668 const UnicodeString
& srcText
,
670 int32_t srcLimit
) const;
673 * Compare two strings case-insensitively using full case folding.
674 * This is equivalent to this->foldCase(options).compare(text.foldCase(options)).
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.
681 * - U_COMPARE_CODE_POINT_ORDER
682 * Set to choose code point order instead of code unit order
683 * (see u_strCompare for details).
685 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
687 * @return A negative, zero, or positive integer indicating the comparison result.
690 inline int8_t caseCompare(const UnicodeString
& text
, uint32_t options
) const;
693 * Compare two strings case-insensitively using full case folding.
694 * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)).
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.
703 * - U_COMPARE_CODE_POINT_ORDER
704 * Set to choose code point order instead of code unit order
705 * (see u_strCompare for details).
707 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
709 * @return A negative, zero, or positive integer indicating the comparison result.
712 inline int8_t caseCompare(int32_t start
,
714 const UnicodeString
& srcText
,
715 uint32_t options
) const;
718 * Compare two strings case-insensitively using full case folding.
719 * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)).
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.
730 * - U_COMPARE_CODE_POINT_ORDER
731 * Set to choose code point order instead of code unit order
732 * (see u_strCompare for details).
734 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
736 * @return A negative, zero, or positive integer indicating the comparison result.
739 inline int8_t caseCompare(int32_t start
,
741 const UnicodeString
& srcText
,
744 uint32_t options
) const;
747 * Compare two strings case-insensitively using full case folding.
748 * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
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.
756 * - U_COMPARE_CODE_POINT_ORDER
757 * Set to choose code point order instead of code unit order
758 * (see u_strCompare for details).
760 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
762 * @return A negative, zero, or positive integer indicating the comparison result.
765 inline int8_t caseCompare(ConstChar16Ptr srcChars
,
767 uint32_t options
) const;
770 * Compare two strings case-insensitively using full case folding.
771 * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
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.
780 * - U_COMPARE_CODE_POINT_ORDER
781 * Set to choose code point order instead of code unit order
782 * (see u_strCompare for details).
784 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
786 * @return A negative, zero, or positive integer indicating the comparison result.
789 inline int8_t caseCompare(int32_t start
,
791 const char16_t *srcChars
,
792 uint32_t options
) const;
795 * Compare two strings case-insensitively using full case folding.
796 * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
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.
807 * - U_COMPARE_CODE_POINT_ORDER
808 * Set to choose code point order instead of code unit order
809 * (see u_strCompare for details).
811 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
813 * @return A negative, zero, or positive integer indicating the comparison result.
816 inline int8_t caseCompare(int32_t start
,
818 const char16_t *srcChars
,
821 uint32_t options
) const;
824 * Compare two strings case-insensitively using full case folding.
825 * This is equivalent to this->foldCase(options).compareBetween(text.foldCase(options)).
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.
836 * - U_COMPARE_CODE_POINT_ORDER
837 * Set to choose code point order instead of code unit order
838 * (see u_strCompare for details).
840 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
842 * @return A negative, zero, or positive integer indicating the comparison result.
845 inline int8_t caseCompareBetween(int32_t start
,
847 const UnicodeString
& srcText
,
850 uint32_t options
) const;
853 * Determine if this starts with the characters in <TT>text</TT>
854 * @param text The text to match.
855 * @return TRUE if this starts with the characters in <TT>text</TT>,
859 inline UBool
startsWith(const UnicodeString
& text
) const;
862 * Determine if this starts with the characters in <TT>srcText</TT>
863 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
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
867 * @return TRUE if this starts with the characters in <TT>text</TT>,
871 inline UBool
startsWith(const UnicodeString
& srcText
,
873 int32_t srcLength
) const;
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>
879 * @return TRUE if this starts with the characters in <TT>srcChars</TT>,
883 inline UBool
startsWith(ConstChar16Ptr srcChars
,
884 int32_t srcLength
) const;
887 * Determine if this ends with the characters in <TT>srcChars</TT>
888 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
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
895 inline UBool
startsWith(const char16_t *srcChars
,
897 int32_t srcLength
) const;
900 * Determine if this ends with the characters in <TT>text</TT>
901 * @param text The text to match.
902 * @return TRUE if this ends with the characters in <TT>text</TT>,
906 inline UBool
endsWith(const UnicodeString
& text
) const;
909 * Determine if this ends with the characters in <TT>srcText</TT>
910 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
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
914 * @return TRUE if this ends with the characters in <TT>text</TT>,
918 inline UBool
endsWith(const UnicodeString
& srcText
,
920 int32_t srcLength
) const;
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>
926 * @return TRUE if this ends with the characters in <TT>srcChars</TT>,
930 inline UBool
endsWith(ConstChar16Ptr srcChars
,
931 int32_t srcLength
) const;
934 * Determine if this ends with the characters in <TT>srcChars</TT>
935 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
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
939 * @return TRUE if this ends with the characters in <TT>srcChars</TT>,
943 inline UBool
endsWith(const char16_t *srcChars
,
945 int32_t srcLength
) const;
948 /* Searching - bitwise only */
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.
954 * @return The offset into this of the start of <TT>text</TT>,
955 * or -1 if not found.
958 inline int32_t indexOf(const UnicodeString
& text
) const;
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.
965 * @return The offset into this of the start of <TT>text</TT>,
966 * or -1 if not found.
969 inline int32_t indexOf(const UnicodeString
& text
,
970 int32_t start
) const;
973 * Locate in this the first occurrence in the range
974 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
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
979 * @return The offset into this of the start of <TT>text</TT>,
980 * or -1 if not found.
983 inline int32_t indexOf(const UnicodeString
& text
,
985 int32_t length
) const;
988 * Locate in this the first occurrence in the range
989 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
990 * in <TT>srcText</TT> in the range
991 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>),
992 * using bitwise comparison.
993 * @param srcText The text to search for.
994 * @param srcStart the offset into <TT>srcText</TT> at which
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
999 * @return The offset into this of the start of <TT>text</TT>,
1000 * or -1 if not found.
1003 inline int32_t indexOf(const UnicodeString
& srcText
,
1007 int32_t length
) const;
1010 * Locate in this the first occurrence of the characters in
1012 * starting at offset <TT>start</TT>, using bitwise comparison.
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
1016 * @return The offset into this of the start of <TT>text</TT>,
1017 * or -1 if not found.
1020 inline int32_t indexOf(const char16_t *srcChars
,
1022 int32_t start
) const;
1025 * Locate in this the first occurrence in the range
1026 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
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
1032 * @return The offset into this of the start of <TT>srcChars</TT>,
1033 * or -1 if not found.
1036 inline int32_t indexOf(ConstChar16Ptr srcChars
,
1039 int32_t length
) const;
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.
1047 * @param srcChars The text to search for.
1048 * @param srcStart the offset into <TT>srcChars</TT> at which
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
1053 * @return The offset into this of the start of <TT>text</TT>,
1054 * or -1 if not found.
1057 int32_t indexOf(const char16_t *srcChars
,
1061 int32_t length
) const;
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.
1070 inline int32_t indexOf(char16_t c
) const;
1073 * Locate in this the first occurrence of the code point <TT>c</TT>,
1074 * using bitwise comparison.
1076 * @param c The code point to search for.
1077 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1080 inline int32_t indexOf(UChar32 c
) const;
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.
1090 inline int32_t indexOf(char16_t c
,
1091 int32_t start
) const;
1094 * Locate in this the first occurrence of the code point <TT>c</TT>
1095 * starting at offset <TT>start</TT>, using bitwise comparison.
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.
1102 inline int32_t indexOf(UChar32 c
,
1103 int32_t start
) const;
1106 * Locate in this the first occurrence of the BMP code point <code>c</code>
1107 * in the range [<TT>start</TT>, <TT>start + length</TT>),
1108 * using bitwise comparison.
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.
1115 inline int32_t indexOf(char16_t c
,
1117 int32_t length
) const;
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.
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.
1130 inline int32_t indexOf(UChar32 c
,
1132 int32_t length
) const;
1135 * Locate in this the last occurrence of the characters in <TT>text</TT>,
1136 * using bitwise comparison.
1137 * @param text The text to search for.
1138 * @return The offset into this of the start of <TT>text</TT>,
1139 * or -1 if not found.
1142 inline int32_t lastIndexOf(const UnicodeString
& text
) const;
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.
1149 * @return The offset into this of the start of <TT>text</TT>,
1150 * or -1 if not found.
1153 inline int32_t lastIndexOf(const UnicodeString
& text
,
1154 int32_t start
) const;
1157 * Locate in this the last occurrence in the range
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
1163 * @return The offset into this of the start of <TT>text</TT>,
1164 * or -1 if not found.
1167 inline int32_t lastIndexOf(const UnicodeString
& text
,
1169 int32_t length
) const;
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.
1177 * @param srcText The text to search for.
1178 * @param srcStart the offset into <TT>srcText</TT> at which
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
1183 * @return The offset into this of the start of <TT>text</TT>,
1184 * or -1 if not found.
1187 inline int32_t lastIndexOf(const UnicodeString
& srcText
,
1191 int32_t length
) const;
1194 * Locate in this the last occurrence of the characters in <TT>srcChars</TT>
1195 * starting at offset <TT>start</TT>, using bitwise comparison.
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
1199 * @return The offset into this of the start of <TT>text</TT>,
1200 * or -1 if not found.
1203 inline int32_t lastIndexOf(const char16_t *srcChars
,
1205 int32_t start
) const;
1208 * Locate in this the last occurrence in the range
1209 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
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
1215 * @return The offset into this of the start of <TT>srcChars</TT>,
1216 * or -1 if not found.
1219 inline int32_t lastIndexOf(ConstChar16Ptr srcChars
,
1222 int32_t length
) const;
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.
1230 * @param srcChars The text to search for.
1231 * @param srcStart the offset into <TT>srcChars</TT> at which
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
1236 * @return The offset into this of the start of <TT>text</TT>,
1237 * or -1 if not found.
1240 int32_t lastIndexOf(const char16_t *srcChars
,
1244 int32_t length
) const;
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.
1253 inline int32_t lastIndexOf(char16_t c
) const;
1256 * Locate in this the last occurrence of the code point <TT>c</TT>,
1257 * using bitwise comparison.
1259 * @param c The code point to search for.
1260 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1263 inline int32_t lastIndexOf(UChar32 c
) const;
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.
1273 inline int32_t lastIndexOf(char16_t c
,
1274 int32_t start
) const;
1277 * Locate in this the last occurrence of the code point <TT>c</TT>
1278 * starting at offset <TT>start</TT>, using bitwise comparison.
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.
1285 inline int32_t lastIndexOf(UChar32 c
,
1286 int32_t start
) const;
1289 * Locate in this the last occurrence of the BMP code point <code>c</code>
1290 * in the range [<TT>start</TT>, <TT>start + length</TT>),
1291 * using bitwise comparison.
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.
1298 inline int32_t lastIndexOf(char16_t c
,
1300 int32_t length
) const;
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.
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.
1313 inline int32_t lastIndexOf(UChar32 c
,
1315 int32_t length
) const;
1318 /* Character access */
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
1328 inline char16_t charAt(int32_t offset
) const;
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>
1337 inline char16_t operator[] (int32_t offset
) const;
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
1350 UChar32
char32At(int32_t offset
) const;
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
1367 int32_t getChar32Start(int32_t offset
) const;
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
1385 int32_t getChar32Limit(int32_t offset
) const;
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.
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).
1400 * This behaves like CharacterIterator::move32(delta, kCurrent).
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.
1413 * // s has code points 'a' U+10000 'b' U+10ffff U+2029
1414 * UnicodeString s=UNICODE_STRING("a\\U00010000b\\U0010ffff\\u2029", 31).unescape();
1416 * // initial index: position of U+10000
1419 * // the following examples will all result in index==4, position of U+10ffff
1421 * // skip 2 code points from some position in the string
1422 * index=s.moveIndex32(index, 2); // skips U+10000 and 'b'
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'
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
1431 * @param index input code unit index
1432 * @param delta (signed) code point count to move the index forward or backward
1434 * @return the resulting code unit index
1437 int32_t moveIndex32(int32_t index
, int32_t delta
) const;
1439 /* Substring extraction */
1442 * Copy the characters in the range
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.
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
1456 inline void extract(int32_t start
,
1459 int32_t dstStart
= 0) const;
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.
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.
1473 * If the string aliases to <code>dest</code> itself as an external buffer,
1474 * then extract() will not copy the contents.
1476 * @param dest Destination string buffer.
1477 * @param destCapacity Number of char16_ts available at dest.
1478 * @param errorCode ICU error code.
1483 extract(Char16Ptr dest
, int32_t destCapacity
,
1484 UErrorCode
&errorCode
) const;
1487 * Copy the characters in the range
1488 * [<tt>start</tt>, <tt>start + length</tt>) into the UnicodeString
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>
1496 inline void extract(int32_t start
,
1498 UnicodeString
& target
) const;
1501 * Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>)
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
1505 * @param dst array in which to copy characters. The length of <tt>dst</tt>
1506 * must be at least (<tt>dstStart + (limit - start)</tt>).
1507 * @param dstStart the offset in <TT>dst</TT> where the first character
1511 inline void extractBetween(int32_t start
,
1514 int32_t dstStart
= 0) const;
1517 * Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>)
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>
1525 virtual void extractBetween(int32_t start
,
1527 UnicodeString
& target
) const;
1530 * Copy the characters in the range
1531 * [<tt>start</TT>, <tt>start + startLength</TT>) into an array of characters.
1532 * All characters must be invariant (see utypes.h).
1533 * Use US_INV as the last, signature-distinguishing parameter.
1535 * This function does not write any more than <code>targetCapacity</code>
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
1539 * The output string is NUL-terminated if possible.
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
1550 int32_t extract(int32_t start
,
1551 int32_t startLength
,
1553 int32_t targetCapacity
,
1554 enum EInvariant inv
) const;
1556 #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
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
1566 * The output string is NUL-terminated if possible.
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
1577 int32_t extract(int32_t start
,
1578 int32_t startLength
,
1580 uint32_t targetLength
) const;
1584 #if !UCONFIG_NO_CONVERSION
1587 * Copy the characters in the range
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.
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.
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
1600 * @param codepage the desired codepage for the characters. 0 has
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
1611 inline int32_t extract(int32_t start
,
1612 int32_t startLength
,
1614 const char *codepage
= 0) const;
1617 * Copy the characters in the range
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
1624 * The output string is NUL-terminated if possible.
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.
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
1635 * @param codepage the desired codepage for the characters. 0 has
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
1645 int32_t extract(int32_t start
,
1646 int32_t startLength
,
1648 uint32_t targetLength
,
1649 const char *codepage
) const;
1652 * Convert the UnicodeString into a codepage string using an existing UConverter.
1653 * The output string is NUL-terminated if possible.
1655 * This function avoids the overhead of opening and closing a converter if
1656 * multiple strings are extracted.
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
1668 int32_t extract(char *dest
, int32_t destCapacity
,
1670 UErrorCode
&errorCode
) const;
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
1687 UnicodeString
tempSubString(int32_t start
=0, int32_t length
=INT32_MAX
) const;
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
1699 inline UnicodeString
tempSubStringBetween(int32_t start
, int32_t limit
=INT32_MAX
) const;
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().
1707 * @param sink A ByteSink to which the UTF-8 version of the string is written.
1708 * sink.Flush() is called at the end.
1712 void toUTF8(ByteSink
&sink
) const;
1715 * Convert the UnicodeString to UTF-8 and append the result
1716 * to a standard string.
1717 * Unpaired surrogates are replaced with U+FFFD.
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.
1726 template<typename StringClass
>
1727 StringClass
&toUTF8String(StringClass
&result
) const {
1728 StringByteSink
<StringClass
> sbs(&result
, length());
1734 * Convert the UnicodeString to UTF-32.
1735 * Unpaired surrogates are replaced with U+FFFD.
1736 * Calls u_strToUTF32WithSub().
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.
1748 int32_t toUTF32(UChar32
*utf32
, int32_t capacity
, UErrorCode
&errorCode
) const;
1750 /* Length operations */
1753 * Return the length of the UnicodeString object.
1754 * The length is the number of char16_t code units are in the UnicodeString.
1755 * If you want the number of code points, please use countChar32().
1756 * @return the length of the UnicodeString object
1760 inline int32_t length(void) const;
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.
1765 * Counting code points involves reading all code units.
1767 * This functions is basically the inverse of moveIndex32().
1769 * @param start the index of the first code unit to check
1770 * @param length the number of char16_t code units to check
1771 * @return the number of code points in the specified code units
1776 countChar32(int32_t start
=0, int32_t length
=INT32_MAX
) const;
1779 * Check if the length char16_t code units of the string
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).
1787 * A Unicode code point may occupy either one or two char16_t code units.
1789 * @param start the index of the first code unit to check (0 for the entire string)
1790 * @param length the number of char16_t code units to check
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).
1798 * @see u_strHasMoreChar32Than
1802 hasMoreChar32Than(int32_t start
, int32_t length
, int32_t number
) const;
1805 * Determine if this string is empty.
1806 * @return TRUE if this string contains 0 characters, FALSE otherwise.
1809 inline UBool
isEmpty(void) const;
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.
1816 * @return the number of char16_ts available in the internal buffer
1820 inline int32_t getCapacity(void) const;
1822 /* Other operations */
1825 * Generate a hash code for this object.
1826 * @return The hash code of this UnicodeString.
1829 inline int32_t hashCode(void) const;
1832 * Determine if this object contains a valid string.
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
1837 * length() returns 0.
1839 * @return TRUE if the string is bogus/invalid, FALSE otherwise
1843 inline UBool
isBogus(void) const;
1846 //========================================
1848 //========================================
1850 /* Assignment operations */
1853 * Assignment operator. Replace the characters in this UnicodeString
1854 * with the characters from <TT>srcText</TT>.
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.
1862 * If the source object has an "open" buffer from getBuffer(minCapacity),
1863 * then the copy is an empty string.
1865 * @param srcText The text containing the characters to replace
1866 * @return a reference to this
1870 UnicodeString
&operator=(const UnicodeString
&srcText
);
1873 * Almost the same as the assignment operator.
1874 * Replace the characters in this UnicodeString
1875 * with the characters from <code>srcText</code>.
1877 * This function works the same as the assignment operator
1878 * for all strings except for ones that are readonly aliases.
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.
1885 * The fastCopyFrom function must be used only if it is known that the lifetime of
1886 * this UnicodeString does not exceed the lifetime of the aliased buffer
1887 * including its contents, for example for strings from resource bundles
1888 * or aliases to string constants.
1890 * If the source object has an "open" buffer from getBuffer(minCapacity),
1891 * then the copy is an empty string.
1893 * @param src The text containing the characters to replace.
1894 * @return a reference to this
1897 UnicodeString
&fastCopyFrom(const UnicodeString
&src
);
1900 * Move assignment operator; might leave src in bogus state.
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
1907 UnicodeString
&operator=(UnicodeString
&&src
) U_NOEXCEPT
{
1908 return moveFrom(src
);
1911 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
1913 * Move assignment; might leave src in bogus state.
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.
1917 * Can be called explicitly, does not need C++11 support.
1918 * @param src source string
1922 UnicodeString
&moveFrom(UnicodeString
&src
) U_NOEXCEPT
;
1926 * @param other other string
1929 void swap(UnicodeString
&other
) U_NOEXCEPT
;
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
1937 friend U_COMMON_API
inline void U_EXPORT2
1938 swap(UnicodeString
&s1
, UnicodeString
&s2
) U_NOEXCEPT
{
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
1949 inline UnicodeString
& operator= (char16_t ch
);
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
1958 inline UnicodeString
& operator= (UChar32 ch
);
1961 * Set the text in the UnicodeString object to the characters
1962 * in <TT>srcText</TT> in the range
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
1968 * @return a reference to this
1971 inline UnicodeString
& setTo(const UnicodeString
& srcText
,
1975 * Set the text in the UnicodeString object to the characters
1976 * in <TT>srcText</TT> in the range
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
1982 * @param srcLength the number of characters in <TT>srcText</TT> in the
1984 * @return a reference to this
1987 inline UnicodeString
& setTo(const UnicodeString
& srcText
,
1992 * Set the text in the UnicodeString object to the characters in
1994 * <TT>srcText</TT> is not modified.
1995 * @param srcText the source for the new characters
1996 * @return a reference to this
1999 inline UnicodeString
& setTo(const UnicodeString
& srcText
);
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
2009 inline UnicodeString
& setTo(const char16_t *srcChars
,
2013 * Set the characters in the UnicodeString object to the code unit
2015 * @param srcChar the code unit which becomes the UnicodeString's character
2017 * @return a reference to this
2020 UnicodeString
& setTo(char16_t srcChar
);
2023 * Set the characters in the UnicodeString object to the code point
2025 * @param srcChar the code point which becomes the UnicodeString's character
2027 * @return a reference to this
2030 UnicodeString
& setTo(UChar32 srcChar
);
2033 * Aliasing setTo() function, analogous to the readonly-aliasing char16_t* constructor.
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.
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,
2044 * so that both strings then alias the same readonly-text.
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
2055 UnicodeString
&setTo(UBool isTerminated
,
2056 ConstChar16Ptr text
,
2057 int32_t textLength
);
2060 * Aliasing setTo() function, analogous to the writable-aliasing char16_t* constructor.
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.
2069 * The extract(Char16Ptr dst) function detects whether the dst pointer is the same
2070 * as the string buffer itself and will in this case not copy the contents.
2072 * @param buffer The characters to alias for the UnicodeString.
2073 * @param buffLength The number of Unicode characters in <code>buffer</code> to alias.
2074 * @param buffCapacity The size of <code>buffer</code> in char16_ts.
2075 * @return a reference to this
2078 UnicodeString
&setTo(char16_t *buffer
,
2080 int32_t buffCapacity
);
2083 * Make this UnicodeString object invalid.
2084 * The string will test TRUE with isBogus().
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.
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.
2097 * The following methods, and no others, will clear a string object's bogus flag:
2099 * - remove(0, INT32_MAX)
2101 * - operator=() (assignment operator)
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":
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
2114 * static const char16_t nul=0;
2115 * s.setTo(&nul, 0); // set to an empty C Unicode string
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
2131 UnicodeString
& setCharAt(int32_t offset
,
2135 /* Append operations */
2138 * Append operator. Append the code unit <TT>ch</TT> to the UnicodeString
2140 * @param ch the code unit to be appended
2141 * @return a reference to this
2144 inline UnicodeString
& operator+= (char16_t ch
);
2147 * Append operator. Append the code point <TT>ch</TT> to the UnicodeString
2149 * @param ch the code point to be appended
2150 * @return a reference to this
2153 inline UnicodeString
& operator+= (UChar32 ch
);
2156 * Append operator. Append the characters in <TT>srcText</TT> to the
2157 * UnicodeString object. <TT>srcText</TT> is not modified.
2158 * @param srcText the source for the new characters
2159 * @return a reference to this
2162 inline UnicodeString
& operator+= (const UnicodeString
& srcText
);
2165 * Append the characters
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>
2170 * @param srcText the source for the new characters
2171 * @param srcStart the offset into <TT>srcText</TT> where new characters
2173 * @param srcLength the number of characters in <TT>srcText</TT> in
2175 * @return a reference to this
2178 inline UnicodeString
& append(const UnicodeString
& srcText
,
2183 * Append the characters in <TT>srcText</TT> to the UnicodeString object.
2184 * <TT>srcText</TT> is not modified.
2185 * @param srcText the source for the new characters
2186 * @return a reference to this
2189 inline UnicodeString
& append(const UnicodeString
& srcText
);
2192 * Append the characters in <TT>srcChars</TT> in the range
2193 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) to the UnicodeString
2195 * <TT>start</TT>. <TT>srcChars</TT> is not modified.
2196 * @param srcChars the source for the new characters
2197 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2199 * @param srcLength the number of characters in <TT>srcChars</TT> in
2200 * the append string; can be -1 if <TT>srcChars</TT> is NUL-terminated
2201 * @return a reference to this
2204 inline UnicodeString
& append(const char16_t *srcChars
,
2209 * Append the characters in <TT>srcChars</TT> to the UnicodeString object
2210 * at offset <TT>start</TT>. <TT>srcChars</TT> is not modified.
2211 * @param srcChars the source for the new characters
2212 * @param srcLength the number of Unicode characters in <TT>srcChars</TT>;
2213 * can be -1 if <TT>srcChars</TT> is NUL-terminated
2214 * @return a reference to this
2217 inline UnicodeString
& append(ConstChar16Ptr srcChars
,
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
2226 inline UnicodeString
& append(char16_t srcChar
);
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
2234 UnicodeString
& append(UChar32 srcChar
);
2237 /* Insert operations */
2240 * Insert the characters in <TT>srcText</TT> in the range
2241 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) into the UnicodeString
2242 * object at offset <TT>start</TT>. <TT>srcText</TT> is not modified.
2243 * @param start the offset where the insertion begins
2244 * @param srcText the source for the new characters
2245 * @param srcStart the offset into <TT>srcText</TT> where new characters
2247 * @param srcLength the number of characters in <TT>srcText</TT> in
2249 * @return a reference to this
2252 inline UnicodeString
& insert(int32_t start
,
2253 const UnicodeString
& srcText
,
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
2261 * @param srcText the source for the new characters
2262 * @return a reference to this
2265 inline UnicodeString
& insert(int32_t start
,
2266 const UnicodeString
& srcText
);
2269 * Insert the characters in <TT>srcChars</TT> in the range
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
2274 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2276 * @param srcLength the number of characters in <TT>srcChars</TT>
2277 * in the insert string
2278 * @return a reference to this
2281 inline UnicodeString
& insert(int32_t start
,
2282 const char16_t *srcChars
,
2287 * Insert the characters in <TT>srcChars</TT> into the UnicodeString object
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
2295 inline UnicodeString
& insert(int32_t start
,
2296 ConstChar16Ptr srcChars
,
2300 * Insert the code unit <TT>srcChar</TT> into the UnicodeString object at
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
2307 inline UnicodeString
& insert(int32_t start
,
2311 * Insert the code point <TT>srcChar</TT> into the UnicodeString object at
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
2318 inline UnicodeString
& insert(int32_t start
,
2322 /* Replace operations */
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>).
2329 * <TT>srcText</TT> is not modified.
2330 * @param start the offset at which the replace operation begins
2331 * @param length the number of characters to replace. The character at
2332 * <TT>start + length</TT> is not modified.
2333 * @param srcText the source for the new characters
2334 * @param srcStart the offset into <TT>srcText</TT> where new characters
2336 * @param srcLength the number of characters in <TT>srcText</TT> in
2337 * the replace string
2338 * @return a reference to this
2341 UnicodeString
& replace(int32_t start
,
2343 const UnicodeString
& srcText
,
2348 * Replace the characters in the range
2349 * [<TT>start</TT>, <TT>start + length</TT>)
2350 * with the characters in <TT>srcText</TT>. <TT>srcText</TT> is
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
2359 UnicodeString
& replace(int32_t start
,
2361 const UnicodeString
& srcText
);
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>
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
2375 * @param srcLength the number of characters in <TT>srcChars</TT>
2376 * in the replace string
2377 * @return a reference to this
2380 UnicodeString
& replace(int32_t start
,
2382 const char16_t *srcChars
,
2387 * Replace the characters in the range
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
2398 inline UnicodeString
& replace(int32_t start
,
2400 ConstChar16Ptr srcChars
,
2404 * Replace the characters in the range
2405 * [<TT>start</TT>, <TT>start + length</TT>) with the code unit
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
2414 inline UnicodeString
& replace(int32_t start
,
2419 * Replace the characters in the range
2420 * [<TT>start</TT>, <TT>start + length</TT>) with the code point
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
2429 UnicodeString
& replace(int32_t start
, int32_t length
, UChar32 srcChar
);
2432 * Replace the characters in the range [<TT>start</TT>, <TT>limit</TT>)
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
2440 inline UnicodeString
& replaceBetween(int32_t start
,
2442 const UnicodeString
& srcText
);
2445 * Replace the characters in the range [<TT>start</TT>, <TT>limit</TT>)
2446 * with the characters in <TT>srcText</TT> in the range
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
2451 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2453 * @param srcLimit the offset immediately following the range to copy
2454 * in <TT>srcText</TT>
2455 * @return a reference to this
2458 inline UnicodeString
& replaceBetween(int32_t start
,
2460 const UnicodeString
& srcText
,
2465 * Replace a substring of this object with the given text.
2466 * @param start the beginning index, inclusive; <code>0 <= start
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>
2474 virtual void handleReplaceBetween(int32_t start
,
2476 const UnicodeString
& text
);
2480 * @return TRUE if it has MetaData
2483 virtual UBool
hasMetaData() const;
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.
2490 * @param start the beginning index, inclusive; <code>0 <= start <=
2492 * @param limit the ending index, exclusive; <code>start <= limit <=
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>.
2500 virtual void copy(int32_t start
, int32_t limit
, int32_t dest
);
2502 /* Search and replace operations */
2505 * Replace all occurrences of characters in oldText with the characters
2507 * @param oldText the text containing the search text
2508 * @param newText the text containing the replacement text
2509 * @return a reference to this
2512 inline UnicodeString
& findAndReplace(const UnicodeString
& oldText
,
2513 const UnicodeString
& newText
);
2516 * Replace all occurrences of characters in oldText with characters
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
2526 inline UnicodeString
& findAndReplace(int32_t start
,
2528 const UnicodeString
& oldText
,
2529 const UnicodeString
& newText
);
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>)
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
2548 UnicodeString
& findAndReplace(int32_t start
,
2550 const UnicodeString
& oldText
,
2553 const UnicodeString
& newText
,
2558 /* Remove operations */
2561 * Remove all characters from the UnicodeString object.
2562 * @return a reference to this
2565 inline UnicodeString
& remove(void);
2568 * Remove the characters in the range
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
2575 inline UnicodeString
& remove(int32_t start
,
2576 int32_t length
= (int32_t)INT32_MAX
);
2579 * Remove the characters in the range
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
2586 inline UnicodeString
& removeBetween(int32_t start
,
2587 int32_t limit
= (int32_t)INT32_MAX
);
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
2598 inline UnicodeString
&retainBetween(int32_t start
, int32_t limit
= INT32_MAX
);
2600 /* Length operations */
2603 * Pad the start of this UnicodeString with the character <TT>padChar</TT>.
2604 * If the length of this UnicodeString is less than targetLength,
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
2608 * @param padChar the character to use for padding. Defaults to
2610 * @return TRUE if the text was padded, FALSE otherwise.
2613 UBool
padLeading(int32_t targetLength
,
2614 char16_t padChar
= 0x0020);
2617 * Pad the end of this UnicodeString with the character <TT>padChar</TT>.
2618 * If the length of this UnicodeString is less than targetLength,
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
2622 * @param padChar the character to use for padding. Defaults to
2624 * @return TRUE if the text was padded, FALSE otherwise.
2627 UBool
padTrailing(int32_t targetLength
,
2628 char16_t padChar
= 0x0020);
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
2636 inline UBool
truncate(int32_t targetLength
);
2639 * Trims leading and trailing whitespace from this UnicodeString.
2640 * @return a reference to this
2643 UnicodeString
& trim(void);
2646 /* Miscellaneous operations */
2649 * Reverse this UnicodeString in place.
2650 * @return a reference to this
2653 inline UnicodeString
& reverse(void);
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
2663 inline UnicodeString
& reverse(int32_t start
,
2667 * Convert the characters in this to UPPER CASE following the conventions of
2668 * the default locale.
2669 * @return A reference to this.
2672 UnicodeString
& toUpper(void);
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.
2681 UnicodeString
& toUpper(const Locale
& locale
);
2684 * Convert the characters in this to lower case following the conventions of
2685 * the default locale.
2686 * @return A reference to this.
2689 UnicodeString
& toLower(void);
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.
2698 UnicodeString
& toLower(const Locale
& locale
);
2700 #if !UCONFIG_NO_BREAK_ITERATION
2703 * Titlecase this string, convenience function using the default locale.
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
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.
2717 * This function uses only the setText(), first() and next() methods of the
2718 * provided break iterator.
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.
2724 * Otherwise the provided iterator is set to the string's text.
2725 * @return A reference to this.
2728 UnicodeString
&toTitle(BreakIterator
*titleIter
);
2731 * Titlecase this string.
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
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.
2745 * This function uses only the setText(), first() and next() methods of the
2746 * provided break iterator.
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.
2752 * Otherwise the provided iterator is set to the string's text.
2753 * @param locale The locale to consider.
2754 * @return A reference to this.
2757 UnicodeString
&toTitle(BreakIterator
*titleIter
, const Locale
&locale
);
2760 * Titlecase this string, with options.
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.)
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.
2774 * This function uses only the setText(), first() and next() methods of the
2775 * provided break iterator.
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.
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.
2786 * @param options Options bit set, see ucasemap_open().
2787 * @return A reference to this.
2790 UnicodeString
&toTitle(BreakIterator
*titleIter
, const Locale
&locale
, uint32_t options
);
2795 * Case-folds the characters in this string.
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
2799 * and dotless i that are marked with 'T' in CaseFolding.txt.
2801 * The result may be longer or shorter than the original.
2803 * @param options Either U_FOLD_CASE_DEFAULT or U_FOLD_CASE_EXCLUDE_SPECIAL_I
2804 * @return A reference to this.
2807 UnicodeString
&foldCase(uint32_t options
=0 /*U_FOLD_CASE_DEFAULT*/);
2809 //========================================
2810 // Access to the internal buffer
2811 //========================================
2814 * Get a read/write pointer to the internal buffer.
2815 * The buffer is guaranteed to be large enough for at least minCapacity char16_ts,
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.
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.
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.
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
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.
2846 * @param minCapacity the minimum number of char16_ts that are to be available
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,
2850 * or nullptr if an error occurs (nested calls, out of memory)
2852 * @see releaseBuffer
2853 * @see getTerminatedBuffer()
2856 char16_t *getBuffer(int32_t minCapacity
);
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".
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.
2868 * After calling releaseBuffer(newLength) the UnicodeString is back to normal operation.
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
2875 * @see getBuffer(int32_t minCapacity)
2878 void releaseBuffer(int32_t newLength
=-1);
2881 * Get a read-only pointer to the internal buffer.
2882 * This can be called at any time on a valid UnicodeString.
2884 * It returns 0 if the string is bogus, or
2885 * during an "open" getBuffer(minCapacity).
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.
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.
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().)
2900 * The buffer may reside in read-only memory. Its contents must not
2903 * @return a read-only pointer to the internal string buffer,
2904 * or nullptr if the string is empty or bogus
2906 * @see getBuffer(int32_t minCapacity)
2907 * @see getTerminatedBuffer()
2910 inline const char16_t *getBuffer() const;
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.
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).
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.
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.
2929 * The buffer contents is guaranteed to be NUL-terminated.
2930 * getTerminatedBuffer() may reallocate the buffer if a terminating NUL
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.
2935 * The buffer may reside in read-only memory. Its contents must not
2938 * @return a read-only pointer to the internal string buffer,
2939 * or 0 if the string is empty or bogus
2941 * @see getBuffer(int32_t minCapacity)
2945 const char16_t *getTerminatedBuffer();
2947 //========================================
2949 //========================================
2951 /** Construct an empty UnicodeString.
2954 inline UnicodeString();
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
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
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
2967 UnicodeString(int32_t capacity
, UChar32 c
, int32_t count
);
2970 * Single char16_t (code unit) constructor.
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.
2975 * @param ch the character to place in the UnicodeString
2978 UNISTR_FROM_CHAR_EXPLICIT
UnicodeString(char16_t ch
);
2981 * Single UChar32 (code point) constructor.
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.
2986 * @param ch the character to place in the UnicodeString
2989 UNISTR_FROM_CHAR_EXPLICIT
UnicodeString(UChar32 ch
);
2992 * char16_t* constructor.
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.
2997 * @param text The characters to place in the UnicodeString. <TT>text</TT>
2998 * must be NULL (U+0000) terminated.
3001 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const char16_t *text
);
3003 #if !U_CHAR16_IS_TYPEDEF
3005 * uint16_t * constructor.
3006 * Delegates to UnicodeString(const char16_t *).
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
3014 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const uint16_t *text
) :
3015 UnicodeString(ConstChar16Ptr(text
)) {}
3018 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3020 * wchar_t * constructor.
3021 * (Only defined if U_SIZEOF_WCHAR_T==2.)
3022 * Delegates to UnicodeString(const char16_t *).
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
3030 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const wchar_t *text
) :
3031 UnicodeString(ConstChar16Ptr(text
)) {}
3035 * nullptr_t constructor.
3036 * Effectively the same as the default constructor, makes an empty string object.
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
3044 UNISTR_FROM_STRING_EXPLICIT
inline UnicodeString(const std::nullptr_t text
);
3047 * char16_t* constructor.
3048 * @param text The characters to place in the UnicodeString.
3049 * @param textLength The number of Unicode characters in <TT>text</TT>
3053 UnicodeString(const char16_t *text
,
3054 int32_t textLength
);
3056 #if !U_CHAR16_IS_TYPEDEF
3058 * uint16_t * constructor.
3059 * Delegates to UnicodeString(const char16_t *, int32_t).
3060 * @param text UTF-16 string
3061 * @param length string length
3064 UnicodeString(const uint16_t *text
, int32_t length
) :
3065 UnicodeString(ConstChar16Ptr(text
), length
) {}
3068 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
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
3077 UnicodeString(const wchar_t *text
, int32_t length
) :
3078 UnicodeString(ConstChar16Ptr(text
), length
) {}
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
3088 inline UnicodeString(const std::nullptr_t text
, int32_t length
);
3091 * Readonly-aliasing char16_t* constructor.
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.
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,
3102 * so that both strings then alias the same readonly-text.
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>.
3112 UnicodeString(UBool isTerminated
,
3113 ConstChar16Ptr text
,
3114 int32_t textLength
);
3117 * Writable-aliasing char16_t* constructor.
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.
3126 * The extract(Char16Ptr dst) function detects whether the dst pointer is the same
3127 * as the string buffer itself and will in this case not copy the contents.
3129 * @param buffer The characters to alias for the UnicodeString.
3130 * @param buffLength The number of Unicode characters in <code>buffer</code> to alias.
3131 * @param buffCapacity The size of <code>buffer</code> in char16_ts.
3134 UnicodeString(char16_t *buffer
, int32_t buffLength
, int32_t buffCapacity
);
3136 #if !U_CHAR16_IS_TYPEDEF
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
3145 UnicodeString(uint16_t *buffer
, int32_t buffLength
, int32_t buffCapacity
) :
3146 UnicodeString(Char16Ptr(buffer
), buffLength
, buffCapacity
) {}
3149 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
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
3159 UnicodeString(wchar_t *buffer
, int32_t buffLength
, int32_t buffCapacity
) :
3160 UnicodeString(Char16Ptr(buffer
), buffLength
, buffCapacity
) {}
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
3171 inline UnicodeString(std::nullptr_t buffer
, int32_t buffLength
, int32_t buffCapacity
);
3173 #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
3176 * char* constructor.
3177 * Uses the default converter (and thus depends on the ICU conversion code)
3178 * unless U_CHARSET_IS_UTF8 is set to 1.
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.
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.
3188 * @param codepageData an array of bytes, null-terminated,
3189 * in the platform's default codepage.
3191 * @see UNICODE_STRING
3192 * @see UNICODE_STRING_SIMPLE
3194 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const char *codepageData
);
3197 * char* constructor.
3198 * Uses the default converter (and thus depends on the ICU conversion code)
3199 * unless U_CHARSET_IS_UTF8 is set to 1.
3200 * @param codepageData an array of bytes in the platform's default codepage.
3201 * @param dataLength The number of bytes in <TT>codepageData</TT>.
3204 UnicodeString(const char *codepageData
, int32_t dataLength
);
3208 #if !UCONFIG_NO_CONVERSION
3211 * char* constructor.
3212 * @param codepageData an array of bytes, null-terminated
3213 * @param codepage the encoding of <TT>codepageData</TT>. The special
3214 * value 0 for <TT>codepage</TT> indicates that the text is in the
3215 * platform's default codepage.
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.
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.
3227 UnicodeString(const char *codepageData
, const char *codepage
);
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
3234 * value 0 for <TT>codepage</TT> indicates that the text is in the
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.
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.
3246 UnicodeString(const char *codepageData
, int32_t dataLength
, const char *codepage
);
3249 * char * / UConverter constructor.
3250 * This constructor uses an existing UConverter object to
3251 * convert the codepage string to Unicode and construct a UnicodeString
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.
3259 * This function avoids the overhead of opening and closing a converter if
3260 * multiple strings are constructed.
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
3270 const char *src
, int32_t srcLength
,
3272 UErrorCode
&errorCode
);
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).
3283 * Use the macro US_INV as the third, signature-distinguishing parameter.
3287 * void fn(const char *s) {
3288 * UnicodeString ustr(s, -1, US_INV);
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.
3300 UnicodeString(const char *src
, int32_t length
, enum EInvariant inv
);
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.
3312 * If the source object has an "open" buffer from getBuffer(minCapacity),
3313 * then the copy is an empty string.
3315 * @param that The UnicodeString object to copy.
3319 UnicodeString(const UnicodeString
& that
);
3322 * Move constructor; might leave src in bogus state.
3323 * This string will have the same contents and state that the source string had.
3324 * @param src source string
3327 UnicodeString(UnicodeString
&&src
) U_NOEXCEPT
;
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.
3335 UnicodeString(const UnicodeString
& src
, int32_t srcStart
);
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.
3344 UnicodeString(const UnicodeString
& src
, int32_t srcStart
, int32_t srcLength
);
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.
3356 * @return a clone of this object
3358 * @see Replaceable::clone
3359 * @see getDynamicClassID
3362 virtual Replaceable
*clone() const;
3367 virtual ~UnicodeString();
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().
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.
3382 static UnicodeString
fromUTF8(StringPiece utf8
);
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().
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.
3395 static UnicodeString
fromUTF32(const UChar32
*utf32
, int32_t length
);
3397 /* Miscellaneous operations */
3400 * Unescape a string of characters and return a string containing
3401 * the result. The following escape sequences are recognized:
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
3409 * as well as the standard ANSI C escapes:
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,
3413 * \\" => U+0022, \\' => U+0027, \\? => U+003F, \\\\ => U+005C
3415 * Anything else following a backslash is generically escaped. For
3416 * example, "[a\\-z]" returns "[a-z]".
3418 * If an escape sequence is ill-formed, this method returns an empty
3419 * string. An example of an ill-formed sequence is "\\u" followed by
3420 * fewer than 4 hex digits.
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.
3426 * @return a string with backslash escapes interpreted, or an
3427 * empty string on error.
3428 * @see UnicodeString#unescapeAt()
3430 * @see u_unescapeAt()
3433 UnicodeString
unescape() const;
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
3440 * ill-formed, or the offset is out of range, U_SENTINEL=-1 is
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
3448 * offset, or U_SENTINEL=-1 on error.
3449 * @see UnicodeString#unescape()
3451 * @see u_unescapeAt()
3454 UChar32
unescapeAt(int32_t &offset
) const;
3457 * ICU "poor man's RTTI", returns a UClassID for this class.
3461 static UClassID U_EXPORT2
getStaticClassID();
3464 * ICU "poor man's RTTI", returns a UClassID for the actual class.
3468 virtual UClassID
getDynamicClassID() const;
3470 //========================================
3471 // Implementation methods
3472 //========================================
3476 * Implement Replaceable::getLength() (see jitterbug 1027).
3479 virtual int32_t getLength() const;
3482 * The change in Replaceable to use virtual getCharAt() allows
3483 * UnicodeString::charAt() to be inline again (see jitterbug 709).
3486 virtual char16_t getCharAt(int32_t offset
) const;
3489 * The change in Replaceable to use virtual getChar32At() allows
3490 * UnicodeString::char32At() to be inline again (see jitterbug 709).
3493 virtual UChar32
getChar32At(int32_t offset
) const;
3496 // For char* constructors. Could be made public.
3497 UnicodeString
&setToUTF8(StringPiece utf8
);
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&.
3504 toUTF8(int32_t start
, int32_t len
,
3505 char *target
, int32_t capacity
) const;
3508 * Internal string contents comparison, called by operator==.
3509 * Requires: this & text not bogus and have same lengths.
3511 UBool
doEquals(const UnicodeString
&text
, int32_t len
) const;
3514 doCompare(int32_t start
,
3516 const UnicodeString
& srcText
,
3518 int32_t srcLength
) const;
3520 int8_t doCompare(int32_t start
,
3522 const char16_t *srcChars
,
3524 int32_t srcLength
) const;
3527 doCompareCodePointOrder(int32_t start
,
3529 const UnicodeString
& srcText
,
3531 int32_t srcLength
) const;
3533 int8_t doCompareCodePointOrder(int32_t start
,
3535 const char16_t *srcChars
,
3537 int32_t srcLength
) const;
3540 doCaseCompare(int32_t start
,
3542 const UnicodeString
&srcText
,
3545 uint32_t options
) const;
3548 doCaseCompare(int32_t start
,
3550 const char16_t *srcChars
,
3553 uint32_t options
) const;
3555 int32_t doIndexOf(char16_t c
,
3557 int32_t length
) const;
3559 int32_t doIndexOf(UChar32 c
,
3561 int32_t length
) const;
3563 int32_t doLastIndexOf(char16_t c
,
3565 int32_t length
) const;
3567 int32_t doLastIndexOf(UChar32 c
,
3569 int32_t length
) const;
3571 void doExtract(int32_t start
,
3574 int32_t dstStart
) const;
3576 inline void doExtract(int32_t start
,
3578 UnicodeString
& target
) const;
3580 inline char16_t doCharAt(int32_t offset
) const;
3582 UnicodeString
& doReplace(int32_t start
,
3584 const UnicodeString
& srcText
,
3588 UnicodeString
& doReplace(int32_t start
,
3590 const char16_t *srcChars
,
3594 UnicodeString
& doAppend(const UnicodeString
& src
, int32_t srcStart
, int32_t srcLength
);
3595 UnicodeString
& doAppend(const char16_t *srcChars
, int32_t srcStart
, int32_t srcLength
);
3597 UnicodeString
& doReverse(int32_t start
,
3600 // calculate hash code
3601 int32_t doHashCode(void) const;
3603 // get pointer to start of array
3604 // these do not check for kOpenGetBuffer, unlike the public getBuffer() function
3605 inline char16_t* getArrayStart(void);
3606 inline const char16_t* getArrayStart(void) const;
3608 inline UBool
hasShortLength() const;
3609 inline int32_t getShortLength() const;
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;
3615 // Is the current buffer writable?
3616 inline UBool
isBufferWritable() const;
3618 // None of the following does releaseArray().
3619 inline void setZeroLength();
3620 inline void setShortLength(int32_t len
);
3621 inline void setLength(int32_t len
);
3622 inline void setToEmpty();
3623 inline void setArray(char16_t *array
, int32_t len
, int32_t capacity
); // sets length but not flags
3625 // allocate the array; result may be the stack buffer
3626 // sets refCount to 1 if appropriate
3627 // sets fArray, fCapacity, and flags
3629 // returns boolean for success or failure
3630 UBool
allocate(int32_t capacity
);
3632 // release the array if owned
3633 void releaseArray(void);
3635 // turn a bogus string into an empty one
3638 // implements assigment operator, copy constructor, and fastCopyFrom()
3639 UnicodeString
©From(const UnicodeString
&src
, UBool fastCopy
=FALSE
);
3641 // Copies just the fields without memory management.
3642 void copyFieldsFrom(UnicodeString
&src
, UBool setSrcToBogus
) U_NOEXCEPT
;
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;
3649 #if !UCONFIG_NO_CONVERSION
3651 /* Internal extract() using UConverter. */
3652 int32_t doExtract(int32_t start
, int32_t length
,
3653 char *dest
, int32_t destCapacity
,
3655 UErrorCode
&errorCode
) const;
3658 * Real constructor for converting from codepage data.
3659 * It assumes that it is called with !fRefCounted.
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.
3667 void doCodepageCreate(const char *codepageData
,
3669 const char *codepage
);
3672 * Worker function for creating a UnicodeString from
3673 * a codepage string using a UConverter.
3676 doCodepageCreate(const char *codepageData
,
3678 UConverter
*converter
,
3679 UErrorCode
&status
);
3684 * This function is called when write access to the array
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.
3692 * Return FALSE if memory could not be allocated.
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
);
3701 * Common function for UnicodeString case mappings.
3702 * The stringCaseMapper has the same type UStringCaseMapper
3703 * as in ustr_imp.h for ustrcase_map().
3706 caseMap(int32_t caseLocale
, uint32_t options
,
3707 #if !UCONFIG_NO_BREAK_ITERATION
3708 BreakIterator
*iter
,
3710 UStringCaseMapper
*stringCaseMapper
);
3714 int32_t removeRef(void);
3715 int32_t refCount(void) const;
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
3724 US_STACKBUF_SIZE
=(int32_t)(UNISTR_OBJECT_SIZE
-sizeof(void *)-2)/U_SIZEOF_UCHAR
,
3725 kInvalidUChar
=0xffff, // U+FFFF returned by charAt(invalid index)
3726 kInvalidHashCode
=0, // invalid hash code
3727 kEmptyHashCode
=1, // hash code for empty string
3729 // bit flag values for fLengthAndFlags
3730 kIsBogus
=1, // this string is bogus, i.e., not valid or NULL
3731 kUsingStackBuffer
=2,// using fUnion.fStackFields instead of fUnion.fFields
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
3736 kAllStorageFlags
=0x1f,
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
3743 // combined values for convenience
3744 kShortString
=kUsingStackBuffer
,
3745 kLongString
=kRefCounted
,
3746 kReadonlyAlias
=kBufferIsReadonly
,
3750 friend class UnicodeStringAppendable
;
3752 union StackBufferOrFields
; // forward declaration necessary before friend declaration
3753 friend union StackBufferOrFields
; // make US_STACKBUF_SIZE visible inside fUnion
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.
3761 * The fields should be aligned such that no padding is necessary.
3762 * On 32-bit machines, the size should be 32 bytes,
3763 * on 64-bit machines (8-byte pointers), it should be 40 bytes.
3765 * We use a hack to achieve this.
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)
3773 * - sizeof(fStackFields)
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.
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.
3787 * (Padding at the end of fFields is ok:
3788 * As long as it is no larger than fStackFields, it is not wasted space.)
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?"
3795 // (implicit) *vtable;
3796 union StackBufferOrFields
{
3797 // fStackFields is used iff (fLengthAndFlags&kUsingStackBuffer) else fFields is used.
3798 // Each struct of the union must begin with fLengthAndFlags.
3800 int16_t fLengthAndFlags
; // bit fields: see constants above
3801 char16_t fBuffer
[US_STACKBUF_SIZE
]; // buffer for short strings
3804 int16_t fLengthAndFlags
; // bit fields: see constants above
3805 int32_t fLength
; // number of characters in fArray if >127; else undefined
3806 int32_t fCapacity
; // capacity of fArray (in char16_ts)
3807 // array pointer last to minimize padding for machines with P128 data model
3808 // or pointer sizes that are not a power of 2
3809 char16_t *fArray
; // the Unicode data
3815 * Create a new UnicodeString with the concatenation of two others.
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)
3822 U_COMMON_API UnicodeString U_EXPORT2
3823 operator+ (const UnicodeString
&s1
, const UnicodeString
&s2
);
3825 //========================================
3827 //========================================
3829 //========================================
3831 //========================================
3834 UnicodeString::pinIndex(int32_t& start
) const
3839 } else if(start
> length()) {
3845 UnicodeString::pinIndices(int32_t& start
,
3846 int32_t& _length
) const
3849 int32_t len
= length();
3852 } else if(start
> len
) {
3857 } else if(_length
> (len
- start
)) {
3858 _length
= (len
- start
);
3863 UnicodeString::getArrayStart() {
3864 return (fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) ?
3865 fUnion
.fStackFields
.fBuffer
: fUnion
.fFields
.fArray
;
3868 inline const char16_t*
3869 UnicodeString::getArrayStart() const {
3870 return (fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) ?
3871 fUnion
.fStackFields
.fBuffer
: fUnion
.fFields
.fArray
;
3874 //========================================
3875 // Default constructor
3876 //========================================
3879 UnicodeString::UnicodeString() {
3880 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3883 inline UnicodeString::UnicodeString(const std::nullptr_t
/*text*/) {
3884 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3887 inline UnicodeString::UnicodeString(const std::nullptr_t
/*text*/, int32_t /*length*/) {
3888 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3891 inline UnicodeString::UnicodeString(std::nullptr_t
/*buffer*/, int32_t /*buffLength*/, int32_t /*buffCapacity*/) {
3892 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3895 //========================================
3896 // Read-only implementation methods
3897 //========================================
3899 UnicodeString::hasShortLength() const {
3900 return fUnion
.fFields
.fLengthAndFlags
>=0;
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
;
3911 UnicodeString::length() const {
3912 return hasShortLength() ? getShortLength() : fUnion
.fFields
.fLength
;
3916 UnicodeString::getCapacity() const {
3917 return (fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) ?
3918 US_STACKBUF_SIZE
: fUnion
.fFields
.fCapacity
;
3922 UnicodeString::hashCode() const
3923 { return doHashCode(); }
3926 UnicodeString::isBogus() const
3927 { return (UBool
)(fUnion
.fFields
.fLengthAndFlags
& kIsBogus
); }
3930 UnicodeString::isWritable() const
3931 { return (UBool
)!(fUnion
.fFields
.fLengthAndFlags
&(kOpenGetBuffer
|kIsBogus
)); }
3934 UnicodeString::isBufferWritable() const
3937 !(fUnion
.fFields
.fLengthAndFlags
&(kOpenGetBuffer
|kIsBogus
|kBufferIsReadonly
)) &&
3938 (!(fUnion
.fFields
.fLengthAndFlags
&kRefCounted
) || refCount()==1));
3941 inline const char16_t *
3942 UnicodeString::getBuffer() const {
3943 if(fUnion
.fFields
.fLengthAndFlags
&(kIsBogus
|kOpenGetBuffer
)) {
3945 } else if(fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) {
3946 return fUnion
.fStackFields
.fBuffer
;
3948 return fUnion
.fFields
.fArray
;
3952 //========================================
3953 // Read-only alias methods
3954 //========================================
3956 UnicodeString::doCompare(int32_t start
,
3958 const UnicodeString
& srcText
,
3960 int32_t srcLength
) const
3962 if(srcText
.isBogus()) {
3963 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
3965 srcText
.pinIndices(srcStart
, srcLength
);
3966 return doCompare(start
, thisLength
, srcText
.getArrayStart(), srcStart
, srcLength
);
3971 UnicodeString::operator== (const UnicodeString
& text
) const
3974 return text
.isBogus();
3976 int32_t len
= length(), textLength
= text
.length();
3977 return !text
.isBogus() && len
== textLength
&& doEquals(text
, len
);
3982 UnicodeString::operator!= (const UnicodeString
& text
) const
3983 { return (! operator==(text
)); }
3986 UnicodeString::operator> (const UnicodeString
& text
) const
3987 { return doCompare(0, length(), text
, 0, text
.length()) == 1; }
3990 UnicodeString::operator< (const UnicodeString
& text
) const
3991 { return doCompare(0, length(), text
, 0, text
.length()) == -1; }
3994 UnicodeString::operator>= (const UnicodeString
& text
) const
3995 { return doCompare(0, length(), text
, 0, text
.length()) != -1; }
3998 UnicodeString::operator<= (const UnicodeString
& text
) const
3999 { return doCompare(0, length(), text
, 0, text
.length()) != 1; }
4002 UnicodeString::compare(const UnicodeString
& text
) const
4003 { return doCompare(0, length(), text
, 0, text
.length()); }
4006 UnicodeString::compare(int32_t start
,
4008 const UnicodeString
& srcText
) const
4009 { return doCompare(start
, _length
, srcText
, 0, srcText
.length()); }
4012 UnicodeString::compare(ConstChar16Ptr srcChars
,
4013 int32_t srcLength
) const
4014 { return doCompare(0, length(), srcChars
, 0, srcLength
); }
4017 UnicodeString::compare(int32_t start
,
4019 const UnicodeString
& srcText
,
4021 int32_t srcLength
) const
4022 { return doCompare(start
, _length
, srcText
, srcStart
, srcLength
); }
4025 UnicodeString::compare(int32_t start
,
4027 const char16_t *srcChars
) const
4028 { return doCompare(start
, _length
, srcChars
, 0, _length
); }
4031 UnicodeString::compare(int32_t start
,
4033 const char16_t *srcChars
,
4035 int32_t srcLength
) const
4036 { return doCompare(start
, _length
, srcChars
, srcStart
, srcLength
); }
4039 UnicodeString::compareBetween(int32_t start
,
4041 const UnicodeString
& srcText
,
4043 int32_t srcLimit
) const
4044 { return doCompare(start
, limit
- start
,
4045 srcText
, srcStart
, srcLimit
- srcStart
); }
4048 UnicodeString::doCompareCodePointOrder(int32_t start
,
4050 const UnicodeString
& srcText
,
4052 int32_t srcLength
) const
4054 if(srcText
.isBogus()) {
4055 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4057 srcText
.pinIndices(srcStart
, srcLength
);
4058 return doCompareCodePointOrder(start
, thisLength
, srcText
.getArrayStart(), srcStart
, srcLength
);
4063 UnicodeString::compareCodePointOrder(const UnicodeString
& text
) const
4064 { return doCompareCodePointOrder(0, length(), text
, 0, text
.length()); }
4067 UnicodeString::compareCodePointOrder(int32_t start
,
4069 const UnicodeString
& srcText
) const
4070 { return doCompareCodePointOrder(start
, _length
, srcText
, 0, srcText
.length()); }
4073 UnicodeString::compareCodePointOrder(ConstChar16Ptr srcChars
,
4074 int32_t srcLength
) const
4075 { return doCompareCodePointOrder(0, length(), srcChars
, 0, srcLength
); }
4078 UnicodeString::compareCodePointOrder(int32_t start
,
4080 const UnicodeString
& srcText
,
4082 int32_t srcLength
) const
4083 { return doCompareCodePointOrder(start
, _length
, srcText
, srcStart
, srcLength
); }
4086 UnicodeString::compareCodePointOrder(int32_t start
,
4088 const char16_t *srcChars
) const
4089 { return doCompareCodePointOrder(start
, _length
, srcChars
, 0, _length
); }
4092 UnicodeString::compareCodePointOrder(int32_t start
,
4094 const char16_t *srcChars
,
4096 int32_t srcLength
) const
4097 { return doCompareCodePointOrder(start
, _length
, srcChars
, srcStart
, srcLength
); }
4100 UnicodeString::compareCodePointOrderBetween(int32_t start
,
4102 const UnicodeString
& srcText
,
4104 int32_t srcLimit
) const
4105 { return doCompareCodePointOrder(start
, limit
- start
,
4106 srcText
, srcStart
, srcLimit
- srcStart
); }
4109 UnicodeString::doCaseCompare(int32_t start
,
4111 const UnicodeString
&srcText
,
4114 uint32_t options
) const
4116 if(srcText
.isBogus()) {
4117 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4119 srcText
.pinIndices(srcStart
, srcLength
);
4120 return doCaseCompare(start
, thisLength
, srcText
.getArrayStart(), srcStart
, srcLength
, options
);
4125 UnicodeString::caseCompare(const UnicodeString
&text
, uint32_t options
) const {
4126 return doCaseCompare(0, length(), text
, 0, text
.length(), options
);
4130 UnicodeString::caseCompare(int32_t start
,
4132 const UnicodeString
&srcText
,
4133 uint32_t options
) const {
4134 return doCaseCompare(start
, _length
, srcText
, 0, srcText
.length(), options
);
4138 UnicodeString::caseCompare(ConstChar16Ptr srcChars
,
4140 uint32_t options
) const {
4141 return doCaseCompare(0, length(), srcChars
, 0, srcLength
, options
);
4145 UnicodeString::caseCompare(int32_t start
,
4147 const UnicodeString
&srcText
,
4150 uint32_t options
) const {
4151 return doCaseCompare(start
, _length
, srcText
, srcStart
, srcLength
, options
);
4155 UnicodeString::caseCompare(int32_t start
,
4157 const char16_t *srcChars
,
4158 uint32_t options
) const {
4159 return doCaseCompare(start
, _length
, srcChars
, 0, _length
, options
);
4163 UnicodeString::caseCompare(int32_t start
,
4165 const char16_t *srcChars
,
4168 uint32_t options
) const {
4169 return doCaseCompare(start
, _length
, srcChars
, srcStart
, srcLength
, options
);
4173 UnicodeString::caseCompareBetween(int32_t start
,
4175 const UnicodeString
&srcText
,
4178 uint32_t options
) const {
4179 return doCaseCompare(start
, limit
- start
, srcText
, srcStart
, srcLimit
- srcStart
, options
);
4183 UnicodeString::indexOf(const UnicodeString
& srcText
,
4187 int32_t _length
) const
4189 if(!srcText
.isBogus()) {
4190 srcText
.pinIndices(srcStart
, srcLength
);
4192 return indexOf(srcText
.getArrayStart(), srcStart
, srcLength
, start
, _length
);
4199 UnicodeString::indexOf(const UnicodeString
& text
) const
4200 { return indexOf(text
, 0, text
.length(), 0, length()); }
4203 UnicodeString::indexOf(const UnicodeString
& text
,
4204 int32_t start
) const {
4206 return indexOf(text
, 0, text
.length(), start
, length() - start
);
4210 UnicodeString::indexOf(const UnicodeString
& text
,
4212 int32_t _length
) const
4213 { return indexOf(text
, 0, text
.length(), start
, _length
); }
4216 UnicodeString::indexOf(const char16_t *srcChars
,
4218 int32_t start
) const {
4220 return indexOf(srcChars
, 0, srcLength
, start
, length() - start
);
4224 UnicodeString::indexOf(ConstChar16Ptr srcChars
,
4227 int32_t _length
) const
4228 { return indexOf(srcChars
, 0, srcLength
, start
, _length
); }
4231 UnicodeString::indexOf(char16_t c
,
4233 int32_t _length
) const
4234 { return doIndexOf(c
, start
, _length
); }
4237 UnicodeString::indexOf(UChar32 c
,
4239 int32_t _length
) const
4240 { return doIndexOf(c
, start
, _length
); }
4243 UnicodeString::indexOf(char16_t c
) const
4244 { return doIndexOf(c
, 0, length()); }
4247 UnicodeString::indexOf(UChar32 c
) const
4248 { return indexOf(c
, 0, length()); }
4251 UnicodeString::indexOf(char16_t c
,
4252 int32_t start
) const {
4254 return doIndexOf(c
, start
, length() - start
);
4258 UnicodeString::indexOf(UChar32 c
,
4259 int32_t start
) const {
4261 return indexOf(c
, start
, length() - start
);
4265 UnicodeString::lastIndexOf(ConstChar16Ptr srcChars
,
4268 int32_t _length
) const
4269 { return lastIndexOf(srcChars
, 0, srcLength
, start
, _length
); }
4272 UnicodeString::lastIndexOf(const char16_t *srcChars
,
4274 int32_t start
) const {
4276 return lastIndexOf(srcChars
, 0, srcLength
, start
, length() - start
);
4280 UnicodeString::lastIndexOf(const UnicodeString
& srcText
,
4284 int32_t _length
) const
4286 if(!srcText
.isBogus()) {
4287 srcText
.pinIndices(srcStart
, srcLength
);
4289 return lastIndexOf(srcText
.getArrayStart(), srcStart
, srcLength
, start
, _length
);
4296 UnicodeString::lastIndexOf(const UnicodeString
& text
,
4298 int32_t _length
) const
4299 { return lastIndexOf(text
, 0, text
.length(), start
, _length
); }
4302 UnicodeString::lastIndexOf(const UnicodeString
& text
,
4303 int32_t start
) const {
4305 return lastIndexOf(text
, 0, text
.length(), start
, length() - start
);
4309 UnicodeString::lastIndexOf(const UnicodeString
& text
) const
4310 { return lastIndexOf(text
, 0, text
.length(), 0, length()); }
4313 UnicodeString::lastIndexOf(char16_t c
,
4315 int32_t _length
) const
4316 { return doLastIndexOf(c
, start
, _length
); }
4319 UnicodeString::lastIndexOf(UChar32 c
,
4321 int32_t _length
) const {
4322 return doLastIndexOf(c
, start
, _length
);
4326 UnicodeString::lastIndexOf(char16_t c
) const
4327 { return doLastIndexOf(c
, 0, length()); }
4330 UnicodeString::lastIndexOf(UChar32 c
) const {
4331 return lastIndexOf(c
, 0, length());
4335 UnicodeString::lastIndexOf(char16_t c
,
4336 int32_t start
) const {
4338 return doLastIndexOf(c
, start
, length() - start
);
4342 UnicodeString::lastIndexOf(UChar32 c
,
4343 int32_t start
) const {
4345 return lastIndexOf(c
, start
, length() - start
);
4349 UnicodeString::startsWith(const UnicodeString
& text
) const
4350 { return compare(0, text
.length(), text
, 0, text
.length()) == 0; }
4353 UnicodeString::startsWith(const UnicodeString
& srcText
,
4355 int32_t srcLength
) const
4356 { return doCompare(0, srcLength
, srcText
, srcStart
, srcLength
) == 0; }
4359 UnicodeString::startsWith(ConstChar16Ptr srcChars
, int32_t srcLength
) const {
4361 srcLength
= u_strlen(toUCharPtr(srcChars
));
4363 return doCompare(0, srcLength
, srcChars
, 0, srcLength
) == 0;
4367 UnicodeString::startsWith(const char16_t *srcChars
, int32_t srcStart
, int32_t srcLength
) const {
4369 srcLength
= u_strlen(toUCharPtr(srcChars
));
4371 return doCompare(0, srcLength
, srcChars
, srcStart
, srcLength
) == 0;
4375 UnicodeString::endsWith(const UnicodeString
& text
) const
4376 { return doCompare(length() - text
.length(), text
.length(),
4377 text
, 0, text
.length()) == 0; }
4380 UnicodeString::endsWith(const UnicodeString
& srcText
,
4382 int32_t srcLength
) const {
4383 srcText
.pinIndices(srcStart
, srcLength
);
4384 return doCompare(length() - srcLength
, srcLength
,
4385 srcText
, srcStart
, srcLength
) == 0;
4389 UnicodeString::endsWith(ConstChar16Ptr srcChars
,
4390 int32_t srcLength
) const {
4392 srcLength
= u_strlen(toUCharPtr(srcChars
));
4394 return doCompare(length() - srcLength
, srcLength
,
4395 srcChars
, 0, srcLength
) == 0;
4399 UnicodeString::endsWith(const char16_t *srcChars
,
4401 int32_t srcLength
) const {
4403 srcLength
= u_strlen(toUCharPtr(srcChars
+ srcStart
));
4405 return doCompare(length() - srcLength
, srcLength
,
4406 srcChars
, srcStart
, srcLength
) == 0;
4409 //========================================
4411 //========================================
4412 inline UnicodeString
&
4413 UnicodeString::replace(int32_t start
,
4415 const UnicodeString
& srcText
)
4416 { return doReplace(start
, _length
, srcText
, 0, srcText
.length()); }
4418 inline UnicodeString
&
4419 UnicodeString::replace(int32_t start
,
4421 const UnicodeString
& srcText
,
4424 { return doReplace(start
, _length
, srcText
, srcStart
, srcLength
); }
4426 inline UnicodeString
&
4427 UnicodeString::replace(int32_t start
,
4429 ConstChar16Ptr srcChars
,
4431 { return doReplace(start
, _length
, srcChars
, 0, srcLength
); }
4433 inline UnicodeString
&
4434 UnicodeString::replace(int32_t start
,
4436 const char16_t *srcChars
,
4439 { return doReplace(start
, _length
, srcChars
, srcStart
, srcLength
); }
4441 inline UnicodeString
&
4442 UnicodeString::replace(int32_t start
,
4445 { return doReplace(start
, _length
, &srcChar
, 0, 1); }
4447 inline UnicodeString
&
4448 UnicodeString::replaceBetween(int32_t start
,
4450 const UnicodeString
& srcText
)
4451 { return doReplace(start
, limit
- start
, srcText
, 0, srcText
.length()); }
4453 inline UnicodeString
&
4454 UnicodeString::replaceBetween(int32_t start
,
4456 const UnicodeString
& srcText
,
4459 { return doReplace(start
, limit
- start
, srcText
, srcStart
, srcLimit
- srcStart
); }
4461 inline UnicodeString
&
4462 UnicodeString::findAndReplace(const UnicodeString
& oldText
,
4463 const UnicodeString
& newText
)
4464 { return findAndReplace(0, length(), oldText
, 0, oldText
.length(),
4465 newText
, 0, newText
.length()); }
4467 inline UnicodeString
&
4468 UnicodeString::findAndReplace(int32_t start
,
4470 const UnicodeString
& oldText
,
4471 const UnicodeString
& newText
)
4472 { return findAndReplace(start
, _length
, oldText
, 0, oldText
.length(),
4473 newText
, 0, newText
.length()); }
4475 // ============================
4477 // ============================
4479 UnicodeString::doExtract(int32_t start
,
4481 UnicodeString
& target
) const
4482 { target
.replace(0, target
.length(), *this, start
, _length
); }
4485 UnicodeString::extract(int32_t start
,
4488 int32_t targetStart
) const
4489 { doExtract(start
, _length
, target
, targetStart
); }
4492 UnicodeString::extract(int32_t start
,
4494 UnicodeString
& target
) const
4495 { doExtract(start
, _length
, target
); }
4497 #if !UCONFIG_NO_CONVERSION
4500 UnicodeString::extract(int32_t start
,
4503 const char *codepage
) const
4506 // This dstSize value will be checked explicitly
4507 return extract(start
, _length
, dst
, dst
!=0 ? 0xffffffff : 0, codepage
);
4513 UnicodeString::extractBetween(int32_t start
,
4516 int32_t dstStart
) const {
4519 doExtract(start
, limit
- start
, dst
, dstStart
);
4522 inline UnicodeString
4523 UnicodeString::tempSubStringBetween(int32_t start
, int32_t limit
) const {
4524 return tempSubString(start
, limit
- start
);
4528 UnicodeString::doCharAt(int32_t offset
) const
4530 if((uint32_t)offset
< (uint32_t)length()) {
4531 return getArrayStart()[offset
];
4533 return kInvalidUChar
;
4538 UnicodeString::charAt(int32_t offset
) const
4539 { return doCharAt(offset
); }
4542 UnicodeString::operator[] (int32_t offset
) const
4543 { return doCharAt(offset
); }
4546 UnicodeString::isEmpty() const {
4547 // Arithmetic or logical right shift does not matter: only testing for 0.
4548 return (fUnion
.fFields
.fLengthAndFlags
>>kLengthShift
) == 0;
4551 //========================================
4552 // Write implementation methods
4553 //========================================
4555 UnicodeString::setZeroLength() {
4556 fUnion
.fFields
.fLengthAndFlags
&= kAllStorageFlags
;
4560 UnicodeString::setShortLength(int32_t len
) {
4561 // requires 0 <= len <= kMaxShortLength
4562 fUnion
.fFields
.fLengthAndFlags
=
4563 (int16_t)((fUnion
.fFields
.fLengthAndFlags
& kAllStorageFlags
) | (len
<< kLengthShift
));
4567 UnicodeString::setLength(int32_t len
) {
4568 if(len
<= kMaxShortLength
) {
4569 setShortLength(len
);
4571 fUnion
.fFields
.fLengthAndFlags
|= kLengthIsLarge
;
4572 fUnion
.fFields
.fLength
= len
;
4577 UnicodeString::setToEmpty() {
4578 fUnion
.fFields
.fLengthAndFlags
= kShortString
;
4582 UnicodeString::setArray(char16_t *array
, int32_t len
, int32_t capacity
) {
4584 fUnion
.fFields
.fArray
= array
;
4585 fUnion
.fFields
.fCapacity
= capacity
;
4588 inline UnicodeString
&
4589 UnicodeString::operator= (char16_t ch
)
4590 { return doReplace(0, length(), &ch
, 0, 1); }
4592 inline UnicodeString
&
4593 UnicodeString::operator= (UChar32 ch
)
4594 { return replace(0, length(), ch
); }
4596 inline UnicodeString
&
4597 UnicodeString::setTo(const UnicodeString
& srcText
,
4602 return doReplace(0, length(), srcText
, srcStart
, srcLength
);
4605 inline UnicodeString
&
4606 UnicodeString::setTo(const UnicodeString
& srcText
,
4610 srcText
.pinIndex(srcStart
);
4611 return doReplace(0, length(), srcText
, srcStart
, srcText
.length() - srcStart
);
4614 inline UnicodeString
&
4615 UnicodeString::setTo(const UnicodeString
& srcText
)
4617 return copyFrom(srcText
);
4620 inline UnicodeString
&
4621 UnicodeString::setTo(const char16_t *srcChars
,
4625 return doReplace(0, length(), srcChars
, 0, srcLength
);
4628 inline UnicodeString
&
4629 UnicodeString::setTo(char16_t srcChar
)
4632 return doReplace(0, length(), &srcChar
, 0, 1);
4635 inline UnicodeString
&
4636 UnicodeString::setTo(UChar32 srcChar
)
4639 return replace(0, length(), srcChar
);
4642 inline UnicodeString
&
4643 UnicodeString::append(const UnicodeString
& srcText
,
4646 { return doAppend(srcText
, srcStart
, srcLength
); }
4648 inline UnicodeString
&
4649 UnicodeString::append(const UnicodeString
& srcText
)
4650 { return doAppend(srcText
, 0, srcText
.length()); }
4652 inline UnicodeString
&
4653 UnicodeString::append(const char16_t *srcChars
,
4656 { return doAppend(srcChars
, srcStart
, srcLength
); }
4658 inline UnicodeString
&
4659 UnicodeString::append(ConstChar16Ptr srcChars
,
4661 { return doAppend(srcChars
, 0, srcLength
); }
4663 inline UnicodeString
&
4664 UnicodeString::append(char16_t srcChar
)
4665 { return doAppend(&srcChar
, 0, 1); }
4667 inline UnicodeString
&
4668 UnicodeString::operator+= (char16_t ch
)
4669 { return doAppend(&ch
, 0, 1); }
4671 inline UnicodeString
&
4672 UnicodeString::operator+= (UChar32 ch
) {
4676 inline UnicodeString
&
4677 UnicodeString::operator+= (const UnicodeString
& srcText
)
4678 { return doAppend(srcText
, 0, srcText
.length()); }
4680 inline UnicodeString
&
4681 UnicodeString::insert(int32_t start
,
4682 const UnicodeString
& srcText
,
4685 { return doReplace(start
, 0, srcText
, srcStart
, srcLength
); }
4687 inline UnicodeString
&
4688 UnicodeString::insert(int32_t start
,
4689 const UnicodeString
& srcText
)
4690 { return doReplace(start
, 0, srcText
, 0, srcText
.length()); }
4692 inline UnicodeString
&
4693 UnicodeString::insert(int32_t start
,
4694 const char16_t *srcChars
,
4697 { return doReplace(start
, 0, srcChars
, srcStart
, srcLength
); }
4699 inline UnicodeString
&
4700 UnicodeString::insert(int32_t start
,
4701 ConstChar16Ptr srcChars
,
4703 { return doReplace(start
, 0, srcChars
, 0, srcLength
); }
4705 inline UnicodeString
&
4706 UnicodeString::insert(int32_t start
,
4708 { return doReplace(start
, 0, &srcChar
, 0, 1); }
4710 inline UnicodeString
&
4711 UnicodeString::insert(int32_t start
,
4713 { return replace(start
, 0, srcChar
); }
4716 inline UnicodeString
&
4717 UnicodeString::remove()
4719 // remove() of a bogus string makes the string empty and non-bogus
4728 inline UnicodeString
&
4729 UnicodeString::remove(int32_t start
,
4732 if(start
<= 0 && _length
== INT32_MAX
) {
4733 // remove(guaranteed everything) of a bogus string makes the string empty and non-bogus
4736 return doReplace(start
, _length
, NULL
, 0, 0);
4739 inline UnicodeString
&
4740 UnicodeString::removeBetween(int32_t start
,
4742 { return doReplace(start
, limit
- start
, NULL
, 0, 0); }
4744 inline UnicodeString
&
4745 UnicodeString::retainBetween(int32_t start
, int32_t limit
) {
4747 return doReplace(0, start
, NULL
, 0, 0);
4751 UnicodeString::truncate(int32_t targetLength
)
4753 if(isBogus() && targetLength
== 0) {
4754 // truncate(0) of a bogus string makes the string empty and non-bogus
4757 } else if((uint32_t)targetLength
< (uint32_t)length()) {
4758 setLength(targetLength
);
4765 inline UnicodeString
&
4766 UnicodeString::reverse()
4767 { return doReverse(0, length()); }
4769 inline UnicodeString
&
4770 UnicodeString::reverse(int32_t start
,
4772 { return doReverse(start
, _length
); }
4775 #endif // U_SHOW_CPLUSPLUS_API