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
41 #ifndef U_COMPARE_CODE_POINT_ORDER
42 /* see also ustring.h and unorm.h */
44 * Option bit for u_strCaseCompare, u_strcasecmp, unorm_compare, etc:
45 * Compare strings in code point order instead of code unit order.
48 #define U_COMPARE_CODE_POINT_ORDER 0x8000
53 * \ingroup ustring_ustrlen
55 U_STABLE
int32_t U_EXPORT2
56 u_strlen(const UChar
*s
);
59 #if U_SHOW_CPLUSPLUS_API
62 #if !UCONFIG_NO_BREAK_ITERATION
63 class BreakIterator
; // unicode/brkiter.h
68 #endif // U_SHOW_CPLUSPLUS_API
70 #if U_SHOW_CPLUSPLUS_API
71 // Not #ifndef U_HIDE_INTERNAL_API because UnicodeString needs the UStringCaseMapper.
73 * Internal string case mapping function type.
74 * All error checking must be done.
75 * src and dest must not overlap.
78 typedef int32_t U_CALLCONV
79 UStringCaseMapper(int32_t caseLocale
, uint32_t options
,
80 #if !UCONFIG_NO_BREAK_ITERATION
81 icu::BreakIterator
*iter
,
83 char16_t *dest
, int32_t destCapacity
,
84 const char16_t *src
, int32_t srcLength
,
86 UErrorCode
&errorCode
);
87 #endif // U_SHOW_CPLUSPLUS_API
89 #if U_SHOW_CPLUSPLUS_API
92 class Locale
; // unicode/locid.h
93 class StringCharacterIterator
;
94 class UnicodeStringAppendable
; // unicode/appendable.h
96 /* The <iostream> include has been moved to unicode/ustream.h */
99 * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor
100 * which constructs a Unicode string from an invariant-character char * string.
101 * About invariant characters see utypes.h.
102 * This constructor has no runtime dependency on conversion code and is
103 * therefore recommended over ones taking a charset name string
104 * (where the empty string "" indicates invariant-character conversion).
108 #define US_INV icu::UnicodeString::kInvariant
111 * Unicode String literals in C++.
113 * Note: these macros are not recommended for new code.
114 * Prior to the availability of C++11 and u"unicode string literals",
115 * these macros were provided for portability and efficiency when
116 * initializing UnicodeStrings from literals.
118 * They work only for strings that contain "invariant characters", i.e.,
119 * only latin letters, digits, and some punctuation.
120 * See utypes.h for details.
122 * The string parameter must be a C string literal.
123 * The length of the string, not including the terminating
124 * <code>NUL</code>, must be specified as a constant.
127 #if !U_CHAR16_IS_TYPEDEF
128 # define UNICODE_STRING(cs, _length) icu::UnicodeString(TRUE, u ## cs, _length)
130 # define UNICODE_STRING(cs, _length) icu::UnicodeString(TRUE, (const char16_t*)u ## cs, _length)
134 * Unicode String literals in C++.
135 * Dependent on the platform properties, different UnicodeString
136 * constructors should be used to create a UnicodeString object from
138 * The macros are defined for improved performance.
139 * They work only for strings that contain "invariant characters", i.e.,
140 * only latin letters, digits, and some punctuation.
141 * See utypes.h for details.
143 * The string parameter must be a C string literal.
146 #define UNICODE_STRING_SIMPLE(cs) UNICODE_STRING(cs, -1)
149 * \def UNISTR_FROM_CHAR_EXPLICIT
150 * This can be defined to be empty or "explicit".
151 * If explicit, then the UnicodeString(char16_t) and UnicodeString(UChar32)
152 * constructors are marked as explicit, preventing their inadvertent use.
155 #ifndef UNISTR_FROM_CHAR_EXPLICIT
156 # if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
157 // Auto-"explicit" in ICU library code.
158 # define UNISTR_FROM_CHAR_EXPLICIT explicit
160 // Empty by default for source code compatibility.
161 # define UNISTR_FROM_CHAR_EXPLICIT
166 * \def UNISTR_FROM_STRING_EXPLICIT
167 * This can be defined to be empty or "explicit".
168 * If explicit, then the UnicodeString(const char *) and UnicodeString(const char16_t *)
169 * constructors are marked as explicit, preventing their inadvertent use.
171 * In particular, this helps prevent accidentally depending on ICU conversion code
172 * by passing a string literal into an API with a const UnicodeString & parameter.
175 #ifndef UNISTR_FROM_STRING_EXPLICIT
176 # if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
177 // Auto-"explicit" in ICU library code.
178 # define UNISTR_FROM_STRING_EXPLICIT explicit
180 // Empty by default for source code compatibility.
181 # define UNISTR_FROM_STRING_EXPLICIT
186 * \def UNISTR_OBJECT_SIZE
187 * Desired sizeof(UnicodeString) in bytes.
188 * It should be a multiple of sizeof(pointer) to avoid unusable space for padding.
189 * The object size may want to be a multiple of 16 bytes,
190 * which is a common granularity for heap allocation.
192 * Any space inside the object beyond sizeof(vtable pointer) + 2
193 * is available for storing short strings inside the object.
194 * The bigger the object, the longer a string that can be stored inside the object,
195 * without additional heap allocation.
197 * Depending on a platform's pointer size, pointer alignment requirements,
198 * and struct padding, the compiler will usually round up sizeof(UnicodeString)
199 * to 4 * sizeof(pointer) (or 3 * sizeof(pointer) for P128 data models),
200 * to hold the fields for heap-allocated strings.
201 * Such a minimum size also ensures that the object is easily large enough
202 * to hold at least 2 char16_ts, for one supplementary code point (U16_MAX_LENGTH).
204 * sizeof(UnicodeString) >= 48 should work for all known platforms.
206 * For example, on a 64-bit machine where sizeof(vtable pointer) is 8,
207 * sizeof(UnicodeString) = 64 would leave space for
208 * (64 - sizeof(vtable pointer) - 2) / U_SIZEOF_UCHAR = (64 - 8 - 2) / 2 = 27
209 * char16_ts stored inside the object.
211 * The minimum object size on a 64-bit machine would be
212 * 4 * sizeof(pointer) = 4 * 8 = 32 bytes,
213 * and the internal buffer would hold up to 11 char16_ts in that case.
215 * @see U16_MAX_LENGTH
218 #ifndef UNISTR_OBJECT_SIZE
219 # define UNISTR_OBJECT_SIZE 64
223 * UnicodeString is a string class that stores Unicode characters directly and provides
224 * similar functionality as the Java String and StringBuffer/StringBuilder classes.
225 * It is a concrete implementation of the abstract class Replaceable (for transliteration).
227 * A UnicodeString may also "alias" an external array of characters
228 * (that is, point to it, rather than own the array)
229 * whose lifetime must then at least match the lifetime of the aliasing object.
230 * This aliasing may be preserved when returning a UnicodeString by value,
231 * depending on the compiler and the function implementation,
232 * via Return Value Optimization (RVO) or the move assignment operator.
233 * (However, the copy assignment operator does not preserve aliasing.)
234 * For details see the description of storage models at the end of the class API docs
235 * and in the User Guide chapter linked from there.
237 * The UnicodeString class is not suitable for subclassing.
239 * <p>For an overview of Unicode strings in C and C++ see the
240 * <a href="http://userguide.icu-project.org/strings#TOC-Strings-in-C-C-">User Guide Strings chapter</a>.</p>
242 * <p>In ICU, a Unicode string consists of 16-bit Unicode <em>code units</em>.
243 * A Unicode character may be stored with either one code unit
244 * (the most common case) or with a matched pair of special code units
245 * ("surrogates"). The data type for code units is char16_t.
246 * For single-character handling, a Unicode character code <em>point</em> is a value
247 * in the range 0..0x10ffff. ICU uses the UChar32 type for code points.</p>
249 * <p>Indexes and offsets into and lengths of strings always count code units, not code points.
250 * This is the same as with multi-byte char* strings in traditional string handling.
251 * Operations on partial strings typically do not test for code point boundaries.
252 * If necessary, the user needs to take care of such boundaries by testing for the code unit
253 * values or by using functions like
254 * UnicodeString::getChar32Start() and UnicodeString::getChar32Limit()
255 * (or, in C, the equivalent macros U16_SET_CP_START() and U16_SET_CP_LIMIT(), see utf.h).</p>
257 * UnicodeString methods are more lenient with regard to input parameter values
258 * than other ICU APIs. In particular:
259 * - If indexes are out of bounds for a UnicodeString object
260 * (<0 or >length()) then they are "pinned" to the nearest boundary.
261 * - If primitive string pointer values (e.g., const char16_t * or char *)
262 * for input strings are NULL, then those input string parameters are treated
263 * as if they pointed to an empty string.
264 * However, this is <em>not</em> the case for char * parameters for charset names
266 * - Most UnicodeString methods do not take a UErrorCode parameter because
267 * there are usually very few opportunities for failure other than a shortage
268 * of memory, error codes in low-level C++ string methods would be inconvenient,
269 * and the error code as the last parameter (ICU convention) would prevent
270 * the use of default parameter values.
271 * Instead, such methods set the UnicodeString into a "bogus" state
272 * (see isBogus()) if an error occurs.
274 * In string comparisons, two UnicodeString objects that are both "bogus"
275 * compare equal (to be transitive and prevent endless loops in sorting),
276 * and a "bogus" string compares less than any non-"bogus" one.
278 * Const UnicodeString methods are thread-safe. Multiple threads can use
279 * const methods on the same UnicodeString object simultaneously,
280 * but non-const methods must not be called concurrently (in multiple threads)
281 * with any other (const or non-const) methods.
283 * Similarly, const UnicodeString & parameters are thread-safe.
284 * One object may be passed in as such a parameter concurrently in multiple threads.
285 * This includes the const UnicodeString & parameters for
286 * copy construction, assignment, and cloning.
288 * <p>UnicodeString uses several storage methods.
289 * String contents can be stored inside the UnicodeString object itself,
290 * in an allocated and shared buffer, or in an outside buffer that is "aliased".
291 * Most of this is done transparently, but careful aliasing in particular provides
292 * significant performance improvements.
293 * Also, the internal buffer is accessible via special functions.
294 * For details see the
295 * <a href="http://userguide.icu-project.org/strings#TOC-Maximizing-Performance-with-the-UnicodeString-Storage-Model">User Guide Strings chapter</a>.</p>
298 * @see CharacterIterator
301 class U_COMMON_API UnicodeString
: public Replaceable
306 * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor
307 * which constructs a Unicode string from an invariant-character char * string.
308 * Use the macro US_INV instead of the full qualification for this value.
321 //========================================
322 // Read-only operations
323 //========================================
325 /* Comparison - bitwise only - for international comparison use collation */
328 * Equality operator. Performs only bitwise comparison.
329 * @param text The UnicodeString to compare to this one.
330 * @return TRUE if <TT>text</TT> contains the same characters as this one,
334 inline UBool
operator== (const UnicodeString
& text
) const;
337 * Inequality operator. Performs only bitwise comparison.
338 * @param text The UnicodeString to compare to this one.
339 * @return FALSE if <TT>text</TT> contains the same characters as this one,
343 inline UBool
operator!= (const UnicodeString
& text
) const;
346 * Greater than operator. Performs only bitwise comparison.
347 * @param text The UnicodeString to compare to this one.
348 * @return TRUE if the characters in this are bitwise
349 * greater than the characters in <code>text</code>, FALSE otherwise
352 inline UBool
operator> (const UnicodeString
& text
) const;
355 * Less than operator. Performs only bitwise comparison.
356 * @param text The UnicodeString to compare to this one.
357 * @return TRUE if the characters in this are bitwise
358 * less than the characters in <code>text</code>, FALSE otherwise
361 inline UBool
operator< (const UnicodeString
& text
) const;
364 * Greater than or equal operator. Performs only bitwise comparison.
365 * @param text The UnicodeString to compare to this one.
366 * @return TRUE if the characters in this are bitwise
367 * greater than or equal to the characters in <code>text</code>, FALSE otherwise
370 inline UBool
operator>= (const UnicodeString
& text
) const;
373 * Less than or equal operator. Performs only bitwise comparison.
374 * @param text The UnicodeString to compare to this one.
375 * @return TRUE if the characters in this are bitwise
376 * less than or equal to the characters in <code>text</code>, FALSE otherwise
379 inline UBool
operator<= (const UnicodeString
& text
) const;
382 * Compare the characters bitwise in this UnicodeString to
383 * the characters in <code>text</code>.
384 * @param text The UnicodeString to compare to this one.
385 * @return The result of bitwise character comparison: 0 if this
386 * contains the same characters as <code>text</code>, -1 if the characters in
387 * this are bitwise less than the characters in <code>text</code>, +1 if the
388 * characters in this are bitwise greater than the characters
389 * in <code>text</code>.
392 inline int8_t compare(const UnicodeString
& text
) const;
395 * Compare the characters bitwise in the range
396 * [<TT>start</TT>, <TT>start + length</TT>) with the characters
397 * in the <b>entire string</b> <TT>text</TT>.
398 * (The parameters "start" and "length" are not applied to the other text "text".)
399 * @param start the offset at which the compare operation begins
400 * @param length the number of characters of text to compare.
401 * @param text the other text to be compared against this string.
402 * @return The result of bitwise character comparison: 0 if this
403 * contains the same characters as <code>text</code>, -1 if the characters in
404 * this are bitwise less than the characters in <code>text</code>, +1 if the
405 * characters in this are bitwise greater than the characters
406 * in <code>text</code>.
409 inline int8_t compare(int32_t start
,
411 const UnicodeString
& text
) const;
414 * Compare the characters bitwise in the range
415 * [<TT>start</TT>, <TT>start + length</TT>) with the characters
416 * in <TT>srcText</TT> in the range
417 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
418 * @param start the offset at which the compare operation begins
419 * @param length the number of characters in this to compare.
420 * @param srcText the text to be compared
421 * @param srcStart the offset into <TT>srcText</TT> to start comparison
422 * @param srcLength the number of characters in <TT>src</TT> to compare
423 * @return The result of bitwise character comparison: 0 if this
424 * contains the same characters as <code>srcText</code>, -1 if the characters in
425 * this are bitwise less than the characters in <code>srcText</code>, +1 if the
426 * characters in this are bitwise greater than the characters
427 * in <code>srcText</code>.
430 inline int8_t compare(int32_t start
,
432 const UnicodeString
& srcText
,
434 int32_t srcLength
) const;
437 * Compare the characters bitwise in this UnicodeString with the first
438 * <TT>srcLength</TT> characters in <TT>srcChars</TT>.
439 * @param srcChars The characters to compare to this UnicodeString.
440 * @param srcLength the number of characters in <TT>srcChars</TT> to compare
441 * @return The result of bitwise character comparison: 0 if this
442 * contains the same characters as <code>srcChars</code>, -1 if the characters in
443 * this are bitwise less than the characters in <code>srcChars</code>, +1 if the
444 * characters in this are bitwise greater than the characters
445 * in <code>srcChars</code>.
448 inline int8_t compare(ConstChar16Ptr srcChars
,
449 int32_t srcLength
) const;
452 * Compare the characters bitwise in the range
453 * [<TT>start</TT>, <TT>start + length</TT>) with the first
454 * <TT>length</TT> characters in <TT>srcChars</TT>
455 * @param start the offset at which the compare operation begins
456 * @param length the number of characters to compare.
457 * @param srcChars the characters to be compared
458 * @return The result of bitwise character comparison: 0 if this
459 * contains the same characters as <code>srcChars</code>, -1 if the characters in
460 * this are bitwise less than the characters in <code>srcChars</code>, +1 if the
461 * characters in this are bitwise greater than the characters
462 * in <code>srcChars</code>.
465 inline int8_t compare(int32_t start
,
467 const char16_t *srcChars
) const;
470 * Compare the characters bitwise in the range
471 * [<TT>start</TT>, <TT>start + length</TT>) with the characters
472 * in <TT>srcChars</TT> in the range
473 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
474 * @param start the offset at which the compare operation begins
475 * @param length the number of characters in this to compare
476 * @param srcChars the characters to be compared
477 * @param srcStart the offset into <TT>srcChars</TT> to start comparison
478 * @param srcLength the number of characters in <TT>srcChars</TT> to compare
479 * @return The result of bitwise character comparison: 0 if this
480 * contains the same characters as <code>srcChars</code>, -1 if the characters in
481 * this are bitwise less than the characters in <code>srcChars</code>, +1 if the
482 * characters in this are bitwise greater than the characters
483 * in <code>srcChars</code>.
486 inline int8_t compare(int32_t start
,
488 const char16_t *srcChars
,
490 int32_t srcLength
) const;
493 * Compare the characters bitwise in the range
494 * [<TT>start</TT>, <TT>limit</TT>) with the characters
495 * in <TT>srcText</TT> in the range
496 * [<TT>srcStart</TT>, <TT>srcLimit</TT>).
497 * @param start the offset at which the compare operation begins
498 * @param limit the offset immediately following the compare operation
499 * @param srcText the text to be compared
500 * @param srcStart the offset into <TT>srcText</TT> to start comparison
501 * @param srcLimit the offset into <TT>srcText</TT> to limit comparison
502 * @return The result of bitwise character comparison: 0 if this
503 * contains the same characters as <code>srcText</code>, -1 if the characters in
504 * this are bitwise less than the characters in <code>srcText</code>, +1 if the
505 * characters in this are bitwise greater than the characters
506 * in <code>srcText</code>.
509 inline int8_t compareBetween(int32_t start
,
511 const UnicodeString
& srcText
,
513 int32_t srcLimit
) const;
516 * Compare two Unicode strings in code point order.
517 * The result may be different from the results of compare(), operator<, etc.
518 * if supplementary characters are present:
520 * In UTF-16, supplementary characters (with code points U+10000 and above) are
521 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
522 * which means that they compare as less than some other BMP characters like U+feff.
523 * This function compares Unicode strings in code point order.
524 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
526 * @param text Another string to compare this one to.
527 * @return a negative/zero/positive integer corresponding to whether
528 * this string is less than/equal to/greater than the second one
529 * in code point order
532 inline int8_t compareCodePointOrder(const UnicodeString
& text
) const;
535 * Compare two Unicode strings in code point order.
536 * The result may be different from the results of compare(), operator<, etc.
537 * if supplementary characters are present:
539 * In UTF-16, supplementary characters (with code points U+10000 and above) are
540 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
541 * which means that they compare as less than some other BMP characters like U+feff.
542 * This function compares Unicode strings in code point order.
543 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
545 * @param start The start offset in this string at which the compare operation begins.
546 * @param length The number of code units from this string to compare.
547 * @param srcText Another string to compare this one to.
548 * @return a negative/zero/positive integer corresponding to whether
549 * this string is less than/equal to/greater than the second one
550 * in code point order
553 inline int8_t compareCodePointOrder(int32_t start
,
555 const UnicodeString
& srcText
) const;
558 * Compare two Unicode strings in code point order.
559 * The result may be different from the results of compare(), operator<, etc.
560 * if supplementary characters are present:
562 * In UTF-16, supplementary characters (with code points U+10000 and above) are
563 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
564 * which means that they compare as less than some other BMP characters like U+feff.
565 * This function compares Unicode strings in code point order.
566 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
568 * @param start The start offset in this string at which the compare operation begins.
569 * @param length The number of code units from this string to compare.
570 * @param srcText Another string to compare this one to.
571 * @param srcStart The start offset in that string at which the compare operation begins.
572 * @param srcLength The number of code units from that string to compare.
573 * @return a negative/zero/positive integer corresponding to whether
574 * this string is less than/equal to/greater than the second one
575 * in code point order
578 inline int8_t compareCodePointOrder(int32_t start
,
580 const UnicodeString
& srcText
,
582 int32_t srcLength
) const;
585 * Compare two Unicode strings in code point order.
586 * The result may be different from the results of compare(), operator<, etc.
587 * if supplementary characters are present:
589 * In UTF-16, supplementary characters (with code points U+10000 and above) are
590 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
591 * which means that they compare as less than some other BMP characters like U+feff.
592 * This function compares Unicode strings in code point order.
593 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
595 * @param srcChars A pointer to another string to compare this one to.
596 * @param srcLength The number of code units from that string to compare.
597 * @return a negative/zero/positive integer corresponding to whether
598 * this string is less than/equal to/greater than the second one
599 * in code point order
602 inline int8_t compareCodePointOrder(ConstChar16Ptr srcChars
,
603 int32_t srcLength
) const;
606 * Compare two Unicode strings in code point order.
607 * The result may be different from the results of compare(), operator<, etc.
608 * if supplementary characters are present:
610 * In UTF-16, supplementary characters (with code points U+10000 and above) are
611 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
612 * which means that they compare as less than some other BMP characters like U+feff.
613 * This function compares Unicode strings in code point order.
614 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
616 * @param start The start offset in this string at which the compare operation begins.
617 * @param length The number of code units from this string to compare.
618 * @param srcChars A pointer to another string to compare this one to.
619 * @return a negative/zero/positive integer corresponding to whether
620 * this string is less than/equal to/greater than the second one
621 * in code point order
624 inline int8_t compareCodePointOrder(int32_t start
,
626 const char16_t *srcChars
) const;
629 * Compare two Unicode strings in code point order.
630 * The result may be different from the results of compare(), operator<, etc.
631 * if supplementary characters are present:
633 * In UTF-16, supplementary characters (with code points U+10000 and above) are
634 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
635 * which means that they compare as less than some other BMP characters like U+feff.
636 * This function compares Unicode strings in code point order.
637 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
639 * @param start The start offset in this string at which the compare operation begins.
640 * @param length The number of code units from this string to compare.
641 * @param srcChars A pointer to another string to compare this one to.
642 * @param srcStart The start offset in that string at which the compare operation begins.
643 * @param srcLength The number of code units from that string to compare.
644 * @return a negative/zero/positive integer corresponding to whether
645 * this string is less than/equal to/greater than the second one
646 * in code point order
649 inline int8_t compareCodePointOrder(int32_t start
,
651 const char16_t *srcChars
,
653 int32_t srcLength
) const;
656 * Compare two Unicode strings in code point order.
657 * The result may be different from the results of compare(), operator<, etc.
658 * if supplementary characters are present:
660 * In UTF-16, supplementary characters (with code points U+10000 and above) are
661 * stored with pairs of surrogate code units. These have values from 0xd800 to 0xdfff,
662 * which means that they compare as less than some other BMP characters like U+feff.
663 * This function compares Unicode strings in code point order.
664 * If either of the UTF-16 strings is malformed (i.e., it contains unpaired surrogates), then the result is not defined.
666 * @param start The start offset in this string at which the compare operation begins.
667 * @param limit The offset after the last code unit from this string to compare.
668 * @param srcText Another string to compare this one to.
669 * @param srcStart The start offset in that string at which the compare operation begins.
670 * @param srcLimit The offset after the last code unit from that string to compare.
671 * @return a negative/zero/positive integer corresponding to whether
672 * this string is less than/equal to/greater than the second one
673 * in code point order
676 inline int8_t compareCodePointOrderBetween(int32_t start
,
678 const UnicodeString
& srcText
,
680 int32_t srcLimit
) const;
683 * Compare two strings case-insensitively using full case folding.
684 * This is equivalent to this->foldCase(options).compare(text.foldCase(options)).
686 * @param text Another string to compare this one to.
687 * @param options A bit set of options:
688 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
689 * Comparison in code unit order with default case folding.
691 * - U_COMPARE_CODE_POINT_ORDER
692 * Set to choose code point order instead of code unit order
693 * (see u_strCompare for details).
695 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
697 * @return A negative, zero, or positive integer indicating the comparison result.
700 inline int8_t caseCompare(const UnicodeString
& text
, uint32_t options
) const;
703 * Compare two strings case-insensitively using full case folding.
704 * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)).
706 * @param start The start offset in this string at which the compare operation begins.
707 * @param length The number of code units from this string to compare.
708 * @param srcText Another string to compare this one to.
709 * @param options A bit set of options:
710 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
711 * Comparison in code unit order with default case folding.
713 * - U_COMPARE_CODE_POINT_ORDER
714 * Set to choose code point order instead of code unit order
715 * (see u_strCompare for details).
717 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
719 * @return A negative, zero, or positive integer indicating the comparison result.
722 inline int8_t caseCompare(int32_t start
,
724 const UnicodeString
& srcText
,
725 uint32_t options
) const;
728 * Compare two strings case-insensitively using full case folding.
729 * This is equivalent to this->foldCase(options).compare(srcText.foldCase(options)).
731 * @param start The start offset in this string at which the compare operation begins.
732 * @param length The number of code units from this string to compare.
733 * @param srcText Another string to compare this one to.
734 * @param srcStart The start offset in that string at which the compare operation begins.
735 * @param srcLength The number of code units from that string to compare.
736 * @param options A bit set of options:
737 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
738 * Comparison in code unit order with default case folding.
740 * - U_COMPARE_CODE_POINT_ORDER
741 * Set to choose code point order instead of code unit order
742 * (see u_strCompare for details).
744 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
746 * @return A negative, zero, or positive integer indicating the comparison result.
749 inline int8_t caseCompare(int32_t start
,
751 const UnicodeString
& srcText
,
754 uint32_t options
) const;
757 * Compare two strings case-insensitively using full case folding.
758 * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
760 * @param srcChars A pointer to another string to compare this one to.
761 * @param srcLength The number of code units from that string to compare.
762 * @param options A bit set of options:
763 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
764 * Comparison in code unit order with default case folding.
766 * - U_COMPARE_CODE_POINT_ORDER
767 * Set to choose code point order instead of code unit order
768 * (see u_strCompare for details).
770 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
772 * @return A negative, zero, or positive integer indicating the comparison result.
775 inline int8_t caseCompare(ConstChar16Ptr srcChars
,
777 uint32_t options
) const;
780 * Compare two strings case-insensitively using full case folding.
781 * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
783 * @param start The start offset in this string at which the compare operation begins.
784 * @param length The number of code units from this string to compare.
785 * @param srcChars A pointer to another string to compare this one to.
786 * @param options A bit set of options:
787 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
788 * Comparison in code unit order with default case folding.
790 * - U_COMPARE_CODE_POINT_ORDER
791 * Set to choose code point order instead of code unit order
792 * (see u_strCompare for details).
794 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
796 * @return A negative, zero, or positive integer indicating the comparison result.
799 inline int8_t caseCompare(int32_t start
,
801 const char16_t *srcChars
,
802 uint32_t options
) const;
805 * Compare two strings case-insensitively using full case folding.
806 * This is equivalent to this->foldCase(options).compare(srcChars.foldCase(options)).
808 * @param start The start offset in this string at which the compare operation begins.
809 * @param length The number of code units from this string to compare.
810 * @param srcChars A pointer to another string to compare this one to.
811 * @param srcStart The start offset in that string at which the compare operation begins.
812 * @param srcLength The number of code units from that string to compare.
813 * @param options A bit set of options:
814 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
815 * Comparison in code unit order with default case folding.
817 * - U_COMPARE_CODE_POINT_ORDER
818 * Set to choose code point order instead of code unit order
819 * (see u_strCompare for details).
821 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
823 * @return A negative, zero, or positive integer indicating the comparison result.
826 inline int8_t caseCompare(int32_t start
,
828 const char16_t *srcChars
,
831 uint32_t options
) const;
834 * Compare two strings case-insensitively using full case folding.
835 * This is equivalent to this->foldCase(options).compareBetween(text.foldCase(options)).
837 * @param start The start offset in this string at which the compare operation begins.
838 * @param limit The offset after the last code unit from this string to compare.
839 * @param srcText Another string to compare this one to.
840 * @param srcStart The start offset in that string at which the compare operation begins.
841 * @param srcLimit The offset after the last code unit from that string to compare.
842 * @param options A bit set of options:
843 * - U_FOLD_CASE_DEFAULT or 0 is used for default options:
844 * Comparison in code unit order with default case folding.
846 * - U_COMPARE_CODE_POINT_ORDER
847 * Set to choose code point order instead of code unit order
848 * (see u_strCompare for details).
850 * - U_FOLD_CASE_EXCLUDE_SPECIAL_I
852 * @return A negative, zero, or positive integer indicating the comparison result.
855 inline int8_t caseCompareBetween(int32_t start
,
857 const UnicodeString
& srcText
,
860 uint32_t options
) const;
863 * Determine if this starts with the characters in <TT>text</TT>
864 * @param text The text to match.
865 * @return TRUE if this starts with the characters in <TT>text</TT>,
869 inline UBool
startsWith(const UnicodeString
& text
) const;
872 * Determine if this starts with the characters in <TT>srcText</TT>
873 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
874 * @param srcText The text to match.
875 * @param srcStart the offset into <TT>srcText</TT> to start matching
876 * @param srcLength the number of characters in <TT>srcText</TT> to match
877 * @return TRUE if this starts with the characters in <TT>text</TT>,
881 inline UBool
startsWith(const UnicodeString
& srcText
,
883 int32_t srcLength
) const;
886 * Determine if this starts with the characters in <TT>srcChars</TT>
887 * @param srcChars The characters to match.
888 * @param srcLength the number of characters in <TT>srcChars</TT>
889 * @return TRUE if this starts with the characters in <TT>srcChars</TT>,
893 inline UBool
startsWith(ConstChar16Ptr srcChars
,
894 int32_t srcLength
) const;
897 * Determine if this ends with the characters in <TT>srcChars</TT>
898 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
899 * @param srcChars The characters to match.
900 * @param srcStart the offset into <TT>srcText</TT> to start matching
901 * @param srcLength the number of characters in <TT>srcChars</TT> to match
902 * @return TRUE if this ends with the characters in <TT>srcChars</TT>, FALSE otherwise
905 inline UBool
startsWith(const char16_t *srcChars
,
907 int32_t srcLength
) const;
910 * Determine if this ends with the characters in <TT>text</TT>
911 * @param text The text to match.
912 * @return TRUE if this ends with the characters in <TT>text</TT>,
916 inline UBool
endsWith(const UnicodeString
& text
) const;
919 * Determine if this ends with the characters in <TT>srcText</TT>
920 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
921 * @param srcText The text to match.
922 * @param srcStart the offset into <TT>srcText</TT> to start matching
923 * @param srcLength the number of characters in <TT>srcText</TT> to match
924 * @return TRUE if this ends with the characters in <TT>text</TT>,
928 inline UBool
endsWith(const UnicodeString
& srcText
,
930 int32_t srcLength
) const;
933 * Determine if this ends with the characters in <TT>srcChars</TT>
934 * @param srcChars The characters to match.
935 * @param srcLength the number of characters in <TT>srcChars</TT>
936 * @return TRUE if this ends with the characters in <TT>srcChars</TT>,
940 inline UBool
endsWith(ConstChar16Ptr srcChars
,
941 int32_t srcLength
) const;
944 * Determine if this ends with the characters in <TT>srcChars</TT>
945 * in the range [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
946 * @param srcChars The characters to match.
947 * @param srcStart the offset into <TT>srcText</TT> to start matching
948 * @param srcLength the number of characters in <TT>srcChars</TT> to match
949 * @return TRUE if this ends with the characters in <TT>srcChars</TT>,
953 inline UBool
endsWith(const char16_t *srcChars
,
955 int32_t srcLength
) const;
958 /* Searching - bitwise only */
961 * Locate in this the first occurrence of the characters in <TT>text</TT>,
962 * using bitwise comparison.
963 * @param text The text to search for.
964 * @return The offset into this of the start of <TT>text</TT>,
965 * or -1 if not found.
968 inline int32_t indexOf(const UnicodeString
& text
) const;
971 * Locate in this the first occurrence of the characters in <TT>text</TT>
972 * starting at offset <TT>start</TT>, using bitwise comparison.
973 * @param text The text to search for.
974 * @param start The offset at which searching will start.
975 * @return The offset into this of the start of <TT>text</TT>,
976 * or -1 if not found.
979 inline int32_t indexOf(const UnicodeString
& text
,
980 int32_t start
) const;
983 * Locate in this the first occurrence in the range
984 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
985 * in <TT>text</TT>, using bitwise comparison.
986 * @param text The text to search for.
987 * @param start The offset at which searching will start.
988 * @param length The number of characters to search
989 * @return The offset into this of the start of <TT>text</TT>,
990 * or -1 if not found.
993 inline int32_t indexOf(const UnicodeString
& text
,
995 int32_t length
) const;
998 * Locate in this the first occurrence in the range
999 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1000 * in <TT>srcText</TT> in the range
1001 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>),
1002 * using bitwise comparison.
1003 * @param srcText The text to search for.
1004 * @param srcStart the offset into <TT>srcText</TT> at which
1006 * @param srcLength the number of characters in <TT>srcText</TT> to match
1007 * @param start the offset into this at which to start matching
1008 * @param length the number of characters in this to search
1009 * @return The offset into this of the start of <TT>text</TT>,
1010 * or -1 if not found.
1013 inline int32_t indexOf(const UnicodeString
& srcText
,
1017 int32_t length
) const;
1020 * Locate in this the first occurrence of the characters in
1022 * starting at offset <TT>start</TT>, using bitwise comparison.
1023 * @param srcChars The text to search for.
1024 * @param srcLength the number of characters in <TT>srcChars</TT> to match
1025 * @param start the offset into this at which to start matching
1026 * @return The offset into this of the start of <TT>text</TT>,
1027 * or -1 if not found.
1030 inline int32_t indexOf(const char16_t *srcChars
,
1032 int32_t start
) const;
1035 * Locate in this the first occurrence in the range
1036 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1037 * in <TT>srcChars</TT>, using bitwise comparison.
1038 * @param srcChars The text to search for.
1039 * @param srcLength the number of characters in <TT>srcChars</TT>
1040 * @param start The offset at which searching will start.
1041 * @param length The number of characters to search
1042 * @return The offset into this of the start of <TT>srcChars</TT>,
1043 * or -1 if not found.
1046 inline int32_t indexOf(ConstChar16Ptr srcChars
,
1049 int32_t length
) const;
1052 * Locate in this the first occurrence in the range
1053 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1054 * in <TT>srcChars</TT> in the range
1055 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>),
1056 * using bitwise comparison.
1057 * @param srcChars The text to search for.
1058 * @param srcStart the offset into <TT>srcChars</TT> at which
1060 * @param srcLength the number of characters in <TT>srcChars</TT> to match
1061 * @param start the offset into this at which to start matching
1062 * @param length the number of characters in this to search
1063 * @return The offset into this of the start of <TT>text</TT>,
1064 * or -1 if not found.
1067 int32_t indexOf(const char16_t *srcChars
,
1071 int32_t length
) const;
1074 * Locate in this the first occurrence of the BMP code point <code>c</code>,
1075 * using bitwise comparison.
1076 * @param c The code unit to search for.
1077 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1080 inline int32_t indexOf(char16_t c
) const;
1083 * Locate in this the first occurrence of the code point <TT>c</TT>,
1084 * using bitwise comparison.
1086 * @param c The code point to search for.
1087 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1090 inline int32_t indexOf(UChar32 c
) const;
1093 * Locate in this the first occurrence of the BMP code point <code>c</code>,
1094 * starting at offset <TT>start</TT>, using bitwise comparison.
1095 * @param c The code unit to search for.
1096 * @param start The offset at which searching will start.
1097 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1100 inline int32_t indexOf(char16_t c
,
1101 int32_t start
) const;
1104 * Locate in this the first occurrence of the code point <TT>c</TT>
1105 * starting at offset <TT>start</TT>, using bitwise comparison.
1107 * @param c The code point to search for.
1108 * @param start The offset at which searching will start.
1109 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1112 inline int32_t indexOf(UChar32 c
,
1113 int32_t start
) const;
1116 * Locate in this the first occurrence of the BMP code point <code>c</code>
1117 * in the range [<TT>start</TT>, <TT>start + length</TT>),
1118 * using bitwise comparison.
1119 * @param c The code unit to search for.
1120 * @param start the offset into this at which to start matching
1121 * @param length the number of characters in this to search
1122 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1125 inline int32_t indexOf(char16_t c
,
1127 int32_t length
) const;
1130 * Locate in this the first occurrence of the code point <TT>c</TT>
1131 * in the range [<TT>start</TT>, <TT>start + length</TT>),
1132 * using bitwise comparison.
1134 * @param c The code point to search for.
1135 * @param start the offset into this at which to start matching
1136 * @param length the number of characters in this to search
1137 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1140 inline int32_t indexOf(UChar32 c
,
1142 int32_t length
) const;
1145 * Locate in this the last occurrence of the characters in <TT>text</TT>,
1146 * using bitwise comparison.
1147 * @param text The text to search for.
1148 * @return The offset into this of the start of <TT>text</TT>,
1149 * or -1 if not found.
1152 inline int32_t lastIndexOf(const UnicodeString
& text
) const;
1155 * Locate in this the last occurrence of the characters in <TT>text</TT>
1156 * starting at offset <TT>start</TT>, using bitwise comparison.
1157 * @param text The text to search for.
1158 * @param start The offset at which searching will start.
1159 * @return The offset into this of the start of <TT>text</TT>,
1160 * or -1 if not found.
1163 inline int32_t lastIndexOf(const UnicodeString
& text
,
1164 int32_t start
) const;
1167 * Locate in this the last occurrence in the range
1168 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1169 * in <TT>text</TT>, using bitwise comparison.
1170 * @param text The text to search for.
1171 * @param start The offset at which searching will start.
1172 * @param length The number of characters to search
1173 * @return The offset into this of the start of <TT>text</TT>,
1174 * or -1 if not found.
1177 inline int32_t lastIndexOf(const UnicodeString
& text
,
1179 int32_t length
) const;
1182 * Locate in this the last occurrence in the range
1183 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1184 * in <TT>srcText</TT> in the range
1185 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>),
1186 * using bitwise comparison.
1187 * @param srcText The text to search for.
1188 * @param srcStart the offset into <TT>srcText</TT> at which
1190 * @param srcLength the number of characters in <TT>srcText</TT> to match
1191 * @param start the offset into this at which to start matching
1192 * @param length the number of characters in this to search
1193 * @return The offset into this of the start of <TT>text</TT>,
1194 * or -1 if not found.
1197 inline int32_t lastIndexOf(const UnicodeString
& srcText
,
1201 int32_t length
) const;
1204 * Locate in this the last occurrence of the characters in <TT>srcChars</TT>
1205 * starting at offset <TT>start</TT>, using bitwise comparison.
1206 * @param srcChars The text to search for.
1207 * @param srcLength the number of characters in <TT>srcChars</TT> to match
1208 * @param start the offset into this at which to start matching
1209 * @return The offset into this of the start of <TT>text</TT>,
1210 * or -1 if not found.
1213 inline int32_t lastIndexOf(const char16_t *srcChars
,
1215 int32_t start
) const;
1218 * Locate in this the last occurrence in the range
1219 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1220 * in <TT>srcChars</TT>, using bitwise comparison.
1221 * @param srcChars The text to search for.
1222 * @param srcLength the number of characters in <TT>srcChars</TT>
1223 * @param start The offset at which searching will start.
1224 * @param length The number of characters to search
1225 * @return The offset into this of the start of <TT>srcChars</TT>,
1226 * or -1 if not found.
1229 inline int32_t lastIndexOf(ConstChar16Ptr srcChars
,
1232 int32_t length
) const;
1235 * Locate in this the last occurrence in the range
1236 * [<TT>start</TT>, <TT>start + length</TT>) of the characters
1237 * in <TT>srcChars</TT> in the range
1238 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>),
1239 * using bitwise comparison.
1240 * @param srcChars The text to search for.
1241 * @param srcStart the offset into <TT>srcChars</TT> at which
1243 * @param srcLength the number of characters in <TT>srcChars</TT> to match
1244 * @param start the offset into this at which to start matching
1245 * @param length the number of characters in this to search
1246 * @return The offset into this of the start of <TT>text</TT>,
1247 * or -1 if not found.
1250 int32_t lastIndexOf(const char16_t *srcChars
,
1254 int32_t length
) const;
1257 * Locate in this the last occurrence of the BMP code point <code>c</code>,
1258 * using bitwise comparison.
1259 * @param c The code unit to search for.
1260 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1263 inline int32_t lastIndexOf(char16_t c
) const;
1266 * Locate in this the last occurrence of the code point <TT>c</TT>,
1267 * using bitwise comparison.
1269 * @param c The code point to search for.
1270 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1273 inline int32_t lastIndexOf(UChar32 c
) const;
1276 * Locate in this the last occurrence of the BMP code point <code>c</code>
1277 * starting at offset <TT>start</TT>, using bitwise comparison.
1278 * @param c The code unit to search for.
1279 * @param start The offset at which searching will start.
1280 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1283 inline int32_t lastIndexOf(char16_t c
,
1284 int32_t start
) const;
1287 * Locate in this the last occurrence of the code point <TT>c</TT>
1288 * starting at offset <TT>start</TT>, using bitwise comparison.
1290 * @param c The code point to search for.
1291 * @param start The offset at which searching will start.
1292 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1295 inline int32_t lastIndexOf(UChar32 c
,
1296 int32_t start
) const;
1299 * Locate in this the last occurrence of the BMP code point <code>c</code>
1300 * in the range [<TT>start</TT>, <TT>start + length</TT>),
1301 * using bitwise comparison.
1302 * @param c The code unit to search for.
1303 * @param start the offset into this at which to start matching
1304 * @param length the number of characters in this to search
1305 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1308 inline int32_t lastIndexOf(char16_t c
,
1310 int32_t length
) const;
1313 * Locate in this the last occurrence of the code point <TT>c</TT>
1314 * in the range [<TT>start</TT>, <TT>start + length</TT>),
1315 * using bitwise comparison.
1317 * @param c The code point to search for.
1318 * @param start the offset into this at which to start matching
1319 * @param length the number of characters in this to search
1320 * @return The offset into this of <TT>c</TT>, or -1 if not found.
1323 inline int32_t lastIndexOf(UChar32 c
,
1325 int32_t length
) const;
1328 /* Character access */
1331 * Return the code unit at offset <tt>offset</tt>.
1332 * If the offset is not valid (0..length()-1) then U+ffff is returned.
1333 * @param offset a valid offset into the text
1334 * @return the code unit at offset <tt>offset</tt>
1335 * or 0xffff if the offset is not valid for this string
1338 inline char16_t charAt(int32_t offset
) const;
1341 * Return the code unit 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 * @return the code unit at offset <tt>offset</tt>
1347 inline char16_t operator[] (int32_t offset
) const;
1350 * Return the code point that contains the code unit
1351 * at offset <tt>offset</tt>.
1352 * If the offset is not valid (0..length()-1) then U+ffff is returned.
1353 * @param offset a valid offset into the text
1354 * that indicates the text offset of any of the code units
1355 * that will be assembled into a code point (21-bit value) and returned
1356 * @return the code point of text at <tt>offset</tt>
1357 * or 0xffff if the offset is not valid for this string
1360 UChar32
char32At(int32_t offset
) const;
1363 * Adjust a random-access offset so that
1364 * it points to the beginning of a Unicode character.
1365 * The offset that is passed in points to
1366 * any code unit of a code point,
1367 * while the returned offset will point to the first code unit
1368 * of the same code point.
1369 * In UTF-16, if the input offset points to a second surrogate
1370 * of a surrogate pair, then the returned offset will point
1371 * to the first surrogate.
1372 * @param offset a valid offset into one code point of the text
1373 * @return offset of the first code unit of the same code point
1374 * @see U16_SET_CP_START
1377 int32_t getChar32Start(int32_t offset
) const;
1380 * Adjust a random-access offset so that
1381 * it points behind a Unicode character.
1382 * The offset that is passed in points behind
1383 * any code unit of a code point,
1384 * while the returned offset will point behind the last code unit
1385 * of the same code point.
1386 * In UTF-16, if the input offset points behind the first surrogate
1387 * (i.e., to the second surrogate)
1388 * of a surrogate pair, then the returned offset will point
1389 * behind the second surrogate (i.e., to the first surrogate).
1390 * @param offset a valid offset after any code unit of a code point of the text
1391 * @return offset of the first code unit after the same code point
1392 * @see U16_SET_CP_LIMIT
1395 int32_t getChar32Limit(int32_t offset
) const;
1398 * Move the code unit index along the string by delta code points.
1399 * Interpret the input index as a code unit-based offset into the string,
1400 * move the index forward or backward by delta code points, and
1401 * return the resulting index.
1402 * The input index should point to the first code unit of a code point,
1403 * if there is more than one.
1405 * Both input and output indexes are code unit-based as for all
1406 * string indexes/offsets in ICU (and other libraries, like MBCS char*).
1407 * If delta<0 then the index is moved backward (toward the start of the string).
1408 * If delta>0 then the index is moved forward (toward the end of the string).
1410 * This behaves like CharacterIterator::move32(delta, kCurrent).
1412 * Behavior for out-of-bounds indexes:
1413 * <code>moveIndex32</code> pins the input index to 0..length(), i.e.,
1414 * if the input index<0 then it is pinned to 0;
1415 * if it is index>length() then it is pinned to length().
1416 * Afterwards, the index is moved by <code>delta</code> code points
1417 * forward or backward,
1418 * but no further backward than to 0 and no further forward than to length().
1419 * The resulting index return value will be in between 0 and length(), inclusively.
1423 * // s has code points 'a' U+10000 'b' U+10ffff U+2029
1424 * UnicodeString s=UNICODE_STRING("a\\U00010000b\\U0010ffff\\u2029", 31).unescape();
1426 * // initial index: position of U+10000
1429 * // the following examples will all result in index==4, position of U+10ffff
1431 * // skip 2 code points from some position in the string
1432 * index=s.moveIndex32(index, 2); // skips U+10000 and 'b'
1434 * // go to the 3rd code point from the start of s (0-based)
1435 * index=s.moveIndex32(0, 3); // skips 'a', U+10000, and 'b'
1437 * // go to the next-to-last code point of s
1438 * index=s.moveIndex32(s.length(), -2); // backward-skips U+2029 and U+10ffff
1441 * @param index input code unit index
1442 * @param delta (signed) code point count to move the index forward or backward
1444 * @return the resulting code unit index
1447 int32_t moveIndex32(int32_t index
, int32_t delta
) const;
1449 /* Substring extraction */
1452 * Copy the characters in the range
1453 * [<tt>start</tt>, <tt>start + length</tt>) into the array <tt>dst</tt>,
1454 * beginning at <tt>dstStart</tt>.
1455 * If the string aliases to <code>dst</code> itself as an external buffer,
1456 * then extract() will not copy the contents.
1458 * @param start offset of first character which will be copied into the array
1459 * @param length the number of characters to extract
1460 * @param dst array in which to copy characters. The length of <tt>dst</tt>
1461 * must be at least (<tt>dstStart + length</tt>).
1462 * @param dstStart the offset in <TT>dst</TT> where the first character
1466 inline void extract(int32_t start
,
1469 int32_t dstStart
= 0) const;
1472 * Copy the contents of the string into dest.
1473 * This is a convenience function that
1474 * checks if there is enough space in dest,
1475 * extracts the entire string if possible,
1476 * and NUL-terminates dest if possible.
1478 * If the string fits into dest but cannot be NUL-terminated
1479 * (length()==destCapacity) then the error code is set to U_STRING_NOT_TERMINATED_WARNING.
1480 * If the string itself does not fit into dest
1481 * (length()>destCapacity) then the error code is set to U_BUFFER_OVERFLOW_ERROR.
1483 * If the string aliases to <code>dest</code> itself as an external buffer,
1484 * then extract() will not copy the contents.
1486 * @param dest Destination string buffer.
1487 * @param destCapacity Number of char16_ts available at dest.
1488 * @param errorCode ICU error code.
1493 extract(Char16Ptr dest
, int32_t destCapacity
,
1494 UErrorCode
&errorCode
) const;
1497 * Copy the characters in the range
1498 * [<tt>start</tt>, <tt>start + length</tt>) into the UnicodeString
1500 * @param start offset of first character which will be copied
1501 * @param length the number of characters to extract
1502 * @param target UnicodeString into which to copy characters.
1503 * @return A reference to <TT>target</TT>
1506 inline void extract(int32_t start
,
1508 UnicodeString
& target
) const;
1511 * Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>)
1512 * into the array <tt>dst</tt>, beginning at <tt>dstStart</tt>.
1513 * @param start offset of first character which will be copied into the array
1514 * @param limit offset immediately following the last character to be copied
1515 * @param dst array in which to copy characters. The length of <tt>dst</tt>
1516 * must be at least (<tt>dstStart + (limit - start)</tt>).
1517 * @param dstStart the offset in <TT>dst</TT> where the first character
1521 inline void extractBetween(int32_t start
,
1524 int32_t dstStart
= 0) const;
1527 * Copy the characters in the range [<tt>start</tt>, <tt>limit</tt>)
1528 * into the UnicodeString <tt>target</tt>. Replaceable API.
1529 * @param start offset of first character which will be copied
1530 * @param limit offset immediately following the last character to be copied
1531 * @param target UnicodeString into which to copy characters.
1532 * @return A reference to <TT>target</TT>
1535 virtual void extractBetween(int32_t start
,
1537 UnicodeString
& target
) const;
1540 * Copy the characters in the range
1541 * [<tt>start</TT>, <tt>start + startLength</TT>) into an array of characters.
1542 * All characters must be invariant (see utypes.h).
1543 * Use US_INV as the last, signature-distinguishing parameter.
1545 * This function does not write any more than <code>targetCapacity</code>
1546 * characters but returns the length of the entire output string
1547 * so that one can allocate a larger buffer and call the function again
1549 * The output string is NUL-terminated if possible.
1551 * @param start offset of first character which will be copied
1552 * @param startLength the number of characters to extract
1553 * @param target the target buffer for extraction, can be NULL
1554 * if targetLength is 0
1555 * @param targetCapacity the length of the target buffer
1556 * @param inv Signature-distinguishing paramater, use US_INV.
1557 * @return the output string length, not including the terminating NUL
1560 int32_t extract(int32_t start
,
1561 int32_t startLength
,
1563 int32_t targetCapacity
,
1564 enum EInvariant inv
) const;
1566 #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
1569 * Copy the characters in the range
1570 * [<tt>start</TT>, <tt>start + length</TT>) into an array of characters
1571 * in the platform's default codepage.
1572 * This function does not write any more than <code>targetLength</code>
1573 * characters but returns the length of the entire output string
1574 * so that one can allocate a larger buffer and call the function again
1576 * The output string is NUL-terminated if possible.
1578 * @param start offset of first character which will be copied
1579 * @param startLength the number of characters to extract
1580 * @param target the target buffer for extraction
1581 * @param targetLength the length of the target buffer
1582 * If <TT>target</TT> is NULL, then the number of bytes required for
1583 * <TT>target</TT> is returned.
1584 * @return the output string length, not including the terminating NUL
1587 int32_t extract(int32_t start
,
1588 int32_t startLength
,
1590 uint32_t targetLength
) const;
1594 #if !UCONFIG_NO_CONVERSION
1597 * Copy the characters in the range
1598 * [<tt>start</TT>, <tt>start + length</TT>) into an array of characters
1599 * in a specified codepage.
1600 * The output string is NUL-terminated.
1602 * Recommendation: For invariant-character strings use
1603 * extract(int32_t start, int32_t length, char *target, int32_t targetCapacity, enum EInvariant inv) const
1604 * because it avoids object code dependencies of UnicodeString on
1605 * the conversion code.
1607 * @param start offset of first character which will be copied
1608 * @param startLength the number of characters to extract
1609 * @param target the target buffer for extraction
1610 * @param codepage the desired codepage for the characters. 0 has
1611 * the special meaning of the default codepage
1612 * If <code>codepage</code> is an empty string (<code>""</code>),
1613 * then a simple conversion is performed on the codepage-invariant
1614 * subset ("invariant characters") of the platform encoding. See utypes.h.
1615 * If <TT>target</TT> is NULL, then the number of bytes required for
1616 * <TT>target</TT> is returned. It is assumed that the target is big enough
1617 * to fit all of the characters.
1618 * @return the output string length, not including the terminating NUL
1621 inline int32_t extract(int32_t start
,
1622 int32_t startLength
,
1624 const char *codepage
= 0) const;
1627 * Copy the characters in the range
1628 * [<tt>start</TT>, <tt>start + length</TT>) into an array of characters
1629 * in a specified codepage.
1630 * This function does not write any more than <code>targetLength</code>
1631 * characters but returns the length of the entire output string
1632 * so that one can allocate a larger buffer and call the function again
1634 * The output string is NUL-terminated if possible.
1636 * Recommendation: For invariant-character strings use
1637 * extract(int32_t start, int32_t length, char *target, int32_t targetCapacity, enum EInvariant inv) const
1638 * because it avoids object code dependencies of UnicodeString on
1639 * the conversion code.
1641 * @param start offset of first character which will be copied
1642 * @param startLength the number of characters to extract
1643 * @param target the target buffer for extraction
1644 * @param targetLength the length of the target buffer
1645 * @param codepage the desired codepage for the characters. 0 has
1646 * the special meaning of the default codepage
1647 * If <code>codepage</code> is an empty string (<code>""</code>),
1648 * then a simple conversion is performed on the codepage-invariant
1649 * subset ("invariant characters") of the platform encoding. See utypes.h.
1650 * If <TT>target</TT> is NULL, then the number of bytes required for
1651 * <TT>target</TT> is returned.
1652 * @return the output string length, not including the terminating NUL
1655 int32_t extract(int32_t start
,
1656 int32_t startLength
,
1658 uint32_t targetLength
,
1659 const char *codepage
) const;
1662 * Convert the UnicodeString into a codepage string using an existing UConverter.
1663 * The output string is NUL-terminated if possible.
1665 * This function avoids the overhead of opening and closing a converter if
1666 * multiple strings are extracted.
1668 * @param dest destination string buffer, can be NULL if destCapacity==0
1669 * @param destCapacity the number of chars available at dest
1670 * @param cnv the converter object to be used (ucnv_resetFromUnicode() will be called),
1671 * or NULL for the default converter
1672 * @param errorCode normal ICU error code
1673 * @return the length of the output string, not counting the terminating NUL;
1674 * if the length is greater than destCapacity, then the string will not fit
1675 * and a buffer of the indicated length would need to be passed in
1678 int32_t extract(char *dest
, int32_t destCapacity
,
1680 UErrorCode
&errorCode
) const;
1685 * Create a temporary substring for the specified range.
1686 * Unlike the substring constructor and setTo() functions,
1687 * the object returned here will be a read-only alias (using getBuffer())
1688 * rather than copying the text.
1689 * As a result, this substring operation is much faster but requires
1690 * that the original string not be modified or deleted during the lifetime
1691 * of the returned substring object.
1692 * @param start offset of the first character visible in the substring
1693 * @param length length of the substring
1694 * @return a read-only alias UnicodeString object for the substring
1697 UnicodeString
tempSubString(int32_t start
=0, int32_t length
=INT32_MAX
) const;
1700 * Create a temporary substring for the specified range.
1701 * Same as tempSubString(start, length) except that the substring range
1702 * is specified as a (start, limit) pair (with an exclusive limit index)
1703 * rather than a (start, length) pair.
1704 * @param start offset of the first character visible in the substring
1705 * @param limit offset immediately following the last character visible in the substring
1706 * @return a read-only alias UnicodeString object for the substring
1709 inline UnicodeString
tempSubStringBetween(int32_t start
, int32_t limit
=INT32_MAX
) const;
1712 * Convert the UnicodeString to UTF-8 and write the result
1713 * to a ByteSink. This is called by toUTF8String().
1714 * Unpaired surrogates are replaced with U+FFFD.
1715 * Calls u_strToUTF8WithSub().
1717 * @param sink A ByteSink to which the UTF-8 version of the string is written.
1718 * sink.Flush() is called at the end.
1722 void toUTF8(ByteSink
&sink
) const;
1725 * Convert the UnicodeString to UTF-8 and append the result
1726 * to a standard string.
1727 * Unpaired surrogates are replaced with U+FFFD.
1730 * @param result A standard string (or a compatible object)
1731 * to which the UTF-8 version of the string is appended.
1732 * @return The string object.
1736 template<typename StringClass
>
1737 StringClass
&toUTF8String(StringClass
&result
) const {
1738 StringByteSink
<StringClass
> sbs(&result
);
1744 * Convert the UnicodeString to UTF-32.
1745 * Unpaired surrogates are replaced with U+FFFD.
1746 * Calls u_strToUTF32WithSub().
1748 * @param utf32 destination string buffer, can be NULL if capacity==0
1749 * @param capacity the number of UChar32s available at utf32
1750 * @param errorCode Standard ICU error code. Its input value must
1751 * pass the U_SUCCESS() test, or else the function returns
1752 * immediately. Check for U_FAILURE() on output or use with
1753 * function chaining. (See User Guide for details.)
1754 * @return The length of the UTF-32 string.
1758 int32_t toUTF32(UChar32
*utf32
, int32_t capacity
, UErrorCode
&errorCode
) const;
1760 /* Length operations */
1763 * Return the length of the UnicodeString object.
1764 * The length is the number of char16_t code units are in the UnicodeString.
1765 * If you want the number of code points, please use countChar32().
1766 * @return the length of the UnicodeString object
1770 inline int32_t length(void) const;
1773 * Count Unicode code points in the length char16_t code units of the string.
1774 * A code point may occupy either one or two char16_t code units.
1775 * Counting code points involves reading all code units.
1777 * This functions is basically the inverse of moveIndex32().
1779 * @param start the index of the first code unit to check
1780 * @param length the number of char16_t code units to check
1781 * @return the number of code points in the specified code units
1786 countChar32(int32_t start
=0, int32_t length
=INT32_MAX
) const;
1789 * Check if the length char16_t code units of the string
1790 * contain more Unicode code points than a certain number.
1791 * This is more efficient than counting all code points in this part of the string
1792 * and comparing that number with a threshold.
1793 * This function may not need to scan the string at all if the length
1794 * falls within a certain range, and
1795 * never needs to count more than 'number+1' code points.
1796 * Logically equivalent to (countChar32(start, length)>number).
1797 * A Unicode code point may occupy either one or two char16_t code units.
1799 * @param start the index of the first code unit to check (0 for the entire string)
1800 * @param length the number of char16_t code units to check
1801 * (use INT32_MAX for the entire string; remember that start/length
1802 * values are pinned)
1803 * @param number The number of code points in the (sub)string is compared against
1804 * the 'number' parameter.
1805 * @return Boolean value for whether the string contains more Unicode code points
1806 * than 'number'. Same as (u_countChar32(s, length)>number).
1808 * @see u_strHasMoreChar32Than
1812 hasMoreChar32Than(int32_t start
, int32_t length
, int32_t number
) const;
1815 * Determine if this string is empty.
1816 * @return TRUE if this string contains 0 characters, FALSE otherwise.
1819 inline UBool
isEmpty(void) const;
1822 * Return the capacity of the internal buffer of the UnicodeString object.
1823 * This is useful together with the getBuffer functions.
1824 * See there for details.
1826 * @return the number of char16_ts available in the internal buffer
1830 inline int32_t getCapacity(void) const;
1832 /* Other operations */
1835 * Generate a hash code for this object.
1836 * @return The hash code of this UnicodeString.
1839 inline int32_t hashCode(void) const;
1842 * Determine if this object contains a valid string.
1843 * A bogus string has no value. It is different from an empty string,
1844 * although in both cases isEmpty() returns TRUE and length() returns 0.
1845 * setToBogus() and isBogus() can be used to indicate that no string value is available.
1846 * For a bogus string, getBuffer() and getTerminatedBuffer() return NULL, and
1847 * length() returns 0.
1849 * @return TRUE if the string is bogus/invalid, FALSE otherwise
1853 inline UBool
isBogus(void) const;
1856 //========================================
1858 //========================================
1860 /* Assignment operations */
1863 * Assignment operator. Replace the characters in this UnicodeString
1864 * with the characters from <TT>srcText</TT>.
1866 * Starting with ICU 2.4, the assignment operator and the copy constructor
1867 * allocate a new buffer and copy the buffer contents even for readonly aliases.
1868 * By contrast, the fastCopyFrom() function implements the old,
1869 * more efficient but less safe behavior
1870 * of making this string also a readonly alias to the same buffer.
1872 * If the source object has an "open" buffer from getBuffer(minCapacity),
1873 * then the copy is an empty string.
1875 * @param srcText The text containing the characters to replace
1876 * @return a reference to this
1880 UnicodeString
&operator=(const UnicodeString
&srcText
);
1883 * Almost the same as the assignment operator.
1884 * Replace the characters in this UnicodeString
1885 * with the characters from <code>srcText</code>.
1887 * This function works the same as the assignment operator
1888 * for all strings except for ones that are readonly aliases.
1890 * Starting with ICU 2.4, the assignment operator and the copy constructor
1891 * allocate a new buffer and copy the buffer contents even for readonly aliases.
1892 * This function implements the old, more efficient but less safe behavior
1893 * of making this string also a readonly alias to the same buffer.
1895 * The fastCopyFrom function must be used only if it is known that the lifetime of
1896 * this UnicodeString does not exceed the lifetime of the aliased buffer
1897 * including its contents, for example for strings from resource bundles
1898 * or aliases to string constants.
1900 * If the source object has an "open" buffer from getBuffer(minCapacity),
1901 * then the copy is an empty string.
1903 * @param src The text containing the characters to replace.
1904 * @return a reference to this
1907 UnicodeString
&fastCopyFrom(const UnicodeString
&src
);
1909 #if U_HAVE_RVALUE_REFERENCES
1911 * Move assignment operator, might leave src in bogus state.
1912 * This string will have the same contents and state that the source string had.
1913 * The behavior is undefined if *this and src are the same object.
1914 * @param src source string
1918 UnicodeString
&operator=(UnicodeString
&&src
) U_NOEXCEPT
{
1919 return moveFrom(src
);
1922 // do not use #ifndef U_HIDE_DRAFT_API for moveFrom, needed by non-draft API
1924 * Move assignment, might leave src in bogus state.
1925 * This string will have the same contents and state that the source string had.
1926 * The behavior is undefined if *this and src are the same object.
1928 * Can be called explicitly, does not need C++11 support.
1929 * @param src source string
1933 UnicodeString
&moveFrom(UnicodeString
&src
) U_NOEXCEPT
;
1937 * @param other other string
1940 void swap(UnicodeString
&other
) U_NOEXCEPT
;
1943 * Non-member UnicodeString swap function.
1944 * @param s1 will get s2's contents and state
1945 * @param s2 will get s1's contents and state
1948 friend U_COMMON_API
inline void U_EXPORT2
1949 swap(UnicodeString
&s1
, UnicodeString
&s2
) U_NOEXCEPT
{
1954 * Assignment operator. Replace the characters in this UnicodeString
1955 * with the code unit <TT>ch</TT>.
1956 * @param ch the code unit to replace
1957 * @return a reference to this
1960 inline UnicodeString
& operator= (char16_t ch
);
1963 * Assignment operator. Replace the characters in this UnicodeString
1964 * with the code point <TT>ch</TT>.
1965 * @param ch the code point to replace
1966 * @return a reference to this
1969 inline UnicodeString
& operator= (UChar32 ch
);
1972 * Set the text in the UnicodeString object to the characters
1973 * in <TT>srcText</TT> in the range
1974 * [<TT>srcStart</TT>, <TT>srcText.length()</TT>).
1975 * <TT>srcText</TT> is not modified.
1976 * @param srcText the source for the new characters
1977 * @param srcStart the offset into <TT>srcText</TT> where new characters
1979 * @return a reference to this
1982 inline UnicodeString
& setTo(const UnicodeString
& srcText
,
1986 * Set the text in the UnicodeString object to the characters
1987 * in <TT>srcText</TT> in the range
1988 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
1989 * <TT>srcText</TT> is not modified.
1990 * @param srcText the source for the new characters
1991 * @param srcStart the offset into <TT>srcText</TT> where new characters
1993 * @param srcLength the number of characters in <TT>srcText</TT> in the
1995 * @return a reference to this
1998 inline UnicodeString
& setTo(const UnicodeString
& srcText
,
2003 * Set the text in the UnicodeString object to the characters in
2005 * <TT>srcText</TT> is not modified.
2006 * @param srcText the source for the new characters
2007 * @return a reference to this
2010 inline UnicodeString
& setTo(const UnicodeString
& srcText
);
2013 * Set the characters in the UnicodeString object to the characters
2014 * in <TT>srcChars</TT>. <TT>srcChars</TT> is not modified.
2015 * @param srcChars the source for the new characters
2016 * @param srcLength the number of Unicode characters in srcChars.
2017 * @return a reference to this
2020 inline UnicodeString
& setTo(const char16_t *srcChars
,
2024 * Set the characters in the UnicodeString object to the code unit
2026 * @param srcChar the code unit which becomes the UnicodeString's character
2028 * @return a reference to this
2031 UnicodeString
& setTo(char16_t srcChar
);
2034 * Set the characters in the UnicodeString object to the code point
2036 * @param srcChar the code point which becomes the UnicodeString's character
2038 * @return a reference to this
2041 UnicodeString
& setTo(UChar32 srcChar
);
2044 * Aliasing setTo() function, analogous to the readonly-aliasing char16_t* constructor.
2045 * The text will be used for the UnicodeString object, but
2046 * it will not be released when the UnicodeString is destroyed.
2047 * This has copy-on-write semantics:
2048 * When the string is modified, then the buffer is first copied into
2049 * newly allocated memory.
2050 * The aliased buffer is never modified.
2052 * In an assignment to another UnicodeString, when using the copy constructor
2053 * or the assignment operator, the text will be copied.
2054 * When using fastCopyFrom(), the text will be aliased again,
2055 * so that both strings then alias the same readonly-text.
2057 * @param isTerminated specifies if <code>text</code> is <code>NUL</code>-terminated.
2058 * This must be true if <code>textLength==-1</code>.
2059 * @param text The characters to alias for the UnicodeString.
2060 * @param textLength The number of Unicode characters in <code>text</code> to alias.
2061 * If -1, then this constructor will determine the length
2062 * by calling <code>u_strlen()</code>.
2063 * @return a reference to this
2066 UnicodeString
&setTo(UBool isTerminated
,
2067 ConstChar16Ptr text
,
2068 int32_t textLength
);
2071 * Aliasing setTo() function, analogous to the writable-aliasing char16_t* constructor.
2072 * The text will be used for the UnicodeString object, but
2073 * it will not be released when the UnicodeString is destroyed.
2074 * This has write-through semantics:
2075 * For as long as the capacity of the buffer is sufficient, write operations
2076 * will directly affect the buffer. When more capacity is necessary, then
2077 * a new buffer will be allocated and the contents copied as with regularly
2078 * constructed strings.
2079 * In an assignment to another UnicodeString, the buffer will be copied.
2080 * The extract(Char16Ptr dst) function detects whether the dst pointer is the same
2081 * as the string buffer itself and will in this case not copy the contents.
2083 * @param buffer The characters to alias for the UnicodeString.
2084 * @param buffLength The number of Unicode characters in <code>buffer</code> to alias.
2085 * @param buffCapacity The size of <code>buffer</code> in char16_ts.
2086 * @return a reference to this
2089 UnicodeString
&setTo(char16_t *buffer
,
2091 int32_t buffCapacity
);
2094 * Make this UnicodeString object invalid.
2095 * The string will test TRUE with isBogus().
2097 * A bogus string has no value. It is different from an empty string.
2098 * It can be used to indicate that no string value is available.
2099 * getBuffer() and getTerminatedBuffer() return NULL, and
2100 * length() returns 0.
2102 * This utility function is used throughout the UnicodeString
2103 * implementation to indicate that a UnicodeString operation failed,
2104 * and may be used in other functions,
2105 * especially but not exclusively when such functions do not
2106 * take a UErrorCode for simplicity.
2108 * The following methods, and no others, will clear a string object's bogus flag:
2110 * - remove(0, INT32_MAX)
2112 * - operator=() (assignment operator)
2115 * The simplest ways to turn a bogus string into an empty one
2116 * is to use the remove() function.
2117 * Examples for other functions that are equivalent to "set to empty string":
2120 * s.remove(); // set to an empty string (remove all), or
2121 * s.remove(0, INT32_MAX); // set to an empty string (remove all), or
2122 * s.truncate(0); // set to an empty string (complete truncation), or
2123 * s=UnicodeString(); // assign an empty string, or
2124 * s.setTo((UChar32)-1); // set to a pseudo code point that is out of range, or
2125 * static const char16_t nul=0;
2126 * s.setTo(&nul, 0); // set to an empty C Unicode string
2136 * Set the character at the specified offset to the specified character.
2137 * @param offset A valid offset into the text of the character to set
2138 * @param ch The new character
2139 * @return A reference to this
2142 UnicodeString
& setCharAt(int32_t offset
,
2146 /* Append operations */
2149 * Append operator. Append the code unit <TT>ch</TT> to the UnicodeString
2151 * @param ch the code unit to be appended
2152 * @return a reference to this
2155 inline UnicodeString
& operator+= (char16_t ch
);
2158 * Append operator. Append the code point <TT>ch</TT> to the UnicodeString
2160 * @param ch the code point to be appended
2161 * @return a reference to this
2164 inline UnicodeString
& operator+= (UChar32 ch
);
2167 * Append operator. Append the characters in <TT>srcText</TT> to the
2168 * UnicodeString object. <TT>srcText</TT> is not modified.
2169 * @param srcText the source for the new characters
2170 * @return a reference to this
2173 inline UnicodeString
& operator+= (const UnicodeString
& srcText
);
2176 * Append the characters
2177 * in <TT>srcText</TT> in the range
2178 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) to the
2179 * UnicodeString object at offset <TT>start</TT>. <TT>srcText</TT>
2181 * @param srcText the source for the new characters
2182 * @param srcStart the offset into <TT>srcText</TT> where new characters
2184 * @param srcLength the number of characters in <TT>srcText</TT> in
2186 * @return a reference to this
2189 inline UnicodeString
& append(const UnicodeString
& srcText
,
2194 * Append the characters in <TT>srcText</TT> to the UnicodeString object.
2195 * <TT>srcText</TT> is not modified.
2196 * @param srcText the source for the new characters
2197 * @return a reference to this
2200 inline UnicodeString
& append(const UnicodeString
& srcText
);
2203 * Append the characters in <TT>srcChars</TT> in the range
2204 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) to the UnicodeString
2206 * <TT>start</TT>. <TT>srcChars</TT> is not modified.
2207 * @param srcChars the source for the new characters
2208 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2210 * @param srcLength the number of characters in <TT>srcChars</TT> in
2211 * the append string; can be -1 if <TT>srcChars</TT> is NUL-terminated
2212 * @return a reference to this
2215 inline UnicodeString
& append(const char16_t *srcChars
,
2220 * Append the characters in <TT>srcChars</TT> to the UnicodeString object
2221 * at offset <TT>start</TT>. <TT>srcChars</TT> is not modified.
2222 * @param srcChars the source for the new characters
2223 * @param srcLength the number of Unicode characters in <TT>srcChars</TT>;
2224 * can be -1 if <TT>srcChars</TT> is NUL-terminated
2225 * @return a reference to this
2228 inline UnicodeString
& append(ConstChar16Ptr srcChars
,
2232 * Append the code unit <TT>srcChar</TT> to the UnicodeString object.
2233 * @param srcChar the code unit to append
2234 * @return a reference to this
2237 inline UnicodeString
& append(char16_t srcChar
);
2240 * Append the code point <TT>srcChar</TT> to the UnicodeString object.
2241 * @param srcChar the code point to append
2242 * @return a reference to this
2245 UnicodeString
& append(UChar32 srcChar
);
2248 /* Insert operations */
2251 * Insert the characters in <TT>srcText</TT> in the range
2252 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) into the UnicodeString
2253 * object at offset <TT>start</TT>. <TT>srcText</TT> is not modified.
2254 * @param start the offset where the insertion begins
2255 * @param srcText the source for the new characters
2256 * @param srcStart the offset into <TT>srcText</TT> where new characters
2258 * @param srcLength the number of characters in <TT>srcText</TT> in
2260 * @return a reference to this
2263 inline UnicodeString
& insert(int32_t start
,
2264 const UnicodeString
& srcText
,
2269 * Insert the characters in <TT>srcText</TT> into the UnicodeString object
2270 * at offset <TT>start</TT>. <TT>srcText</TT> is not modified.
2271 * @param start the offset where the insertion begins
2272 * @param srcText the source for the new characters
2273 * @return a reference to this
2276 inline UnicodeString
& insert(int32_t start
,
2277 const UnicodeString
& srcText
);
2280 * Insert the characters in <TT>srcChars</TT> in the range
2281 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>) into the UnicodeString
2282 * object at offset <TT>start</TT>. <TT>srcChars</TT> is not modified.
2283 * @param start the offset at which the insertion begins
2284 * @param srcChars the source for the new characters
2285 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2287 * @param srcLength the number of characters in <TT>srcChars</TT>
2288 * in the insert string
2289 * @return a reference to this
2292 inline UnicodeString
& insert(int32_t start
,
2293 const char16_t *srcChars
,
2298 * Insert the characters in <TT>srcChars</TT> into the UnicodeString object
2299 * at offset <TT>start</TT>. <TT>srcChars</TT> is not modified.
2300 * @param start the offset where the insertion begins
2301 * @param srcChars the source for the new characters
2302 * @param srcLength the number of Unicode characters in srcChars.
2303 * @return a reference to this
2306 inline UnicodeString
& insert(int32_t start
,
2307 ConstChar16Ptr srcChars
,
2311 * Insert the code unit <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 unit to insert
2315 * @return a reference to this
2318 inline UnicodeString
& insert(int32_t start
,
2322 * Insert the code point <TT>srcChar</TT> into the UnicodeString object at
2323 * offset <TT>start</TT>.
2324 * @param start the offset at which the insertion occurs
2325 * @param srcChar the code point to insert
2326 * @return a reference to this
2329 inline UnicodeString
& insert(int32_t start
,
2333 /* Replace operations */
2336 * Replace the characters in the range
2337 * [<TT>start</TT>, <TT>start + length</TT>) with the characters in
2338 * <TT>srcText</TT> in the range
2339 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>).
2340 * <TT>srcText</TT> is not modified.
2341 * @param start the offset at which the replace operation begins
2342 * @param length the number of characters to replace. The character at
2343 * <TT>start + length</TT> is not modified.
2344 * @param srcText the source for the new characters
2345 * @param srcStart the offset into <TT>srcText</TT> where new characters
2347 * @param srcLength the number of characters in <TT>srcText</TT> in
2348 * the replace string
2349 * @return a reference to this
2352 UnicodeString
& replace(int32_t start
,
2354 const UnicodeString
& srcText
,
2359 * Replace the characters in the range
2360 * [<TT>start</TT>, <TT>start + length</TT>)
2361 * with the characters in <TT>srcText</TT>. <TT>srcText</TT> is
2363 * @param start the offset at which the replace operation begins
2364 * @param length the number of characters to replace. The character at
2365 * <TT>start + length</TT> is not modified.
2366 * @param srcText the source for the new characters
2367 * @return a reference to this
2370 UnicodeString
& replace(int32_t start
,
2372 const UnicodeString
& srcText
);
2375 * Replace the characters in the range
2376 * [<TT>start</TT>, <TT>start + length</TT>) with the characters in
2377 * <TT>srcChars</TT> in the range
2378 * [<TT>srcStart</TT>, <TT>srcStart + srcLength</TT>). <TT>srcChars</TT>
2380 * @param start the offset at which the replace operation begins
2381 * @param length the number of characters to replace. The character at
2382 * <TT>start + length</TT> is not modified.
2383 * @param srcChars the source for the new characters
2384 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2386 * @param srcLength the number of characters in <TT>srcChars</TT>
2387 * in the replace string
2388 * @return a reference to this
2391 UnicodeString
& replace(int32_t start
,
2393 const char16_t *srcChars
,
2398 * Replace the characters in the range
2399 * [<TT>start</TT>, <TT>start + length</TT>) with the characters in
2400 * <TT>srcChars</TT>. <TT>srcChars</TT> is not modified.
2401 * @param start the offset at which the replace operation begins
2402 * @param length number of characters to replace. The character at
2403 * <TT>start + length</TT> is not modified.
2404 * @param srcChars the source for the new characters
2405 * @param srcLength the number of Unicode characters in srcChars
2406 * @return a reference to this
2409 inline UnicodeString
& replace(int32_t start
,
2411 ConstChar16Ptr srcChars
,
2415 * Replace the characters in the range
2416 * [<TT>start</TT>, <TT>start + length</TT>) with the code unit
2418 * @param start the offset at which the replace operation begins
2419 * @param length the number of characters to replace. The character at
2420 * <TT>start + length</TT> is not modified.
2421 * @param srcChar the new code unit
2422 * @return a reference to this
2425 inline UnicodeString
& replace(int32_t start
,
2430 * Replace the characters in the range
2431 * [<TT>start</TT>, <TT>start + length</TT>) with the code point
2433 * @param start the offset at which the replace operation begins
2434 * @param length the number of characters to replace. The character at
2435 * <TT>start + length</TT> is not modified.
2436 * @param srcChar the new code point
2437 * @return a reference to this
2440 UnicodeString
& replace(int32_t start
, int32_t length
, UChar32 srcChar
);
2443 * Replace the characters in the range [<TT>start</TT>, <TT>limit</TT>)
2444 * with the characters in <TT>srcText</TT>. <TT>srcText</TT> is not modified.
2445 * @param start the offset at which the replace operation begins
2446 * @param limit the offset immediately following the replace range
2447 * @param srcText the source for the new characters
2448 * @return a reference to this
2451 inline UnicodeString
& replaceBetween(int32_t start
,
2453 const UnicodeString
& srcText
);
2456 * Replace the characters in the range [<TT>start</TT>, <TT>limit</TT>)
2457 * with the characters in <TT>srcText</TT> in the range
2458 * [<TT>srcStart</TT>, <TT>srcLimit</TT>). <TT>srcText</TT> is not modified.
2459 * @param start the offset at which the replace operation begins
2460 * @param limit the offset immediately following the replace range
2461 * @param srcText the source for the new characters
2462 * @param srcStart the offset into <TT>srcChars</TT> where new characters
2464 * @param srcLimit the offset immediately following the range to copy
2465 * in <TT>srcText</TT>
2466 * @return a reference to this
2469 inline UnicodeString
& replaceBetween(int32_t start
,
2471 const UnicodeString
& srcText
,
2476 * Replace a substring of this object with the given text.
2477 * @param start the beginning index, inclusive; <code>0 <= start
2479 * @param limit the ending index, exclusive; <code>start <= limit
2480 * <= length()</code>.
2481 * @param text the text to replace characters <code>start</code>
2482 * to <code>limit - 1</code>
2485 virtual void handleReplaceBetween(int32_t start
,
2487 const UnicodeString
& text
);
2491 * @return TRUE if it has MetaData
2494 virtual UBool
hasMetaData() const;
2497 * Copy a substring of this object, retaining attribute (out-of-band)
2498 * information. This method is used to duplicate or reorder substrings.
2499 * The destination index must not overlap the source range.
2501 * @param start the beginning index, inclusive; <code>0 <= start <=
2503 * @param limit the ending index, exclusive; <code>start <= limit <=
2505 * @param dest the destination index. The characters from
2506 * <code>start..limit-1</code> will be copied to <code>dest</code>.
2507 * Implementations of this method may assume that <code>dest <= start ||
2508 * dest >= limit</code>.
2511 virtual void copy(int32_t start
, int32_t limit
, int32_t dest
);
2513 /* Search and replace operations */
2516 * Replace all occurrences of characters in oldText with the characters
2518 * @param oldText the text containing the search text
2519 * @param newText the text containing the replacement text
2520 * @return a reference to this
2523 inline UnicodeString
& findAndReplace(const UnicodeString
& oldText
,
2524 const UnicodeString
& newText
);
2527 * Replace all occurrences of characters in oldText with characters
2529 * in the range [<TT>start</TT>, <TT>start + length</TT>).
2530 * @param start the start of the range in which replace will performed
2531 * @param length the length of the range in which replace will be performed
2532 * @param oldText the text containing the search text
2533 * @param newText the text containing the replacement text
2534 * @return a reference to this
2537 inline UnicodeString
& findAndReplace(int32_t start
,
2539 const UnicodeString
& oldText
,
2540 const UnicodeString
& newText
);
2543 * Replace all occurrences of characters in oldText in the range
2544 * [<TT>oldStart</TT>, <TT>oldStart + oldLength</TT>) with the characters
2545 * in newText in the range
2546 * [<TT>newStart</TT>, <TT>newStart + newLength</TT>)
2547 * in the range [<TT>start</TT>, <TT>start + length</TT>).
2548 * @param start the start of the range in which replace will performed
2549 * @param length the length of the range in which replace will be performed
2550 * @param oldText the text containing the search text
2551 * @param oldStart the start of the search range in <TT>oldText</TT>
2552 * @param oldLength the length of the search range in <TT>oldText</TT>
2553 * @param newText the text containing the replacement text
2554 * @param newStart the start of the replacement range in <TT>newText</TT>
2555 * @param newLength the length of the replacement range in <TT>newText</TT>
2556 * @return a reference to this
2559 UnicodeString
& findAndReplace(int32_t start
,
2561 const UnicodeString
& oldText
,
2564 const UnicodeString
& newText
,
2569 /* Remove operations */
2572 * Remove all characters from the UnicodeString object.
2573 * @return a reference to this
2576 inline UnicodeString
& remove(void);
2579 * Remove the characters in the range
2580 * [<TT>start</TT>, <TT>start + length</TT>) from the UnicodeString object.
2581 * @param start the offset of the first character to remove
2582 * @param length the number of characters to remove
2583 * @return a reference to this
2586 inline UnicodeString
& remove(int32_t start
,
2587 int32_t length
= (int32_t)INT32_MAX
);
2590 * Remove the characters in the range
2591 * [<TT>start</TT>, <TT>limit</TT>) from the UnicodeString object.
2592 * @param start the offset of the first character to remove
2593 * @param limit the offset immediately following the range to remove
2594 * @return a reference to this
2597 inline UnicodeString
& removeBetween(int32_t start
,
2598 int32_t limit
= (int32_t)INT32_MAX
);
2601 * Retain only the characters in the range
2602 * [<code>start</code>, <code>limit</code>) from the UnicodeString object.
2603 * Removes characters before <code>start</code> and at and after <code>limit</code>.
2604 * @param start the offset of the first character to retain
2605 * @param limit the offset immediately following the range to retain
2606 * @return a reference to this
2609 inline UnicodeString
&retainBetween(int32_t start
, int32_t limit
= INT32_MAX
);
2611 /* Length operations */
2614 * Pad the start of this UnicodeString with the character <TT>padChar</TT>.
2615 * If the length of this UnicodeString is less than targetLength,
2616 * length() - targetLength copies of padChar will be added to the
2617 * beginning of this UnicodeString.
2618 * @param targetLength the desired length of the string
2619 * @param padChar the character to use for padding. Defaults to
2621 * @return TRUE if the text was padded, FALSE otherwise.
2624 UBool
padLeading(int32_t targetLength
,
2625 char16_t padChar
= 0x0020);
2628 * Pad the end of this UnicodeString with the character <TT>padChar</TT>.
2629 * If the length of this UnicodeString is less than targetLength,
2630 * length() - targetLength copies of padChar will be added to the
2631 * end of this UnicodeString.
2632 * @param targetLength the desired length of the string
2633 * @param padChar the character to use for padding. Defaults to
2635 * @return TRUE if the text was padded, FALSE otherwise.
2638 UBool
padTrailing(int32_t targetLength
,
2639 char16_t padChar
= 0x0020);
2642 * Truncate this UnicodeString to the <TT>targetLength</TT>.
2643 * @param targetLength the desired length of this UnicodeString.
2644 * @return TRUE if the text was truncated, FALSE otherwise
2647 inline UBool
truncate(int32_t targetLength
);
2650 * Trims leading and trailing whitespace from this UnicodeString.
2651 * @return a reference to this
2654 UnicodeString
& trim(void);
2657 /* Miscellaneous operations */
2660 * Reverse this UnicodeString in place.
2661 * @return a reference to this
2664 inline UnicodeString
& reverse(void);
2667 * Reverse the range [<TT>start</TT>, <TT>start + length</TT>) in
2668 * this UnicodeString.
2669 * @param start the start of the range to reverse
2670 * @param length the number of characters to to reverse
2671 * @return a reference to this
2674 inline UnicodeString
& reverse(int32_t start
,
2678 * Convert the characters in this to UPPER CASE following the conventions of
2679 * the default locale.
2680 * @return A reference to this.
2683 UnicodeString
& toUpper(void);
2686 * Convert the characters in this to UPPER CASE following the conventions of
2687 * a specific locale.
2688 * @param locale The locale containing the conventions to use.
2689 * @return A reference to this.
2692 UnicodeString
& toUpper(const Locale
& locale
);
2695 * Convert the characters in this to lower case following the conventions of
2696 * the default locale.
2697 * @return A reference to this.
2700 UnicodeString
& toLower(void);
2703 * Convert the characters in this to lower case following the conventions of
2704 * a specific locale.
2705 * @param locale The locale containing the conventions to use.
2706 * @return A reference to this.
2709 UnicodeString
& toLower(const Locale
& locale
);
2711 #if !UCONFIG_NO_BREAK_ITERATION
2714 * Titlecase this string, convenience function using the default locale.
2716 * Casing is locale-dependent and context-sensitive.
2717 * Titlecasing uses a break iterator to find the first characters of words
2718 * that are to be titlecased. It titlecases those characters and lowercases
2721 * The titlecase break iterator can be provided to customize for arbitrary
2722 * styles, using rules and dictionaries beyond the standard iterators.
2723 * It may be more efficient to always provide an iterator to avoid
2724 * opening and closing one for each string.
2725 * The standard titlecase iterator for the root locale implements the
2726 * algorithm of Unicode TR 21.
2728 * This function uses only the setText(), first() and next() methods of the
2729 * provided break iterator.
2731 * @param titleIter A break iterator to find the first characters of words
2732 * that are to be titlecased.
2733 * If none is provided (0), then a standard titlecase
2734 * break iterator is opened.
2735 * Otherwise the provided iterator is set to the string's text.
2736 * @return A reference to this.
2739 UnicodeString
&toTitle(BreakIterator
*titleIter
);
2742 * Titlecase this string.
2744 * Casing is locale-dependent and context-sensitive.
2745 * Titlecasing uses a break iterator to find the first characters of words
2746 * that are to be titlecased. It titlecases those characters and lowercases
2749 * The titlecase break iterator can be provided to customize for arbitrary
2750 * styles, using rules and dictionaries beyond the standard iterators.
2751 * It may be more efficient to always provide an iterator to avoid
2752 * opening and closing one for each string.
2753 * The standard titlecase iterator for the root locale implements the
2754 * algorithm of Unicode TR 21.
2756 * This function uses only the setText(), first() and next() methods of the
2757 * provided break iterator.
2759 * @param titleIter A break iterator to find the first characters of words
2760 * that are to be titlecased.
2761 * If none is provided (0), then a standard titlecase
2762 * break iterator is opened.
2763 * Otherwise the provided iterator is set to the string's text.
2764 * @param locale The locale to consider.
2765 * @return A reference to this.
2768 UnicodeString
&toTitle(BreakIterator
*titleIter
, const Locale
&locale
);
2771 * Titlecase this string, with options.
2773 * Casing is locale-dependent and context-sensitive.
2774 * Titlecasing uses a break iterator to find the first characters of words
2775 * that are to be titlecased. It titlecases those characters and lowercases
2776 * all others. (This can be modified with options.)
2778 * The titlecase break iterator can be provided to customize for arbitrary
2779 * styles, using rules and dictionaries beyond the standard iterators.
2780 * It may be more efficient to always provide an iterator to avoid
2781 * opening and closing one for each string.
2782 * The standard titlecase iterator for the root locale implements the
2783 * algorithm of Unicode TR 21.
2785 * This function uses only the setText(), first() and next() methods of the
2786 * provided break iterator.
2788 * @param titleIter A break iterator to find the first characters of words
2789 * that are to be titlecased.
2790 * If none is provided (0), then a standard titlecase
2791 * break iterator is opened.
2792 * Otherwise the provided iterator is set to the string's text.
2793 * @param locale The locale to consider.
2794 * @param options Options bit set, see ucasemap_open().
2795 * @return A reference to this.
2796 * @see U_TITLECASE_NO_LOWERCASE
2797 * @see U_TITLECASE_NO_BREAK_ADJUSTMENT
2798 * @see ucasemap_open
2801 UnicodeString
&toTitle(BreakIterator
*titleIter
, const Locale
&locale
, uint32_t options
);
2806 * Case-folds the characters in this string.
2808 * Case-folding is locale-independent and not context-sensitive,
2809 * but there is an option for whether to include or exclude mappings for dotted I
2810 * and dotless i that are marked with 'T' in CaseFolding.txt.
2812 * The result may be longer or shorter than the original.
2814 * @param options Either U_FOLD_CASE_DEFAULT or U_FOLD_CASE_EXCLUDE_SPECIAL_I
2815 * @return A reference to this.
2818 UnicodeString
&foldCase(uint32_t options
=0 /*U_FOLD_CASE_DEFAULT*/);
2820 //========================================
2821 // Access to the internal buffer
2822 //========================================
2825 * Get a read/write pointer to the internal buffer.
2826 * The buffer is guaranteed to be large enough for at least minCapacity char16_ts,
2827 * writable, and is still owned by the UnicodeString object.
2828 * Calls to getBuffer(minCapacity) must not be nested, and
2829 * must be matched with calls to releaseBuffer(newLength).
2830 * If the string buffer was read-only or shared,
2831 * then it will be reallocated and copied.
2833 * An attempted nested call will return 0, and will not further modify the
2834 * state of the UnicodeString object.
2835 * It also returns 0 if the string is bogus.
2837 * The actual capacity of the string buffer may be larger than minCapacity.
2838 * getCapacity() returns the actual capacity.
2839 * For many operations, the full capacity should be used to avoid reallocations.
2841 * While the buffer is "open" between getBuffer(minCapacity)
2842 * and releaseBuffer(newLength), the following applies:
2843 * - The string length is set to 0.
2844 * - Any read API call on the UnicodeString object will behave like on a 0-length string.
2845 * - Any write API call on the UnicodeString object is disallowed and will have no effect.
2846 * - You can read from and write to the returned buffer.
2847 * - The previous string contents will still be in the buffer;
2848 * if you want to use it, then you need to call length() before getBuffer(minCapacity).
2849 * If the length() was greater than minCapacity, then any contents after minCapacity
2851 * The buffer contents is not NUL-terminated by getBuffer().
2852 * If length()<getCapacity() then you can terminate it by writing a NUL
2853 * at index length().
2854 * - You must call releaseBuffer(newLength) before and in order to
2855 * return to normal UnicodeString operation.
2857 * @param minCapacity the minimum number of char16_ts that are to be available
2858 * in the buffer, starting at the returned pointer;
2859 * default to the current string capacity if minCapacity==-1
2860 * @return a writable pointer to the internal string buffer,
2861 * or nullptr if an error occurs (nested calls, out of memory)
2863 * @see releaseBuffer
2864 * @see getTerminatedBuffer()
2867 char16_t *getBuffer(int32_t minCapacity
);
2870 * Release a read/write buffer on a UnicodeString object with an
2871 * "open" getBuffer(minCapacity).
2872 * This function must be called in a matched pair with getBuffer(minCapacity).
2873 * releaseBuffer(newLength) must be called if and only if a getBuffer(minCapacity) is "open".
2875 * It will set the string length to newLength, at most to the current capacity.
2876 * If newLength==-1 then it will set the length according to the
2877 * first NUL in the buffer, or to the capacity if there is no NUL.
2879 * After calling releaseBuffer(newLength) the UnicodeString is back to normal operation.
2881 * @param newLength the new length of the UnicodeString object;
2882 * defaults to the current capacity if newLength is greater than that;
2883 * if newLength==-1, it defaults to u_strlen(buffer) but not more than
2884 * the current capacity of the string
2886 * @see getBuffer(int32_t minCapacity)
2889 void releaseBuffer(int32_t newLength
=-1);
2892 * Get a read-only pointer to the internal buffer.
2893 * This can be called at any time on a valid UnicodeString.
2895 * It returns 0 if the string is bogus, or
2896 * during an "open" getBuffer(minCapacity).
2898 * It can be called as many times as desired.
2899 * The pointer that it returns will remain valid until the UnicodeString object is modified,
2900 * at which time the pointer is semantically invalidated and must not be used any more.
2902 * The capacity of the buffer can be determined with getCapacity().
2903 * The part after length() may or may not be initialized and valid,
2904 * depending on the history of the UnicodeString object.
2906 * The buffer contents is (probably) not NUL-terminated.
2907 * You can check if it is with
2908 * <code>(s.length()<s.getCapacity() && buffer[s.length()]==0)</code>.
2909 * (See getTerminatedBuffer().)
2911 * The buffer may reside in read-only memory. Its contents must not
2914 * @return a read-only pointer to the internal string buffer,
2915 * or nullptr if the string is empty or bogus
2917 * @see getBuffer(int32_t minCapacity)
2918 * @see getTerminatedBuffer()
2921 inline const char16_t *getBuffer() const;
2924 * Get a read-only pointer to the internal buffer,
2925 * making sure that it is NUL-terminated.
2926 * This can be called at any time on a valid UnicodeString.
2928 * It returns 0 if the string is bogus, or
2929 * during an "open" getBuffer(minCapacity), or if the buffer cannot
2930 * be NUL-terminated (because memory allocation failed).
2932 * It can be called as many times as desired.
2933 * The pointer that it returns will remain valid until the UnicodeString object is modified,
2934 * at which time the pointer is semantically invalidated and must not be used any more.
2936 * The capacity of the buffer can be determined with getCapacity().
2937 * The part after length()+1 may or may not be initialized and valid,
2938 * depending on the history of the UnicodeString object.
2940 * The buffer contents is guaranteed to be NUL-terminated.
2941 * getTerminatedBuffer() may reallocate the buffer if a terminating NUL
2943 * For this reason, this function is not const, unlike getBuffer().
2944 * Note that a UnicodeString may also contain NUL characters as part of its contents.
2946 * The buffer may reside in read-only memory. Its contents must not
2949 * @return a read-only pointer to the internal string buffer,
2950 * or 0 if the string is empty or bogus
2952 * @see getBuffer(int32_t minCapacity)
2956 const char16_t *getTerminatedBuffer();
2958 //========================================
2960 //========================================
2962 /** Construct an empty UnicodeString.
2965 inline UnicodeString();
2968 * Construct a UnicodeString with capacity to hold <TT>capacity</TT> char16_ts
2969 * @param capacity the number of char16_ts this UnicodeString should hold
2970 * before a resize is necessary; if count is greater than 0 and count
2971 * code points c take up more space than capacity, then capacity is adjusted
2973 * @param c is used to initially fill the string
2974 * @param count specifies how many code points c are to be written in the
2978 UnicodeString(int32_t capacity
, UChar32 c
, int32_t count
);
2981 * Single char16_t (code unit) 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(char16_t ch
);
2992 * Single UChar32 (code point) constructor.
2994 * It is recommended to mark this constructor "explicit" by
2995 * <code>-DUNISTR_FROM_CHAR_EXPLICIT=explicit</code>
2996 * on the compiler command line or similar.
2997 * @param ch the character to place in the UnicodeString
3000 UNISTR_FROM_CHAR_EXPLICIT
UnicodeString(UChar32 ch
);
3003 * char16_t* constructor.
3005 * It is recommended to mark this constructor "explicit" by
3006 * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code>
3007 * on the compiler command line or similar.
3008 * @param text The characters to place in the UnicodeString. <TT>text</TT>
3009 * must be NULL (U+0000) terminated.
3012 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const char16_t *text
);
3015 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3016 * it should always be available regardless of U_HIDE_DRAFT_API status
3018 #if !U_CHAR16_IS_TYPEDEF
3020 * uint16_t * constructor.
3021 * Delegates to UnicodeString(const char16_t *).
3023 * It is recommended to mark this constructor "explicit" by
3024 * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code>
3025 * on the compiler command line or similar.
3026 * @param text NUL-terminated UTF-16 string
3029 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const uint16_t *text
) :
3030 UnicodeString(ConstChar16Ptr(text
)) {}
3034 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3035 * it should always be available regardless of U_HIDE_DRAFT_API status
3037 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3039 * wchar_t * constructor.
3040 * (Only defined if U_SIZEOF_WCHAR_T==2.)
3041 * Delegates to UnicodeString(const char16_t *).
3043 * It is recommended to mark this constructor "explicit" by
3044 * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code>
3045 * on the compiler command line or similar.
3046 * @param text NUL-terminated UTF-16 string
3049 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const wchar_t *text
) :
3050 UnicodeString(ConstChar16Ptr(text
)) {}
3054 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3055 * it should always be available regardless of U_HIDE_DRAFT_API status
3058 * nullptr_t constructor.
3059 * Effectively the same as the default constructor, makes an empty string object.
3061 * It is recommended to mark this constructor "explicit" by
3062 * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code>
3063 * on the compiler command line or similar.
3064 * @param text nullptr
3067 UNISTR_FROM_STRING_EXPLICIT
inline UnicodeString(const std::nullptr_t text
);
3070 * char16_t* constructor.
3071 * @param text The characters to place in the UnicodeString.
3072 * @param textLength The number of Unicode characters in <TT>text</TT>
3076 UnicodeString(const char16_t *text
,
3077 int32_t textLength
);
3080 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3081 * it should always be available regardless of U_HIDE_DRAFT_API status
3083 #if !U_CHAR16_IS_TYPEDEF
3085 * uint16_t * constructor.
3086 * Delegates to UnicodeString(const char16_t *, int32_t).
3087 * @param text UTF-16 string
3088 * @param length string length
3091 UnicodeString(const uint16_t *text
, int32_t length
) :
3092 UnicodeString(ConstChar16Ptr(text
), length
) {}
3096 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3097 * it should always be available regardless of U_HIDE_DRAFT_API status
3099 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3101 * wchar_t * constructor.
3102 * (Only defined if U_SIZEOF_WCHAR_T==2.)
3103 * Delegates to UnicodeString(const char16_t *, int32_t).
3104 * @param text NUL-terminated UTF-16 string
3105 * @param length string length
3108 UnicodeString(const wchar_t *text
, int32_t length
) :
3109 UnicodeString(ConstChar16Ptr(text
), length
) {}
3113 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3114 * it should always be available regardless of U_HIDE_DRAFT_API status
3117 * nullptr_t constructor.
3118 * Effectively the same as the default constructor, makes an empty string object.
3119 * @param text nullptr
3120 * @param length ignored
3123 inline UnicodeString(const std::nullptr_t text
, int32_t length
);
3126 * Readonly-aliasing char16_t* constructor.
3127 * The text will be used for the UnicodeString object, but
3128 * it will not be released when the UnicodeString is destroyed.
3129 * This has copy-on-write semantics:
3130 * When the string is modified, then the buffer is first copied into
3131 * newly allocated memory.
3132 * The aliased buffer is never modified.
3134 * In an assignment to another UnicodeString, when using the copy constructor
3135 * or the assignment operator, the text will be copied.
3136 * When using fastCopyFrom(), the text will be aliased again,
3137 * so that both strings then alias the same readonly-text.
3139 * @param isTerminated specifies if <code>text</code> is <code>NUL</code>-terminated.
3140 * This must be true if <code>textLength==-1</code>.
3141 * @param text The characters to alias for the UnicodeString.
3142 * @param textLength The number of Unicode characters in <code>text</code> to alias.
3143 * If -1, then this constructor will determine the length
3144 * by calling <code>u_strlen()</code>.
3147 UnicodeString(UBool isTerminated
,
3148 ConstChar16Ptr text
,
3149 int32_t textLength
);
3152 * Writable-aliasing char16_t* constructor.
3153 * The text will be used for the UnicodeString object, but
3154 * it will not be released when the UnicodeString is destroyed.
3155 * This has write-through semantics:
3156 * For as long as the capacity of the buffer is sufficient, write operations
3157 * will directly affect the buffer. When more capacity is necessary, then
3158 * a new buffer will be allocated and the contents copied as with regularly
3159 * constructed strings.
3160 * In an assignment to another UnicodeString, the buffer will be copied.
3161 * The extract(Char16Ptr dst) function detects whether the dst pointer is the same
3162 * as the string buffer itself and will in this case not copy the contents.
3164 * @param buffer The characters to alias for the UnicodeString.
3165 * @param buffLength The number of Unicode characters in <code>buffer</code> to alias.
3166 * @param buffCapacity The size of <code>buffer</code> in char16_ts.
3169 UnicodeString(char16_t *buffer
, int32_t buffLength
, int32_t buffCapacity
);
3172 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3173 * it should always be available regardless of U_HIDE_DRAFT_API status
3175 #if !U_CHAR16_IS_TYPEDEF
3177 * Writable-aliasing uint16_t * constructor.
3178 * Delegates to UnicodeString(const char16_t *, int32_t, int32_t).
3179 * @param buffer writable buffer of/for UTF-16 text
3180 * @param buffLength length of the current buffer contents
3181 * @param buffCapacity buffer capacity
3184 UnicodeString(uint16_t *buffer
, int32_t buffLength
, int32_t buffCapacity
) :
3185 UnicodeString(Char16Ptr(buffer
), buffLength
, buffCapacity
) {}
3189 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3190 * it should always be available regardless of U_HIDE_DRAFT_API status
3192 #if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3194 * Writable-aliasing wchar_t * constructor.
3195 * (Only defined if U_SIZEOF_WCHAR_T==2.)
3196 * Delegates to UnicodeString(const char16_t *, int32_t, int32_t).
3197 * @param buffer writable buffer of/for UTF-16 text
3198 * @param buffLength length of the current buffer contents
3199 * @param buffCapacity buffer capacity
3202 UnicodeString(wchar_t *buffer
, int32_t buffLength
, int32_t buffCapacity
) :
3203 UnicodeString(Char16Ptr(buffer
), buffLength
, buffCapacity
) {}
3207 * Do not use #ifndef U_HIDE_DRAFT_API for the following constructor,
3208 * it should always be available regardless of U_HIDE_DRAFT_API status
3211 * Writable-aliasing nullptr_t constructor.
3212 * Effectively the same as the default constructor, makes an empty string object.
3213 * @param buffer nullptr
3214 * @param buffLength ignored
3215 * @param buffCapacity ignored
3218 inline UnicodeString(std::nullptr_t buffer
, int32_t buffLength
, int32_t buffCapacity
);
3220 #if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
3223 * char* constructor.
3224 * Uses the default converter (and thus depends on the ICU conversion code)
3225 * unless U_CHARSET_IS_UTF8 is set to 1.
3227 * For ASCII (really "invariant character") strings it is more efficient to use
3228 * the constructor that takes a US_INV (for its enum EInvariant).
3229 * For ASCII (invariant-character) string literals, see UNICODE_STRING and
3230 * UNICODE_STRING_SIMPLE.
3232 * It is recommended to mark this constructor "explicit" by
3233 * <code>-DUNISTR_FROM_STRING_EXPLICIT=explicit</code>
3234 * on the compiler command line or similar.
3235 * @param codepageData an array of bytes, null-terminated,
3236 * in the platform's default codepage.
3238 * @see UNICODE_STRING
3239 * @see UNICODE_STRING_SIMPLE
3241 UNISTR_FROM_STRING_EXPLICIT
UnicodeString(const char *codepageData
);
3244 * char* constructor.
3245 * Uses the default converter (and thus depends on the ICU conversion code)
3246 * unless U_CHARSET_IS_UTF8 is set to 1.
3247 * @param codepageData an array of bytes in the platform's default codepage.
3248 * @param dataLength The number of bytes in <TT>codepageData</TT>.
3251 UnicodeString(const char *codepageData
, int32_t dataLength
);
3255 #if !UCONFIG_NO_CONVERSION
3258 * char* constructor.
3259 * @param codepageData an array of bytes, null-terminated
3260 * @param codepage the encoding of <TT>codepageData</TT>. The special
3261 * value 0 for <TT>codepage</TT> indicates that the text is in the
3262 * platform's default codepage.
3264 * If <code>codepage</code> is an empty string (<code>""</code>),
3265 * then a simple conversion is performed on the codepage-invariant
3266 * subset ("invariant characters") of the platform encoding. See utypes.h.
3267 * Recommendation: For invariant-character strings use the constructor
3268 * UnicodeString(const char *src, int32_t length, enum EInvariant inv)
3269 * because it avoids object code dependencies of UnicodeString on
3270 * the conversion code.
3274 UnicodeString(const char *codepageData
, const char *codepage
);
3277 * char* constructor.
3278 * @param codepageData an array of bytes.
3279 * @param dataLength The number of bytes in <TT>codepageData</TT>.
3280 * @param codepage the encoding of <TT>codepageData</TT>. The special
3281 * value 0 for <TT>codepage</TT> indicates that the text is in the
3282 * platform's default codepage.
3283 * If <code>codepage</code> is an empty string (<code>""</code>),
3284 * then a simple conversion is performed on the codepage-invariant
3285 * subset ("invariant characters") of the platform encoding. See utypes.h.
3286 * Recommendation: For invariant-character strings use the constructor
3287 * UnicodeString(const char *src, int32_t length, enum EInvariant inv)
3288 * because it avoids object code dependencies of UnicodeString on
3289 * the conversion code.
3293 UnicodeString(const char *codepageData
, int32_t dataLength
, const char *codepage
);
3296 * char * / UConverter constructor.
3297 * This constructor uses an existing UConverter object to
3298 * convert the codepage string to Unicode and construct a UnicodeString
3301 * The converter is reset at first.
3302 * If the error code indicates a failure before this constructor is called,
3303 * or if an error occurs during conversion or construction,
3304 * then the string will be bogus.
3306 * This function avoids the overhead of opening and closing a converter if
3307 * multiple strings are constructed.
3309 * @param src input codepage string
3310 * @param srcLength length of the input string, can be -1 for NUL-terminated strings
3311 * @param cnv converter object (ucnv_resetToUnicode() will be called),
3312 * can be NULL for the default converter
3313 * @param errorCode normal ICU error code
3317 const char *src
, int32_t srcLength
,
3319 UErrorCode
&errorCode
);
3324 * Constructs a Unicode string from an invariant-character char * string.
3325 * About invariant characters see utypes.h.
3326 * This constructor has no runtime dependency on conversion code and is
3327 * therefore recommended over ones taking a charset name string
3328 * (where the empty string "" indicates invariant-character conversion).
3330 * Use the macro US_INV as the third, signature-distinguishing parameter.
3334 * void fn(const char *s) {
3335 * UnicodeString ustr(s, -1, US_INV);
3340 * @param src String using only invariant characters.
3341 * @param length Length of src, or -1 if NUL-terminated.
3342 * @param inv Signature-distinguishing paramater, use US_INV.
3347 UnicodeString(const char *src
, int32_t length
, enum EInvariant inv
);
3353 * Starting with ICU 2.4, the assignment operator and the copy constructor
3354 * allocate a new buffer and copy the buffer contents even for readonly aliases.
3355 * By contrast, the fastCopyFrom() function implements the old,
3356 * more efficient but less safe behavior
3357 * of making this string also a readonly alias to the same buffer.
3359 * If the source object has an "open" buffer from getBuffer(minCapacity),
3360 * then the copy is an empty string.
3362 * @param that The UnicodeString object to copy.
3366 UnicodeString(const UnicodeString
& that
);
3368 #if U_HAVE_RVALUE_REFERENCES
3370 * Move constructor, might leave src in bogus state.
3371 * This string will have the same contents and state that the source string had.
3372 * @param src source string
3375 UnicodeString(UnicodeString
&&src
) U_NOEXCEPT
;
3379 * 'Substring' constructor from tail of source string.
3380 * @param src The UnicodeString object to copy.
3381 * @param srcStart The offset into <tt>src</tt> at which to start copying.
3384 UnicodeString(const UnicodeString
& src
, int32_t srcStart
);
3387 * 'Substring' constructor from subrange of source string.
3388 * @param src The UnicodeString object to copy.
3389 * @param srcStart The offset into <tt>src</tt> at which to start copying.
3390 * @param srcLength The number of characters from <tt>src</tt> to copy.
3393 UnicodeString(const UnicodeString
& src
, int32_t srcStart
, int32_t srcLength
);
3396 * Clone this object, an instance of a subclass of Replaceable.
3397 * Clones can be used concurrently in multiple threads.
3398 * If a subclass does not implement clone(), or if an error occurs,
3399 * then NULL is returned.
3400 * The clone functions in all subclasses return a pointer to a Replaceable
3401 * because some compilers do not support covariant (same-as-this)
3402 * return types; cast to the appropriate subclass if necessary.
3403 * The caller must delete the clone.
3405 * @return a clone of this object
3407 * @see Replaceable::clone
3408 * @see getDynamicClassID
3411 virtual Replaceable
*clone() const;
3416 virtual ~UnicodeString();
3419 * Create a UnicodeString from a UTF-8 string.
3420 * Illegal input is replaced with U+FFFD. Otherwise, errors result in a bogus string.
3421 * Calls u_strFromUTF8WithSub().
3423 * @param utf8 UTF-8 input string.
3424 * Note that a StringPiece can be implicitly constructed
3425 * from a std::string or a NUL-terminated const char * string.
3426 * @return A UnicodeString with equivalent UTF-16 contents.
3431 static UnicodeString
fromUTF8(StringPiece utf8
);
3434 * Create a UnicodeString from a UTF-32 string.
3435 * Illegal input is replaced with U+FFFD. Otherwise, errors result in a bogus string.
3436 * Calls u_strFromUTF32WithSub().
3438 * @param utf32 UTF-32 input string. Must not be NULL.
3439 * @param length Length of the input string, or -1 if NUL-terminated.
3440 * @return A UnicodeString with equivalent UTF-16 contents.
3444 static UnicodeString
fromUTF32(const UChar32
*utf32
, int32_t length
);
3446 /* Miscellaneous operations */
3449 * Unescape a string of characters and return a string containing
3450 * the result. The following escape sequences are recognized:
3452 * \\uhhhh 4 hex digits; h in [0-9A-Fa-f]
3453 * \\Uhhhhhhhh 8 hex digits
3454 * \\xhh 1-2 hex digits
3455 * \\ooo 1-3 octal digits; o in [0-7]
3456 * \\cX control-X; X is masked with 0x1F
3458 * as well as the standard ANSI C escapes:
3460 * \\a => U+0007, \\b => U+0008, \\t => U+0009, \\n => U+000A,
3461 * \\v => U+000B, \\f => U+000C, \\r => U+000D, \\e => U+001B,
3462 * \\" => U+0022, \\' => U+0027, \\? => U+003F, \\\\ => U+005C
3464 * Anything else following a backslash is generically escaped. For
3465 * example, "[a\\-z]" returns "[a-z]".
3467 * If an escape sequence is ill-formed, this method returns an empty
3468 * string. An example of an ill-formed sequence is "\\u" followed by
3469 * fewer than 4 hex digits.
3471 * This function is similar to u_unescape() but not identical to it.
3472 * The latter takes a source char*, so it does escape recognition
3473 * and also invariant conversion.
3475 * @return a string with backslash escapes interpreted, or an
3476 * empty string on error.
3477 * @see UnicodeString#unescapeAt()
3479 * @see u_unescapeAt()
3482 UnicodeString
unescape() const;
3485 * Unescape a single escape sequence and return the represented
3486 * character. See unescape() for a listing of the recognized escape
3487 * sequences. The character at offset-1 is assumed (without
3488 * checking) to be a backslash. If the escape sequence is
3489 * ill-formed, or the offset is out of range, U_SENTINEL=-1 is
3492 * @param offset an input output parameter. On input, it is the
3493 * offset into this string where the escape sequence is located,
3494 * after the initial backslash. On output, it is advanced after the
3495 * last character parsed. On error, it is not advanced at all.
3496 * @return the character represented by the escape sequence at
3497 * offset, or U_SENTINEL=-1 on error.
3498 * @see UnicodeString#unescape()
3500 * @see u_unescapeAt()
3503 UChar32
unescapeAt(int32_t &offset
) const;
3506 * ICU "poor man's RTTI", returns a UClassID for this class.
3510 static UClassID U_EXPORT2
getStaticClassID();
3513 * ICU "poor man's RTTI", returns a UClassID for the actual class.
3517 virtual UClassID
getDynamicClassID() const;
3519 //========================================
3520 // Implementation methods
3521 //========================================
3525 * Implement Replaceable::getLength() (see jitterbug 1027).
3528 virtual int32_t getLength() const;
3531 * The change in Replaceable to use virtual getCharAt() allows
3532 * UnicodeString::charAt() to be inline again (see jitterbug 709).
3535 virtual char16_t getCharAt(int32_t offset
) const;
3538 * The change in Replaceable to use virtual getChar32At() allows
3539 * UnicodeString::char32At() to be inline again (see jitterbug 709).
3542 virtual UChar32
getChar32At(int32_t offset
) const;
3545 // For char* constructors. Could be made public.
3546 UnicodeString
&setToUTF8(StringPiece utf8
);
3547 // For extract(char*).
3548 // We could make a toUTF8(target, capacity, errorCode) public but not
3549 // this version: New API will be cleaner if we make callers create substrings
3550 // rather than having start+length on every method,
3551 // and it should take a UErrorCode&.
3553 toUTF8(int32_t start
, int32_t len
,
3554 char *target
, int32_t capacity
) const;
3557 * Internal string contents comparison, called by operator==.
3558 * Requires: this & text not bogus and have same lengths.
3560 UBool
doEquals(const UnicodeString
&text
, int32_t len
) const;
3563 doCompare(int32_t start
,
3565 const UnicodeString
& srcText
,
3567 int32_t srcLength
) const;
3569 int8_t doCompare(int32_t start
,
3571 const char16_t *srcChars
,
3573 int32_t srcLength
) const;
3576 doCompareCodePointOrder(int32_t start
,
3578 const UnicodeString
& srcText
,
3580 int32_t srcLength
) const;
3582 int8_t doCompareCodePointOrder(int32_t start
,
3584 const char16_t *srcChars
,
3586 int32_t srcLength
) const;
3589 doCaseCompare(int32_t start
,
3591 const UnicodeString
&srcText
,
3594 uint32_t options
) const;
3597 doCaseCompare(int32_t start
,
3599 const char16_t *srcChars
,
3602 uint32_t options
) const;
3604 int32_t doIndexOf(char16_t c
,
3606 int32_t length
) const;
3608 int32_t doIndexOf(UChar32 c
,
3610 int32_t length
) const;
3612 int32_t doLastIndexOf(char16_t c
,
3614 int32_t length
) const;
3616 int32_t doLastIndexOf(UChar32 c
,
3618 int32_t length
) const;
3620 void doExtract(int32_t start
,
3623 int32_t dstStart
) const;
3625 inline void doExtract(int32_t start
,
3627 UnicodeString
& target
) const;
3629 inline char16_t doCharAt(int32_t offset
) const;
3631 UnicodeString
& doReplace(int32_t start
,
3633 const UnicodeString
& srcText
,
3637 UnicodeString
& doReplace(int32_t start
,
3639 const char16_t *srcChars
,
3643 UnicodeString
& doAppend(const UnicodeString
& src
, int32_t srcStart
, int32_t srcLength
);
3644 UnicodeString
& doAppend(const char16_t *srcChars
, int32_t srcStart
, int32_t srcLength
);
3646 UnicodeString
& doReverse(int32_t start
,
3649 // calculate hash code
3650 int32_t doHashCode(void) const;
3652 // get pointer to start of array
3653 // these do not check for kOpenGetBuffer, unlike the public getBuffer() function
3654 inline char16_t* getArrayStart(void);
3655 inline const char16_t* getArrayStart(void) const;
3657 inline UBool
hasShortLength() const;
3658 inline int32_t getShortLength() const;
3660 // A UnicodeString object (not necessarily its current buffer)
3661 // is writable unless it isBogus() or it has an "open" getBuffer(minCapacity).
3662 inline UBool
isWritable() const;
3664 // Is the current buffer writable?
3665 inline UBool
isBufferWritable() const;
3667 // None of the following does releaseArray().
3668 inline void setZeroLength();
3669 inline void setShortLength(int32_t len
);
3670 inline void setLength(int32_t len
);
3671 inline void setToEmpty();
3672 inline void setArray(char16_t *array
, int32_t len
, int32_t capacity
); // sets length but not flags
3674 // allocate the array; result may be the stack buffer
3675 // sets refCount to 1 if appropriate
3676 // sets fArray, fCapacity, and flags
3678 // returns boolean for success or failure
3679 UBool
allocate(int32_t capacity
);
3681 // release the array if owned
3682 void releaseArray(void);
3684 // turn a bogus string into an empty one
3687 // implements assigment operator, copy constructor, and fastCopyFrom()
3688 UnicodeString
©From(const UnicodeString
&src
, UBool fastCopy
=FALSE
);
3690 // Copies just the fields without memory management.
3691 void copyFieldsFrom(UnicodeString
&src
, UBool setSrcToBogus
) U_NOEXCEPT
;
3693 // Pin start and limit to acceptable values.
3694 inline void pinIndex(int32_t& start
) const;
3695 inline void pinIndices(int32_t& start
,
3696 int32_t& length
) const;
3698 #if !UCONFIG_NO_CONVERSION
3700 /* Internal extract() using UConverter. */
3701 int32_t doExtract(int32_t start
, int32_t length
,
3702 char *dest
, int32_t destCapacity
,
3704 UErrorCode
&errorCode
) const;
3707 * Real constructor for converting from codepage data.
3708 * It assumes that it is called with !fRefCounted.
3710 * If <code>codepage==0</code>, then the default converter
3711 * is used for the platform encoding.
3712 * If <code>codepage</code> is an empty string (<code>""</code>),
3713 * then a simple conversion is performed on the codepage-invariant
3714 * subset ("invariant characters") of the platform encoding. See utypes.h.
3716 void doCodepageCreate(const char *codepageData
,
3718 const char *codepage
);
3721 * Worker function for creating a UnicodeString from
3722 * a codepage string using a UConverter.
3725 doCodepageCreate(const char *codepageData
,
3727 UConverter
*converter
,
3728 UErrorCode
&status
);
3733 * This function is called when write access to the array
3736 * We need to make a copy of the array if
3737 * the buffer is read-only, or
3738 * the buffer is refCounted (shared), and refCount>1, or
3739 * the buffer is too small.
3741 * Return FALSE if memory could not be allocated.
3743 UBool
cloneArrayIfNeeded(int32_t newCapacity
= -1,
3744 int32_t growCapacity
= -1,
3745 UBool doCopyArray
= TRUE
,
3746 int32_t **pBufferToDelete
= 0,
3747 UBool forceClone
= FALSE
);
3750 * Common function for UnicodeString case mappings.
3751 * The stringCaseMapper has the same type UStringCaseMapper
3752 * as in ustr_imp.h for ustrcase_map().
3755 caseMap(int32_t caseLocale
, uint32_t options
,
3756 #if !UCONFIG_NO_BREAK_ITERATION
3757 BreakIterator
*iter
,
3759 UStringCaseMapper
*stringCaseMapper
);
3763 int32_t removeRef(void);
3764 int32_t refCount(void) const;
3769 * Size of stack buffer for short strings.
3770 * Must be at least U16_MAX_LENGTH for the single-code point constructor to work.
3771 * @see UNISTR_OBJECT_SIZE
3773 US_STACKBUF_SIZE
=(int32_t)(UNISTR_OBJECT_SIZE
-sizeof(void *)-2)/U_SIZEOF_UCHAR
,
3774 kInvalidUChar
=0xffff, // U+FFFF returned by charAt(invalid index)
3775 kInvalidHashCode
=0, // invalid hash code
3776 kEmptyHashCode
=1, // hash code for empty string
3778 // bit flag values for fLengthAndFlags
3779 kIsBogus
=1, // this string is bogus, i.e., not valid or NULL
3780 kUsingStackBuffer
=2,// using fUnion.fStackFields instead of fUnion.fFields
3781 kRefCounted
=4, // there is a refCount field before the characters in fArray
3782 kBufferIsReadonly
=8,// do not write to this buffer
3783 kOpenGetBuffer
=16, // getBuffer(minCapacity) was called (is "open"),
3784 // and releaseBuffer(newLength) must be called
3785 kAllStorageFlags
=0x1f,
3787 kLengthShift
=5, // remaining 11 bits for non-negative short length, or negative if long
3788 kLength1
=1<<kLengthShift
,
3789 kMaxShortLength
=0x3ff, // max non-negative short length (leaves top bit 0)
3790 kLengthIsLarge
=0xffe0, // short length < 0, real length is in fUnion.fFields.fLength
3792 // combined values for convenience
3793 kShortString
=kUsingStackBuffer
,
3794 kLongString
=kRefCounted
,
3795 kReadonlyAlias
=kBufferIsReadonly
,
3799 friend class UnicodeStringAppendable
;
3801 union StackBufferOrFields
; // forward declaration necessary before friend declaration
3802 friend union StackBufferOrFields
; // make US_STACKBUF_SIZE visible inside fUnion
3805 * The following are all the class fields that are stored
3806 * in each UnicodeString object.
3807 * Note that UnicodeString has virtual functions,
3808 * therefore there is an implicit vtable pointer
3809 * as the first real field.
3810 * The fields should be aligned such that no padding is necessary.
3811 * On 32-bit machines, the size should be 32 bytes,
3812 * on 64-bit machines (8-byte pointers), it should be 40 bytes.
3814 * We use a hack to achieve this.
3816 * With at least some compilers, each of the following is forced to
3817 * a multiple of sizeof(pointer) [the largest field base unit here is a data pointer],
3818 * rounded up with additional padding if the fields do not already fit that requirement:
3819 * - sizeof(class UnicodeString)
3820 * - offsetof(UnicodeString, fUnion)
3822 * - sizeof(fStackFields)
3824 * We optimize for the longest possible internal buffer for short strings.
3825 * fUnion.fStackFields begins with 2 bytes for storage flags
3826 * and the length of relatively short strings,
3827 * followed by the buffer for short string contents.
3828 * There is no padding inside fStackFields.
3830 * Heap-allocated and aliased strings use fUnion.fFields.
3831 * Both fStackFields and fFields must begin with the same fields for flags and short length,
3832 * that is, those must have the same memory offsets inside the object,
3833 * because the flags must be inspected in order to decide which half of fUnion is being used.
3834 * We assume that the compiler does not reorder the fields.
3836 * (Padding at the end of fFields is ok:
3837 * As long as it is no larger than fStackFields, it is not wasted space.)
3839 * For some of the history of the UnicodeString class fields layout, see
3840 * - ICU ticket #11551 "longer UnicodeString contents in stack buffer"
3841 * - ICU ticket #11336 "UnicodeString: recombine stack buffer arrays"
3842 * - ICU ticket #8322 "why is sizeof(UnicodeString)==48?"
3844 // (implicit) *vtable;
3845 union StackBufferOrFields
{
3846 // fStackFields is used iff (fLengthAndFlags&kUsingStackBuffer) else fFields is used.
3847 // Each struct of the union must begin with fLengthAndFlags.
3849 int16_t fLengthAndFlags
; // bit fields: see constants above
3850 char16_t fBuffer
[US_STACKBUF_SIZE
]; // buffer for short strings
3853 int16_t fLengthAndFlags
; // bit fields: see constants above
3854 int32_t fLength
; // number of characters in fArray if >127; else undefined
3855 int32_t fCapacity
; // capacity of fArray (in char16_ts)
3856 // array pointer last to minimize padding for machines with P128 data model
3857 // or pointer sizes that are not a power of 2
3858 char16_t *fArray
; // the Unicode data
3864 * Create a new UnicodeString with the concatenation of two others.
3866 * @param s1 The first string to be copied to the new one.
3867 * @param s2 The second string to be copied to the new one, after s1.
3868 * @return UnicodeString(s1).append(s2)
3871 U_COMMON_API UnicodeString U_EXPORT2
3872 operator+ (const UnicodeString
&s1
, const UnicodeString
&s2
);
3874 //========================================
3876 //========================================
3878 //========================================
3880 //========================================
3883 UnicodeString::pinIndex(int32_t& start
) const
3888 } else if(start
> length()) {
3894 UnicodeString::pinIndices(int32_t& start
,
3895 int32_t& _length
) const
3898 int32_t len
= length();
3901 } else if(start
> len
) {
3906 } else if(_length
> (len
- start
)) {
3907 _length
= (len
- start
);
3912 UnicodeString::getArrayStart() {
3913 return (fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) ?
3914 fUnion
.fStackFields
.fBuffer
: fUnion
.fFields
.fArray
;
3917 inline const char16_t*
3918 UnicodeString::getArrayStart() const {
3919 return (fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) ?
3920 fUnion
.fStackFields
.fBuffer
: fUnion
.fFields
.fArray
;
3923 //========================================
3924 // Default constructor
3925 //========================================
3928 UnicodeString::UnicodeString() {
3929 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3932 inline UnicodeString::UnicodeString(const std::nullptr_t
/*text*/) {
3933 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3936 inline UnicodeString::UnicodeString(const std::nullptr_t
/*text*/, int32_t /*length*/) {
3937 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3940 inline UnicodeString::UnicodeString(std::nullptr_t
/*buffer*/, int32_t /*buffLength*/, int32_t /*buffCapacity*/) {
3941 fUnion
.fStackFields
.fLengthAndFlags
=kShortString
;
3944 //========================================
3945 // Read-only implementation methods
3946 //========================================
3948 UnicodeString::hasShortLength() const {
3949 return fUnion
.fFields
.fLengthAndFlags
>=0;
3953 UnicodeString::getShortLength() const {
3954 // fLengthAndFlags must be non-negative -> short length >= 0
3955 // and arithmetic or logical shift does not matter.
3956 return fUnion
.fFields
.fLengthAndFlags
>>kLengthShift
;
3960 UnicodeString::length() const {
3961 return hasShortLength() ? getShortLength() : fUnion
.fFields
.fLength
;
3965 UnicodeString::getCapacity() const {
3966 return (fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) ?
3967 US_STACKBUF_SIZE
: fUnion
.fFields
.fCapacity
;
3971 UnicodeString::hashCode() const
3972 { return doHashCode(); }
3975 UnicodeString::isBogus() const
3976 { return (UBool
)(fUnion
.fFields
.fLengthAndFlags
& kIsBogus
); }
3979 UnicodeString::isWritable() const
3980 { return (UBool
)!(fUnion
.fFields
.fLengthAndFlags
&(kOpenGetBuffer
|kIsBogus
)); }
3983 UnicodeString::isBufferWritable() const
3986 !(fUnion
.fFields
.fLengthAndFlags
&(kOpenGetBuffer
|kIsBogus
|kBufferIsReadonly
)) &&
3987 (!(fUnion
.fFields
.fLengthAndFlags
&kRefCounted
) || refCount()==1));
3990 inline const char16_t *
3991 UnicodeString::getBuffer() const {
3992 if(fUnion
.fFields
.fLengthAndFlags
&(kIsBogus
|kOpenGetBuffer
)) {
3994 } else if(fUnion
.fFields
.fLengthAndFlags
&kUsingStackBuffer
) {
3995 return fUnion
.fStackFields
.fBuffer
;
3997 return fUnion
.fFields
.fArray
;
4001 //========================================
4002 // Read-only alias methods
4003 //========================================
4005 UnicodeString::doCompare(int32_t start
,
4007 const UnicodeString
& srcText
,
4009 int32_t srcLength
) const
4011 if(srcText
.isBogus()) {
4012 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4014 srcText
.pinIndices(srcStart
, srcLength
);
4015 return doCompare(start
, thisLength
, srcText
.getArrayStart(), srcStart
, srcLength
);
4020 UnicodeString::operator== (const UnicodeString
& text
) const
4023 return text
.isBogus();
4025 int32_t len
= length(), textLength
= text
.length();
4026 return !text
.isBogus() && len
== textLength
&& doEquals(text
, len
);
4031 UnicodeString::operator!= (const UnicodeString
& text
) const
4032 { return (! operator==(text
)); }
4035 UnicodeString::operator> (const UnicodeString
& text
) const
4036 { return doCompare(0, length(), text
, 0, text
.length()) == 1; }
4039 UnicodeString::operator< (const UnicodeString
& text
) const
4040 { return doCompare(0, length(), text
, 0, text
.length()) == -1; }
4043 UnicodeString::operator>= (const UnicodeString
& text
) const
4044 { return doCompare(0, length(), text
, 0, text
.length()) != -1; }
4047 UnicodeString::operator<= (const UnicodeString
& text
) const
4048 { return doCompare(0, length(), text
, 0, text
.length()) != 1; }
4051 UnicodeString::compare(const UnicodeString
& text
) const
4052 { return doCompare(0, length(), text
, 0, text
.length()); }
4055 UnicodeString::compare(int32_t start
,
4057 const UnicodeString
& srcText
) const
4058 { return doCompare(start
, _length
, srcText
, 0, srcText
.length()); }
4061 UnicodeString::compare(ConstChar16Ptr srcChars
,
4062 int32_t srcLength
) const
4063 { return doCompare(0, length(), srcChars
, 0, srcLength
); }
4066 UnicodeString::compare(int32_t start
,
4068 const UnicodeString
& srcText
,
4070 int32_t srcLength
) const
4071 { return doCompare(start
, _length
, srcText
, srcStart
, srcLength
); }
4074 UnicodeString::compare(int32_t start
,
4076 const char16_t *srcChars
) const
4077 { return doCompare(start
, _length
, srcChars
, 0, _length
); }
4080 UnicodeString::compare(int32_t start
,
4082 const char16_t *srcChars
,
4084 int32_t srcLength
) const
4085 { return doCompare(start
, _length
, srcChars
, srcStart
, srcLength
); }
4088 UnicodeString::compareBetween(int32_t start
,
4090 const UnicodeString
& srcText
,
4092 int32_t srcLimit
) const
4093 { return doCompare(start
, limit
- start
,
4094 srcText
, srcStart
, srcLimit
- srcStart
); }
4097 UnicodeString::doCompareCodePointOrder(int32_t start
,
4099 const UnicodeString
& srcText
,
4101 int32_t srcLength
) const
4103 if(srcText
.isBogus()) {
4104 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4106 srcText
.pinIndices(srcStart
, srcLength
);
4107 return doCompareCodePointOrder(start
, thisLength
, srcText
.getArrayStart(), srcStart
, srcLength
);
4112 UnicodeString::compareCodePointOrder(const UnicodeString
& text
) const
4113 { return doCompareCodePointOrder(0, length(), text
, 0, text
.length()); }
4116 UnicodeString::compareCodePointOrder(int32_t start
,
4118 const UnicodeString
& srcText
) const
4119 { return doCompareCodePointOrder(start
, _length
, srcText
, 0, srcText
.length()); }
4122 UnicodeString::compareCodePointOrder(ConstChar16Ptr srcChars
,
4123 int32_t srcLength
) const
4124 { return doCompareCodePointOrder(0, length(), srcChars
, 0, srcLength
); }
4127 UnicodeString::compareCodePointOrder(int32_t start
,
4129 const UnicodeString
& srcText
,
4131 int32_t srcLength
) const
4132 { return doCompareCodePointOrder(start
, _length
, srcText
, srcStart
, srcLength
); }
4135 UnicodeString::compareCodePointOrder(int32_t start
,
4137 const char16_t *srcChars
) const
4138 { return doCompareCodePointOrder(start
, _length
, srcChars
, 0, _length
); }
4141 UnicodeString::compareCodePointOrder(int32_t start
,
4143 const char16_t *srcChars
,
4145 int32_t srcLength
) const
4146 { return doCompareCodePointOrder(start
, _length
, srcChars
, srcStart
, srcLength
); }
4149 UnicodeString::compareCodePointOrderBetween(int32_t start
,
4151 const UnicodeString
& srcText
,
4153 int32_t srcLimit
) const
4154 { return doCompareCodePointOrder(start
, limit
- start
,
4155 srcText
, srcStart
, srcLimit
- srcStart
); }
4158 UnicodeString::doCaseCompare(int32_t start
,
4160 const UnicodeString
&srcText
,
4163 uint32_t options
) const
4165 if(srcText
.isBogus()) {
4166 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4168 srcText
.pinIndices(srcStart
, srcLength
);
4169 return doCaseCompare(start
, thisLength
, srcText
.getArrayStart(), srcStart
, srcLength
, options
);
4174 UnicodeString::caseCompare(const UnicodeString
&text
, uint32_t options
) const {
4175 return doCaseCompare(0, length(), text
, 0, text
.length(), options
);
4179 UnicodeString::caseCompare(int32_t start
,
4181 const UnicodeString
&srcText
,
4182 uint32_t options
) const {
4183 return doCaseCompare(start
, _length
, srcText
, 0, srcText
.length(), options
);
4187 UnicodeString::caseCompare(ConstChar16Ptr srcChars
,
4189 uint32_t options
) const {
4190 return doCaseCompare(0, length(), srcChars
, 0, srcLength
, options
);
4194 UnicodeString::caseCompare(int32_t start
,
4196 const UnicodeString
&srcText
,
4199 uint32_t options
) const {
4200 return doCaseCompare(start
, _length
, srcText
, srcStart
, srcLength
, options
);
4204 UnicodeString::caseCompare(int32_t start
,
4206 const char16_t *srcChars
,
4207 uint32_t options
) const {
4208 return doCaseCompare(start
, _length
, srcChars
, 0, _length
, options
);
4212 UnicodeString::caseCompare(int32_t start
,
4214 const char16_t *srcChars
,
4217 uint32_t options
) const {
4218 return doCaseCompare(start
, _length
, srcChars
, srcStart
, srcLength
, options
);
4222 UnicodeString::caseCompareBetween(int32_t start
,
4224 const UnicodeString
&srcText
,
4227 uint32_t options
) const {
4228 return doCaseCompare(start
, limit
- start
, srcText
, srcStart
, srcLimit
- srcStart
, options
);
4232 UnicodeString::indexOf(const UnicodeString
& srcText
,
4236 int32_t _length
) const
4238 if(!srcText
.isBogus()) {
4239 srcText
.pinIndices(srcStart
, srcLength
);
4241 return indexOf(srcText
.getArrayStart(), srcStart
, srcLength
, start
, _length
);
4248 UnicodeString::indexOf(const UnicodeString
& text
) const
4249 { return indexOf(text
, 0, text
.length(), 0, length()); }
4252 UnicodeString::indexOf(const UnicodeString
& text
,
4253 int32_t start
) const {
4255 return indexOf(text
, 0, text
.length(), start
, length() - start
);
4259 UnicodeString::indexOf(const UnicodeString
& text
,
4261 int32_t _length
) const
4262 { return indexOf(text
, 0, text
.length(), start
, _length
); }
4265 UnicodeString::indexOf(const char16_t *srcChars
,
4267 int32_t start
) const {
4269 return indexOf(srcChars
, 0, srcLength
, start
, length() - start
);
4273 UnicodeString::indexOf(ConstChar16Ptr srcChars
,
4276 int32_t _length
) const
4277 { return indexOf(srcChars
, 0, srcLength
, start
, _length
); }
4280 UnicodeString::indexOf(char16_t c
,
4282 int32_t _length
) const
4283 { return doIndexOf(c
, start
, _length
); }
4286 UnicodeString::indexOf(UChar32 c
,
4288 int32_t _length
) const
4289 { return doIndexOf(c
, start
, _length
); }
4292 UnicodeString::indexOf(char16_t c
) const
4293 { return doIndexOf(c
, 0, length()); }
4296 UnicodeString::indexOf(UChar32 c
) const
4297 { return indexOf(c
, 0, length()); }
4300 UnicodeString::indexOf(char16_t c
,
4301 int32_t start
) const {
4303 return doIndexOf(c
, start
, length() - start
);
4307 UnicodeString::indexOf(UChar32 c
,
4308 int32_t start
) const {
4310 return indexOf(c
, start
, length() - start
);
4314 UnicodeString::lastIndexOf(ConstChar16Ptr srcChars
,
4317 int32_t _length
) const
4318 { return lastIndexOf(srcChars
, 0, srcLength
, start
, _length
); }
4321 UnicodeString::lastIndexOf(const char16_t *srcChars
,
4323 int32_t start
) const {
4325 return lastIndexOf(srcChars
, 0, srcLength
, start
, length() - start
);
4329 UnicodeString::lastIndexOf(const UnicodeString
& srcText
,
4333 int32_t _length
) const
4335 if(!srcText
.isBogus()) {
4336 srcText
.pinIndices(srcStart
, srcLength
);
4338 return lastIndexOf(srcText
.getArrayStart(), srcStart
, srcLength
, start
, _length
);
4345 UnicodeString::lastIndexOf(const UnicodeString
& text
,
4347 int32_t _length
) const
4348 { return lastIndexOf(text
, 0, text
.length(), start
, _length
); }
4351 UnicodeString::lastIndexOf(const UnicodeString
& text
,
4352 int32_t start
) const {
4354 return lastIndexOf(text
, 0, text
.length(), start
, length() - start
);
4358 UnicodeString::lastIndexOf(const UnicodeString
& text
) const
4359 { return lastIndexOf(text
, 0, text
.length(), 0, length()); }
4362 UnicodeString::lastIndexOf(char16_t c
,
4364 int32_t _length
) const
4365 { return doLastIndexOf(c
, start
, _length
); }
4368 UnicodeString::lastIndexOf(UChar32 c
,
4370 int32_t _length
) const {
4371 return doLastIndexOf(c
, start
, _length
);
4375 UnicodeString::lastIndexOf(char16_t c
) const
4376 { return doLastIndexOf(c
, 0, length()); }
4379 UnicodeString::lastIndexOf(UChar32 c
) const {
4380 return lastIndexOf(c
, 0, length());
4384 UnicodeString::lastIndexOf(char16_t c
,
4385 int32_t start
) const {
4387 return doLastIndexOf(c
, start
, length() - start
);
4391 UnicodeString::lastIndexOf(UChar32 c
,
4392 int32_t start
) const {
4394 return lastIndexOf(c
, start
, length() - start
);
4398 UnicodeString::startsWith(const UnicodeString
& text
) const
4399 { return compare(0, text
.length(), text
, 0, text
.length()) == 0; }
4402 UnicodeString::startsWith(const UnicodeString
& srcText
,
4404 int32_t srcLength
) const
4405 { return doCompare(0, srcLength
, srcText
, srcStart
, srcLength
) == 0; }
4408 UnicodeString::startsWith(ConstChar16Ptr srcChars
, int32_t srcLength
) const {
4410 srcLength
= u_strlen(toUCharPtr(srcChars
));
4412 return doCompare(0, srcLength
, srcChars
, 0, srcLength
) == 0;
4416 UnicodeString::startsWith(const char16_t *srcChars
, int32_t srcStart
, int32_t srcLength
) const {
4418 srcLength
= u_strlen(toUCharPtr(srcChars
));
4420 return doCompare(0, srcLength
, srcChars
, srcStart
, srcLength
) == 0;
4424 UnicodeString::endsWith(const UnicodeString
& text
) const
4425 { return doCompare(length() - text
.length(), text
.length(),
4426 text
, 0, text
.length()) == 0; }
4429 UnicodeString::endsWith(const UnicodeString
& srcText
,
4431 int32_t srcLength
) const {
4432 srcText
.pinIndices(srcStart
, srcLength
);
4433 return doCompare(length() - srcLength
, srcLength
,
4434 srcText
, srcStart
, srcLength
) == 0;
4438 UnicodeString::endsWith(ConstChar16Ptr srcChars
,
4439 int32_t srcLength
) const {
4441 srcLength
= u_strlen(toUCharPtr(srcChars
));
4443 return doCompare(length() - srcLength
, srcLength
,
4444 srcChars
, 0, srcLength
) == 0;
4448 UnicodeString::endsWith(const char16_t *srcChars
,
4450 int32_t srcLength
) const {
4452 srcLength
= u_strlen(toUCharPtr(srcChars
+ srcStart
));
4454 return doCompare(length() - srcLength
, srcLength
,
4455 srcChars
, srcStart
, srcLength
) == 0;
4458 //========================================
4460 //========================================
4461 inline UnicodeString
&
4462 UnicodeString::replace(int32_t start
,
4464 const UnicodeString
& srcText
)
4465 { return doReplace(start
, _length
, srcText
, 0, srcText
.length()); }
4467 inline UnicodeString
&
4468 UnicodeString::replace(int32_t start
,
4470 const UnicodeString
& srcText
,
4473 { return doReplace(start
, _length
, srcText
, srcStart
, srcLength
); }
4475 inline UnicodeString
&
4476 UnicodeString::replace(int32_t start
,
4478 ConstChar16Ptr srcChars
,
4480 { return doReplace(start
, _length
, srcChars
, 0, srcLength
); }
4482 inline UnicodeString
&
4483 UnicodeString::replace(int32_t start
,
4485 const char16_t *srcChars
,
4488 { return doReplace(start
, _length
, srcChars
, srcStart
, srcLength
); }
4490 inline UnicodeString
&
4491 UnicodeString::replace(int32_t start
,
4494 { return doReplace(start
, _length
, &srcChar
, 0, 1); }
4496 inline UnicodeString
&
4497 UnicodeString::replaceBetween(int32_t start
,
4499 const UnicodeString
& srcText
)
4500 { return doReplace(start
, limit
- start
, srcText
, 0, srcText
.length()); }
4502 inline UnicodeString
&
4503 UnicodeString::replaceBetween(int32_t start
,
4505 const UnicodeString
& srcText
,
4508 { return doReplace(start
, limit
- start
, srcText
, srcStart
, srcLimit
- srcStart
); }
4510 inline UnicodeString
&
4511 UnicodeString::findAndReplace(const UnicodeString
& oldText
,
4512 const UnicodeString
& newText
)
4513 { return findAndReplace(0, length(), oldText
, 0, oldText
.length(),
4514 newText
, 0, newText
.length()); }
4516 inline UnicodeString
&
4517 UnicodeString::findAndReplace(int32_t start
,
4519 const UnicodeString
& oldText
,
4520 const UnicodeString
& newText
)
4521 { return findAndReplace(start
, _length
, oldText
, 0, oldText
.length(),
4522 newText
, 0, newText
.length()); }
4524 // ============================
4526 // ============================
4528 UnicodeString::doExtract(int32_t start
,
4530 UnicodeString
& target
) const
4531 { target
.replace(0, target
.length(), *this, start
, _length
); }
4534 UnicodeString::extract(int32_t start
,
4537 int32_t targetStart
) const
4538 { doExtract(start
, _length
, target
, targetStart
); }
4541 UnicodeString::extract(int32_t start
,
4543 UnicodeString
& target
) const
4544 { doExtract(start
, _length
, target
); }
4546 #if !UCONFIG_NO_CONVERSION
4549 UnicodeString::extract(int32_t start
,
4552 const char *codepage
) const
4555 // This dstSize value will be checked explicitly
4556 return extract(start
, _length
, dst
, dst
!=0 ? 0xffffffff : 0, codepage
);
4562 UnicodeString::extractBetween(int32_t start
,
4565 int32_t dstStart
) const {
4568 doExtract(start
, limit
- start
, dst
, dstStart
);
4571 inline UnicodeString
4572 UnicodeString::tempSubStringBetween(int32_t start
, int32_t limit
) const {
4573 return tempSubString(start
, limit
- start
);
4577 UnicodeString::doCharAt(int32_t offset
) const
4579 if((uint32_t)offset
< (uint32_t)length()) {
4580 return getArrayStart()[offset
];
4582 return kInvalidUChar
;
4587 UnicodeString::charAt(int32_t offset
) const
4588 { return doCharAt(offset
); }
4591 UnicodeString::operator[] (int32_t offset
) const
4592 { return doCharAt(offset
); }
4595 UnicodeString::isEmpty() const {
4596 // Arithmetic or logical right shift does not matter: only testing for 0.
4597 return (fUnion
.fFields
.fLengthAndFlags
>>kLengthShift
) == 0;
4600 //========================================
4601 // Write implementation methods
4602 //========================================
4604 UnicodeString::setZeroLength() {
4605 fUnion
.fFields
.fLengthAndFlags
&= kAllStorageFlags
;
4609 UnicodeString::setShortLength(int32_t len
) {
4610 // requires 0 <= len <= kMaxShortLength
4611 fUnion
.fFields
.fLengthAndFlags
=
4612 (int16_t)((fUnion
.fFields
.fLengthAndFlags
& kAllStorageFlags
) | (len
<< kLengthShift
));
4616 UnicodeString::setLength(int32_t len
) {
4617 if(len
<= kMaxShortLength
) {
4618 setShortLength(len
);
4620 fUnion
.fFields
.fLengthAndFlags
|= kLengthIsLarge
;
4621 fUnion
.fFields
.fLength
= len
;
4626 UnicodeString::setToEmpty() {
4627 fUnion
.fFields
.fLengthAndFlags
= kShortString
;
4631 UnicodeString::setArray(char16_t *array
, int32_t len
, int32_t capacity
) {
4633 fUnion
.fFields
.fArray
= array
;
4634 fUnion
.fFields
.fCapacity
= capacity
;
4637 inline UnicodeString
&
4638 UnicodeString::operator= (char16_t ch
)
4639 { return doReplace(0, length(), &ch
, 0, 1); }
4641 inline UnicodeString
&
4642 UnicodeString::operator= (UChar32 ch
)
4643 { return replace(0, length(), ch
); }
4645 inline UnicodeString
&
4646 UnicodeString::setTo(const UnicodeString
& srcText
,
4651 return doReplace(0, length(), srcText
, srcStart
, srcLength
);
4654 inline UnicodeString
&
4655 UnicodeString::setTo(const UnicodeString
& srcText
,
4659 srcText
.pinIndex(srcStart
);
4660 return doReplace(0, length(), srcText
, srcStart
, srcText
.length() - srcStart
);
4663 inline UnicodeString
&
4664 UnicodeString::setTo(const UnicodeString
& srcText
)
4666 return copyFrom(srcText
);
4669 inline UnicodeString
&
4670 UnicodeString::setTo(const char16_t *srcChars
,
4674 return doReplace(0, length(), srcChars
, 0, srcLength
);
4677 inline UnicodeString
&
4678 UnicodeString::setTo(char16_t srcChar
)
4681 return doReplace(0, length(), &srcChar
, 0, 1);
4684 inline UnicodeString
&
4685 UnicodeString::setTo(UChar32 srcChar
)
4688 return replace(0, length(), srcChar
);
4691 inline UnicodeString
&
4692 UnicodeString::append(const UnicodeString
& srcText
,
4695 { return doAppend(srcText
, srcStart
, srcLength
); }
4697 inline UnicodeString
&
4698 UnicodeString::append(const UnicodeString
& srcText
)
4699 { return doAppend(srcText
, 0, srcText
.length()); }
4701 inline UnicodeString
&
4702 UnicodeString::append(const char16_t *srcChars
,
4705 { return doAppend(srcChars
, srcStart
, srcLength
); }
4707 inline UnicodeString
&
4708 UnicodeString::append(ConstChar16Ptr srcChars
,
4710 { return doAppend(srcChars
, 0, srcLength
); }
4712 inline UnicodeString
&
4713 UnicodeString::append(char16_t srcChar
)
4714 { return doAppend(&srcChar
, 0, 1); }
4716 inline UnicodeString
&
4717 UnicodeString::operator+= (char16_t ch
)
4718 { return doAppend(&ch
, 0, 1); }
4720 inline UnicodeString
&
4721 UnicodeString::operator+= (UChar32 ch
) {
4725 inline UnicodeString
&
4726 UnicodeString::operator+= (const UnicodeString
& srcText
)
4727 { return doAppend(srcText
, 0, srcText
.length()); }
4729 inline UnicodeString
&
4730 UnicodeString::insert(int32_t start
,
4731 const UnicodeString
& srcText
,
4734 { return doReplace(start
, 0, srcText
, srcStart
, srcLength
); }
4736 inline UnicodeString
&
4737 UnicodeString::insert(int32_t start
,
4738 const UnicodeString
& srcText
)
4739 { return doReplace(start
, 0, srcText
, 0, srcText
.length()); }
4741 inline UnicodeString
&
4742 UnicodeString::insert(int32_t start
,
4743 const char16_t *srcChars
,
4746 { return doReplace(start
, 0, srcChars
, srcStart
, srcLength
); }
4748 inline UnicodeString
&
4749 UnicodeString::insert(int32_t start
,
4750 ConstChar16Ptr srcChars
,
4752 { return doReplace(start
, 0, srcChars
, 0, srcLength
); }
4754 inline UnicodeString
&
4755 UnicodeString::insert(int32_t start
,
4757 { return doReplace(start
, 0, &srcChar
, 0, 1); }
4759 inline UnicodeString
&
4760 UnicodeString::insert(int32_t start
,
4762 { return replace(start
, 0, srcChar
); }
4765 inline UnicodeString
&
4766 UnicodeString::remove()
4768 // remove() of a bogus string makes the string empty and non-bogus
4777 inline UnicodeString
&
4778 UnicodeString::remove(int32_t start
,
4781 if(start
<= 0 && _length
== INT32_MAX
) {
4782 // remove(guaranteed everything) of a bogus string makes the string empty and non-bogus
4785 return doReplace(start
, _length
, NULL
, 0, 0);
4788 inline UnicodeString
&
4789 UnicodeString::removeBetween(int32_t start
,
4791 { return doReplace(start
, limit
- start
, NULL
, 0, 0); }
4793 inline UnicodeString
&
4794 UnicodeString::retainBetween(int32_t start
, int32_t limit
) {
4796 return doReplace(0, start
, NULL
, 0, 0);
4800 UnicodeString::truncate(int32_t targetLength
)
4802 if(isBogus() && targetLength
== 0) {
4803 // truncate(0) of a bogus string makes the string empty and non-bogus
4806 } else if((uint32_t)targetLength
< (uint32_t)length()) {
4807 setLength(targetLength
);
4814 inline UnicodeString
&
4815 UnicodeString::reverse()
4816 { return doReverse(0, length()); }
4818 inline UnicodeString
&
4819 UnicodeString::reverse(int32_t start
,
4821 { return doReverse(start
, _length
); }
4824 #endif // U_SHOW_CPLUSPLUS_API