ICU4C 79
Loading...
Searching...
No Matches
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
30
31#include "unicode/utypes.h"
32
33#if U_SHOW_CPLUSPLUS_API
34
35#include <cstddef>
36#include <string_view>
37#include "unicode/char16ptr.h"
38#include "unicode/rep.h"
39#include "unicode/std_string.h"
40#include "unicode/stringpiece.h"
41#include "unicode/bytestream.h"
42
43struct UConverter; // unicode/ucnv.h
44
45#ifndef USTRING_H
51U_CAPI int32_t U_EXPORT2 u_strlen(const UChar *s);
52#endif
53
54U_NAMESPACE_BEGIN
55
56#if !UCONFIG_NO_BREAK_ITERATION
57class BreakIterator; // unicode/brkiter.h
58#endif
59class Edits;
60
61U_NAMESPACE_END
62
63// Not #ifndef U_HIDE_INTERNAL_API because UnicodeString needs the UStringCaseMapper.
70typedef int32_t U_CALLCONV
71UStringCaseMapper(int32_t caseLocale, uint32_t options,
74#endif
75 char16_t *dest, int32_t destCapacity,
76 const char16_t *src, int32_t srcLength,
77 icu::Edits *edits,
78 UErrorCode &errorCode);
79
80U_NAMESPACE_BEGIN
81
82class Locale; // unicode/locid.h
83class StringCharacterIterator;
84class UnicodeStringAppendable; // unicode/appendable.h
85
86/* The <iostream> include has been moved to unicode/ustream.h */
87
98#define US_INV icu::UnicodeString::kInvariant
99
120#if !U_CHAR16_IS_TYPEDEF
121# define UNICODE_STRING(cs, _length) icu::UnicodeString(true, u ## cs, _length)
122#else
123# define UNICODE_STRING(cs, _length) icu::UnicodeString(true, (const char16_t*)u ## cs, _length)
124#endif
125
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
303{
304public:
306 using value_type = char16_t;
307
323
324 //========================================
325 // Read-only operations
326 //========================================
327
328 /* Comparison - bitwise only - for international comparison use collation */
329
337 inline bool operator== (const UnicodeString& text) const;
338
354 template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
355 inline bool operator==(const S &text) const {
356 std::u16string_view sv(internal::toU16StringView(text));
357 uint32_t len; // unsigned to avoid a compiler warning
358 return !isBogus() && (len = length()) == sv.length() && doEquals(sv.data(), len);
359 }
360
368 inline bool operator!= (const UnicodeString& text) const;
369
387 template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
388 inline bool operator!=(const S &text) const {
389 return !operator==(text);
390 }
391
399 inline UBool operator> (const UnicodeString& text) const;
400
408 inline UBool operator< (const UnicodeString& text) const;
409
417 inline UBool operator>= (const UnicodeString& text) const;
418
426 inline UBool operator<= (const UnicodeString& text) const;
427
439 inline int8_t compare(const UnicodeString& text) const;
440
456 inline int8_t compare(int32_t start,
457 int32_t length,
458 const UnicodeString& text) const;
459
477 inline int8_t compare(int32_t start,
478 int32_t length,
479 const UnicodeString& srcText,
480 int32_t srcStart,
481 int32_t srcLength) const;
482
495 inline int8_t compare(ConstChar16Ptr srcChars,
496 int32_t srcLength) const;
497
512 inline int8_t compare(int32_t start,
513 int32_t length,
514 const char16_t *srcChars) const;
515
533 inline int8_t compare(int32_t start,
534 int32_t length,
535 const char16_t *srcChars,
536 int32_t srcStart,
537 int32_t srcLength) const;
538
556 inline int8_t compareBetween(int32_t start,
557 int32_t limit,
558 const UnicodeString& srcText,
559 int32_t srcStart,
560 int32_t srcLimit) const;
561
579 inline int8_t compareCodePointOrder(const UnicodeString& text) const;
580
600 inline int8_t compareCodePointOrder(int32_t start,
601 int32_t length,
602 const UnicodeString& srcText) const;
603
625 inline int8_t compareCodePointOrder(int32_t start,
626 int32_t length,
627 const UnicodeString& srcText,
628 int32_t srcStart,
629 int32_t srcLength) const;
630
649 inline int8_t compareCodePointOrder(ConstChar16Ptr srcChars,
650 int32_t srcLength) const;
651
671 inline int8_t compareCodePointOrder(int32_t start,
672 int32_t length,
673 const char16_t *srcChars) const;
674
696 inline int8_t compareCodePointOrder(int32_t start,
697 int32_t length,
698 const char16_t *srcChars,
699 int32_t srcStart,
700 int32_t srcLength) const;
701
723 inline int8_t compareCodePointOrderBetween(int32_t start,
724 int32_t limit,
725 const UnicodeString& srcText,
726 int32_t srcStart,
727 int32_t srcLimit) const;
728
747 inline int8_t caseCompare(const UnicodeString& text, uint32_t options) const;
748
769 inline int8_t caseCompare(int32_t start,
770 int32_t length,
771 const UnicodeString& srcText,
772 uint32_t options) const;
773
796 inline int8_t caseCompare(int32_t start,
797 int32_t length,
798 const UnicodeString& srcText,
799 int32_t srcStart,
800 int32_t srcLength,
801 uint32_t options) const;
802
822 inline int8_t caseCompare(ConstChar16Ptr srcChars,
823 int32_t srcLength,
824 uint32_t options) const;
825
846 inline int8_t caseCompare(int32_t start,
847 int32_t length,
848 const char16_t *srcChars,
849 uint32_t options) const;
850
873 inline int8_t caseCompare(int32_t start,
874 int32_t length,
875 const char16_t *srcChars,
876 int32_t srcStart,
877 int32_t srcLength,
878 uint32_t options) const;
879
902 inline int8_t caseCompareBetween(int32_t start,
903 int32_t limit,
904 const UnicodeString& srcText,
905 int32_t srcStart,
906 int32_t srcLimit,
907 uint32_t options) const;
908
916 inline UBool startsWith(const UnicodeString& text) const;
917
928 inline UBool startsWith(const UnicodeString& srcText,
929 int32_t srcStart,
930 int32_t srcLength) const;
931
940 inline UBool startsWith(ConstChar16Ptr srcChars,
941 int32_t srcLength) const;
942
952 inline UBool startsWith(const char16_t *srcChars,
953 int32_t srcStart,
954 int32_t srcLength) const;
955
963 inline UBool endsWith(const UnicodeString& text) const;
964
975 inline UBool endsWith(const UnicodeString& srcText,
976 int32_t srcStart,
977 int32_t srcLength) const;
978
987 inline UBool endsWith(ConstChar16Ptr srcChars,
988 int32_t srcLength) const;
989
1000 inline UBool endsWith(const char16_t *srcChars,
1001 int32_t srcStart,
1002 int32_t srcLength) const;
1003
1004
1005 /* Searching - bitwise only */
1006
1015 inline int32_t indexOf(const UnicodeString& text) const;
1016
1026 inline int32_t indexOf(const UnicodeString& text,
1027 int32_t start) const;
1028
1040 inline int32_t indexOf(const UnicodeString& text,
1041 int32_t start,
1042 int32_t length) const;
1043
1060 inline int32_t indexOf(const UnicodeString& srcText,
1061 int32_t srcStart,
1062 int32_t srcLength,
1063 int32_t start,
1064 int32_t length) const;
1065
1077 inline int32_t indexOf(const char16_t *srcChars,
1078 int32_t srcLength,
1079 int32_t start) const;
1080
1093 inline int32_t indexOf(ConstChar16Ptr srcChars,
1094 int32_t srcLength,
1095 int32_t start,
1096 int32_t length) const;
1097
1114 int32_t indexOf(const char16_t *srcChars,
1115 int32_t srcStart,
1116 int32_t srcLength,
1117 int32_t start,
1118 int32_t length) const;
1119
1127 inline int32_t indexOf(char16_t c) const;
1128
1137 inline int32_t indexOf(UChar32 c) const;
1138
1147 inline int32_t indexOf(char16_t c,
1148 int32_t start) const;
1149
1159 inline int32_t indexOf(UChar32 c,
1160 int32_t start) const;
1161
1172 inline int32_t indexOf(char16_t c,
1173 int32_t start,
1174 int32_t length) const;
1175
1187 inline int32_t indexOf(UChar32 c,
1188 int32_t start,
1189 int32_t length) const;
1190
1199 inline int32_t lastIndexOf(const UnicodeString& text) const;
1200
1210 inline int32_t lastIndexOf(const UnicodeString& text,
1211 int32_t start) const;
1212
1224 inline int32_t lastIndexOf(const UnicodeString& text,
1225 int32_t start,
1226 int32_t length) const;
1227
1244 inline int32_t lastIndexOf(const UnicodeString& srcText,
1245 int32_t srcStart,
1246 int32_t srcLength,
1247 int32_t start,
1248 int32_t length) const;
1249
1260 inline int32_t lastIndexOf(const char16_t *srcChars,
1261 int32_t srcLength,
1262 int32_t start) const;
1263
1276 inline int32_t lastIndexOf(ConstChar16Ptr srcChars,
1277 int32_t srcLength,
1278 int32_t start,
1279 int32_t length) const;
1280
1297 int32_t lastIndexOf(const char16_t *srcChars,
1298 int32_t srcStart,
1299 int32_t srcLength,
1300 int32_t start,
1301 int32_t length) const;
1302
1310 inline int32_t lastIndexOf(char16_t c) const;
1311
1320 inline int32_t lastIndexOf(UChar32 c) const;
1321
1330 inline int32_t lastIndexOf(char16_t c,
1331 int32_t start) const;
1332
1342 inline int32_t lastIndexOf(UChar32 c,
1343 int32_t start) const;
1344
1355 inline int32_t lastIndexOf(char16_t c,
1356 int32_t start,
1357 int32_t length) const;
1358
1370 inline int32_t lastIndexOf(UChar32 c,
1371 int32_t start,
1372 int32_t length) const;
1373
1374
1375 /* Character access */
1376
1385 inline char16_t charAt(int32_t offset) const;
1386
1394 inline char16_t operator[] (int32_t offset) const;
1395
1407 UChar32 char32At(int32_t offset) const;
1408
1424 int32_t getChar32Start(int32_t offset) const;
1425
1442 int32_t getChar32Limit(int32_t offset) const;
1443
1494 int32_t moveIndex32(int32_t index, int32_t delta) const;
1495
1496 /* Substring extraction */
1497
1513 inline void extract(int32_t start,
1514 int32_t length,
1515 Char16Ptr dst,
1516 int32_t dstStart = 0) const;
1517
1539 int32_t
1540 extract(Char16Ptr dest, int32_t destCapacity,
1541 UErrorCode &errorCode) const;
1542
1552 inline void extract(int32_t start,
1553 int32_t length,
1554 UnicodeString& target) const;
1555
1567 inline void extractBetween(int32_t start,
1568 int32_t limit,
1569 char16_t *dst,
1570 int32_t dstStart = 0) const;
1571
1580 virtual void extractBetween(int32_t start,
1581 int32_t limit,
1582 UnicodeString& target) const override;
1583
1605 int32_t extract(int32_t start,
1606 int32_t startLength,
1607 char *target,
1608 int32_t targetCapacity,
1609 enum EInvariant inv) const;
1610
1611#if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
1612
1632 int32_t extract(int32_t start,
1633 int32_t startLength,
1634 char *target,
1635 uint32_t targetLength) const;
1636
1637#endif
1638
1639#if !UCONFIG_NO_CONVERSION
1640
1666 inline int32_t extract(int32_t start,
1667 int32_t startLength,
1668 char* target,
1669 const char* codepage = nullptr) const;
1670
1700 int32_t extract(int32_t start,
1701 int32_t startLength,
1702 char *target,
1703 uint32_t targetLength,
1704 const char *codepage) const;
1705
1723 int32_t extract(char *dest, int32_t destCapacity,
1724 UConverter *cnv,
1725 UErrorCode &errorCode) const;
1726
1727#endif
1728
1742 UnicodeString tempSubString(int32_t start=0, int32_t length=INT32_MAX) const U_LIFETIME_BOUND;
1743
1754 inline UnicodeString tempSubStringBetween(int32_t start,
1755 int32_t limit = INT32_MAX) const U_LIFETIME_BOUND;
1756
1768 void toUTF8(ByteSink &sink) const;
1769
1783 template <typename StringClass>
1784 StringClass& toUTF8String(StringClass& result U_LIFETIME_BOUND) const {
1785 StringByteSink<StringClass> sbs(&result, length());
1786 toUTF8(sbs);
1787 return result;
1788 }
1789
1790#ifndef U_HIDE_DRAFT_API
1802 template<typename StringClass>
1803 StringClass toUTF8String() const {
1804 StringClass result;
1805 StringByteSink<StringClass> sbs(&result, length());
1806 toUTF8(sbs);
1807 return result;
1808 }
1809#endif // U_HIDE_DRAFT_API
1810
1826 int32_t toUTF32(UChar32 *utf32, int32_t capacity, UErrorCode &errorCode) const;
1827
1828 /* Length operations */
1829
1838 inline int32_t length() const;
1839
1853 int32_t
1854 countChar32(int32_t start=0, int32_t length=INT32_MAX) const;
1855
1879 UBool
1880 hasMoreChar32Than(int32_t start, int32_t length, int32_t number) const;
1881
1887 inline UBool isEmpty() const;
1888
1898 inline int32_t getCapacity() const;
1899
1900 /* Other operations */
1901
1907 inline int32_t hashCode() const;
1908
1921 inline UBool isBogus() const;
1922
1923#ifndef U_HIDE_DRAFT_API
1924private:
1925 // These type aliases are private; there is no guarantee that they will remain
1926 // aliases to the same types in subsequent versions of ICU.
1927 // Note that whether `std::u16string_view::const_iterator` is a pointer or a
1928 // class that models contiguous_iterator is platform-dependent.
1929 using unspecified_iterator = std::u16string_view::const_iterator;
1930 using unspecified_reverse_iterator = std::u16string_view::const_reverse_iterator;
1931
1932public:
1938 unspecified_iterator begin() const { return std::u16string_view(*this).begin(); }
1944 unspecified_iterator end() const { return std::u16string_view(*this).end(); }
1950 unspecified_reverse_iterator rbegin() const { return std::u16string_view(*this).rbegin(); }
1956 unspecified_reverse_iterator rend() const { return std::u16string_view(*this).rend(); }
1957#endif // U_HIDE_DRAFT_API
1958
1959 //========================================
1960 // Write operations
1961 //========================================
1962
1963 /* Assignment operations */
1964
1983 UnicodeString& operator=(const UnicodeString& srcText) U_LIFETIME_BOUND;
1984
2010 UnicodeString& fastCopyFrom(const UnicodeString& src) U_LIFETIME_BOUND;
2011
2022 template <typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
2023 inline UnicodeString& operator=(const S& src) U_LIFETIME_BOUND {
2024 unBogus();
2025 return doReplace(0, length(), internal::toU16StringView(src));
2026 }
2027
2036 UnicodeString& operator=(UnicodeString&& src) noexcept U_LIFETIME_BOUND;
2037
2043 void swap(UnicodeString &other) noexcept;
2044
2051 friend inline void U_EXPORT2
2052 swap(UnicodeString &s1, UnicodeString &s2) noexcept {
2053 s1.swap(s2);
2054 }
2055
2063 inline UnicodeString& operator=(char16_t ch) U_LIFETIME_BOUND;
2064
2072 inline UnicodeString& operator=(UChar32 ch) U_LIFETIME_BOUND;
2073
2085 inline UnicodeString& setTo(const UnicodeString& srcText, int32_t srcStart) U_LIFETIME_BOUND;
2086
2100 inline UnicodeString& setTo(const UnicodeString& srcText,
2101 int32_t srcStart,
2102 int32_t srcLength) U_LIFETIME_BOUND;
2103
2112 inline UnicodeString& setTo(const UnicodeString& srcText) U_LIFETIME_BOUND;
2113
2122 inline UnicodeString& setTo(const char16_t* srcChars, int32_t srcLength) U_LIFETIME_BOUND;
2123
2132 inline UnicodeString& setTo(char16_t srcChar) U_LIFETIME_BOUND;
2133
2142 inline UnicodeString& setTo(UChar32 srcChar) U_LIFETIME_BOUND;
2143
2168 ConstChar16Ptr text U_LIFETIME_BOUND,
2169 int32_t textLength) U_LIFETIME_BOUND;
2170
2190 UnicodeString& setTo(char16_t* buffer U_LIFETIME_BOUND,
2191 int32_t buffLength,
2192 int32_t buffCapacity) U_LIFETIME_BOUND;
2193
2234
2242 UnicodeString& setCharAt(int32_t offset, char16_t ch) U_LIFETIME_BOUND;
2243
2244 /* Append operations */
2245
2253 inline UnicodeString& operator+=(char16_t ch) U_LIFETIME_BOUND;
2254
2262 inline UnicodeString& operator+=(UChar32 ch) U_LIFETIME_BOUND;
2263
2271 inline UnicodeString& operator+=(const UnicodeString& srcText) U_LIFETIME_BOUND;
2272
2283 template <typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
2284 inline UnicodeString& operator+=(const S& src) U_LIFETIME_BOUND {
2285 return doAppend(internal::toU16StringView(src));
2286 }
2287
2302 inline UnicodeString& append(const UnicodeString& srcText,
2303 int32_t srcStart,
2304 int32_t srcLength) U_LIFETIME_BOUND;
2305
2313 inline UnicodeString& append(const UnicodeString& srcText) U_LIFETIME_BOUND;
2314
2328 inline UnicodeString& append(const char16_t* srcChars,
2329 int32_t srcStart,
2330 int32_t srcLength) U_LIFETIME_BOUND;
2331
2341 inline UnicodeString& append(ConstChar16Ptr srcChars, int32_t srcLength) U_LIFETIME_BOUND;
2342
2353 template <typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
2354 inline UnicodeString& append(const S& src) U_LIFETIME_BOUND {
2355 return doAppend(internal::toU16StringView(src));
2356 }
2357
2364 inline UnicodeString& append(char16_t srcChar) U_LIFETIME_BOUND;
2365
2372 UnicodeString& append(UChar32 srcChar) U_LIFETIME_BOUND;
2373
2374#ifndef U_HIDE_DRAFT_API
2382 inline void push_back(char16_t c) { append(c); }
2383#endif // U_HIDE_DRAFT_API
2384
2385 /* Insert operations */
2386
2400 inline UnicodeString& insert(int32_t start,
2401 const UnicodeString& srcText,
2402 int32_t srcStart,
2403 int32_t srcLength) U_LIFETIME_BOUND;
2404
2413 inline UnicodeString& insert(int32_t start, const UnicodeString& srcText) U_LIFETIME_BOUND;
2414
2428 inline UnicodeString& insert(int32_t start,
2429 const char16_t* srcChars,
2430 int32_t srcStart,
2431 int32_t srcLength) U_LIFETIME_BOUND;
2432
2442 inline UnicodeString& insert(int32_t start,
2443 ConstChar16Ptr srcChars,
2444 int32_t srcLength) U_LIFETIME_BOUND;
2445
2454 inline UnicodeString& insert(int32_t start, char16_t srcChar) U_LIFETIME_BOUND;
2455
2464 inline UnicodeString& insert(int32_t start, UChar32 srcChar) U_LIFETIME_BOUND;
2465
2466 /* Replace operations */
2467
2485 inline UnicodeString& replace(int32_t start,
2486 int32_t length,
2487 const UnicodeString& srcText,
2488 int32_t srcStart,
2489 int32_t srcLength) U_LIFETIME_BOUND;
2490
2503 inline UnicodeString& replace(int32_t start,
2504 int32_t length,
2505 const UnicodeString& srcText) U_LIFETIME_BOUND;
2506
2524 inline UnicodeString& replace(int32_t start,
2525 int32_t length,
2526 const char16_t* srcChars,
2527 int32_t srcStart,
2528 int32_t srcLength) U_LIFETIME_BOUND;
2529
2542 inline UnicodeString& replace(int32_t start,
2543 int32_t length,
2544 ConstChar16Ptr srcChars,
2545 int32_t srcLength) U_LIFETIME_BOUND;
2546
2558 inline UnicodeString& replace(int32_t start, int32_t length, char16_t srcChar) U_LIFETIME_BOUND;
2559
2571 UnicodeString& replace(int32_t start, int32_t length, UChar32 srcChar) U_LIFETIME_BOUND;
2572
2582 inline UnicodeString& replaceBetween(int32_t start,
2583 int32_t limit,
2584 const UnicodeString& srcText) U_LIFETIME_BOUND;
2585
2600 inline UnicodeString& replaceBetween(int32_t start,
2601 int32_t limit,
2602 const UnicodeString& srcText,
2603 int32_t srcStart,
2604 int32_t srcLimit) U_LIFETIME_BOUND;
2605
2613 virtual void handleReplaceBetween(int32_t start,
2614 int32_t limit,
2615 const UnicodeString& text) override;
2616
2622 virtual UBool hasMetaData() const override;
2623
2637 virtual void copy(int32_t start, int32_t limit, int32_t dest) override;
2638
2639 /* Search and replace operations */
2640
2649 inline UnicodeString& findAndReplace(const UnicodeString& oldText,
2650 const UnicodeString& newText) U_LIFETIME_BOUND;
2651
2663 inline UnicodeString& findAndReplace(int32_t start,
2664 int32_t length,
2665 const UnicodeString& oldText,
2666 const UnicodeString& newText) U_LIFETIME_BOUND;
2667
2686 int32_t length,
2687 const UnicodeString& oldText,
2688 int32_t oldStart,
2689 int32_t oldLength,
2690 const UnicodeString& newText,
2691 int32_t newStart,
2692 int32_t newLength) U_LIFETIME_BOUND;
2693
2694
2695 /* Remove operations */
2696
2705 inline UnicodeString& remove() U_LIFETIME_BOUND;
2706
2715 inline UnicodeString& remove(int32_t start,
2716 int32_t length = static_cast<int32_t>(INT32_MAX)) U_LIFETIME_BOUND;
2717
2726 inline UnicodeString&
2727 removeBetween(int32_t start, int32_t limit = static_cast<int32_t>(INT32_MAX)) U_LIFETIME_BOUND;
2728
2738 inline UnicodeString& retainBetween(int32_t start, int32_t limit = INT32_MAX) U_LIFETIME_BOUND;
2739
2740 /* Length operations */
2741
2753 UBool padLeading(int32_t targetLength,
2754 char16_t padChar = 0x0020);
2755
2767 UBool padTrailing(int32_t targetLength,
2768 char16_t padChar = 0x0020);
2769
2776 inline UBool truncate(int32_t targetLength);
2777
2783 UnicodeString& trim() U_LIFETIME_BOUND;
2784
2785 /* Miscellaneous operations */
2786
2792 inline UnicodeString& reverse() U_LIFETIME_BOUND;
2793
2802 inline UnicodeString& reverse(int32_t start, int32_t length) U_LIFETIME_BOUND;
2803
2810 UnicodeString& toUpper() U_LIFETIME_BOUND;
2811
2819 UnicodeString& toUpper(const Locale& locale) U_LIFETIME_BOUND;
2820
2827 UnicodeString& toLower() U_LIFETIME_BOUND;
2828
2836 UnicodeString& toLower(const Locale& locale) U_LIFETIME_BOUND;
2837
2838#if !UCONFIG_NO_BREAK_ITERATION
2839
2866 UnicodeString& toTitle(BreakIterator* titleIter) U_LIFETIME_BOUND;
2867
2895 UnicodeString& toTitle(BreakIterator* titleIter, const Locale& locale) U_LIFETIME_BOUND;
2896
2928 const Locale& locale,
2929 uint32_t options) U_LIFETIME_BOUND;
2930
2931#endif
2932
2946 UnicodeString& foldCase(uint32_t options = 0 /*U_FOLD_CASE_DEFAULT*/) U_LIFETIME_BOUND;
2947
2948 //========================================
2949 // Access to the internal buffer
2950 //========================================
2951
2995 char16_t* getBuffer(int32_t minCapacity) U_LIFETIME_BOUND;
2996
3017 void releaseBuffer(int32_t newLength=-1);
3018
3049 inline const char16_t* getBuffer() const U_LIFETIME_BOUND;
3050
3084 const char16_t* getTerminatedBuffer() U_LIFETIME_BOUND;
3085
3092 inline operator std::u16string_view() const {
3093 return {getBuffer(), static_cast<std::u16string_view::size_type>(length())};
3094 }
3095
3096#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3106 inline operator std::wstring_view() const {
3107 const char16_t *p = getBuffer();
3108#ifdef U_ALIASING_BARRIER
3110#endif
3111 return { reinterpret_cast<const wchar_t *>(p), (std::wstring_view::size_type)length() };
3112 }
3113#endif // U_SIZEOF_WCHAR_T
3114
3115 //========================================
3116 // Constructors
3117 //========================================
3118
3122 inline UnicodeString();
3123
3135 UnicodeString(int32_t capacity, UChar32 c, int32_t count);
3136
3147
3158
3159#ifdef U_HIDE_DRAFT_API
3179 UNISTR_FROM_STRING_EXPLICIT UnicodeString(const char16_t *text) :
3180 UnicodeString(text, -1) {}
3181#endif // U_HIDE_DRAFT_API
3182
3183#if !U_CHAR16_IS_TYPEDEF && \
3184 (defined(U_HIDE_DRAFT_API) || (defined(_LIBCPP_VERSION) && _LIBCPP_VERSION >= 180000))
3204 UNISTR_FROM_STRING_EXPLICIT UnicodeString(const uint16_t *text) :
3205 UnicodeString(ConstChar16Ptr(text), -1) {}
3206#endif
3207
3208#if defined(U_HIDE_DRAFT_API) && (U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN))
3229 UNISTR_FROM_STRING_EXPLICIT UnicodeString(const wchar_t *text) :
3230 UnicodeString(ConstChar16Ptr(text), -1) {}
3231#endif
3232
3243 UNISTR_FROM_STRING_EXPLICIT inline UnicodeString(const std::nullptr_t text);
3244
3261 UnicodeString(const char16_t *text,
3262 int32_t textLength);
3263
3264#if !U_CHAR16_IS_TYPEDEF
3281 UnicodeString(const uint16_t *text, int32_t textLength) :
3282 UnicodeString(ConstChar16Ptr(text), textLength) {}
3283#endif
3284
3285#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3303 UnicodeString(const wchar_t *text, int32_t textLength) :
3304 UnicodeString(ConstChar16Ptr(text), textLength) {}
3305#endif
3306
3314 inline UnicodeString(const std::nullptr_t text, int32_t textLength);
3315
3328 template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
3330 fUnion.fFields.fLengthAndFlags = kShortString;
3331 doAppend(internal::toU16StringViewNullable(text));
3332 }
3333
3364 UnicodeString(UBool isTerminated,
3365 ConstChar16Ptr text U_LIFETIME_BOUND,
3366 int32_t textLength);
3367
3386 UnicodeString(char16_t* buffer U_LIFETIME_BOUND, int32_t buffLength, int32_t buffCapacity);
3387
3388#if !U_CHAR16_IS_TYPEDEF
3397 UnicodeString(uint16_t* buffer U_LIFETIME_BOUND, int32_t buffLength, int32_t buffCapacity) :
3398 UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {}
3399#endif
3400
3401#if U_SIZEOF_WCHAR_T==2 || defined(U_IN_DOXYGEN)
3411 UnicodeString(wchar_t* buffer U_LIFETIME_BOUND, int32_t buffLength, int32_t buffCapacity) :
3412 UnicodeString(Char16Ptr(buffer), buffLength, buffCapacity) {}
3413#endif
3414
3423 inline UnicodeString(std::nullptr_t buffer, int32_t buffLength, int32_t buffCapacity);
3424
3425#if U_CHARSET_IS_UTF8 || !UCONFIG_NO_CONVERSION
3426
3451
3460 UnicodeString(const char *codepageData, int32_t dataLength);
3461
3462#endif
3463
3464#if !UCONFIG_NO_CONVERSION
3465
3483 UnicodeString(const char *codepageData, const char *codepage);
3484
3502 UnicodeString(const char *codepageData, int32_t dataLength, const char *codepage);
3503
3526 const char *src, int32_t srcLength,
3527 UConverter *cnv,
3528 UErrorCode &errorCode);
3529
3530#endif
3531
3564 UnicodeString(const char *src, int32_t textLength, enum EInvariant inv);
3565
3566
3584
3592
3599 UnicodeString(const UnicodeString& src, int32_t srcStart);
3600
3608 UnicodeString(const UnicodeString& src, int32_t srcStart, int32_t srcLength);
3609
3623 virtual UnicodeString *clone() const override;
3624
3629
3652 template <typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
3653 static inline UnicodeString readOnlyAlias(const S& text U_LIFETIME_BOUND) {
3654 return readOnlyAliasFromU16StringView(internal::toU16StringView(text));
3655 }
3656
3676 static inline UnicodeString readOnlyAlias(const UnicodeString& text U_LIFETIME_BOUND) {
3677 return readOnlyAliasFromUnicodeString(text);
3678 }
3679
3694
3706 static UnicodeString fromUTF32(const UChar32 *utf32, int32_t length);
3707
3708 /* Miscellaneous operations */
3709
3745
3765 UChar32 unescapeAt(int32_t &offset) const;
3766
3772 static UClassID U_EXPORT2 getStaticClassID();
3773
3779 virtual UClassID getDynamicClassID() const override;
3780
3781 //========================================
3782 // Implementation methods
3783 //========================================
3784
3785protected:
3790 virtual int32_t getLength() const override;
3791
3797 virtual char16_t getCharAt(int32_t offset) const override;
3798
3804 virtual UChar32 getChar32At(int32_t offset) const override;
3805
3806private:
3807 static UnicodeString readOnlyAliasFromU16StringView(std::u16string_view text U_LIFETIME_BOUND);
3808 static UnicodeString readOnlyAliasFromUnicodeString(const UnicodeString& text U_LIFETIME_BOUND);
3809
3810 // For char* constructors. Could be made public.
3811 UnicodeString& setToUTF8(StringPiece utf8) U_LIFETIME_BOUND;
3812 // For extract(char*).
3813 // We could make a toUTF8(target, capacity, errorCode) public but not
3814 // this version: New API will be cleaner if we make callers create substrings
3815 // rather than having start+length on every method,
3816 // and it should take a UErrorCode&.
3817 int32_t
3818 toUTF8(int32_t start, int32_t len,
3819 char *target, int32_t capacity) const;
3820
3825 inline UBool doEquals(const UnicodeString &text, int32_t len) const {
3826 return doEquals(text.getArrayStart(), len);
3827 }
3828 UBool doEquals(const char16_t *text, int32_t len) const;
3829
3830 inline UBool
3831 doEqualsSubstring(int32_t start,
3832 int32_t length,
3833 const UnicodeString& srcText,
3834 int32_t srcStart,
3835 int32_t srcLength) const;
3836
3837 UBool doEqualsSubstring(int32_t start,
3838 int32_t length,
3839 const char16_t *srcChars,
3840 int32_t srcStart,
3841 int32_t srcLength) const;
3842
3843 inline int8_t
3844 doCompare(int32_t start,
3845 int32_t length,
3846 const UnicodeString& srcText,
3847 int32_t srcStart,
3848 int32_t srcLength) const;
3849
3850 int8_t doCompare(int32_t start,
3851 int32_t length,
3852 const char16_t *srcChars,
3853 int32_t srcStart,
3854 int32_t srcLength) const;
3855
3856 inline int8_t
3857 doCompareCodePointOrder(int32_t start,
3858 int32_t length,
3859 const UnicodeString& srcText,
3860 int32_t srcStart,
3861 int32_t srcLength) const;
3862
3863 int8_t doCompareCodePointOrder(int32_t start,
3864 int32_t length,
3865 const char16_t *srcChars,
3866 int32_t srcStart,
3867 int32_t srcLength) const;
3868
3869 inline int8_t
3870 doCaseCompare(int32_t start,
3871 int32_t length,
3872 const UnicodeString &srcText,
3873 int32_t srcStart,
3874 int32_t srcLength,
3875 uint32_t options) const;
3876
3877 int8_t
3878 doCaseCompare(int32_t start,
3879 int32_t length,
3880 const char16_t *srcChars,
3881 int32_t srcStart,
3882 int32_t srcLength,
3883 uint32_t options) const;
3884
3885 int32_t doIndexOf(char16_t c,
3886 int32_t start,
3887 int32_t length) const;
3888
3889 int32_t doIndexOf(UChar32 c,
3890 int32_t start,
3891 int32_t length) const;
3892
3893 int32_t doLastIndexOf(char16_t c,
3894 int32_t start,
3895 int32_t length) const;
3896
3897 int32_t doLastIndexOf(UChar32 c,
3898 int32_t start,
3899 int32_t length) const;
3900
3901 void doExtract(int32_t start,
3902 int32_t length,
3903 char16_t *dst,
3904 int32_t dstStart) const;
3905
3906 inline void doExtract(int32_t start,
3907 int32_t length,
3908 UnicodeString& target) const;
3909
3910 inline char16_t doCharAt(int32_t offset) const;
3911
3912 UnicodeString& doReplace(int32_t start,
3913 int32_t length,
3914 const UnicodeString& srcText,
3915 int32_t srcStart,
3916 int32_t srcLength) U_LIFETIME_BOUND;
3917
3918 UnicodeString& doReplace(int32_t start,
3919 int32_t length,
3920 const char16_t* srcChars,
3921 int32_t srcStart,
3922 int32_t srcLength) U_LIFETIME_BOUND;
3923 UnicodeString& doReplace(int32_t start, int32_t length, std::u16string_view src) U_LIFETIME_BOUND;
3924
3925 UnicodeString& doAppend(const UnicodeString& src,
3926 int32_t srcStart,
3927 int32_t srcLength) U_LIFETIME_BOUND;
3928 UnicodeString& doAppend(const char16_t* srcChars,
3929 int32_t srcStart,
3930 int32_t srcLength) U_LIFETIME_BOUND;
3931 UnicodeString& doAppend(std::u16string_view src) U_LIFETIME_BOUND;
3932
3933 UnicodeString& doReverse(int32_t start, int32_t length) U_LIFETIME_BOUND;
3934
3935 // calculate hash code
3936 int32_t doHashCode() const;
3937
3938 // get pointer to start of array
3939 // these do not check for kOpenGetBuffer, unlike the public getBuffer() function
3940 inline char16_t* getArrayStart() U_LIFETIME_BOUND;
3941 inline const char16_t* getArrayStart() const U_LIFETIME_BOUND;
3942
3943 inline UBool hasShortLength() const;
3944 inline int32_t getShortLength() const;
3945
3946 // A UnicodeString object (not necessarily its current buffer)
3947 // is writable unless it isBogus() or it has an "open" getBuffer(minCapacity).
3948 inline UBool isWritable() const;
3949
3950 // Is the current buffer writable?
3951 inline UBool isBufferWritable() const;
3952
3953 // None of the following does releaseArray().
3954 inline void setZeroLength();
3955 inline void setShortLength(int32_t len);
3956 inline void setLength(int32_t len);
3957 inline void setToEmpty();
3958 inline void setArray(char16_t *array, int32_t len, int32_t capacity); // sets length but not flags
3959
3960 // allocate the array; result may be the stack buffer
3961 // sets refCount to 1 if appropriate
3962 // sets fArray, fCapacity, and flags
3963 // sets length to 0
3964 // returns boolean for success or failure
3965 UBool allocate(int32_t capacity);
3966
3967 // release the array if owned
3968 void releaseArray();
3969
3970 // turn a bogus string into an empty one
3971 void unBogus();
3972
3973 // implements assignment operator, copy constructor, and fastCopyFrom()
3974 UnicodeString& copyFrom(const UnicodeString& src, UBool fastCopy = false) U_LIFETIME_BOUND;
3975
3976 // Copies just the fields without memory management.
3977 void copyFieldsFrom(UnicodeString &src, UBool setSrcToBogus) noexcept;
3978
3979 // Pin start and limit to acceptable values.
3980 inline void pinIndex(int32_t& start) const;
3981 inline void pinIndices(int32_t& start,
3982 int32_t& length) const;
3983
3984#if !UCONFIG_NO_CONVERSION
3985
3986 /* Internal extract() using UConverter. */
3987 int32_t doExtract(int32_t start, int32_t length,
3988 char *dest, int32_t destCapacity,
3989 UConverter *cnv,
3990 UErrorCode &errorCode) const;
3991
3992 /*
3993 * Real constructor for converting from codepage data.
3994 * It assumes that it is called with !fRefCounted.
3995 *
3996 * If `codepage==0`, then the default converter
3997 * is used for the platform encoding.
3998 * If `codepage` is an empty string (`""`),
3999 * then a simple conversion is performed on the codepage-invariant
4000 * subset ("invariant characters") of the platform encoding. See utypes.h.
4001 */
4002 void doCodepageCreate(const char *codepageData,
4003 int32_t dataLength,
4004 const char *codepage);
4005
4006 /*
4007 * Worker function for creating a UnicodeString from
4008 * a codepage string using a UConverter.
4009 */
4010 void
4011 doCodepageCreate(const char *codepageData,
4012 int32_t dataLength,
4013 UConverter *converter,
4014 UErrorCode &status);
4015
4016#endif
4017
4018 /*
4019 * This function is called when write access to the array
4020 * is necessary.
4021 *
4022 * We need to make a copy of the array if
4023 * the buffer is read-only, or
4024 * the buffer is refCounted (shared), and refCount>1, or
4025 * the buffer is too small.
4026 *
4027 * Return false if memory could not be allocated.
4028 */
4029 UBool cloneArrayIfNeeded(int32_t newCapacity = -1,
4030 int32_t growCapacity = -1,
4031 UBool doCopyArray = true,
4032 int32_t** pBufferToDelete = nullptr,
4033 UBool forceClone = false);
4034
4040 UnicodeString& caseMap(int32_t caseLocale,
4041 uint32_t options,
4043 BreakIterator* iter,
4044#endif
4045 UStringCaseMapper* stringCaseMapper) U_LIFETIME_BOUND;
4046
4047 // ref counting
4048 void addRef();
4049 int32_t removeRef();
4050 int32_t refCount() const;
4051
4052 // constants
4053 enum {
4059 US_STACKBUF_SIZE = static_cast<int32_t>(UNISTR_OBJECT_SIZE - sizeof(void*) - 2) / U_SIZEOF_UCHAR,
4060 kInvalidUChar=0xffff, // U+FFFF returned by charAt(invalid index)
4061 kInvalidHashCode=0, // invalid hash code
4062 kEmptyHashCode=1, // hash code for empty string
4063
4064 // bit flag values for fLengthAndFlags
4065 kIsBogus=1, // this string is bogus, i.e., not valid or nullptr
4066 kUsingStackBuffer=2,// using fUnion.fStackFields instead of fUnion.fFields
4067 kRefCounted=4, // there is a refCount field before the characters in fArray
4068 kBufferIsReadonly=8,// do not write to this buffer
4069 kOpenGetBuffer=16, // getBuffer(minCapacity) was called (is "open"),
4070 // and releaseBuffer(newLength) must be called
4071 kAllStorageFlags=0x1f,
4072
4073 kLengthShift=5, // remaining 11 bits for non-negative short length, or negative if long
4074 kLength1=1<<kLengthShift,
4075 kMaxShortLength=0x3ff, // max non-negative short length (leaves top bit 0)
4076 kLengthIsLarge=0xffe0, // short length < 0, real length is in fUnion.fFields.fLength
4077
4078 // combined values for convenience
4079 kShortString=kUsingStackBuffer,
4080 kLongString=kRefCounted,
4081 kReadonlyAlias=kBufferIsReadonly,
4082 kWritableAlias=0
4083 };
4084
4085 friend class UnicodeStringAppendable;
4086
4087 union StackBufferOrFields; // forward declaration necessary before friend declaration
4088 friend union StackBufferOrFields; // make US_STACKBUF_SIZE visible inside fUnion
4089
4090 /*
4091 * The following are all the class fields that are stored
4092 * in each UnicodeString object.
4093 * Note that UnicodeString has virtual functions,
4094 * therefore there is an implicit vtable pointer
4095 * as the first real field.
4096 * The fields should be aligned such that no padding is necessary.
4097 * On 32-bit machines, the size should be 32 bytes,
4098 * on 64-bit machines (8-byte pointers), it should be 40 bytes.
4099 *
4100 * We use a hack to achieve this.
4101 *
4102 * With at least some compilers, each of the following is forced to
4103 * a multiple of sizeof(pointer) [the largest field base unit here is a data pointer],
4104 * rounded up with additional padding if the fields do not already fit that requirement:
4105 * - sizeof(class UnicodeString)
4106 * - offsetof(UnicodeString, fUnion)
4107 * - sizeof(fUnion)
4108 * - sizeof(fStackFields)
4109 *
4110 * We optimize for the longest possible internal buffer for short strings.
4111 * fUnion.fStackFields begins with 2 bytes for storage flags
4112 * and the length of relatively short strings,
4113 * followed by the buffer for short string contents.
4114 * There is no padding inside fStackFields.
4115 *
4116 * Heap-allocated and aliased strings use fUnion.fFields.
4117 * Both fStackFields and fFields must begin with the same fields for flags and short length,
4118 * that is, those must have the same memory offsets inside the object,
4119 * because the flags must be inspected in order to decide which half of fUnion is being used.
4120 * We assume that the compiler does not reorder the fields.
4121 *
4122 * (Padding at the end of fFields is ok:
4123 * As long as it is no larger than fStackFields, it is not wasted space.)
4124 *
4125 * For some of the history of the UnicodeString class fields layout, see
4126 * - ICU ticket #11551 "longer UnicodeString contents in stack buffer"
4127 * - ICU ticket #11336 "UnicodeString: recombine stack buffer arrays"
4128 * - ICU ticket #8322 "why is sizeof(UnicodeString)==48?"
4129 */
4130 // (implicit) *vtable;
4131 union StackBufferOrFields {
4132 // fStackFields is used iff (fLengthAndFlags&kUsingStackBuffer) else fFields is used.
4133 // Each struct of the union must begin with fLengthAndFlags.
4134 struct {
4135 int16_t fLengthAndFlags; // bit fields: see constants above
4136 char16_t fBuffer[US_STACKBUF_SIZE]; // buffer for short strings
4137 } fStackFields;
4138 struct {
4139 int16_t fLengthAndFlags; // bit fields: see constants above
4140 int32_t fLength; // number of characters in fArray if >127; else undefined
4141 int32_t fCapacity; // capacity of fArray (in char16_ts)
4142 // array pointer last to minimize padding for machines with P128 data model
4143 // or pointer sizes that are not a power of 2
4144 char16_t *fArray; // the Unicode data
4145 } fFields;
4146 } fUnion;
4147};
4148
4157U_COMMON_API UnicodeString U_EXPORT2
4158operator+ (const UnicodeString &s1, const UnicodeString &s2);
4159
4170template<
4171 typename US, typename S,
4172 typename = std::enable_if_t<ConvertibleToU16StringView<S> && std::is_same_v<US, UnicodeString>>>
4173inline UnicodeString operator+(const US &s1, const S &s2) {
4175}
4176
4177#ifndef U_FORCE_HIDE_INTERNAL_API
4179U_COMMON_API UnicodeString U_EXPORT2
4180unistr_internalConcat(const UnicodeString &s1, std::u16string_view s2);
4181#endif
4182
4183//========================================
4184// Inline members
4185//========================================
4186
4187//========================================
4188// Privates
4189//========================================
4190
4191inline void
4192UnicodeString::pinIndex(int32_t& start) const
4193{
4194 // pin index
4195 if(start < 0) {
4196 start = 0;
4197 } else if(start > length()) {
4198 start = length();
4199 }
4200}
4201
4202inline void
4203UnicodeString::pinIndices(int32_t& start,
4204 int32_t& _length) const
4205{
4206 // pin indices
4207 int32_t len = length();
4208 if(start < 0) {
4209 start = 0;
4210 } else if(start > len) {
4211 start = len;
4212 }
4213 if(_length < 0) {
4214 _length = 0;
4215 } else if(_length > (len - start)) {
4216 _length = (len - start);
4217 }
4218}
4219
4220inline char16_t*
4221UnicodeString::getArrayStart() U_LIFETIME_BOUND {
4222 return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
4223 fUnion.fStackFields.fBuffer : fUnion.fFields.fArray;
4224}
4225
4226inline const char16_t*
4227UnicodeString::getArrayStart() const U_LIFETIME_BOUND {
4228 return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
4229 fUnion.fStackFields.fBuffer : fUnion.fFields.fArray;
4230}
4231
4232//========================================
4233// Default constructor
4234//========================================
4235
4236inline
4238 fUnion.fStackFields.fLengthAndFlags=kShortString;
4239}
4240
4241inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/) {
4242 fUnion.fStackFields.fLengthAndFlags=kShortString;
4243}
4244
4245inline UnicodeString::UnicodeString(const std::nullptr_t /*text*/, int32_t /*length*/) {
4246 fUnion.fStackFields.fLengthAndFlags=kShortString;
4247}
4248
4249inline UnicodeString::UnicodeString(std::nullptr_t /*buffer*/, int32_t /*buffLength*/, int32_t /*buffCapacity*/) {
4250 fUnion.fStackFields.fLengthAndFlags=kShortString;
4251}
4252
4253//========================================
4254// Read-only implementation methods
4255//========================================
4256inline UBool
4257UnicodeString::hasShortLength() const {
4258 return fUnion.fFields.fLengthAndFlags>=0;
4259}
4260
4261inline int32_t
4262UnicodeString::getShortLength() const {
4263 // fLengthAndFlags must be non-negative -> short length >= 0
4264 // and arithmetic or logical shift does not matter.
4265 return fUnion.fFields.fLengthAndFlags>>kLengthShift;
4266}
4267
4268inline int32_t
4270 return hasShortLength() ? getShortLength() : fUnion.fFields.fLength;
4271}
4272
4273inline int32_t
4275 return (fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) ?
4276 US_STACKBUF_SIZE : fUnion.fFields.fCapacity;
4277}
4278
4279inline int32_t
4281{ return doHashCode(); }
4282
4283inline UBool
4285{ return fUnion.fFields.fLengthAndFlags & kIsBogus; }
4286
4287inline UBool
4288UnicodeString::isWritable() const
4289{ return !(fUnion.fFields.fLengthAndFlags & (kOpenGetBuffer | kIsBogus)); }
4290
4291inline UBool
4292UnicodeString::isBufferWritable() const
4293{
4294 return
4295 !(fUnion.fFields.fLengthAndFlags&(kOpenGetBuffer|kIsBogus|kBufferIsReadonly)) &&
4296 (!(fUnion.fFields.fLengthAndFlags&kRefCounted) || refCount()==1);
4297}
4298
4299inline const char16_t *
4300UnicodeString::getBuffer() const U_LIFETIME_BOUND {
4301 if(fUnion.fFields.fLengthAndFlags&(kIsBogus|kOpenGetBuffer)) {
4302 return nullptr;
4303 } else if(fUnion.fFields.fLengthAndFlags&kUsingStackBuffer) {
4304 return fUnion.fStackFields.fBuffer;
4305 } else {
4306 return fUnion.fFields.fArray;
4307 }
4308}
4309
4310//========================================
4311// Read-only alias methods
4312//========================================
4313inline int8_t
4314UnicodeString::doCompare(int32_t start,
4315 int32_t thisLength,
4316 const UnicodeString& srcText,
4317 int32_t srcStart,
4318 int32_t srcLength) const
4319{
4320 if(srcText.isBogus()) {
4321 return static_cast<int8_t>(!isBogus()); // 0 if both are bogus, 1 otherwise
4322 } else {
4323 srcText.pinIndices(srcStart, srcLength);
4324 return doCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
4325 }
4326}
4327
4328inline UBool
4329UnicodeString::doEqualsSubstring(int32_t start,
4330 int32_t thisLength,
4331 const UnicodeString& srcText,
4332 int32_t srcStart,
4333 int32_t srcLength) const
4334{
4335 if(srcText.isBogus()) {
4336 return isBogus();
4337 } else {
4338 srcText.pinIndices(srcStart, srcLength);
4339 return !isBogus() && doEqualsSubstring(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
4340 }
4341}
4342
4343inline bool
4344UnicodeString::operator== (const UnicodeString& text) const
4345{
4346 if(isBogus()) {
4347 return text.isBogus();
4348 } else {
4349 int32_t len = length(), textLength = text.length();
4350 return !text.isBogus() && len == textLength && doEquals(text, len);
4351 }
4352}
4353
4354inline bool
4355UnicodeString::operator!= (const UnicodeString& text) const
4356{ return (! operator==(text)); }
4357
4358inline UBool
4359UnicodeString::operator> (const UnicodeString& text) const
4360{ return doCompare(0, length(), text, 0, text.length()) == 1; }
4361
4362inline UBool
4363UnicodeString::operator< (const UnicodeString& text) const
4364{ return doCompare(0, length(), text, 0, text.length()) == -1; }
4365
4366inline UBool
4367UnicodeString::operator>= (const UnicodeString& text) const
4368{ return doCompare(0, length(), text, 0, text.length()) != -1; }
4369
4370inline UBool
4371UnicodeString::operator<= (const UnicodeString& text) const
4372{ return doCompare(0, length(), text, 0, text.length()) != 1; }
4373
4374inline int8_t
4376{ return doCompare(0, length(), text, 0, text.length()); }
4377
4378inline int8_t
4380 int32_t _length,
4381 const UnicodeString& srcText) const
4382{ return doCompare(start, _length, srcText, 0, srcText.length()); }
4383
4384inline int8_t
4386 int32_t srcLength) const
4387{ return doCompare(0, length(), srcChars, 0, srcLength); }
4388
4389inline int8_t
4391 int32_t _length,
4392 const UnicodeString& srcText,
4393 int32_t srcStart,
4394 int32_t srcLength) const
4395{ return doCompare(start, _length, srcText, srcStart, srcLength); }
4396
4397inline int8_t
4399 int32_t _length,
4400 const char16_t *srcChars) const
4401{ return doCompare(start, _length, srcChars, 0, _length); }
4402
4403inline int8_t
4405 int32_t _length,
4406 const char16_t *srcChars,
4407 int32_t srcStart,
4408 int32_t srcLength) const
4409{ return doCompare(start, _length, srcChars, srcStart, srcLength); }
4410
4411inline int8_t
4413 int32_t limit,
4414 const UnicodeString& srcText,
4415 int32_t srcStart,
4416 int32_t srcLimit) const
4417{ return doCompare(start, limit - start,
4418 srcText, srcStart, srcLimit - srcStart); }
4419
4420inline int8_t
4421UnicodeString::doCompareCodePointOrder(int32_t start,
4422 int32_t thisLength,
4423 const UnicodeString& srcText,
4424 int32_t srcStart,
4425 int32_t srcLength) const
4426{
4427 if(srcText.isBogus()) {
4428 return static_cast<int8_t>(!isBogus()); // 0 if both are bogus, 1 otherwise
4429 } else {
4430 srcText.pinIndices(srcStart, srcLength);
4431 return doCompareCodePointOrder(start, thisLength, srcText.getArrayStart(), srcStart, srcLength);
4432 }
4433}
4434
4435inline int8_t
4437{ return doCompareCodePointOrder(0, length(), text, 0, text.length()); }
4438
4439inline int8_t
4441 int32_t _length,
4442 const UnicodeString& srcText) const
4443{ return doCompareCodePointOrder(start, _length, srcText, 0, srcText.length()); }
4444
4445inline int8_t
4447 int32_t srcLength) const
4448{ return doCompareCodePointOrder(0, length(), srcChars, 0, srcLength); }
4449
4450inline int8_t
4452 int32_t _length,
4453 const UnicodeString& srcText,
4454 int32_t srcStart,
4455 int32_t srcLength) const
4456{ return doCompareCodePointOrder(start, _length, srcText, srcStart, srcLength); }
4457
4458inline int8_t
4460 int32_t _length,
4461 const char16_t *srcChars) const
4462{ return doCompareCodePointOrder(start, _length, srcChars, 0, _length); }
4463
4464inline int8_t
4466 int32_t _length,
4467 const char16_t *srcChars,
4468 int32_t srcStart,
4469 int32_t srcLength) const
4470{ return doCompareCodePointOrder(start, _length, srcChars, srcStart, srcLength); }
4471
4472inline int8_t
4474 int32_t limit,
4475 const UnicodeString& srcText,
4476 int32_t srcStart,
4477 int32_t srcLimit) const
4478{ return doCompareCodePointOrder(start, limit - start,
4479 srcText, srcStart, srcLimit - srcStart); }
4480
4481inline int8_t
4482UnicodeString::doCaseCompare(int32_t start,
4483 int32_t thisLength,
4484 const UnicodeString &srcText,
4485 int32_t srcStart,
4486 int32_t srcLength,
4487 uint32_t options) const
4488{
4489 if(srcText.isBogus()) {
4490 return static_cast<int8_t>(!isBogus()); // 0 if both are bogus, 1 otherwise
4491 } else {
4492 srcText.pinIndices(srcStart, srcLength);
4493 return doCaseCompare(start, thisLength, srcText.getArrayStart(), srcStart, srcLength, options);
4494 }
4495}
4496
4497inline int8_t
4498UnicodeString::caseCompare(const UnicodeString &text, uint32_t options) const {
4499 return doCaseCompare(0, length(), text, 0, text.length(), options);
4500}
4501
4502inline int8_t
4504 int32_t _length,
4505 const UnicodeString &srcText,
4506 uint32_t options) const {
4507 return doCaseCompare(start, _length, srcText, 0, srcText.length(), options);
4508}
4509
4510inline int8_t
4512 int32_t srcLength,
4513 uint32_t options) const {
4514 return doCaseCompare(0, length(), srcChars, 0, srcLength, options);
4515}
4516
4517inline int8_t
4519 int32_t _length,
4520 const UnicodeString &srcText,
4521 int32_t srcStart,
4522 int32_t srcLength,
4523 uint32_t options) const {
4524 return doCaseCompare(start, _length, srcText, srcStart, srcLength, options);
4525}
4526
4527inline int8_t
4529 int32_t _length,
4530 const char16_t *srcChars,
4531 uint32_t options) const {
4532 return doCaseCompare(start, _length, srcChars, 0, _length, options);
4533}
4534
4535inline int8_t
4537 int32_t _length,
4538 const char16_t *srcChars,
4539 int32_t srcStart,
4540 int32_t srcLength,
4541 uint32_t options) const {
4542 return doCaseCompare(start, _length, srcChars, srcStart, srcLength, options);
4543}
4544
4545inline int8_t
4547 int32_t limit,
4548 const UnicodeString &srcText,
4549 int32_t srcStart,
4550 int32_t srcLimit,
4551 uint32_t options) const {
4552 return doCaseCompare(start, limit - start, srcText, srcStart, srcLimit - srcStart, options);
4553}
4554
4555inline int32_t
4557 int32_t srcStart,
4558 int32_t srcLength,
4559 int32_t start,
4560 int32_t _length) const
4561{
4562 if(!srcText.isBogus()) {
4563 srcText.pinIndices(srcStart, srcLength);
4564 if(srcLength > 0) {
4565 return indexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length);
4566 }
4567 }
4568 return -1;
4569}
4570
4571inline int32_t
4573{ return indexOf(text, 0, text.length(), 0, length()); }
4574
4575inline int32_t
4577 int32_t start) const {
4578 pinIndex(start);
4579 return indexOf(text, 0, text.length(), start, length() - start);
4580}
4581
4582inline int32_t
4584 int32_t start,
4585 int32_t _length) const
4586{ return indexOf(text, 0, text.length(), start, _length); }
4587
4588inline int32_t
4589UnicodeString::indexOf(const char16_t *srcChars,
4590 int32_t srcLength,
4591 int32_t start) const {
4592 pinIndex(start);
4593 return indexOf(srcChars, 0, srcLength, start, length() - start);
4594}
4595
4596inline int32_t
4598 int32_t srcLength,
4599 int32_t start,
4600 int32_t _length) const
4601{ return indexOf(srcChars, 0, srcLength, start, _length); }
4602
4603inline int32_t
4605 int32_t start,
4606 int32_t _length) const
4607{ return doIndexOf(c, start, _length); }
4608
4609inline int32_t
4611 int32_t start,
4612 int32_t _length) const
4613{ return doIndexOf(c, start, _length); }
4614
4615inline int32_t
4616UnicodeString::indexOf(char16_t c) const
4617{ return doIndexOf(c, 0, length()); }
4618
4619inline int32_t
4621{ return indexOf(c, 0, length()); }
4622
4623inline int32_t
4625 int32_t start) const {
4626 pinIndex(start);
4627 return doIndexOf(c, start, length() - start);
4628}
4629
4630inline int32_t
4632 int32_t start) const {
4633 pinIndex(start);
4634 return indexOf(c, start, length() - start);
4635}
4636
4637inline int32_t
4639 int32_t srcLength,
4640 int32_t start,
4641 int32_t _length) const
4642{ return lastIndexOf(srcChars, 0, srcLength, start, _length); }
4643
4644inline int32_t
4645UnicodeString::lastIndexOf(const char16_t *srcChars,
4646 int32_t srcLength,
4647 int32_t start) const {
4648 pinIndex(start);
4649 return lastIndexOf(srcChars, 0, srcLength, start, length() - start);
4650}
4651
4652inline int32_t
4654 int32_t srcStart,
4655 int32_t srcLength,
4656 int32_t start,
4657 int32_t _length) const
4658{
4659 if(!srcText.isBogus()) {
4660 srcText.pinIndices(srcStart, srcLength);
4661 if(srcLength > 0) {
4662 return lastIndexOf(srcText.getArrayStart(), srcStart, srcLength, start, _length);
4663 }
4664 }
4665 return -1;
4666}
4667
4668inline int32_t
4670 int32_t start,
4671 int32_t _length) const
4672{ return lastIndexOf(text, 0, text.length(), start, _length); }
4673
4674inline int32_t
4676 int32_t start) const {
4677 pinIndex(start);
4678 return lastIndexOf(text, 0, text.length(), start, length() - start);
4679}
4680
4681inline int32_t
4683{ return lastIndexOf(text, 0, text.length(), 0, length()); }
4684
4685inline int32_t
4687 int32_t start,
4688 int32_t _length) const
4689{ return doLastIndexOf(c, start, _length); }
4690
4691inline int32_t
4693 int32_t start,
4694 int32_t _length) const {
4695 return doLastIndexOf(c, start, _length);
4696}
4697
4698inline int32_t
4700{ return doLastIndexOf(c, 0, length()); }
4701
4702inline int32_t
4704 return lastIndexOf(c, 0, length());
4705}
4706
4707inline int32_t
4709 int32_t start) const {
4710 pinIndex(start);
4711 return doLastIndexOf(c, start, length() - start);
4712}
4713
4714inline int32_t
4716 int32_t start) const {
4717 pinIndex(start);
4718 return lastIndexOf(c, start, length() - start);
4719}
4720
4721inline UBool
4723{ return doEqualsSubstring(0, text.length(), text, 0, text.length()); }
4724
4725inline UBool
4727 int32_t srcStart,
4728 int32_t srcLength) const
4729{ return doEqualsSubstring(0, srcLength, srcText, srcStart, srcLength); }
4730
4731inline UBool
4732UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const {
4733 if(srcLength < 0) {
4734 srcLength = u_strlen(toUCharPtr(srcChars));
4735 }
4736 return doEqualsSubstring(0, srcLength, srcChars, 0, srcLength);
4737}
4738
4739inline UBool
4740UnicodeString::startsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const {
4741 if(srcLength < 0) {
4742 srcLength = u_strlen(toUCharPtr(srcChars));
4743 }
4744 return doEqualsSubstring(0, srcLength, srcChars, srcStart, srcLength);
4745}
4746
4747inline UBool
4749{ return doEqualsSubstring(length() - text.length(), text.length(),
4750 text, 0, text.length()); }
4751
4752inline UBool
4754 int32_t srcStart,
4755 int32_t srcLength) const {
4756 srcText.pinIndices(srcStart, srcLength);
4757 return doEqualsSubstring(length() - srcLength, srcLength,
4758 srcText, srcStart, srcLength);
4759}
4760
4761inline UBool
4763 int32_t srcLength) const {
4764 if(srcLength < 0) {
4765 srcLength = u_strlen(toUCharPtr(srcChars));
4766 }
4767 return doEqualsSubstring(length() - srcLength, srcLength, srcChars, 0, srcLength);
4768}
4769
4770inline UBool
4771UnicodeString::endsWith(const char16_t *srcChars,
4772 int32_t srcStart,
4773 int32_t srcLength) const {
4774 if(srcLength < 0) {
4775 srcLength = u_strlen(toUCharPtr(srcChars + srcStart));
4776 }
4777 return doEqualsSubstring(length() - srcLength, srcLength,
4778 srcChars, srcStart, srcLength);
4779}
4780
4781//========================================
4782// replace
4783//========================================
4784inline UnicodeString&
4786 int32_t _length,
4787 const UnicodeString& srcText) U_LIFETIME_BOUND
4788{ return doReplace(start, _length, srcText, 0, srcText.length()); }
4789
4790inline UnicodeString&
4792 int32_t _length,
4793 const UnicodeString& srcText,
4794 int32_t srcStart,
4795 int32_t srcLength) U_LIFETIME_BOUND
4796{ return doReplace(start, _length, srcText, srcStart, srcLength); }
4797
4798inline UnicodeString&
4800 int32_t _length,
4801 ConstChar16Ptr srcChars,
4802 int32_t srcLength) U_LIFETIME_BOUND
4803{ return doReplace(start, _length, srcChars, 0, srcLength); }
4804
4805inline UnicodeString&
4807 int32_t _length,
4808 const char16_t* srcChars,
4809 int32_t srcStart,
4810 int32_t srcLength) U_LIFETIME_BOUND
4811{ return doReplace(start, _length, srcChars, srcStart, srcLength); }
4812
4813inline UnicodeString&
4815 int32_t _length,
4816 char16_t srcChar) U_LIFETIME_BOUND
4817{ return doReplace(start, _length, &srcChar, 0, 1); }
4818
4819inline UnicodeString&
4821 int32_t limit,
4822 const UnicodeString& srcText) U_LIFETIME_BOUND
4823{ return doReplace(start, limit - start, srcText, 0, srcText.length()); }
4824
4825inline UnicodeString&
4827 int32_t limit,
4828 const UnicodeString& srcText,
4829 int32_t srcStart,
4830 int32_t srcLimit) U_LIFETIME_BOUND
4831{ return doReplace(start, limit - start, srcText, srcStart, srcLimit - srcStart); }
4832
4833inline UnicodeString&
4835 const UnicodeString& newText) U_LIFETIME_BOUND
4836{ return findAndReplace(0, length(), oldText, 0, oldText.length(),
4837 newText, 0, newText.length()); }
4838
4839inline UnicodeString&
4841 int32_t _length,
4842 const UnicodeString& oldText,
4843 const UnicodeString& newText) U_LIFETIME_BOUND
4844{ return findAndReplace(start, _length, oldText, 0, oldText.length(),
4845 newText, 0, newText.length()); }
4846
4847// ============================
4848// extract
4849// ============================
4850inline void
4851UnicodeString::doExtract(int32_t start,
4852 int32_t _length,
4853 UnicodeString& target) const
4854{ target.replace(0, target.length(), *this, start, _length); }
4855
4856inline void
4858 int32_t _length,
4859 Char16Ptr target,
4860 int32_t targetStart) const
4861{ doExtract(start, _length, target, targetStart); }
4862
4863inline void
4865 int32_t _length,
4866 UnicodeString& target) const
4867{ doExtract(start, _length, target); }
4868
4869#if !UCONFIG_NO_CONVERSION
4870
4871inline int32_t
4873 int32_t _length,
4874 char *dst,
4875 const char *codepage) const
4876
4877{
4878 // This dstSize value will be checked explicitly
4879 return extract(start, _length, dst, dst != nullptr ? 0xffffffff : 0, codepage);
4880}
4881
4882#endif
4883
4884inline void
4886 int32_t limit,
4887 char16_t *dst,
4888 int32_t dstStart) const {
4889 pinIndex(start);
4890 pinIndex(limit);
4891 doExtract(start, limit - start, dst, dstStart);
4892}
4893
4894inline UnicodeString
4895UnicodeString::tempSubStringBetween(int32_t start, int32_t limit) const U_LIFETIME_BOUND {
4896 return tempSubString(start, limit - start);
4897}
4898
4899inline char16_t
4900UnicodeString::doCharAt(int32_t offset) const
4901{
4902 if (static_cast<uint32_t>(offset) < static_cast<uint32_t>(length())) {
4903 return getArrayStart()[offset];
4904 } else {
4905 return kInvalidUChar;
4906 }
4907}
4908
4909inline char16_t
4910UnicodeString::charAt(int32_t offset) const
4911{ return doCharAt(offset); }
4912
4913inline char16_t
4914UnicodeString::operator[] (int32_t offset) const
4915{ return doCharAt(offset); }
4916
4917inline UBool
4919 // Arithmetic or logical right shift does not matter: only testing for 0.
4920 return (fUnion.fFields.fLengthAndFlags>>kLengthShift) == 0;
4921}
4922
4923//========================================
4924// Write implementation methods
4925//========================================
4926inline void
4927UnicodeString::setZeroLength() {
4928 fUnion.fFields.fLengthAndFlags &= kAllStorageFlags;
4929}
4930
4931inline void
4932UnicodeString::setShortLength(int32_t len) {
4933 // requires 0 <= len <= kMaxShortLength
4934 fUnion.fFields.fLengthAndFlags =
4935 static_cast<int16_t>((fUnion.fFields.fLengthAndFlags & kAllStorageFlags) | (len << kLengthShift));
4936}
4937
4938inline void
4939UnicodeString::setLength(int32_t len) {
4940 if(len <= kMaxShortLength) {
4941 setShortLength(len);
4942 } else {
4943 fUnion.fFields.fLengthAndFlags |= kLengthIsLarge;
4944 fUnion.fFields.fLength = len;
4945 }
4946}
4947
4948inline void
4949UnicodeString::setToEmpty() {
4950 fUnion.fFields.fLengthAndFlags = kShortString;
4951}
4952
4953inline void
4954UnicodeString::setArray(char16_t *array, int32_t len, int32_t capacity) {
4955 setLength(len);
4956 fUnion.fFields.fArray = array;
4957 fUnion.fFields.fCapacity = capacity;
4958}
4959
4960inline UnicodeString&
4961UnicodeString::operator=(char16_t ch) U_LIFETIME_BOUND
4962{ return doReplace(0, length(), &ch, 0, 1); }
4963
4964inline UnicodeString&
4966{ return replace(0, length(), ch); }
4967
4968inline UnicodeString&
4970 int32_t srcStart,
4971 int32_t srcLength) U_LIFETIME_BOUND
4972{
4973 unBogus();
4974 return doReplace(0, length(), srcText, srcStart, srcLength);
4975}
4976
4977inline UnicodeString&
4979 int32_t srcStart) U_LIFETIME_BOUND
4980{
4981 unBogus();
4982 srcText.pinIndex(srcStart);
4983 return doReplace(0, length(), srcText, srcStart, srcText.length() - srcStart);
4984}
4985
4986inline UnicodeString&
4987UnicodeString::setTo(const UnicodeString& srcText) U_LIFETIME_BOUND
4988{
4989 return copyFrom(srcText);
4990}
4991
4992inline UnicodeString&
4993UnicodeString::setTo(const char16_t* srcChars,
4994 int32_t srcLength) U_LIFETIME_BOUND
4995{
4996 unBogus();
4997 return doReplace(0, length(), srcChars, 0, srcLength);
4998}
4999
5000inline UnicodeString&
5001UnicodeString::setTo(char16_t srcChar) U_LIFETIME_BOUND
5002{
5003 unBogus();
5004 return doReplace(0, length(), &srcChar, 0, 1);
5005}
5006
5007inline UnicodeString&
5008UnicodeString::setTo(UChar32 srcChar) U_LIFETIME_BOUND
5009{
5010 unBogus();
5011 return replace(0, length(), srcChar);
5012}
5013
5014inline UnicodeString&
5016 int32_t srcStart,
5017 int32_t srcLength) U_LIFETIME_BOUND
5018{ return doAppend(srcText, srcStart, srcLength); }
5019
5020inline UnicodeString&
5021UnicodeString::append(const UnicodeString& srcText) U_LIFETIME_BOUND
5022{ return doAppend(srcText, 0, srcText.length()); }
5023
5024inline UnicodeString&
5025UnicodeString::append(const char16_t* srcChars,
5026 int32_t srcStart,
5027 int32_t srcLength) U_LIFETIME_BOUND
5028{ return doAppend(srcChars, srcStart, srcLength); }
5029
5030inline UnicodeString&
5032 int32_t srcLength) U_LIFETIME_BOUND
5033{ return doAppend(srcChars, 0, srcLength); }
5034
5035inline UnicodeString&
5036UnicodeString::append(char16_t srcChar) U_LIFETIME_BOUND
5037{ return doAppend(&srcChar, 0, 1); }
5038
5039inline UnicodeString&
5040UnicodeString::operator+=(char16_t ch) U_LIFETIME_BOUND
5041{ return doAppend(&ch, 0, 1); }
5042
5043inline UnicodeString&
5045 return append(ch);
5046}
5047
5048inline UnicodeString&
5049UnicodeString::operator+=(const UnicodeString& srcText) U_LIFETIME_BOUND
5050{ return doAppend(srcText, 0, srcText.length()); }
5051
5052inline UnicodeString&
5054 const UnicodeString& srcText,
5055 int32_t srcStart,
5056 int32_t srcLength) U_LIFETIME_BOUND
5057{ return doReplace(start, 0, srcText, srcStart, srcLength); }
5058
5059inline UnicodeString&
5061 const UnicodeString& srcText) U_LIFETIME_BOUND
5062{ return doReplace(start, 0, srcText, 0, srcText.length()); }
5063
5064inline UnicodeString&
5066 const char16_t* srcChars,
5067 int32_t srcStart,
5068 int32_t srcLength) U_LIFETIME_BOUND
5069{ return doReplace(start, 0, srcChars, srcStart, srcLength); }
5070
5071inline UnicodeString&
5073 ConstChar16Ptr srcChars,
5074 int32_t srcLength) U_LIFETIME_BOUND
5075{ return doReplace(start, 0, srcChars, 0, srcLength); }
5076
5077inline UnicodeString&
5079 char16_t srcChar) U_LIFETIME_BOUND
5080{ return doReplace(start, 0, &srcChar, 0, 1); }
5081
5082inline UnicodeString&
5084 UChar32 srcChar) U_LIFETIME_BOUND
5085{ return replace(start, 0, srcChar); }
5086
5087
5088inline UnicodeString&
5089UnicodeString::remove() U_LIFETIME_BOUND
5090{
5091 // remove() of a bogus string makes the string empty and non-bogus
5092 if(isBogus()) {
5093 setToEmpty();
5094 } else {
5095 setZeroLength();
5096 }
5097 return *this;
5098}
5099
5100inline UnicodeString&
5102 int32_t _length) U_LIFETIME_BOUND
5103{
5104 if(start <= 0 && _length == INT32_MAX) {
5105 // remove(guaranteed everything) of a bogus string makes the string empty and non-bogus
5106 return remove();
5107 }
5108 return doReplace(start, _length, nullptr, 0, 0);
5109}
5110
5111inline UnicodeString&
5113 int32_t limit) U_LIFETIME_BOUND
5114{ return doReplace(start, limit - start, nullptr, 0, 0); }
5115
5116inline UnicodeString&
5117UnicodeString::retainBetween(int32_t start, int32_t limit) U_LIFETIME_BOUND {
5118 truncate(limit);
5119 return doReplace(0, start, nullptr, 0, 0);
5120}
5121
5122inline UBool
5123UnicodeString::truncate(int32_t targetLength)
5124{
5125 if(isBogus() && targetLength == 0) {
5126 // truncate(0) of a bogus string makes the string empty and non-bogus
5127 unBogus();
5128 return false;
5129 } else if (static_cast<uint32_t>(targetLength) < static_cast<uint32_t>(length())) {
5130 setLength(targetLength);
5131 return true;
5132 } else {
5133 return false;
5134 }
5135}
5136
5137inline UnicodeString&
5138UnicodeString::reverse() U_LIFETIME_BOUND
5139{ return doReverse(0, length()); }
5140
5141inline UnicodeString&
5143 int32_t _length) U_LIFETIME_BOUND
5144{ return doReverse(start, _length); }
5145
5146U_NAMESPACE_END
5147
5148#endif /* U_SHOW_CPLUSPLUS_API */
5149
5150#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.
std::u16string_view toU16StringView(std::u16string_view sv)
Pass-through overload.
Definition char16ptr.h:400
std::u16string_view toU16StringViewNullable(const T &text)
Pass-through overload.
Definition char16ptr.h:430
#define U_ALIASING_BARRIER(ptr)
Barrier for pointer anti-aliasing optimizations even across function boundaries.
Definition char16ptr.h:37
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:55
char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition char16ptr.h:49
const char16_t * wrapper with implicit conversion from distinct but bit-compatible pointer types.
Definition char16ptr.h:156
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:198
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:250
int32_t length() const
Returns the number of 16-bit code units in the text.
Definition rep.h:245
Replaceable()
Default constructor.
Definition rep.h:242
Implementation of ByteSink that writes to a "string".
Definition bytestream.h:293
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:61
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
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.
char16_t charAt(int32_t offset) const
Return the code unit at offset offset.
Definition unistr.h:4910
void push_back(char16_t c)
Appends the code unit c to the UnicodeString object.
Definition unistr.h:2382
void swap(UnicodeString &other) noexcept
Swap strings.
virtual char16_t getCharAt(int32_t offset) const override
The change in Replaceable to use virtual getCharAt() allows UnicodeString::charAt() to be inline agai...
bool operator==(const S &text) const
Equality operator.
Definition unistr.h:355
int8_t caseCompareBetween(int32_t start, int32_t limit, const UnicodeString &srcText, int32_t srcStart, int32_t srcLimit, uint32_t options) const
Compare two strings case-insensitively using full case folding.
Definition unistr.h:4546
virtual int32_t getLength() const override
Implement Replaceable::getLength() (see jitterbug 1027).
int32_t hashCode() const
Generate a hash code for this object.
Definition unistr.h:4280
UnicodeString & setTo(char16_t *buffer, int32_t buffLength, int32_t buffCapacity)
Aliasing setTo() function, analogous to the writable-aliasing char16_t* constructor.
UnicodeString & operator=(const S &src)
Assignment operator.
Definition unistr.h:2023
UnicodeString & operator=(UnicodeString &&src) noexcept
Move assignment operator; might leave src in bogus state.
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:3303
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.
int32_t lastIndexOf(const UnicodeString &text) const
Locate in this the last occurrence of the characters in text, using bitwise comparison.
Definition unistr.h:4682
UnicodeString & reverse()
Reverse this UnicodeString in place.
Definition unistr.h:5138
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.
static UnicodeString readOnlyAlias(const UnicodeString &text)
Readonly-aliasing factory method.
Definition unistr.h:3676
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:316
bool operator!=(const S &text) const
Inequality operator.
Definition unistr.h:388
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.
UnicodeString & operator+=(const S &src)
Append operator.
Definition unistr.h:2284
int32_t getCapacity() const
Return the capacity of the internal buffer of the UnicodeString object.
Definition unistr.h:4274
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...
UnicodeString & operator+=(char16_t ch)
Append operator.
Definition unistr.h:5040
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.
operator std::u16string_view() const
Converts to a std::u16string_view.
Definition unistr.h:3092
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.
int8_t compareCodePointOrder(const UnicodeString &text) const
Compare two Unicode strings in code point order.
Definition unistr.h:4436
UnicodeString & insert(int32_t start, const UnicodeString &srcText, int32_t srcStart, int32_t srcLength)
Insert the characters in srcText in the range [srcStart, srcStart + srcLength) into the UnicodeString...
Definition unistr.h:5053
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:4791
UnicodeString & toTitle(BreakIterator *titleIter)
Titlecase this string, convenience function using the default locale.
int8_t compare(const UnicodeString &text) const
Compare the characters bitwise in this UnicodeString to the characters in text.
Definition unistr.h:4375
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.
UnicodeString & append(const S &src)
Appends the characters in src which is, or which is implicitly convertible to, a std::u16string_view ...
Definition unistr.h:2354
UChar32 char32At(int32_t offset) const
Return the code point that contains the code unit at offset offset.
UnicodeString tempSubStringBetween(int32_t start, int32_t limit=INT32_MAX) const
Create a temporary substring for the specified range.
Definition unistr.h:4895
UnicodeString(const char *src, int32_t textLength, enum EInvariant inv)
Constructs a Unicode string from an invariant-character char * string.
int8_t caseCompare(const UnicodeString &text, uint32_t options) const
Compare two strings case-insensitively using full case folding.
Definition unistr.h:4498
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.
int32_t length() const
Return the length of the UnicodeString object.
Definition unistr.h:4269
unspecified_reverse_iterator rend() const
Definition unistr.h:1956
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:3411
void setToBogus()
Make this UnicodeString object invalid.
friend void swap(UnicodeString &s1, UnicodeString &s2) noexcept
Non-member UnicodeString swap function.
Definition unistr.h:2052
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.
UNISTR_FROM_STRING_EXPLICIT UnicodeString(const S &text)
Constructor from text which is, or which is implicitly convertible to, a std::u16string_view or (if U...
Definition unistr.h:3329
UnicodeString & findAndReplace(const UnicodeString &oldText, const UnicodeString &newText)
Replace all occurrences of characters in oldText with the characters in newText.
Definition unistr.h:4834
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.
StringClass toUTF8String() const
Convert the UnicodeString to a UTF-8 string.
Definition unistr.h:1803
UNISTR_FROM_CHAR_EXPLICIT UnicodeString(UChar32 ch)
Single UChar32 (code point) constructor.
static UnicodeString readOnlyAlias(const S &text)
Readonly-aliasing factory method.
Definition unistr.h:3653
UnicodeString & trim()
Trims leading and trailing whitespace from this UnicodeString.
unspecified_iterator begin() const
Definition unistr.h:1938
UBool startsWith(const UnicodeString &text) const
Determine if this starts with the characters in text.
Definition unistr.h:4722
UnicodeString & operator=(const UnicodeString &srcText)
Assignment operator.
UBool truncate(int32_t targetLength)
Truncate this UnicodeString to the targetLength.
Definition unistr.h:5123
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:1784
UnicodeString(UnicodeString &&src) noexcept
Move constructor; might leave src in bogus state.
void extract(int32_t start, int32_t length, Char16Ptr dst, int32_t dstStart=0) const
Copy the characters in the range [start, start + length) into the array dst, beginning at dstStart.
Definition unistr.h:4857
UnicodeString & toUpper()
Convert the characters in this to UPPER CASE following the conventions of the default locale.
UnicodeString(const char16_t *text, int32_t textLength)
char16_t* constructor.
UBool endsWith(const UnicodeString &text) const
Determine if this ends with the characters in text.
Definition unistr.h:4748
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:3397
int32_t toUTF32(UChar32 *utf32, int32_t capacity, UErrorCode &errorCode) const
Convert the UnicodeString to UTF-32.
UBool isBogus() const
Determine if this object contains a valid string.
Definition unistr.h:4284
UnicodeString & toTitle(BreakIterator *titleIter, const Locale &locale)
Titlecase this string.
UnicodeString & setTo(const UnicodeString &srcText, int32_t srcStart)
Set the text in the UnicodeString object to the characters in srcText in the range [srcStart,...
Definition unistr.h:4978
UnicodeString & foldCase(uint32_t options=0)
Case-folds the characters in this string.
int8_t compareBetween(int32_t start, int32_t limit, const UnicodeString &srcText, int32_t srcStart, int32_t srcLimit) const
Compare the characters bitwise in the range [start, limit) with the characters in srcText in the rang...
Definition unistr.h:4412
UnicodeString()
Construct an empty UnicodeString.
Definition unistr.h:4237
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:3281
unspecified_reverse_iterator rbegin() const
Definition unistr.h:1950
UnicodeString & setCharAt(int32_t offset, char16_t ch)
Set the character at the specified offset to the specified character.
UnicodeString & append(const UnicodeString &srcText, int32_t srcStart, int32_t srcLength)
Append the characters in srcText in the range [srcStart, srcStart + srcLength) to the UnicodeString o...
Definition unistr.h:5015
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.
UnicodeString & replaceBetween(int32_t start, int32_t limit, const UnicodeString &srcText)
Replace the characters in the range [start, limit) with the characters in srcText.
Definition unistr.h:4820
UnicodeString & removeBetween(int32_t start, int32_t limit=static_cast< int32_t >(INT32_MAX))
Remove the characters in the range [start, limit) from the UnicodeString object.
Definition unistr.h:5112
char16_t * getBuffer(int32_t minCapacity)
Get a read/write pointer to the internal buffer.
UnicodeString & toLower()
Convert the characters in this to lower case following the conventions of the default locale.
int32_t indexOf(const UnicodeString &text) const
Locate in this the first occurrence of the characters in text, using bitwise comparison.
Definition unistr.h:4572
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.
void releaseBuffer(int32_t newLength=-1)
Release a read/write buffer on a UnicodeString object with an "open" getBuffer(minCapacity).
void extractBetween(int32_t start, int32_t limit, char16_t *dst, int32_t dstStart=0) const
Copy the characters in the range [start, limit) into the array dst, beginning at dstStart.
Definition unistr.h:4885
UnicodeString & remove()
Removes all characters from the UnicodeString object and clears the bogus flag.
Definition unistr.h:5089
void toUTF8(ByteSink &sink) const
Convert the UnicodeString to UTF-8 and write the result to a ByteSink.
char16_t value_type
C++ boilerplate.
Definition unistr.h:306
unspecified_iterator end() const
Definition unistr.h:1944
UBool isEmpty() const
Determine if this string is empty.
Definition unistr.h:4918
UnicodeString & retainBetween(int32_t start, int32_t limit=INT32_MAX)
Retain only the characters in the range [start, limit) from the UnicodeString object.
Definition unistr.h:5117
virtual UBool hasMetaData() const override
Replaceable API.
int8_t compareCodePointOrderBetween(int32_t start, int32_t limit, const UnicodeString &srcText, int32_t srcStart, int32_t srcLimit) const
Compare two Unicode strings in code point order.
Definition unistr.h:4473
U_CAPI int32_t u_strlen(const UChar *s)
U_COMMON_API UnicodeString unistr_internalConcat(const UnicodeString &s1, std::u16string_view s2)
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
U_COMMON_API UnicodeString operator+(const UnicodeString &s1, const UnicodeString &s2)
Creates a new UnicodeString from the concatenation of two others.
#define U_CALLCONV
Similar to U_CDECL_BEGIN/U_CDECL_END, this qualifier is necessary in callback function typedefs to ma...
Definition platform.h:880
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:358
char16_t UChar
The base type for UTF-16 code units and pointers.
Definition umachine.h:400
int32_t UChar32
Define UChar32 as a type for single Unicode code points.
Definition umachine.h:449
#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
#define U_SIZEOF_UCHAR
Number of bytes in a UChar (always 2).
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:71
#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.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition utypes.h:315