Skip to content

Commit

Permalink
ICU-22954 toU16StringView not header-only
Browse files Browse the repository at this point in the history
  • Loading branch information
markusicu committed Dec 13, 2024
1 parent 65c0ba9 commit edba727
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
60 changes: 35 additions & 25 deletions icu4c/source/common/unicode/char16ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
# define U_ALIASING_BARRIER(ptr)
#endif

// ICU DLL-exported
#if U_SHOW_CPLUSPLUS_API

U_NAMESPACE_BEGIN
Expand Down Expand Up @@ -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<const char16_t *>(p);
#endif
}
#endif

/**
* Converts from const char16_t * to const UChar *.
* Includes an aliasing barrier if available.
Expand Down Expand Up @@ -319,19 +335,16 @@ inline OldUChar *toOldUCharPtr(char16_t *p) {
return reinterpret_cast<OldUChar *>(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<const char16_t *>(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
Expand All @@ -347,22 +360,20 @@ 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)
/**
* Basically undefined behavior but sometimes necessary conversion
* from std::basic_string_view<uint16_t> to std::u16string_view.
* @internal
*/
inline std::u16string_view uprv_toU16StringView(std::basic_string_view<uint16_t> sv) {
inline std::u16string_view toU16StringView(std::basic_string_view<uint16_t> sv) {
return { ConstChar16Ptr(sv.data()), sv.length() };
}
#endif
Expand All @@ -373,7 +384,7 @@ inline std::u16string_view uprv_toU16StringView(std::basic_string_view<uint16_t>
* 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
Expand All @@ -384,8 +395,8 @@ inline std::u16string_view uprv_toU16StringView(std::wstring_view sv) {
*/
template <typename T,
typename = typename std::enable_if_t<!std::is_pointer_v<std::remove_reference_t<T>>>>
inline std::u16string_view uprv_toU16StringViewNullable(const T& text) {
return uprv_toU16StringView(text);
inline std::u16string_view toU16StringViewNullable(const T& text) {
return toU16StringView(text);
}

/**
Expand All @@ -395,17 +406,16 @@ inline std::u16string_view uprv_toU16StringViewNullable(const T& text) {
template <typename T,
typename = typename std::enable_if_t<std::is_pointer_v<std::remove_reference_t<T>>>,
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__
14 changes: 7 additions & 7 deletions icu4c/source/common/unicode/unistr.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ class U_COMMON_API UnicodeString : public Replaceable
*/
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
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);
}
Expand Down Expand Up @@ -1959,7 +1959,7 @@ class U_COMMON_API UnicodeString : public Replaceable
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
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

Expand Down Expand Up @@ -2225,7 +2225,7 @@ class U_COMMON_API UnicodeString : public Replaceable
*/
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
inline UnicodeString& operator+=(const S &src) {
return doAppend(uprv_toU16StringView(src));
return doAppend(internal::toU16StringView(src));
}
#endif // U_HIDE_DRAFT_API

Expand Down Expand Up @@ -2298,7 +2298,7 @@ class U_COMMON_API UnicodeString : public Replaceable
*/
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
inline UnicodeString& append(const S &src) {
return doAppend(uprv_toU16StringView(src));
return doAppend(internal::toU16StringView(src));
}
#endif // U_HIDE_DRAFT_API

Expand Down Expand Up @@ -3273,7 +3273,7 @@ class U_COMMON_API UnicodeString : public Replaceable
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
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

Expand Down Expand Up @@ -3598,7 +3598,7 @@ class U_COMMON_API UnicodeString : public Replaceable
*/
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
static inline UnicodeString readOnlyAlias(const S &text) {
return readOnlyAliasFromU16StringView(uprv_toU16StringView(text));
return readOnlyAliasFromU16StringView(internal::toU16StringView(text));
}

/**
Expand Down Expand Up @@ -4115,7 +4115,7 @@ operator+ (const UnicodeString &s1, const UnicodeString &s2);
*/
template<typename S, typename = std::enable_if_t<ConvertibleToU16StringView<S>>>
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

Expand Down

0 comments on commit edba727

Please sign in to comment.