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