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