From edba727151c1ad2ebf3ff791d1616a1aecddd9de Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Fri, 13 Dec 2024 10:03:35 -0800 Subject: [PATCH] ICU-22954 toU16StringView not header-only --- icu4c/source/common/unicode/char16ptr.h | 60 ++++++++++++++----------- icu4c/source/common/unicode/unistr.h | 14 +++--- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/icu4c/source/common/unicode/char16ptr.h b/icu4c/source/common/unicode/char16ptr.h index 05ad87f79097..4665ed72651e 100644 --- a/icu4c/source/common/unicode/char16ptr.h +++ b/icu4c/source/common/unicode/char16ptr.h @@ -36,6 +36,7 @@ # define U_ALIASING_BARRIER(ptr) #endif +// ICU DLL-exported #if U_SHOW_CPLUSPLUS_API U_NAMESPACE_BEGIN @@ -259,10 +260,25 @@ U_NAMESPACE_END #endif // U_SHOW_CPLUSPLUS_API +// Usable in header-only definitions #if U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API namespace U_ICU_NAMESPACE_OR_INTERNAL { +#ifndef U_FORCE_HIDE_INTERNAL_API +/** @internal */ +inline const char16_t *uprv_char16PtrFromUChar(const UChar *p) { +#if U_SHOW_CPLUSPLUS_API + return ConstChar16Ptr(p).get(); +#else +#ifdef U_ALIASING_BARRIER + U_ALIASING_BARRIER(p); +#endif + return reinterpret_cast(p); +#endif +} +#endif + /** * Converts from const char16_t * to const UChar *. * Includes an aliasing barrier if available. @@ -319,19 +335,16 @@ inline OldUChar *toOldUCharPtr(char16_t *p) { return reinterpret_cast(p); } -#ifndef U_FORCE_HIDE_INTERNAL_API -/** @internal */ -inline const char16_t *uprv_char16PtrFromUChar(const UChar *p) { +} // U_ICU_NAMESPACE_OR_INTERNAL + +#endif // U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API + +// ICU DLL-exported #if U_SHOW_CPLUSPLUS_API - return ConstChar16Ptr(p).get(); -#else -#ifdef U_ALIASING_BARRIER - U_ALIASING_BARRIER(p); -#endif - return reinterpret_cast(p); -#endif -} +U_NAMESPACE_BEGIN + +#ifndef U_FORCE_HIDE_INTERNAL_API /** * Is T convertible to a std::u16string_view or some other 16-bit string view? * @internal @@ -347,14 +360,12 @@ constexpr bool ConvertibleToU16StringView = #endif ; -// The following use ConstChar16Ptr. -#if U_SHOW_CPLUSPLUS_API - +namespace internal { /** * Pass-through overload. * @internal */ -inline std::u16string_view uprv_toU16StringView(std::u16string_view sv) { return sv; } +inline std::u16string_view toU16StringView(std::u16string_view sv) { return sv; } #if !U_CHAR16_IS_TYPEDEF && (!defined(_LIBCPP_VERSION) || _LIBCPP_VERSION < 180000) /** @@ -362,7 +373,7 @@ inline std::u16string_view uprv_toU16StringView(std::u16string_view sv) { return * from std::basic_string_view to std::u16string_view. * @internal */ -inline std::u16string_view uprv_toU16StringView(std::basic_string_view sv) { +inline std::u16string_view toU16StringView(std::basic_string_view sv) { return { ConstChar16Ptr(sv.data()), sv.length() }; } #endif @@ -373,7 +384,7 @@ inline std::u16string_view uprv_toU16StringView(std::basic_string_view * from std::wstring_view to std::u16string_view. * @internal */ -inline std::u16string_view uprv_toU16StringView(std::wstring_view sv) { +inline std::u16string_view toU16StringView(std::wstring_view sv) { return { ConstChar16Ptr(sv.data()), sv.length() }; } #endif @@ -384,8 +395,8 @@ inline std::u16string_view uprv_toU16StringView(std::wstring_view sv) { */ template >>> -inline std::u16string_view uprv_toU16StringViewNullable(const T& text) { - return uprv_toU16StringView(text); +inline std::u16string_view toU16StringViewNullable(const T& text) { + return toU16StringView(text); } /** @@ -395,17 +406,16 @@ inline std::u16string_view uprv_toU16StringViewNullable(const T& text) { template >>, typename = void> -inline std::u16string_view uprv_toU16StringViewNullable(const T& text) { +inline std::u16string_view toU16StringViewNullable(const T& text) { if (text == nullptr) return {}; // For backward compatibility. - return uprv_toU16StringView(text); + return toU16StringView(text); } -#endif // U_SHOW_CPLUSPLUS_API - +} // internal #endif // U_FORCE_HIDE_INTERNAL_API -} // U_ICU_NAMESPACE_OR_INTERNAL +U_NAMESPACE_END -#endif // U_SHOW_CPLUSPLUS_API || U_SHOW_CPLUSPLUS_HEADER_API +#endif // U_SHOW_CPLUSPLUS_API #endif // __CHAR16PTR_H__ diff --git a/icu4c/source/common/unicode/unistr.h b/icu4c/source/common/unicode/unistr.h index c376a03bd30a..a1901c915043 100644 --- a/icu4c/source/common/unicode/unistr.h +++ b/icu4c/source/common/unicode/unistr.h @@ -345,7 +345,7 @@ class U_COMMON_API UnicodeString : public Replaceable */ template>> inline bool operator==(const S &text) const { - std::u16string_view sv(uprv_toU16StringView(text)); + std::u16string_view sv(internal::toU16StringView(text)); uint32_t len; // unsigned to avoid a compiler warning return !isBogus() && (len = length()) == sv.length() && doEquals(sv.data(), len); } @@ -1959,7 +1959,7 @@ class U_COMMON_API UnicodeString : public Replaceable template>> inline UnicodeString &operator=(const S &src) { unBogus(); - return doReplace(0, length(), uprv_toU16StringView(src)); + return doReplace(0, length(), internal::toU16StringView(src)); } #endif // U_HIDE_DRAFT_API @@ -2225,7 +2225,7 @@ class U_COMMON_API UnicodeString : public Replaceable */ template>> inline UnicodeString& operator+=(const S &src) { - return doAppend(uprv_toU16StringView(src)); + return doAppend(internal::toU16StringView(src)); } #endif // U_HIDE_DRAFT_API @@ -2298,7 +2298,7 @@ class U_COMMON_API UnicodeString : public Replaceable */ template>> inline UnicodeString& append(const S &src) { - return doAppend(uprv_toU16StringView(src)); + return doAppend(internal::toU16StringView(src)); } #endif // U_HIDE_DRAFT_API @@ -3273,7 +3273,7 @@ class U_COMMON_API UnicodeString : public Replaceable template>> UNISTR_FROM_STRING_EXPLICIT UnicodeString(const S &text) { fUnion.fFields.fLengthAndFlags = kShortString; - doAppend(uprv_toU16StringViewNullable(text)); + doAppend(internal::toU16StringViewNullable(text)); } #endif // U_HIDE_DRAFT_API @@ -3598,7 +3598,7 @@ class U_COMMON_API UnicodeString : public Replaceable */ template>> static inline UnicodeString readOnlyAlias(const S &text) { - return readOnlyAliasFromU16StringView(uprv_toU16StringView(text)); + return readOnlyAliasFromU16StringView(internal::toU16StringView(text)); } /** @@ -4115,7 +4115,7 @@ operator+ (const UnicodeString &s1, const UnicodeString &s2); */ template>> inline UnicodeString operator+(const UnicodeString &s1, const S &s2) { - return unistr_internalConcat(s1, uprv_toU16StringView(s2)); + return unistr_internalConcat(s1, internal::toU16StringView(s2)); } #endif // U_HIDE_DRAFT_API