ICU 72.1 72.1
unistr.h
Go to the documentation of this file.
1// © 2016 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3/*
4**********************************************************************
5* Copyright (C) 1998-2016, International Business Machines
6* Corporation and others. All Rights Reserved.
7**********************************************************************
8*
9* File unistr.h
10*
11* Modification History:
12*
13* Date Name Description
14* 09/25/98 stephen Creation.
15* 11/11/98 stephen Changed per 11/9 code review.
16* 04/20/99 stephen Overhauled per 4/16 code review.
17* 11/18/99 aliu Made to inherit from Replaceable. Added method
18* handleReplaceBetween(); other methods unchanged.
19* 06/25/01 grhoten Remove dependency on iostream.
20******************************************************************************
21*/
22
23#ifndef UNISTR_H
24#define UNISTR_H
25
31#include "unicode/utypes.h"
32
33#if U_SHOW_CPLUSPLUS_API
34
35#include <cstddef>
36#include "unicode/char16ptr.h"
37#include "unicode/rep.h"
38#include "unicode/std_string.h"
39#include "unicode/stringpiece.h"
40#include "unicode/bytestream.h"
41
42struct UConverter; // unicode/ucnv.h
43
44#ifndef USTRING_H
50U_CAPI int32_t U_EXPORT2 u_strlen(const UChar *s);
51#endif
52
53U_NAMESPACE_BEGIN
54
55#if !UCONFIG_NO_BREAK_ITERATION
56class BreakIterator; // unicode/brkiter.h
57#endif
58class Edits;
59
60U_NAMESPACE_END
61
62// Not #ifndef U_HIDE_INTERNAL_API because UnicodeString needs the UStringCaseMapper.
69typedef int32_t U_CALLCONV
70UStringCaseMapper(int32_t caseLocale, uint32_t options,
73#endif
74 char16_t *dest, int32_t destCapacity,
75 const char16_t *src, int32_t srcLength,
76 icu::Edits *edits,
77 UErrorCode &errorCode);
78
79U_NAMESPACE_BEGIN
80
81class Locale; // unicode/locid.h
82class StringCharacterIterator;
83class UnicodeStringAppendable; // unicode/appendable.h
84
85/* The <iostream> include has been moved to unicode/ustream.h */
86
97#define US_INV icu::UnicodeString::kInvariant
98
116#if !U_CHAR16_IS_TYPEDEF
117# define UNICODE_STRING(cs, _length) icu::UnicodeString(true, u ## cs, _length)
118#else
119# define UNICODE_STRING(cs, _length) icu::UnicodeString(true, (const char16_t*)u ## cs, _length)
120#endif
121
135#define UNICODE_STRING_SIMPLE(cs) UNICODE_STRING(cs, -1)
136
144#ifndef UNISTR_FROM_CHAR_EXPLICIT
145# if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
146 // Auto-"explicit" in ICU library code.
147# define UNISTR_FROM_CHAR_EXPLICIT explicit
148# else
149 // Empty by default for source code compatibility.
150# define UNISTR_FROM_CHAR_EXPLICIT
151# endif
152#endif
153
164#ifndef UNISTR_FROM_STRING_EXPLICIT
165# if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
166 // Auto-"explicit" in ICU library code.
167# define UNISTR_FROM_STRING_EXPLICIT explicit
168# else
169 // Empty by default for source code compatibility.
170# define UNISTR_FROM_STRING_EXPLICIT
171# endif
172#endif
173
207#ifndef UNISTR_OBJECT_SIZE
208# define UNISTR_OBJECT_SIZE 64
209#endif
210
296{
297public:
298
312 kInvariant
313 };
314
315 //========================================
316 // Read-only operations
317 //========================================
318
319 /* Comparison - bitwise only - for international comparison use collation */
320
328 inline bool operator== (const UnicodeString& text) const;
329
337 inline bool operator!= (const UnicodeString& text) const;
338
346 inline UBool operator> (const UnicodeString& text) const;
347
355 inline UBool operator< (const UnicodeString& text) const;
356
364 inline UBool operator>= (const UnicodeString& text) const;
365
373 inline UBool operator<= (const UnicodeString& text) const;
374
386 inline int8_t compare(const UnicodeString& text) const;
387
403 inline int8_t compare(int32_t start,
404 int32_t length,
405 const UnicodeString& text) const;
406
424 inline int8_t compare(int32_t start,
425 int32_t length,
426 const UnicodeString& srcText,
427 int32_t srcStart,
428 int32_t srcLength) const;
429
442 inline int8_t compare(ConstChar16Ptr srcChars,
443 int32_t srcLength) const;
444
459 inline int8_t compare(int32_t start,
460 int32_t length,
461 const char16_t *srcChars) const;
462
480 inline int8_t compare(int32_t start,
481 int32_t length,
482 const char16_t *srcChars,
483 int32_t srcStart,
484 int32_t srcLength) const;
485
503 inline int8_t compareBetween(int32_t start,
504 int32_t limit,
505 const UnicodeString& srcText,
506 int32_t srcStart,
507 int32_t srcLimit) const;
508
526 inline int8_t compareCodePointOrder(const UnicodeString& text) const;
527
547 inline int8_t compareCodePointOrder(int32_t start,
548 int32_t length,
549 const UnicodeString& srcText) const;
550
572 inline int8_t compareCodePointOrder(int32_t start,
573 int32_t length,
574 const UnicodeString& srcText,
575 int32_t srcStart,
576 int32_t srcLength) const;
577
596 inline int8_t compareCodePointOrder(ConstChar16Ptr srcChars,
597 int32_t srcLength) const;
598
618 inline int8_t compareCodePointOrder(int32_t start,
619 int32_t length,
620 const char16_t *srcChars) const;
621
643 inline int8_t compareCodePointOrder(int32_t start,
644 int32_t length,
645 const char16_t *srcChars,
646 int32_t srcStart,
647 int32_t srcLength) const;
648
670 inline int8_t compareCodePointOrderBetween(int32_t start,
671 int32_t limit,
672 const UnicodeString& srcText,
673 int32_t srcStart,
674 int32_t srcLimit) const;
675
694 inline int8_t caseCompare(const UnicodeString& text, uint32_t options) const;
695
716 inline int8_t caseCompare(int32_t start,
717 int32_t length,
718 const UnicodeString& srcText,
719 uint32_t options) const;
720
743 inline int8_t caseCompare(int32_t start,
744 int32_t length,
745 const UnicodeString& srcText,
746 int32_t srcStart,
747 int32_t srcLength,
748 uint32_t options) const;
749
769 inline int8_t caseCompare(ConstChar16Ptr srcChars,
770 int32_t srcLength,
771 uint32_t options) const;
772
793 inline int8_t caseCompare(int32_t start,
794 int32_t length,
795 const char16_t *srcChars,
796 uint32_t options) const;
797
820 inline int8_t caseCompare(int32_t start,
821 int32_t length,
822 const char16_t *srcChars,
823 int32_t srcStart,
824 int32_t srcLength,
825 uint32_t options) const;
826
849 inline int8_t caseCompareBetween(int32_t start,
850 int32_t limit,
851 const UnicodeString& srcText,
852 int32_t srcStart,
853 int32_t srcLimit,
854 uint32_t options) const;
855
863 inline UBool startsWith(const UnicodeString& text) const;
864
875 inline UBool startsWith(const UnicodeString& srcText,
876 int32_t srcStart,
877 int32_t srcLength) const;
878
887 inline UBool startsWith(ConstChar16Ptr srcChars,
888 int32_t srcLength) const;
889
899 inline UBool startsWith(const char16_t *srcChars,
900 int32_t srcStart,
901 int32_t srcLength) const;
902
910 inline UBool endsWith(const UnicodeString& text) const;
911
922 inline UBool endsWith(const UnicodeString& srcText,
923 int32_t srcStart,
924 int32_t srcLength) const;
925
934 inline UBool endsWith(ConstChar16Ptr srcChars,
935 int32_t srcLength) const;
936
947 inline UBool endsWith(const char16_t *srcChars,
948 int32_t srcStart,
949 int32_t srcLength) const;
950
951
952 /* Searching - bitwise only */
953
962 inline int32_t indexOf(const UnicodeString& text) const;
963
973 inline int32_t indexOf(const UnicodeString& text,
974 int32_t start) const;
975
987 inline int32_t indexOf(const UnicodeString& text,
988 int32_t start,
989 int32_t length) const;
990
1007 inline int32_t indexOf(const UnicodeString& srcText,
1008 int32_t srcStart,
1009 int32_t srcLength,
1010 int32_t start,
1011 int32_t length) const;
1012
1024 inline int32_t indexOf(const char16_t *srcChars,
1025 int32_t srcLength,
1026 int32_t start) const;
1027
1040 inline int32_t indexOf(ConstChar16Ptr srcChars,
1041 int32_t srcLength,
1042 int32_t start,
1043 int32_t length) const;
1044
1061 int32_t indexOf(const char16_t *srcChars,
1062 int32_t srcStart,
1063 int32_t srcLength,
1064 int32_t start,
1065 int32_t length) const;
1066
1074 inline int32_t indexOf(char16_t c) const;
1075
1084 inline int32_t indexOf(UChar32 c) const;
1085
1094 inline int32_t indexOf(char16_t c,
1095 int32_t start) const;
1096
1106 inline int32_t indexOf(UChar32 c,
1107 int32_t start) const;
1108
1119 inline int32_t indexOf(char16_t c,
1120 int32_t start,
1121 int32_t length) const;
1122
1134 inline int32_t indexOf(UChar32 c,
1135 int32_t start,
1136 int32_t length) const;
1137
1146 inline int32_t lastIndexOf(const UnicodeString& text) const;
1147
1157 inline int32_t lastIndexOf(const UnicodeString& text,
1158 int32_t start) const;
1159
1171 inline int32_t lastIndexOf(const UnicodeString& text,
1172 int32_t start,
1173 int32_t length) const;
1174
1191 inline int32_t lastIndexOf(const UnicodeString& srcText,
1192 int32_t srcStart,
1193 int32_t srcLength,
1194 int32_t start,
1195 int32_t length) const;
1196
1207 inline int32_t lastIndexOf(const char16_t *srcChars,
1208 int32_t srcLength,
1209 int32_t start) const;
1210
1223 inline int32_t lastIndexOf(ConstChar16Ptr srcChars,
1224 int32_t srcLength,
1225 int32_t start,
1226 int32_t length) const;
1227
1244 int32_t lastIndexOf(const char16_t *srcChars,
1245 int32_t srcStart,
1246 int32_t srcLength,
1247 int32_t start,
1248 int32_t length) const;
1249
1257 inline int32_t lastIndexOf(char16_t c) const;
1258
1267 inline int32_t lastIndexOf(UChar32 c) const;
1268
1277 inline int32_t lastIndexOf(char16_t c,
1278 int32_t start) const;
1279
1289 inline int32_t lastIndexOf(UChar32 c,
1290 int32_t start) const;
1291
1302 inline int32_t lastIndexOf(char16_t c,
1303 int32_t start,
1304 int32_t length) const;
1305
1317 inline int32_t lastIndexOf(UChar32 c,
1318 int32_t start,
1319 int32_t length) const;
1320
1321
1322 /* Character access */
1323
1332 inline char16_t charAt(int32_t offset) const;
1333
1341 inline char16_t operator[] (int32_t offset) const;
1342
1354 UChar32 char32At(int32_t offset) const;
1355
1371 int32_t getChar32Start(int32_t offset) const;
1372
1389 int32_t getChar32Limit(int32_t offset) const;
1390
1441 int32_t moveIndex32(int32_t index, int32_t delta) const;
1442
1443 /* Substring extraction */
1444
1460 inline void extract(int32_t start,
1461 int32_t length,
1462 Char16Ptr dst,
1463 int32_t dstStart = 0) const;
1464
1486 int32_t
1487 extract(Char16Ptr dest, int32_t destCapacity,
1488 UErrorCode &errorCode) const;
1489
1499 inline void extract(int32_t start,
1500 int32_t length,
1501 UnicodeString& target) const;
1502
1514 inline void extractBetween(int32_t start,
1515 int32_t limit,
1516 char16_t *dst,
1517 int32_t dstStart = 0) const;
1518
1527 virtual void extractBetween(int32_t start,
1528 int32_t limit,
1529 UnicodeString& target) const override;
1530
1552 int32_t extract(int32_t start,
1553 int32_t startLength,
1554 char *target,
1555 int32_t targetCapacity,
1556 enum EInvariant inv) const;
1557
1558#if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
1559
1579 int32_t extract(int32_t start,
1580 int32_t startLength,
1581 char *target,
1582 uint32_t targetLength) const;
1583
1584#endif
1585
1586#if !UCONFIG_NO_CONVERSION
1587
1613 inline int32_t extract(int32_t start,
1614 int32_t startLength,
1615 char *target,
1616 const char *codepage = 0) const;
1617
1647 int32_t extract(int32_t start,
1648 int32_t startLength,
1649 char *target,
1650 uint32_t targetLength,
1651 const char *codepage) const;
1652
1670 int32_t extract(char *dest, int32_t destCapacity,
1671 UConverter *cnv,
1672 UErrorCode &errorCode) const;
1673
1674#endif
1675
1689 UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const;
1690
1701 inline UnicodeString tempSubStringBetween(int32_t start, int32_t limit=INT32_MAX) const;
1702
1714 void toUTF8(ByteSink &sink) const;
1715
1728 template<typename StringClass>
1729 StringClass &toUTF8String(StringClass &result) const {
1730 StringByteSink<StringClass> sbs(&result, length());
1731 toUTF8(sbs);
1732 return result;
1733 }
1734
1750 int32_t toUTF32(UChar32 *utf32, int32_t capacity, UErrorCode &errorCode) const;
1751
1752 /* Length operations */
1753
1762 inline int32_t length(void) const;
1763
1777 int32_t
1778 countChar32(int32_t start=0, int32_t length=INT32_MAX) const;
1779
1803 UBool
1804 hasMoreChar32Than(int32_t start, int32_t length, int32_t number) const;
1805
1811 inline UBool isEmpty(void) const;
1812
1822 inline int32_t getCapacity(void) const;
1823
1824 /* Other operations */
1825
1831 inline int32_t hashCode(void) const;
1832
1845 inline UBool isBogus(void) const;
1846
1847
1848 //========================================
1849 // Write operations
1850 //========================================
1851
1852 /* Assignment operations */
1853
1873
1900
1910
1917
1924 friend inline void U_EXPORT2
1926 s1.swap(s2);
1927 }
1928
1936 inline UnicodeString& operator= (char16_t ch);
1937
1945 inline UnicodeString& operator= (UChar32 ch);
1946
1958 inline UnicodeString& setTo(const UnicodeString& srcText,
1959 int32_t srcStart);
1960
1974 inline UnicodeString& setTo(const UnicodeString& srcText,
1975 int32_t srcStart,
1976 int32_t srcLength);
1977
1986 inline UnicodeString& setTo(const UnicodeString& srcText);
1987
1996 inline UnicodeString& setTo(const char16_t *srcChars,
1997 int32_t srcLength);
1998
2007 inline UnicodeString& setTo(char16_t srcChar);
2008
2017 inline UnicodeString& setTo(UChar32 srcChar);
2018
2043 ConstChar16Ptr text,
2044 int32_t textLength);
2045
2065 UnicodeString &setTo(char16_t *buffer,
2066 int32_t buffLength,
2067 int32_t buffCapacity);
2068
2109
2117 UnicodeString& setCharAt(int32_t offset,
2118 char16_t ch);
2119
2120
2121 /* Append operations */
2122
2130 inline UnicodeString& operator+= (char16_t ch);
2131
2139 inline UnicodeString& operator+= (UChar32 ch);
2140
2148 inline UnicodeString& operator+= (const UnicodeString& srcText);
2149
2164 inline UnicodeString& append(const UnicodeString& srcText,
2165 int32_t srcStart,
2166 int32_t srcLength);
2167
2175 inline UnicodeString& append(const UnicodeString& srcText);
2176
2190 inline UnicodeString& append(const char16_t *srcChars,
2191 int32_t srcStart,
2192 int32_t srcLength);
2193
2203 inline UnicodeString& append(ConstChar16Ptr srcChars,
2204 int32_t srcLength);
2205
2212 inline UnicodeString& append(char16_t srcChar);
2213
2221
2222
2223 /* Insert operations */
2224
2238 inline UnicodeString& insert(int32_t start,
2239 const UnicodeString& srcText,
2240 int32_t srcStart,
2241 int32_t srcLength);
2242
2251 inline UnicodeString& insert(int32_t start,
2252 const UnicodeString& srcText);
2253
2267 inline UnicodeString& insert(int32_t start,
2268 const char16_t *srcChars,
2269 int32_t srcStart,
2270 int32_t srcLength);
2271
2281 inline UnicodeString& insert(int32_t start,
2282 ConstChar16Ptr srcChars,
2283 int32_t srcLength);
2284
2293 inline UnicodeString& insert(int32_t start,
2294 char16_t srcChar);
2295
2304 inline UnicodeString& insert(int32_t start,
2305 UChar32 srcChar);
2306
2307
2308 /* Replace operations */
2309
2327 inline UnicodeString& replace(int32_t start,
2328 int32_t length,
2329 const UnicodeString& srcText,
2330 int32_t srcStart,
2331 int32_t srcLength);
2332
2345 inline UnicodeString& replace(int32_t start,
2346 int32_t length,
2347 const UnicodeString& srcText);
2348
2366 inline UnicodeString& replace(int32_t start,
2367 int32_t length,
2368 const char16_t *srcChars,
2369 int32_t srcStart,
2370 int32_t srcLength);
2371
2384 inline UnicodeString& replace(int32_t start,
2385 int32_t length,
2386 ConstChar16Ptr srcChars,
2387 int32_t srcLength);
2388
2400 inline UnicodeString& replace(int32_t start,
2401 int32_t length,
2402 char16_t srcChar);
2403
2415 UnicodeString& replace(int32_t start, int32_t length, UChar32 srcChar);
2416
2426 inline UnicodeString& replaceBetween(int32_t start,
2427 int32_t limit,
2428 const UnicodeString& srcText);
2429
2444 inline UnicodeString& replaceBetween(int32_t start,
2445 int32_t limit,
2446 const UnicodeString& srcText,
2447 int32_t srcStart,
2448 int32_t srcLimit);
2449
2457 virtual void handleReplaceBetween(int32_t start,
2458 int32_t limit,
2459 const UnicodeString& text) override;
2460
2466 virtual UBool hasMetaData() const override;
2467
2481 virtual void copy(int32_t start, int32_t limit, int32_t dest) override;
2482
2483 /* Search and replace operations */
2484
2493 inline UnicodeString& findAndReplace(const UnicodeString& oldText,
2494 const UnicodeString& newText);
2495
2507 inline UnicodeString& findAndReplace(int32_t start,
2508 int32_t length,
2509 const UnicodeString& oldText,
2510 const UnicodeString& newText);
2511
2530 int32_t length,
2531 const UnicodeString& oldText,
2532 int32_t oldStart,
2533 int32_t oldLength,
2534 const UnicodeString& newText,
2535 int32_t newStart,
2536 int32_t newLength);
2537
2538
2539 /* Remove operations */
2540
2549 inline UnicodeString& remove();
2550
2559 inline UnicodeString& remove(int32_t start,
2560 int32_t length = (int32_t)INT32_MAX);
2561
2570 inline UnicodeString& removeBetween(int32_t start,
2571 int32_t limit = (int32_t)INT32_MAX);
2572
2582 inline UnicodeString &retainBetween(int32_t start, int32_t limit = INT32_MAX);
2583
2584 /* Length operations */
2585
2597 UBool padLeading(int32_t targetLength,
2598 char16_t padChar = 0x0020);
2599
2611 UBool padTrailing(int32_t targetLength,
2612 char16_t padChar = 0x0020);
2613
2620 inline UBool truncate(int32_t targetLength);
2621
2628
2629
2630 /* Miscellaneous operations */
2631
2637 inline UnicodeString& reverse(void);
2638
2647 inline UnicodeString& reverse(int32_t start,
2648 int32_t length);
2649
2657
2666
2674
2683
2684#if !UCONFIG_NO_BREAK_ITERATION
2685
2713
2741 UnicodeString &toTitle(BreakIterator *titleIter, const Locale &locale);
2742
2773 UnicodeString &toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options);
2774
2775#endif
2776
2790 UnicodeString &foldCase(uint32_t options=0 /*U_FOLD_CASE_DEFAULT*/);
2791
2792 //========================================
2793 // Access to the internal buffer
2794 //========================================
2795
2839 char16_t *getBuffer(int32_t minCapacity);
2840
2861 void releaseBuffer(int32_t newLength=-1);
2862
2893 inline const char16_t *getBuffer() const;
2894
2928 const char16_t *getTerminatedBuffer();
2929
2930 //========================================
2931 // Constructors
2932 //========================================
2933
2937 inline UnicodeString();
2938
2950 UnicodeString(int32_t capacity, UChar32 c, int32_t count);
2951
2962
2973
2985
2986#if !U_CHAR16_IS_TYPEDEF
2999#endif
3000
3001#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3015#endif
3016
3027 UNISTR_FROM_STRING_EXPLICIT inline UnicodeString(const std::nullptr_t text);
3028
3036 UnicodeString(const char16_t *text,
3037 int32_t textLength);
3038
3039#if !U_CHAR16_IS_TYPEDEF
3047 UnicodeString(const uint16_t *text, int32_t textLength) :
3048 UnicodeString(ConstChar16Ptr(text), textLength) {}
3049#endif
3050
3051#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3060 UnicodeString(const wchar_t *text, int32_t textLength) :
3061 UnicodeString(ConstChar16Ptr(text), textLength) {}
3062#endif
3063
3071 inline UnicodeString(const std::nullptr_t text, int32_t textLength);
3072
3095 UnicodeString(UBool isTerminated,
3096 ConstChar16Ptr text,
3097 int32_t textLength);
3098
3117 UnicodeString(char16_t *buffer, int32_t buffLength, int32_t buffCapacity);
3118
3119#if !U_CHAR16_IS_TYPEDEF
3128 UnicodeString(uint16_t *buffer, int32_t buffLength, int32_t buffCapacity) :
3129 UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {}
3130#endif
3131
3132#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3142 UnicodeString(wchar_t *buffer, int32_t buffLength, int32_t buffCapacity) :
3143 UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {}
3144#endif
3145
3154 inline UnicodeString(std::nullptr_t buffer, int32_t buffLength, int32_t buffCapacity);
3155
3156#if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
3157
3178
3187 UnicodeString(const char *codepageData, int32_t dataLength);
3188
3189#endif
3190
3191#if !UCONFIG_NO_CONVERSION
3192
3210 UnicodeString(const char *codepageData, const char *codepage);
3211
3229 UnicodeString(const char *codepageData, int32_t dataLength, const char *codepage);
3230
3253 const char *src, int32_t srcLength,
3254 UConverter *cnv,
3255 UErrorCode &errorCode);
3256
3257#endif
3258
3282 UnicodeString(const char *src, int32_t textLength, enum EInvariant inv);
3283
3284
3302
3310
3317 UnicodeString(const UnicodeString& src, int32_t srcStart);
3318
3326 UnicodeString(const UnicodeString& src, int32_t srcStart, int32_t srcLength);
3327
3341 virtual UnicodeString *clone() const override;
3342
3347
3362
3374 static UnicodeString fromUTF32(const UChar32 *utf32, int32_t length);
3375
3376 /* Miscellaneous operations */
3377
3413
3433 UChar32 unescapeAt(int32_t &offset) const;
3434
3440 static UClassID U_EXPORT2 getStaticClassID();
3441
3447 virtual UClassID getDynamicClassID() const override;
3448
3449 //========================================
3450 // Implementation methods
3451 //========================================
3452
3453protected:
3458 virtual int32_t getLength() const override;
3459
3465 virtual char16_t getCharAt(int32_t offset) const override;
3466
3472 virtual UChar32 getChar32At(int32_t offset) const override;
3473
3474private:
3475 // For char* constructors. Could be made public.
3476 UnicodeString &setToUTF8(StringPiece utf8);
3477 // For extract(char*).
3478 // We could make a toUTF8(target, capacity, errorCode) public but not
3479 // this version: New API will be cleaner if we make callers create substrings
3480 // rather than having start+length on every method,
3481 // and it should take a UErrorCode&.
3482 int32_t
3483 toUTF8(int32_t start, int32_t len,
3484 char *target, int32_t capacity) const;
3485
3490 UBool doEquals(const UnicodeString &text, int32_t len) const;
3491
3492 inline int8_t
3493 doCompare(int32_t start,
3494 int32_t length,
3495 const UnicodeString& srcText,
3496 int32_t srcStart,
3497 int32_t srcLength) const;
3498
3499 int8_t doCompare(int32_t start,
3500 int32_t length,
3501 const char16_t *srcChars,
3502 int32_t srcStart,
3503 int32_t srcLength) const;
3504
3505 inline int8_t
3506 doCompareCodePointOrder(int32_t start,
3507 int32_t length,
3508 const UnicodeString& srcText,
3509 int32_t srcStart,
3510 int32_t srcLength) const;
3511
3512 int8_t doCompareCodePointOrder(int32_t start,
3513 int32_t length,
3514 const char16_t *srcChars,
3515 int32_t srcStart,
3516 int32_t srcLength) const;
3517
3518 inline int8_t
3519 doCaseCompare(int32_t start,
3520 int32_t length,
3521 const UnicodeString &srcText,
3522 int32_t srcStart,
3523 int32_t srcLength,
3524 uint32_t options) const;
3525
3526 int8_t
3527 doCaseCompare(int32_t start,
3528 int32_t length,
3529 const char16_t *srcChars,
3530 int32_t srcStart,
3531 int32_t srcLength,
3532 uint32_t options) const;
3533
3534 int32_t doIndexOf(char16_t c,
3535 int32_t start,
3536 int32_t length) const;
3537
3538 int32_t doIndexOf(UChar32 c,
3539 int32_t start,
3540 int32_t length) const;
3541
3542 int32_t doLastIndexOf(char16_t c,
3543 int32_t start,
3544 int32_t length) const;
3545
3546 int32_t doLastIndexOf(UChar32 c,
3547 int32_t start,
3548 int32_t length) const;
3549
3550 void doExtract(int32_t start,
3551 int32_t length,
3552 char16_t *dst,
3553 int32_t dstStart) const;
3554
3555 inline void doExtract(int32_t start,
3556 int32_t length,
3557 UnicodeString& target) const;
3558
3559 inline char16_t doCharAt(int32_t offset) const;
3560
3561 UnicodeString& doReplace(int32_t start,
3562 int32_t length,
3563 const UnicodeString& srcText,
3564 int32_t srcStart,
3565 int32_t srcLength);
3566
3567 UnicodeString& doReplace(int32_t start,
3568 int32_t length,
3569 const char16_t *srcChars,
3570 int32_t srcStart,
3571 int32_t srcLength);
3572
3573 UnicodeString& doAppend(const UnicodeString& src, int32_t srcStart, int32_t srcLength);
3574 UnicodeString& doAppend(const char16_t *srcChars, int32_t srcStart, int32_t srcLength);
3575
3576 UnicodeString& doReverse(int32_t start,
3577 int32_t length);
3578
3579 // calculate hash code
3580 int32_t doHashCode(void) const;
3581
3582 // get pointer to start of array
3583 // these do not check for kOpenGetBuffer, unlike the public getBuffer() function
3584 inline char16_t* getArrayStart(void);
3585 inline const char16_t* getArrayStart(void) const;
3586
3587 inline UBool hasShortLength() const;
3588 inline int32_t getShortLength() const;
3589
3590 // A UnicodeString object (not necessarily its current buffer)
3591 // is writable unless it isBogus() or it has an "open" getBuffer(minCapacity).
3592 inline UBool isWritable() const;
3593
3594 // Is the current buffer writable?
3595 inline UBool isBufferWritable() const;
3596
3597 // None of the following does releaseArray().
3598 inline void setZeroLength();
3599 inline void setShortLength(int32_t len);
3600 inline void setLength(int32_t len);
3601 inline void setToEmpty();
3602 inline void setArray(char16_t *array, int32_t len, int32_t capacity); // sets length but not flags
3603
3604 // allocate the array; result may be the stack buffer
3605 // sets refCount to 1 if appropriate
3606 // sets fArray, fCapacity, and flags
3607 // sets length to 0
3608 // returns boolean for success or failure
3609 UBool allocate(int32_t capacity);
3610
3611 // release the array if owned
3612 void releaseArray(void);
3613
3614 // turn a bogus string into an empty one
3615 void unBogus();
3616
3617 // implements assignment operator, copy constructor, and fastCopyFrom()
3618 UnicodeString &copyFrom(const UnicodeString &src, UBool fastCopy=false);
3619
3620 // Copies just the fields without memory management.
3621 void copyFieldsFrom(UnicodeString &src, UBool setSrcToBogus) U_NOEXCEPT;
3622
3623 // Pin start and limit to acceptable values.
3624 inline void pinIndex(int32_t& start) const;
3625 inline void pinIndices(int32_t& start,
3626 int32_t& length) const;
3627
3628#if !UCONFIG_NO_CONVERSION
3629
3630 /* Internal extract() using UConverter. */
3631 int32_t doExtract(int32_t start, int32_t length,
3632 char *dest, int32_t destCapacity,
3633 UConverter *cnv,
3634 UErrorCode &errorCode) const;
3635
3636 /*
3637 * Real constructor for converting from codepage data.
3638 * It assumes that it is called with !fRefCounted.
3639 *
3640 * If `codepage==0`, then the default converter
3641 * is used for the platform encoding.
3642 * If `codepage` is an empty string (`""`),
3643 * then a simple conversion is performed on the codepage-invariant
3644 * subset ("invariant characters") of the platform encoding. See utypes.h.
3645 */
3646 void doCodepageCreate(const char *codepageData,
3647 int32_t dataLength,
3648 const char *codepage);
3649
3650 /*
3651 * Worker function for creating a UnicodeString from
3652 * a codepage string using a UConverter.
3653 */
3654 void
3655 doCodepageCreate(const char *codepageData,
3656 int32_t dataLength,
3657 UConverter *converter,
3658 UErrorCode &status);
3659
3660#endif
3661
3662 /*
3663 * This function is called when write access to the array
3664 * is necessary.
3665 *
3666 * We need to make a copy of the array if
3667 * the buffer is read-only, or
3668 * the buffer is refCounted (shared), and refCount>1, or
3669 * the buffer is too small.
3670 *
3671 * Return false if memory could not be allocated.
3672 */
3673 UBool cloneArrayIfNeeded(int32_t newCapacity = -1,
3674 int32_t growCapacity = -1,
3675 UBool doCopyArray = true,
3676 int32_t **pBufferToDelete = 0,
3677 UBool forceClone = false);
3678
3685 caseMap(int32_t caseLocale, uint32_t options,
3687 BreakIterator *iter,
3688#endif
3689 UStringCaseMapper *stringCaseMapper);
3690
3691 // ref counting
3692 void addRef(void);
3693 int32_t removeRef(void);
3694 int32_t refCount(void) const;
3695
3696 // constants
3697 enum {
3703 US_STACKBUF_SIZE=(int32_t)(UNISTR_OBJECT_SIZE-sizeof(void *)-2)/U_SIZEOF_UCHAR,
3704 kInvalidUChar=0xffff, // U+FFFF returned by charAt(invalid index)
3705 kInvalidHashCode=0, // invalid hash code
3706 kEmptyHashCode=1, // hash code for empty string
3707
3708 // bit flag values for fLengthAndFlags
3709 kIsBogus=1, // this string is bogus, i.e., not valid or NULL
3710 kUsingStackBuffer=2,// using fUnion.fStackFields instead of fUnion.fFields
3711 kRefCounted=4, // there is a refCount field before the characters in fArray
3712 kBufferIsReadonly=8,// do not write to this buffer
3713 kOpenGetBuffer=16, // getBuffer(minCapacity) was called (is "open"),
3714 // and releaseBuffer(newLength) must be called
3715 kAllStorageFlags=0x1f,
3716
3717 kLengthShift=5, // remaining 11 bits for non-negative short length, or negative if long
3718 kLength1=1<<kLengthShift,
3719 kMaxShortLength=0x3ff, // max non-negative short length (leaves top bit 0)
3720 kLengthIsLarge=0xffe0, // short length < 0, real length is in fUnion.fFields.fLength
3721
3722 // combined values for convenience
3723 kShortString=kUsingStackBuffer,
3724 kLongString=kRefCounted,
3725 kReadonlyAlias=kBufferIsReadonly,
3726 kWritableAlias=0
3727 };
3728
3729 friend class UnicodeStringAppendable;
3730
3731 union StackBufferOrFields; // forward declaration necessary before friend declaration
3732 friend union StackBufferOrFields; // make US_STACKBUF_SIZE visible inside fUnion
3733
3734 /*
3735 * The following are all the class fields that are stored
3736 * in each UnicodeString object.
3737 * Note that UnicodeString has virtual functions,
3738 * therefore there is an implicit vtable pointer
3739 * as the first real field.
3740 * The fields should be aligned such that no padding is necessary.
3741 * On 32-bit machines, the size should be 32 bytes,
3742 * on 64-bit machines (8-byte pointers), it should be 40 bytes.
3743 *
3744 * We use a hack to achieve this.
3745 *
3746 * With at least some compilers, each of the following is forced to
3747 * a multiple of sizeof(pointer) [the largest field base unit here is a data pointer],
3748 * rounded up with additional padding if the fields do not already fit that requirement:
3749 * - sizeof(class UnicodeString)
3750 * - offsetof(UnicodeString, fUnion)
3751 * - sizeof(fUnion)
3752 * - sizeof(fStackFields)
3753 *
3754 * We optimize for the longest possible internal buffer for short strings.
3755 * fUnion.fStackFields begins with 2 bytes for storage flags
3756 * and the length of relatively short strings,
3757 * followed by the buffer for short string contents.
3758 * There is no padding inside fStackFields.
3759 *
3760 * Heap-allocated and aliased strings use fUnion.fFields.
3761 * Both fStackFields and fFields must begin with the same fields for flags and short length,
3762 * that is, those must have the same memory offsets inside the object,
3763 * because the flags must be inspected in order to decide which half of fUnion is being used.
3764 * We assume that the compiler does not reorder the fields.
3765 *
3766 * (Padding at the end of fFields is ok:
3767 * As long as it is no larger than fStackFields, it is not wasted space.)
3768 *
3769 * For some of the history of the UnicodeString class fields layout, see
3770 * - ICU ticket #11551 "longer UnicodeString contents in stack buffer"
3771 * - ICU ticket #11336 "UnicodeString: recombine stack buffer arrays"
3772 * - ICU ticket #8322 "why is sizeof(UnicodeString)==48?"
3773 */
3774 // (implicit) *vtable;
3775 union StackBufferOrFields {
3776 // fStackFields is used iff (fLengthAndFlags&kUsingStackBuffer) else fFields is used.
3777 // Each struct of the union must begin with fLengthAndFlags.
3778 struct {
3779 int16_t fLengthAndFlags; // bit fields: see constants above
3780 char16_t fBuffer[US_STACKBUF_SIZE]; // buffer for short strings
3781 } fStackFields;
3782 struct {
3783 int16_t fLengthAndFlags; // bit fields: see constants above
3784 int32_t fLength; // number of characters in fArray if >127; else undefined
3785 int32_t fCapacity; // capacity of fArray (in char16_ts)
3786 // array pointer last to minimize padding for machines with P128 data model
3787 // or pointer sizes that are not a power of 2
3788 char16_t *fArray; // the Unicode data
3789 } fFields;
3790 } fUnion;
3791};
3792
3801U_COMMON_API UnicodeString U_EXPORT2
3803
3804//========================================
3805// Inline members
3806//========================================
3807
3808//========================================
3809// Privates
3810//========================================
3811
3812inline void
3813UnicodeString::pinIndex(int32_t& start) const
3814{
3815 // pin index
3816 if(start < 0) {
3817 start = 0;
3818 } else if(start > length()) {
3819 start = length();
3820 }
3821}
3822
3823inline void
3824UnicodeString::pinIndices(int32_t& start,
3825 int32_t& _length) const
3826{
3827 // pin indices
3828 int32_t len = length();
3829 if(start < 0) {
3830 start = 0;
3831 } else if(start > len) {
3832 start = len;
3833 }
3834 if(_length < 0) {
3835 _length = 0;
3836 } else if(_length > (len - start)) {
3837 _length = (len - start);
3838 }
3839}
3840
3841inline char16_t*
3842UnicodeString::getArrayStart() {
3843 return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
3844 fUnion.fStackFields.fBuffer : fUnion.fFields.fArray;
3845}
3846
3847inline const char16_t*
3848UnicodeString::getArrayStart() const {
3849 return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
3850 fUnion.fStackFields.fBuffer : fUnion.fFields.fArray;
3851}
3852
3853//========================================
3854// Default constructor
3855//========================================
3856
3857inline
3858UnicodeString::UnicodeString() {
3859 fUnion.fStackFields.fLengthAndFlags=kShortString;
3860}
3861
3862inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/) {
3863 fUnion.fStackFields.fLengthAndFlags=kShortString;
3864}
3865
3866inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/, int32_t /*length*/) {
3867 fUnion.fStackFields.fLengthAndFlags=kShortString;
3868}
3869
3870inline UnicodeString::UnicodeString(std::nullptr_t /*buffer*/, int32_t /*buffLength*/, int32_t /*buffCapacity*/) {
3871 fUnion.fStackFields.fLengthAndFlags=kShortString;
3872}
3873
3874//========================================
3875// Read-only implementation methods
3876//========================================
3877inline UBool
3878UnicodeString::hasShortLength() const {
3879 return fUnion.fFields.fLengthAndFlags>=0;
3880}
3881
3882inline int32_t
3883UnicodeString::getShortLength() const {
3884 // fLengthAndFlags must be non-negative -> short length >= 0
3885 // and arithmetic or logical shift does not matter.
3886 return fUnion.fFields.fLengthAndFlags>>kLengthShift;
3887}
3888
3889inline int32_t
3890UnicodeString::length() const {
3891 return hasShortLength() ? getShortLength() : fUnion.fFields.fLength;
3892}
3893
3894inline int32_t
3895UnicodeString::getCapacity() const {
3896 return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
3897 US_STACKBUF_SIZE : fUnion.fFields.fCapacity;
3898}
3899
3900inline int32_t
3901UnicodeString::hashCode() const
3902{ return doHashCode(); }
3903
3904inline UBool
3905UnicodeString::isBogus() const
3906{ return (UBool)(fUnion.fFields.fLengthAndFlags & kIsBogus); }
3907
3908inline UBool
3909UnicodeString::isWritable() const
3910{ return (UBool)!(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus)); }
3911
3912inline UBool
3913UnicodeString::isBufferWritable() const
3914{
3915 return (UBool)(
3916 !(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus|kBufferIsReadonly)) &&
3917 (!(fUnion.fFields.fLengthAndFlags&kRefCounted) || refCount()==1));
3918}
3919
3920inline const char16_t *
3921UnicodeString::getBuffer() const {
3922 if(fUnion.fFields.fLengthAndFlags&(kIsBogus|kOpenGetBuffer)) {
3923 return nullptr;
3924 } else if(fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) {
3925 return fUnion.fStackFields.fBuffer;
3926 } else {
3927 return fUnion.fFields.fArray;
3928 }
3929}
3930
3931//========================================
3932// Read-only alias methods
3933//========================================
3934inline int8_t
3935UnicodeString::doCompare(int32_t start,
3936 int32_t thisLength,
3937 const UnicodeString& srcText,
3938 int32_t srcStart,
3939 int32_t srcLength) const
3940{
3941 if(srcText.isBogus()) {
3942 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
3943 } else {
3944 srcText.pinIndices(srcStart, srcLength);
3945 return doCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
3946 }
3947}
3948
3949inline bool
3951{
3952 if(isBogus()) {
3953 return text.isBogus();
3954 } else {
3955 int32_t len = length(), textLength = text.length();
3956 return !text.isBogus() && len == textLength && doEquals(text, len);
3957 }
3958}
3959
3960inline bool
3962{ return (! operator==(text)); }
3963
3964inline UBool
3965UnicodeString::operator> (const UnicodeString& text) const
3966{ return doCompare(0, length(), text, 0, text.length()) == 1; }
3967
3968inline UBool
3969UnicodeString::operator< (const UnicodeString& text) const
3970{ return doCompare(0, length(), text, 0, text.length()) == -1; }
3971
3972inline UBool
3973UnicodeString::operator>= (const UnicodeString& text) const
3974{ return doCompare(0, length(), text, 0, text.length()) != -1; }
3975
3976inline UBool
3977UnicodeString::operator<= (const UnicodeString& text) const
3978{ return doCompare(0, length(), text, 0, text.length()) != 1; }
3979
3980inline int8_t
3981UnicodeString::compare(const UnicodeString& text) const
3982{ return doCompare(0, length(), text, 0, text.length()); }
3983
3984inline int8_t
3985UnicodeString::compare(int32_t start,
3986 int32_t _length,
3987 const UnicodeString& srcText) const
3988{ return doCompare(start, _length, srcText, 0, srcText.length()); }
3989
3990inline int8_t
3991UnicodeString::compare(ConstChar16Ptr srcChars,
3992 int32_t srcLength) const
3993{ return doCompare(0, length(), srcChars, 0, srcLength); }
3994
3995inline int8_t
3996UnicodeString::compare(int32_t start,
3997 int32_t _length,
3998 const UnicodeString& srcText,
3999 int32_t srcStart,
4000 int32_t srcLength) const
4001{ return doCompare(start, _length, srcText, srcStart, srcLength); }
4002
4003inline int8_t
4004UnicodeString::compare(int32_t start,
4005 int32_t _length,
4006 const char16_t *srcChars) const
4007{ return doCompare(start, _length, srcChars, 0, _length); }
4008
4009inline int8_t
4010UnicodeString::compare(int32_t start,
4011 int32_t _length,
4012 const char16_t *srcChars,
4013 int32_t srcStart,
4014 int32_t srcLength) const
4015{ return doCompare(start, _length, srcChars, srcStart, srcLength); }
4016
4017inline int8_t
4018UnicodeString::compareBetween(int32_t start,
4019 int32_t limit,
4020 const UnicodeString& srcText,
4021 int32_t srcStart,
4022 int32_t srcLimit) const
4023{ return doCompare(start, limit - start,
4024 srcText, srcStart, srcLimit - srcStart); }
4025
4026inline int8_t
4027UnicodeString::doCompareCodePointOrder(int32_t start,
4028 int32_t thisLength,
4029 const UnicodeString& srcText,
4030 int32_t srcStart,
4031 int32_t srcLength) const
4032{
4033 if(srcText.isBogus()) {
4034 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4035 } else {
4036 srcText.pinIndices(srcStart, srcLength);
4037 return doCompareCodePointOrder(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
4038 }
4039}
4040
4041inline int8_t
4042UnicodeString::compareCodePointOrder(const UnicodeString& text) const
4043{ return doCompareCodePointOrder(0, length(), text, 0, text.length()); }
4044
4045inline int8_t
4046UnicodeString::compareCodePointOrder(int32_t start,
4047 int32_t _length,
4048 const UnicodeString& srcText) const
4049{ return doCompareCodePointOrder(start, _length, srcText, 0, srcText.length()); }
4050
4051inline int8_t
4052UnicodeString::compareCodePointOrder(ConstChar16Ptr srcChars,
4053 int32_t srcLength) const
4054{ return doCompareCodePointOrder(0, length(), srcChars, 0, srcLength); }
4055
4056inline int8_t
4057UnicodeString::compareCodePointOrder(int32_t start,
4058 int32_t _length,
4059 const UnicodeString& srcText,
4060 int32_t srcStart,
4061 int32_t srcLength) const
4062{ return doCompareCodePointOrder(start, _length, srcText, srcStart, srcLength); }
4063
4064inline int8_t
4065UnicodeString::compareCodePointOrder(int32_t start,
4066 int32_t _length,
4067 const char16_t *srcChars) const
4068{ return doCompareCodePointOrder(start, _length, srcChars, 0, _length); }
4069
4070inline int8_t
4071UnicodeString::compareCodePointOrder(int32_t start,
4072 int32_t _length,
4073 const char16_t *srcChars,
4074 int32_t srcStart,
4075 int32_t srcLength) const
4076{ return doCompareCodePointOrder(start, _length, srcChars, srcStart, srcLength); }
4077
4078inline int8_t
4079UnicodeString::compareCodePointOrderBetween(int32_t start,
4080 int32_t limit,
4081 const UnicodeString& srcText,
4082 int32_t srcStart,
4083 int32_t srcLimit) const
4084{ return doCompareCodePointOrder(start, limit - start,
4085 srcText, srcStart, srcLimit - srcStart); }
4086
4087inline int8_t
4088UnicodeString::doCaseCompare(int32_t start,
4089 int32_t thisLength,
4090 const UnicodeString &srcText,
4091 int32_t srcStart,
4092 int32_t srcLength,
4093 uint32_t options) const
4094{
4095 if(srcText.isBogus()) {
4096 return (int8_t)!isBogus(); // 0 if both are bogus, 1 otherwise
4097 } else {
4098 srcText.pinIndices(srcStart, srcLength);
4099 return doCaseCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength, options);
4100 }
4101}
4102
4103inline int8_t
4104UnicodeString::caseCompare(const UnicodeString &text, uint32_t options) const {
4105 return doCaseCompare(0, length(), text, 0, text.length(), options);
4106}
4107
4108inline int8_t
4109UnicodeString::caseCompare(int32_t start,
4110 int32_t _length,
4111 const UnicodeString &srcText,
4112 uint32_t options) const {
4113 return doCaseCompare(start, _length, srcText, 0, srcText.length(), options);
4114}
4115
4116inline int8_t
4117UnicodeString::caseCompare(ConstChar16Ptr srcChars,
4118 int32_t srcLength,
4119 uint32_t options) const {
4120 return doCaseCompare(0, length(), srcChars, 0, srcLength, options);
4121}
4122
4123inline int8_t
4124UnicodeString::caseCompare(int32_t start,
4125 int32_t _length,
4126 const UnicodeString &srcText,
4127 int32_t srcStart,
4128 int32_t srcLength,
4129 uint32_t options) const {
4130 return doCaseCompare(start, _length, srcText, srcStart, srcLength, options);
4131}
4132
4133inline int8_t
4134UnicodeString::caseCompare(int32_t start,
4135 int32_t _length,
4136 const char16_t *srcChars,
4137 uint32_t options) const {
4138 return doCaseCompare(start, _length, srcChars, 0, _length, options);
4139}
4140
4141inline int8_t
4142UnicodeString::caseCompare(int32_t start,
4143 int32_t _length,
4144 const char16_t *srcChars,
4145 int32_t srcStart,
4146 int32_t srcLength,
4147 uint32_t options) const {
4148 return doCaseCompare(start, _length, srcChars, srcStart, srcLength, options);
4149}
4150
4151inline int8_t
4152UnicodeString::caseCompareBetween(int32_t start,
4153 int32_t limit,
4154 const UnicodeString &srcText,
4155 int32_t srcStart,
4156 int32_t srcLimit,
4157 uint32_t options) const {
4158 return doCaseCompare(start, limit - start, srcText, srcStart, srcLimit - srcStart, options);
4159}
4160
4161inline int32_t
4162UnicodeString::indexOf(const UnicodeString& srcText,
4163 int32_t srcStart,
4164 int32_t srcLength,
4165 int32_t start,
4166 int32_t _length) const
4167{
4168 if(!srcText.isBogus()) {
4169 srcText.pinIndices(srcStart, srcLength);
4170 if(srcLength > 0) {
4171 return indexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length);
4172 }
4173 }
4174 return -1;
4175}
4176
4177inline int32_t
4178UnicodeString::indexOf(const UnicodeString& text) const
4179{ return indexOf(text, 0, text.length(), 0, length()); }
4180
4181inline int32_t
4182UnicodeString::indexOf(const UnicodeString& text,
4183 int32_t start) const {
4184 pinIndex(start);
4185 return indexOf(text, 0, text.length(), start, length() - start);
4186}
4187
4188inline int32_t
4189UnicodeString::indexOf(const UnicodeString& text,
4190 int32_t start,
4191 int32_t _length) const
4192{ return indexOf(text, 0, text.length(), start, _length); }
4193
4194inline int32_t
4195UnicodeString::indexOf(const char16_t *srcChars,
4196 int32_t srcLength,
4197 int32_t start) const {
4198 pinIndex(start);
4199 return indexOf(srcChars, 0, srcLength, start, length() - start);
4200}
4201
4202inline int32_t
4203UnicodeString::indexOf(ConstChar16Ptr srcChars,
4204 int32_t srcLength,
4205 int32_t start,
4206 int32_t _length) const
4207{ return indexOf(srcChars, 0, srcLength, start, _length); }
4208
4209inline int32_t
4210UnicodeString::indexOf(char16_t c,
4211 int32_t start,
4212 int32_t _length) const
4213{ return doIndexOf(c, start, _length); }
4214
4215inline int32_t
4216UnicodeString::indexOf(UChar32 c,
4217 int32_t start,
4218 int32_t _length) const
4219{ return doIndexOf(c, start, _length); }
4220
4221inline int32_t
4222UnicodeString::indexOf(char16_t c) const
4223{ return doIndexOf(c, 0, length()); }
4224
4225inline int32_t
4226UnicodeString::indexOf(UChar32 c) const
4227{ return indexOf(c, 0, length()); }
4228
4229inline int32_t
4230UnicodeString::indexOf(char16_t c,
4231 int32_t start) const {
4232 pinIndex(start);
4233 return doIndexOf(c, start, length() - start);
4234}
4235
4236inline int32_t
4237UnicodeString::indexOf(UChar32 c,
4238 int32_t start) const {
4239 pinIndex(start);
4240 return indexOf(c, start, length() - start);
4241}
4242
4243inline int32_t
4244UnicodeString::lastIndexOf(ConstChar16Ptr srcChars,
4245 int32_t srcLength,
4246 int32_t start,
4247 int32_t _length) const
4248{ return lastIndexOf(srcChars, 0, srcLength, start, _length); }
4249
4250inline int32_t
4251UnicodeString::lastIndexOf(const char16_t *srcChars,
4252 int32_t srcLength,
4253 int32_t start) const {
4254 pinIndex(start);
4255 return lastIndexOf(srcChars, 0, srcLength, start, length() - start);
4256}
4257
4258inline int32_t
4259UnicodeString::lastIndexOf(const UnicodeString& srcText,
4260 int32_t srcStart,
4261 int32_t srcLength,
4262 int32_t start,
4263 int32_t _length) const
4264{
4265 if(!srcText.isBogus()) {
4266 srcText.pinIndices(srcStart, srcLength);
4267 if(srcLength > 0) {
4268 return lastIndexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length);
4269 }
4270 }
4271 return -1;
4272}
4273
4274inline int32_t
4275UnicodeString::lastIndexOf(const UnicodeString& text,
4276 int32_t start,
4277 int32_t _length) const
4278{ return lastIndexOf(text, 0, text.length(), start, _length); }
4279
4280inline int32_t
4281UnicodeString::lastIndexOf(const UnicodeString& text,
4282 int32_t start) const {
4283 pinIndex(start);
4284 return lastIndexOf(text, 0, text.length(), start, length() - start);
4285}
4286
4287inline int32_t
4288UnicodeString::lastIndexOf(const UnicodeString& text) const
4289{ return lastIndexOf(text, 0, text.length(), 0, length()); }
4290
4291inline int32_t
4292UnicodeString::lastIndexOf(char16_t c,
4293 int32_t start,
4294 int32_t _length) const
4295{ return doLastIndexOf(c, start, _length); }
4296
4297inline int32_t
4298UnicodeString::lastIndexOf(UChar32 c,
4299 int32_t start,
4300 int32_t _length) const {
4301 return doLastIndexOf(c, start, _length);
4302}
4303
4304inline int32_t
4305UnicodeString::lastIndexOf(char16_t c) const
4306{ return doLastIndexOf(c, 0, length()); }
4307
4308inline int32_t
4309UnicodeString::lastIndexOf(UChar32 c) const {
4310 return lastIndexOf(c, 0, length());
4311}
4312
4313inline int32_t
4314UnicodeString::lastIndexOf(char16_t c,
4315 int32_t start) const {
4316 pinIndex(start);
4317 return doLastIndexOf(c, start, length() - start);
4318}
4319
4320inline int32_t
4321UnicodeString::lastIndexOf(UChar32 c,
4322 int32_t start) const {
4323 pinIndex(start);
4324 return lastIndexOf(c, start, length() - start);
4325}
4326
4327inline UBool
4328UnicodeString::startsWith(const UnicodeString& text) const
4329{ return compare(0, text.length(), text, 0, text.length()) == 0; }
4330
4331inline UBool
4332UnicodeString::startsWith(const UnicodeString& srcText,
4333 int32_t srcStart,
4334 int32_t srcLength) const
4335{ return doCompare(0, srcLength, srcText, srcStart, srcLength) == 0; }
4336
4337inline UBool
4338UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const {
4339 if(srcLength < 0) {
4340 srcLength = u_strlen(toUCharPtr(srcChars));
4341 }
4342 return doCompare(0, srcLength, srcChars, 0, srcLength) == 0;
4343}
4344
4345inline UBool
4346UnicodeString::startsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const {
4347 if(srcLength < 0) {
4348 srcLength = u_strlen(toUCharPtr(srcChars));
4349 }
4350 return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0;
4351}
4352
4353inline UBool
4354UnicodeString::endsWith(const UnicodeString& text) const
4355{ return doCompare(length() - text.length(), text.length(),
4356 text, 0, text.length()) == 0; }
4357
4358inline UBool
4359UnicodeString::endsWith(const UnicodeString& srcText,
4360 int32_t srcStart,
4361 int32_t srcLength) const {
4362 srcText.pinIndices(srcStart, srcLength);
4363 return doCompare(length() - srcLength, srcLength,
4364 srcText, srcStart, srcLength) == 0;
4365}
4366
4367inline UBool
4368UnicodeString::endsWith(ConstChar16Ptr srcChars,
4369 int32_t srcLength) const {
4370 if(srcLength < 0) {
4371 srcLength = u_strlen(toUCharPtr(srcChars));
4372 }
4373 return doCompare(length() - srcLength, srcLength,
4374 srcChars, 0, srcLength) == 0;
4375}
4376
4377inline UBool
4378UnicodeString::endsWith(const char16_t *srcChars,
4379 int32_t srcStart,
4380 int32_t srcLength) const {
4381 if(srcLength < 0) {
4382 srcLength = u_strlen(toUCharPtr(srcChars + srcStart));
4383 }
4384 return doCompare(length() - srcLength, srcLength,
4385 srcChars, srcStart, srcLength) == 0;
4386}
4387
4388//========================================
4389// replace
4390//========================================
4391inline UnicodeString&
4392UnicodeString::replace(int32_t start,
4393 int32_t _length,
4394 const UnicodeString& srcText)
4395{ return doReplace(start, _length, srcText, 0, srcText.length()); }
4396
4397inline UnicodeString&
4398UnicodeString::replace(int32_t start,
4399 int32_t _length,
4400 const UnicodeString& srcText,
4401 int32_t srcStart,
4402 int32_t srcLength)
4403{ return doReplace(start, _length, srcText, srcStart, srcLength); }
4404
4405inline UnicodeString&
4406UnicodeString::replace(int32_t start,
4407 int32_t _length,
4408 ConstChar16Ptr srcChars,
4409 int32_t srcLength)
4410{ return doReplace(start, _length, srcChars, 0, srcLength); }
4411
4412inline UnicodeString&
4413UnicodeString::replace(int32_t start,
4414 int32_t _length,
4415 const char16_t *srcChars,
4416 int32_t srcStart,
4417 int32_t srcLength)
4418{ return doReplace(start, _length, srcChars, srcStart, srcLength); }
4419
4420inline UnicodeString&
4421UnicodeString::replace(int32_t start,
4422 int32_t _length,
4423 char16_t srcChar)
4424{ return doReplace(start, _length, &srcChar, 0, 1); }
4425
4426inline UnicodeString&
4427UnicodeString::replaceBetween(int32_t start,
4428 int32_t limit,
4429 const UnicodeString& srcText)
4430{ return doReplace(start, limit - start, srcText, 0, srcText.length()); }
4431
4432inline UnicodeString&
4433UnicodeString::replaceBetween(int32_t start,
4434 int32_t limit,
4435 const UnicodeString& srcText,
4436 int32_t srcStart,
4437 int32_t srcLimit)
4438{ return doReplace(start, limit - start, srcText, srcStart, srcLimit - srcStart); }
4439
4440inline UnicodeString&
4441UnicodeString::findAndReplace(const UnicodeString& oldText,
4442 const UnicodeString& newText)
4443{ return findAndReplace(0, length(), oldText, 0, oldText.length(),
4444 newText, 0, newText.length()); }
4445
4446inline UnicodeString&
4447UnicodeString::findAndReplace(int32_t start,
4448 int32_t _length,
4449 const UnicodeString& oldText,
4450 const UnicodeString& newText)
4451{ return findAndReplace(start, _length, oldText, 0, oldText.length(),
4452 newText, 0, newText.length()); }
4453
4454// ============================
4455// extract
4456// ============================
4457inline void
4458UnicodeString::doExtract(int32_t start,
4459 int32_t _length,
4460 UnicodeString& target) const
4461{ target.replace(0, target.length(), *this, start, _length); }
4462
4463inline void
4464UnicodeString::extract(int32_t start,
4465 int32_t _length,
4466 Char16Ptr target,
4467 int32_t targetStart) const
4468{ doExtract(start, _length, target, targetStart); }
4469
4470inline void
4471UnicodeString::extract(int32_t start,
4472 int32_t _length,
4473 UnicodeString& target) const
4474{ doExtract(start, _length, target); }
4475
4476#if !UCONFIG_NO_CONVERSION
4477
4478inline int32_t
4479UnicodeString::extract(int32_t start,
4480 int32_t _length,
4481 char *dst,
4482 const char *codepage) const
4483
4484{
4485 // This dstSize value will be checked explicitly
4486 return extract(start, _length, dst, dst!=0 ? 0xffffffff : 0, codepage);
4487}
4488
4489#endif
4490
4491inline void
4492UnicodeString::extractBetween(int32_t start,
4493 int32_t limit,
4494 char16_t *dst,
4495 int32_t dstStart) const {
4496 pinIndex(start);
4497 pinIndex(limit);
4498 doExtract(start, limit - start, dst, dstStart);
4499}
4500
4501inline UnicodeString
4502UnicodeString::tempSubStringBetween(int32_t start, int32_t limit) const {
4503 return tempSubString(start, limit - start);
4504}
4505
4506inline char16_t
4507UnicodeString::doCharAt(int32_t offset) const
4508{
4509 if((uint32_t)offset < (uint32_t)length()) {
4510 return getArrayStart()[offset];
4511 } else {
4512 return kInvalidUChar;
4513 }
4514}
4515
4516inline char16_t
4517UnicodeString::charAt(int32_t offset) const
4518{ return doCharAt(offset); }
4519
4520inline char16_t
4521UnicodeString::operator[] (int32_t offset) const
4522{ return doCharAt(offset); }
4523
4524inline UBool
4525UnicodeString::isEmpty() const {
4526 // Arithmetic or logical right shift does not matter: only testing for 0.
4527 return (fUnion.fFields.fLengthAndFlags>>kLengthShift) == 0;
4528}
4529
4530//========================================
4531// Write implementation methods
4532//========================================
4533inline void
4534UnicodeString::setZeroLength() {
4535 fUnion.fFields.fLengthAndFlags &= kAllStorageFlags;
4536}
4537
4538inline void
4539UnicodeString::setShortLength(int32_t len) {
4540 // requires 0 <= len <= kMaxShortLength
4541 fUnion.fFields.fLengthAndFlags =
4542 (int16_t)((fUnion.fFields.fLengthAndFlags & kAllStorageFlags) | (len << kLengthShift));
4543}
4544
4545inline void
4546UnicodeString::setLength(int32_t len) {
4547 if(len <= kMaxShortLength) {
4548 setShortLength(len);
4549 } else {
4550 fUnion.fFields.fLengthAndFlags |= kLengthIsLarge;
4551 fUnion.fFields.fLength = len;
4552 }
4553}
4554
4555inline void
4556UnicodeString::setToEmpty() {
4557 fUnion.fFields.fLengthAndFlags = kShortString;
4558}
4559
4560inline void
4561UnicodeString::setArray(char16_t *array, int32_t len, int32_t capacity) {
4562 setLength(len);
4563 fUnion.fFields.fArray = array;
4564 fUnion.fFields.fCapacity = capacity;
4565}
4566
4567inline UnicodeString&
4568UnicodeString::operator= (char16_t ch)
4569{ return doReplace(0, length(), &ch, 0, 1); }
4570
4571inline UnicodeString&
4572UnicodeString::operator= (UChar32 ch)
4573{ return replace(0, length(), ch); }
4574
4575inline UnicodeString&
4576UnicodeString::setTo(const UnicodeString& srcText,
4577 int32_t srcStart,
4578 int32_t srcLength)
4579{
4580 unBogus();
4581 return doReplace(0, length(), srcText, srcStart, srcLength);
4582}
4583
4584inline UnicodeString&
4585UnicodeString::setTo(const UnicodeString& srcText,
4586 int32_t srcStart)
4587{
4588 unBogus();
4589 srcText.pinIndex(srcStart);
4590 return doReplace(0, length(), srcText, srcStart, srcText.length() - srcStart);
4591}
4592
4593inline UnicodeString&
4594UnicodeString::setTo(const UnicodeString& srcText)
4595{
4596 return copyFrom(srcText);
4597}
4598
4599inline UnicodeString&
4600UnicodeString::setTo(const char16_t *srcChars,
4601 int32_t srcLength)
4602{
4603 unBogus();
4604 return doReplace(0, length(), srcChars, 0, srcLength);
4605}
4606
4607inline UnicodeString&
4608UnicodeString::setTo(char16_t srcChar)
4609{
4610 unBogus();
4611 return doReplace(0, length(), &srcChar, 0, 1);
4612}
4613
4614inline UnicodeString&
4615UnicodeString::setTo(UChar32 srcChar)
4616{
4617 unBogus();
4618 return replace(0, length(), srcChar);
4619}
4620
4621inline UnicodeString&
4622UnicodeString::append(const UnicodeString& srcText,
4623 int32_t srcStart,
4624 int32_t srcLength)
4625{ return doAppend(srcText, srcStart, srcLength); }
4626
4627inline UnicodeString&
4628UnicodeString::append(const UnicodeString& srcText)
4629{ return doAppend(srcText, 0, srcText.length()); }
4630
4631inline UnicodeString&
4632UnicodeString::append(const char16_t *srcChars,
4633 int32_t srcStart,
4634 int32_t srcLength)
4635{ return doAppend(srcChars, srcStart, srcLength); }
4636
4637inline UnicodeString&
4638UnicodeString::append(ConstChar16Ptr srcChars,
4639 int32_t srcLength)
4640{ return doAppend(srcChars, 0, srcLength); }
4641
4642inline UnicodeString&
4643UnicodeString::append(char16_t srcChar)
4644{ return doAppend(&srcChar, 0, 1); }
4645
4646inline UnicodeString&
4647UnicodeString::operator+= (char16_t ch)
4648{ return doAppend(&ch, 0, 1); }
4649
4650inline UnicodeString&
4651UnicodeString::operator+= (UChar32 ch) {
4652 return append(ch);
4653}
4654
4655inline UnicodeString&
4656UnicodeString::operator+= (const UnicodeString& srcText)
4657{ return doAppend(srcText, 0, srcText.length()); }
4658
4659inline UnicodeString&
4660UnicodeString::insert(int32_t start,
4661 const UnicodeString& srcText,
4662 int32_t srcStart,
4663 int32_t srcLength)
4664{ return doReplace(start, 0, srcText, srcStart, srcLength); }
4665
4666inline UnicodeString&
4667UnicodeString::insert(int32_t start,
4668 const UnicodeString& srcText)
4669{ return doReplace(start, 0, srcText, 0, srcText.length()); }
4670
4671inline UnicodeString&
4672UnicodeString::insert(int32_t start,
4673 const char16_t *srcChars,
4674 int32_t srcStart,
4675 int32_t srcLength)
4676{ return doReplace(start, 0, srcChars, srcStart, srcLength); }
4677
4678inline UnicodeString&
4679UnicodeString::insert(int32_t start,
4680 ConstChar16Ptr srcChars,
4681 int32_t srcLength)
4682{ return doReplace(start, 0, srcChars, 0, srcLength); }
4683
4684inline UnicodeString&
4685UnicodeString::insert(int32_t start,
4686 char16_t srcChar)
4687{ return doReplace(start, 0, &srcChar, 0, 1); }
4688
4689inline UnicodeString&
4690UnicodeString::insert(int32_t start,
4691 UChar32 srcChar)
4692{ return replace(start, 0, srcChar); }
4693
4694
4695inline UnicodeString&
4696UnicodeString::remove()
4697{
4698 // remove() of a bogus string makes the string empty and non-bogus
4699 if(isBogus()) {
4700 setToEmpty();
4701 } else {
4702 setZeroLength();
4703 }
4704 return *this;
4705}
4706
4707inline UnicodeString&
4708UnicodeString::remove(int32_t start,
4709 int32_t _length)
4710{
4711 if(start <= 0 && _length == INT32_MAX) {
4712 // remove(guaranteed everything) of a bogus string makes the string empty and non-bogus
4713 return remove();
4714 }
4715 return doReplace(start, _length, NULL, 0, 0);
4716}
4717
4718inline UnicodeString&
4719UnicodeString::removeBetween(int32_t start,
4720 int32_t limit)
4721{ return doReplace(start, limit - start, NULL, 0, 0); }
4722
4723inline UnicodeString &
4724UnicodeString::retainBetween(int32_t start, int32_t limit) {
4725 truncate(limit);
4726 return doReplace(0, start, NULL, 0, 0);
4727}
4728
4729inline UBool
4730UnicodeString::truncate(int32_t targetLength)
4731{
4732 if(isBogus() && targetLength == 0) {
4733 // truncate(0) of a bogus string makes the string empty and non-bogus
4734 unBogus();
4735 return false;
4736 } else if((uint32_t)targetLength < (uint32_t)length()) {
4737 setLength(targetLength);
4738 return true;
4739 } else {
4740 return false;
4741 }
4742}
4743
4744inline UnicodeString&
4745UnicodeString::reverse()
4746{ return doReverse(0, length()); }
4747
4748inline UnicodeString&
4749UnicodeString::reverse(int32_t start,
4750 int32_t _length)
4751{ return doReverse(start, _length); }
4752
4753U_NAMESPACE_END
4754
4755#endif /* U_SHOW_CPLUSPLUS_API */
4756
4757#endif
C++ API: Interface for writing bytes, and implementation classes.
C++ API: char16_t pointer wrappers with implicit conversion from bit-compatible raw pointer types.
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
A ByteSink can be filled with bytes.
Definition: bytestream.h:53
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:42
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition: char16ptr.h:149
Records lengths of string edits but not replacement text.
Definition: edits.h:80
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
Replaceable is an abstract base class representing a string of characters that supports the replaceme...
Definition: rep.h:77
virtual void extractBetween(int32_t start, int32_t limit, UnicodeString &target) const =0
Copies characters in the range [start, limit) into the UnicodeString target.
char16_t charAt(int32_t offset) const
Returns the 16-bit code unit at the given offset into the text.
Definition: rep.h:251
int32_t length() const
Returns the number of 16-bit code units in the text.
Definition: rep.h:246
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:267
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:60
An Appendable implementation which writes to a UnicodeString.
Definition: appendable.h:156
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
int32_t indexOf(const char16_t *srcChars, int32_t srcStart, int32_t srcLength, int32_t start, int32_t length) const
Locate in this the first occurrence in the range [start, start + length) of the characters in srcChar...
UnicodeString(const UnicodeString &that)
Copy constructor.
virtual char16_t getCharAt(int32_t offset) const override
The change in Replaceable to use virtual getCharAt() allows UnicodeString::charAt() to be inline agai...
virtual int32_t getLength() const override
Implement Replaceable::getLength() (see jitterbug 1027).
UnicodeString & setTo(char16_t *buffer, int32_t buffLength, int32_t buffCapacity)
Aliasing setTo() function, analogous to the writable-aliasing char16_t* constructor.
UChar32 unescapeAt(int32_t &offset) const
Unescape a single escape sequence and return the represented character.
UnicodeString(const wchar_t *text, int32_t textLength)
wchar_t * constructor.
Definition: unistr.h:3060
virtual void handleReplaceBetween(int32_t start, int32_t limit, const UnicodeString &text) override
Replace a substring of this object with the given text.
UBool hasMoreChar32Than(int32_t start, int32_t length, int32_t number) const
Check if the length char16_t code units of the string contain more Unicode code points than a certain...
UnicodeString(const UnicodeString &src, int32_t srcStart, int32_t srcLength)
'Substring' constructor from subrange of source string.
virtual ~UnicodeString()
Destructor.
UnicodeString(const char *codepageData, int32_t dataLength, const char *codepage)
char* constructor.
UnicodeString(const char *codepageData, const char *codepage)
char* constructor.
UnicodeString(UBool isTerminated, ConstChar16Ptr text, int32_t textLength)
Readonly-aliasing char16_t* constructor.
UnicodeString & toTitle(BreakIterator *titleIter, const Locale &locale, uint32_t options)
Titlecase this string, with options.
UnicodeString & append(UChar32 srcChar)
Append the code point srcChar to the UnicodeString object.
EInvariant
Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor which constructs a ...
Definition: unistr.h:307
UnicodeString unescape() const
Unescape a string of characters and return a string containing the result.
UnicodeString(const UnicodeString &src, int32_t srcStart)
'Substring' constructor from tail of source string.
int32_t getChar32Limit(int32_t offset) const
Adjust a random-access offset so that it points behind a Unicode character.
UnicodeString(char16_t *buffer, int32_t buffLength, int32_t buffCapacity)
Writable-aliasing char16_t* constructor.
UnicodeString & findAndReplace(int32_t start, int32_t length, const UnicodeString &oldText, int32_t oldStart, int32_t oldLength, const UnicodeString &newText, int32_t newStart, int32_t newLength)
Replace all occurrences of characters in oldText in the range [oldStart, oldStart + oldLength) with t...
UnicodeString(int32_t capacity, UChar32 c, int32_t count)
Construct a UnicodeString with capacity to hold capacity char16_ts.
UNISTR_FROM_STRING_EXPLICIT UnicodeString(const uint16_t *text)
uint16_t * constructor.
Definition: unistr.h:2997
int32_t extract(int32_t start, int32_t startLength, char *target, uint32_t targetLength, const char *codepage) const
Copy the characters in the range [start, start + length) into an array of characters in a specified c...
virtual void copy(int32_t start, int32_t limit, int32_t dest) override
Copy a substring of this object, retaining attribute (out-of-band) information.
virtual void extractBetween(int32_t start, int32_t limit, UnicodeString &target) const override
Copy the characters in the range [start, limit) into the UnicodeString target.
UnicodeString & replace(int32_t start, int32_t length, const UnicodeString &srcText, int32_t srcStart, int32_t srcLength)
Replace the characters in the range [start, start + length) with the characters in srcText in the ran...
Definition: unistr.h:4398
UnicodeString & toTitle(BreakIterator *titleIter)
Titlecase this string, convenience function using the default locale.
UnicodeString & fastCopyFrom(const UnicodeString &src)
Almost the same as the assignment operator.
virtual UnicodeString * clone() const override
Clone this object, an instance of a subclass of Replaceable.
UBool padLeading(int32_t targetLength, char16_t padChar=0x0020)
Pad the start of this UnicodeString with the character padChar.
int32_t getChar32Start(int32_t offset) const
Adjust a random-access offset so that it points to the beginning of a Unicode character.
UChar32 char32At(int32_t offset) const
Return the code point that contains the code unit at offset offset.
UnicodeString(const char *src, int32_t textLength, enum EInvariant inv)
Constructs a Unicode string from an invariant-character char * string.
UnicodeString & trim(void)
Trims leading and trailing whitespace from this UnicodeString.
int32_t length(void) const
Return the length of the UnicodeString object.
Definition: unistr.h:3890
UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const
Create a temporary substring for the specified range.
int32_t extract(Char16Ptr dest, int32_t destCapacity, UErrorCode &errorCode) const
Copy the contents of the string into dest.
virtual UChar32 getChar32At(int32_t offset) const override
The change in Replaceable to use virtual getChar32At() allows UnicodeString::char32At() to be inline ...
int32_t extract(int32_t start, int32_t startLength, char *target, uint32_t targetLength) const
Copy the characters in the range [start, start + length) into an array of characters in the platform'...
static UnicodeString fromUTF8(StringPiece utf8)
Create a UnicodeString from a UTF-8 string.
UNISTR_FROM_CHAR_EXPLICIT UnicodeString(char16_t ch)
Single char16_t (code unit) constructor.
int32_t lastIndexOf(const char16_t *srcChars, int32_t srcStart, int32_t srcLength, int32_t start, int32_t length) const
Locate in this the last occurrence in the range [start, start + length) of the characters in srcChars...
UnicodeString(wchar_t *buffer, int32_t buffLength, int32_t buffCapacity)
Writable-aliasing wchar_t * constructor.
Definition: unistr.h:3142
void setToBogus()
Make this UnicodeString object invalid.
int32_t moveIndex32(int32_t index, int32_t delta) const
Move the code unit index along the string by delta code points.
static UnicodeString fromUTF32(const UChar32 *utf32, int32_t length)
Create a UnicodeString from a UTF-32 string.
UnicodeString & replace(int32_t start, int32_t length, UChar32 srcChar)
Replace the characters in the range [start, start + length) with the code point srcChar.
int32_t countChar32(int32_t start=0, int32_t length=INT32_MAX) const
Count Unicode code points in the length char16_t code units of the string.
UnicodeString & toUpper(void)
Convert the characters in this to UPPER CASE following the conventions of the default locale.
UNISTR_FROM_CHAR_EXPLICIT UnicodeString(UChar32 ch)
Single UChar32 (code point) constructor.
UnicodeString & operator=(const UnicodeString &srcText)
Assignment operator.
UnicodeString & setTo(UBool isTerminated, ConstChar16Ptr text, int32_t textLength)
Aliasing setTo() function, analogous to the readonly-aliasing char16_t* constructor.
int32_t extract(char *dest, int32_t destCapacity, UConverter *cnv, UErrorCode &errorCode) const
Convert the UnicodeString into a codepage string using an existing UConverter.
StringClass & toUTF8String(StringClass &result) const
Convert the UnicodeString to UTF-8 and append the result to a standard string.
Definition: unistr.h:1729
UnicodeString(const char16_t *text, int32_t textLength)
char16_t* constructor.
UnicodeString(const char *src, int32_t srcLength, UConverter *cnv, UErrorCode &errorCode)
char * / UConverter constructor.
UnicodeString(const char *codepageData, int32_t dataLength)
char* constructor.
static UClassID getStaticClassID()
ICU "poor man's RTTI", returns a UClassID for this class.
UnicodeString(uint16_t *buffer, int32_t buffLength, int32_t buffCapacity)
Writable-aliasing uint16_t * constructor.
Definition: unistr.h:3128
int32_t toUTF32(UChar32 *utf32, int32_t capacity, UErrorCode &errorCode) const
Convert the UnicodeString to UTF-32.
UnicodeString & toLower(const Locale &locale)
Convert the characters in this to lower case following the conventions of a specific locale.
UnicodeString & toTitle(BreakIterator *titleIter, const Locale &locale)
Titlecase this string.
UnicodeString & foldCase(uint32_t options=0)
Case-folds the characters in this string.
const char16_t * getTerminatedBuffer()
Get a read-only pointer to the internal buffer, making sure that it is NUL-terminated.
UnicodeString(const uint16_t *text, int32_t textLength)
uint16_t * constructor.
Definition: unistr.h:3047
UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char16_t *text)
char16_t* constructor.
UnicodeString & toUpper(const Locale &locale)
Convert the characters in this to UPPER CASE following the conventions of a specific locale.
UnicodeString & setCharAt(int32_t offset, char16_t ch)
Set the character at the specified offset to the specified character.
virtual UClassID getDynamicClassID() const override
ICU "poor man's RTTI", returns a UClassID for the actual class.
int32_t extract(int32_t start, int32_t startLength, char *target, int32_t targetCapacity, enum EInvariant inv) const
Copy the characters in the range [start, start + startLength) into an array of characters.
UNISTR_FROM_STRING_EXPLICIT UnicodeString(const wchar_t *text)
wchar_t * constructor.
Definition: unistr.h:3013
UBool isBogus(void) const
Determine if this object contains a valid string.
Definition: unistr.h:3905
char16_t * getBuffer(int32_t minCapacity)
Get a read/write pointer to the internal buffer.
friend void swap(UnicodeString &s1, UnicodeString &s2) U_NOEXCEPT
Non-member UnicodeString swap function.
Definition: unistr.h:1925
UBool padTrailing(int32_t targetLength, char16_t padChar=0x0020)
Pad the end of this UnicodeString with the character padChar.
UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char *codepageData)
char* constructor.
UnicodeString(UnicodeString &&src) U_NOEXCEPT
Move constructor; might leave src in bogus state.
void releaseBuffer(int32_t newLength=-1)
Release a read/write buffer on a UnicodeString object with an "open" getBuffer(minCapacity).
void swap(UnicodeString &other) U_NOEXCEPT
Swap strings.
void toUTF8(ByteSink &sink) const
Convert the UnicodeString to UTF-8 and write the result to a ByteSink.
UnicodeString & operator=(UnicodeString &&src) U_NOEXCEPT
Move assignment operator; might leave src in bogus state.
virtual UBool hasMetaData() const override
Replaceable API.
UnicodeString & toLower(void)
Convert the characters in this to lower case following the conventions of the default locale.
U_CAPI int32_t u_strlen(const UChar *s)
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
U_COMMON_API UnicodeString operator+(const UnicodeString &s1, const UnicodeString &s2)
Create a new UnicodeString with the concatenation of two others.
const UChar * toUCharPtr(const char16_t *p)
Converts from const char16_t * to const UChar *.
Definition: char16ptr.h:260
#define U_CALLCONV
Similar to U_CDECL_BEGIN/U_CDECL_END, this qualifier is necessary in callback function typedefs to ma...
Definition: platform.h:885
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
C++ API: Replaceable String.
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
C++ API: StringPiece: Read-only byte string wrapper class.
struct UConverter UConverter
Definition: ucnv_err.h:96
#define UCONFIG_NO_BREAK_ITERATION
This switch turns off break iteration.
Definition: uconfig.h:348
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition: umachine.h:461
#define INT32_MAX
The largest value a 32 bit signed integer can hold.
Definition: umachine.h:208
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
#define U_CAPI
This is used to declare a function as a public ICU C API.
Definition: umachine.h:110
char16_t UChar
The base type for UTF-16 code units and pointers.
Definition: umachine.h:412
#define U_SIZEOF_UCHAR
Number of bytes in a UChar.
Definition: umachine.h:352
#define UNISTR_FROM_CHAR_EXPLICIT
This can be defined to be empty or "explicit".
Definition: unistr.h:150
int32_t UStringCaseMapper(int32_t caseLocale, uint32_t options, icu::BreakIterator *iter, char16_t *dest, int32_t destCapacity, const char16_t *src, int32_t srcLength, icu::Edits *edits, UErrorCode &errorCode)
Internal string case mapping function type.
Definition: unistr.h:70
#define UNISTR_FROM_STRING_EXPLICIT
This can be defined to be empty or "explicit".
Definition: unistr.h:170
#define UNISTR_OBJECT_SIZE
Desired sizeof(UnicodeString) in bytes.
Definition: unistr.h:208
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:300