Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ICU-22843 UnicodeString <-> std::u16string_view / wstring_view #3076

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; }
markusicu marked this conversation as resolved.
Show resolved Hide resolved

#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