@@ -215,6 +215,10 @@ class UnicodeStringAppendable; // unicode/appendable.h
215215 *
216216 * The UnicodeString equivalent of std::string’s clear() is remove().
217217 *
218+ * Starting with ICU 78, a UnicodeString is a C++ "range" of char16_t code units.
219+ * utfStringCodePoints() and unsafeUTFStringCodePoints() can be used to iterate over
220+ * the code points.
221+ *
218222 * A UnicodeString may "alias" an external array of characters
219223 * (that is, point to it, rather than own the array)
220224 * whose lifetime must then at least match the lifetime of the aliasing object.
@@ -289,12 +293,17 @@ class UnicodeStringAppendable; // unicode/appendable.h
289293 * [User Guide Strings chapter](https://unicode-org.github.io/icu/userguide/strings#maximizing-performance-with-the-unicodestring-storage-model).
290294 *
291295 * @see utf.h
296+ * @see utfiterator.h
297+ * @see utfStringCodePoints
298+ * @see unsafeUTFStringCodePoints
292299 * @see CharacterIterator
293300 * @stable ICU 2.0
294301 */
295302class U_COMMON_API UnicodeString : public Replaceable
296303{
297304public:
305+ /* * C++ boilerplate @internal */
306+ using value_type = char16_t ;
298307
299308 /* *
300309 * Constant to be used in the UnicodeString(char *, int32_t, EInvariant) constructor
@@ -1767,7 +1776,8 @@ class U_COMMON_API UnicodeString : public Replaceable
17671776 * Unpaired surrogates are replaced with U+FFFD.
17681777 * Calls toUTF8().
17691778 *
1770- * @param result A standard string (or a compatible object)
1779+ * @tparam StringClass A std::string or a std::u8string (or a compatible type)
1780+ * @param result A std::string or a std::u8string (or a compatible object)
17711781 * to which the UTF-8 version of the string is appended.
17721782 * @return The string object.
17731783 * @stable ICU 4.2
@@ -1780,6 +1790,27 @@ class U_COMMON_API UnicodeString : public Replaceable
17801790 return result;
17811791 }
17821792
1793+ #ifndef U_HIDE_DRAFT_API
1794+ /* *
1795+ * Convert the UnicodeString to a UTF-8 string.
1796+ * Unpaired surrogates are replaced with U+FFFD.
1797+ * Calls toUTF8().
1798+ *
1799+ * @tparam StringClass A std::string or a std::u8string (or a compatible type)
1800+ * @return A std::string or a std::u8string (or a compatible object)
1801+ * with the UTF-8 version of the string.
1802+ * @draft ICU 78
1803+ * @see toUTF8
1804+ */
1805+ template <typename StringClass>
1806+ StringClass toUTF8String () const {
1807+ StringClass result;
1808+ StringByteSink<StringClass> sbs (&result, length ());
1809+ toUTF8 (sbs);
1810+ return result;
1811+ }
1812+ #endif // U_HIDE_DRAFT_API
1813+
17831814 /* *
17841815 * Convert the UnicodeString to UTF-32.
17851816 * Unpaired surrogates are replaced with U+FFFD.
@@ -1892,6 +1923,33 @@ class U_COMMON_API UnicodeString : public Replaceable
18921923 */
18931924 inline UBool isBogus () const ;
18941925
1926+ #ifndef U_HIDE_DRAFT_API
1927+ /* *
1928+ * @return an iterator to the first code unit in this string.
1929+ * The iterator may be a pointer or a contiguous-iterator object.
1930+ * @draft ICU 78
1931+ */
1932+ auto begin () const { return std::u16string_view (*this ).begin (); }
1933+ /* *
1934+ * @return an iterator to just past the last code unit in this string.
1935+ * The iterator may be a pointer or a contiguous-iterator object.
1936+ * @draft ICU 78
1937+ */
1938+ auto end () const { return std::u16string_view (*this ).end (); }
1939+ /* *
1940+ * @return a reverse iterator to the last code unit in this string.
1941+ * The iterator may be a pointer or a contiguous-iterator object.
1942+ * @draft ICU 78
1943+ */
1944+ auto rbegin () const { return std::u16string_view (*this ).rbegin (); }
1945+ /* *
1946+ * @return a reverse iterator to just before the first code unit in this string.
1947+ * The iterator may be a pointer or a contiguous-iterator object.
1948+ * @draft ICU 78
1949+ */
1950+ auto rend () const { return std::u16string_view (*this ).rend (); }
1951+ #endif // U_HIDE_DRAFT_API
1952+
18951953 // ========================================
18961954 // Write operations
18971955 // ========================================
@@ -2318,6 +2376,16 @@ class U_COMMON_API UnicodeString : public Replaceable
23182376 */
23192377 UnicodeString& append (UChar32 srcChar);
23202378
2379+ #ifndef U_HIDE_DRAFT_API
2380+ /* *
2381+ * Appends the code unit `c` to the UnicodeString object.
2382+ * Same as append(c) except does not return *this.
2383+ *
2384+ * @param c the code unit to append
2385+ * @draft ICU 78
2386+ */
2387+ inline void push_back (char16_t c) { append (c); }
2388+ #endif // U_HIDE_DRAFT_API
23212389
23222390 /* Insert operations */
23232391
0 commit comments