Skip to content

Commit

Permalink
ICU-22843 UnicodeString <-> std::u16string_view / wstring_view via te…
Browse files Browse the repository at this point in the history
…mplates
  • Loading branch information
markusicu committed Aug 13, 2024
1 parent 7ffbe77 commit 7220649
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 39 deletions.
31 changes: 31 additions & 0 deletions icu4c/source/common/unicode/char16ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#if U_SHOW_CPLUSPLUS_API

#include <cstddef>
#include <string_view>

/**
* \file
Expand Down Expand Up @@ -306,6 +307,36 @@ inline OldUChar *toOldUCharPtr(char16_t *p) {
return reinterpret_cast<OldUChar *>(p);
}

#ifndef U_FORCE_HIDE_INTERNAL_API
/**
* Is T convertible to a std::u16string_view or to a 16-bit std::wstring_view?
* @internal
*/
template<typename T>
constexpr bool ConvertibleToU16StringView =
std::is_convertible_v<T, std::u16string_view> ||
(U_SIZEOF_WCHAR_T==2 && std::is_convertible_v<T, std::wstring_view>);

namespace internal {
/**
* Pass-through overload.
* @internal
*/
inline std::u16string_view toU16StringView(std::u16string_view sv) { return sv; }

#if U_SIZEOF_WCHAR_T==2
/**
* Basically undefined behavior but sometimes necessary conversion
* from std::wstring_view to std::u16string_view.
* @internal
*/
inline std::u16string_view toU16StringView(std::wstring_view sv) {
return { ConstChar16Ptr(sv.data()), sv.length() };
}
#endif
} // internal
#endif // U_FORCE_HIDE_INTERNAL_API

U_NAMESPACE_END

#endif /* U_SHOW_CPLUSPLUS_API */
Expand Down
4 changes: 3 additions & 1 deletion icu4c/source/common/unicode/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,9 @@
* @{
* \def U_DECLARE_UTF16
* Do not use this macro because it is not defined on all platforms.
* Use the UNICODE_STRING or U_STRING_DECL macros instead.
* In C++, use std::u16string_view literals, see the UNICODE_STRING docs.
* In C, use u"UTF-16 literals".
* See also the public U_STRING_DECL macro.
* @internal
*/
#ifdef U_DECLARE_UTF16
Expand Down
Loading

0 comments on commit 7220649

Please sign in to comment.