Skip to content

Commit

Permalink
ICU-22843 Disambiguate UnicodeString::readOnlyAlias() for MSVC.
Browse files Browse the repository at this point in the history
Both std::u16string_view and std::wstring_view are possible matches for
UnicodeString as a template parameter, but adding an explicit overload
avoids both having to make that choice and taking the detour through
creating any string view at all.
  • Loading branch information
roubert committed Sep 11, 2024
1 parent 376f10d commit 0cb5bc6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions icu4c/source/common/unicode/unistr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3600,6 +3600,29 @@ class U_COMMON_API UnicodeString : public Replaceable
static inline UnicodeString readOnlyAlias(const S &text) {
return readOnlyAliasFromU16StringView(internal::toU16StringView(text));
}

/**
* Readonly-aliasing factory method.
* Aliases the same buffer as the input `text`.
*
* The text will be used for the UnicodeString object, but
* it will not be released when the UnicodeString is destroyed.
* This has copy-on-write semantics:
* When the string is modified, then the buffer is first copied into
* newly allocated memory.
* The aliased buffer is never modified.
*
* In an assignment to another UnicodeString, when using the copy constructor
* or the assignment operator, the text will be copied.
* When using fastCopyFrom(), the text will be aliased again,
* so that both strings then alias the same readonly-text.
*
* @param text The UnicodeString to alias.
* @draft ICU 76
*/
static inline UnicodeString readOnlyAlias(const UnicodeString &text) {
return readOnlyAliasFromUnicodeString(text);
}
#endif // U_HIDE_DRAFT_API

/**
Expand Down Expand Up @@ -3730,6 +3753,7 @@ class U_COMMON_API UnicodeString : public Replaceable

private:
static UnicodeString readOnlyAliasFromU16StringView(std::u16string_view text);
static UnicodeString readOnlyAliasFromUnicodeString(const UnicodeString &text);

// For char* constructors. Could be made public.
UnicodeString &setToUTF8(StringPiece utf8);
Expand Down
10 changes: 10 additions & 0 deletions icu4c/source/common/unistr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ UnicodeString UnicodeString::readOnlyAliasFromU16StringView(std::u16string_view
return result;
}

UnicodeString UnicodeString::readOnlyAliasFromUnicodeString(const UnicodeString &text) {
UnicodeString result;
if (text.isBogus()) {
result.setToBogus();
} else {
result.setTo(false, text.getBuffer(), text.length());
}
return result;
}

#if U_CHARSET_IS_UTF8

UnicodeString::UnicodeString(const char *codepageData) {
Expand Down

0 comments on commit 0cb5bc6

Please sign in to comment.