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