From 5b604dd360ffc162f163962ccb2b1af109a5f93f Mon Sep 17 00:00:00 2001 From: B Stack Date: Thu, 17 Oct 2019 15:59:39 -0400 Subject: add upstream 10.17 --- zen/string_traits.h | 67 +++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) (limited to 'zen/string_traits.h') diff --git a/zen/string_traits.h b/zen/string_traits.h index 93cfd81c..d0f34d54 100644 --- a/zen/string_traits.h +++ b/zen/string_traits.h @@ -7,6 +7,7 @@ #ifndef STRING_TRAITS_H_813274321443234 #define STRING_TRAITS_H_813274321443234 +#include #include //strlen #include "type_traits.h" @@ -36,27 +37,9 @@ strBegin(): -> not null-terminated! -> may be nullptr if length is 0! //reference a sub-string for consumption by zen string_tools -template -class StringRef -{ -public: - template - StringRef(Iterator first, Iterator last) : len_(last - first), - str_(first != last ? &*first : reinterpret_cast(this) /*Win32 APIs like CompareStringOrdinal() choke on nullptr!*/) - { - static_assert(alignof(StringRef) % alignof(Char) == 0); //even though str_ is never dereferenced, make sure the pointer value respects alignment (why? because we can) - } - //StringRef(const Char* str, size_t len) : str_(str), len_(len) {} -> needless constraint! Char* not available for empty range! - - Char* data () const { return str_; } //no null-termination! - size_t length() const { return len_; } - -private: - const size_t len_; - Char* const str_; -}; - - +//=> std::string_view seems decent, but of course fucks up in one regard: construction +template auto makeStringView(Iterator first, Iterator last); +template auto makeStringView(Iterator first, size_t len); @@ -84,7 +67,7 @@ public: }; -template struct GetCharTypeImpl { using Type = void; }; +template struct GetCharTypeImpl { using Type = void; }; template struct GetCharTypeImpl @@ -103,10 +86,10 @@ struct GetCharTypeImpl template <> struct GetCharTypeImpl { using Type = char; }; template <> struct GetCharTypeImpl { using Type = wchar_t; }; -template <> struct GetCharTypeImpl, false> { using Type = char; }; -template <> struct GetCharTypeImpl, false> { using Type = wchar_t; }; -template <> struct GetCharTypeImpl, false> { using Type = char; }; -template <> struct GetCharTypeImpl, false> { using Type = wchar_t; }; +template <> struct GetCharTypeImpl, false> { using Type = char; }; +template <> struct GetCharTypeImpl, false> { using Type = wchar_t; }; +template <> struct GetCharTypeImpl, false> { using Type = char; }; +template <> struct GetCharTypeImpl, false> { using Type = wchar_t; }; ZEN_INIT_DETECT_MEMBER_TYPE(value_type); @@ -184,11 +167,10 @@ inline const wchar_t* strBegin(const wchar_t* str) { return str; } inline const char* strBegin(const char& ch) { return &ch; } inline const wchar_t* strBegin(const wchar_t& ch) { return &ch; } -inline const char* strBegin(const StringRef& ref) { return ref.data(); } -inline const wchar_t* strBegin(const StringRef& ref) { return ref.data(); } -inline const char* strBegin(const StringRef& ref) { return ref.data(); } -inline const wchar_t* strBegin(const StringRef& ref) { return ref.data(); } - +inline const char* strBegin(const std::basic_string_view& ref) { return ref.data(); } +inline const wchar_t* strBegin(const std::basic_string_view& ref) { return ref.data(); } +inline const char* strBegin(const std::basic_string_view& ref) { return ref.data(); } +inline const wchar_t* strBegin(const std::basic_string_view& ref) { return ref.data(); } template ::isStringClass>> inline size_t strLength(const S& str) //SFINAE: T must be a "string" @@ -201,15 +183,15 @@ inline size_t strLength(const wchar_t* str) { return cStringLength(str); } inline size_t strLength(char) { return 1; } inline size_t strLength(wchar_t) { return 1; } -inline size_t strLength(const StringRef& ref) { return ref.length(); } -inline size_t strLength(const StringRef& ref) { return ref.length(); } -inline size_t strLength(const StringRef& ref) { return ref.length(); } -inline size_t strLength(const StringRef& ref) { return ref.length(); } +inline size_t strLength(const std::basic_string_view& ref) { return ref.length(); } +inline size_t strLength(const std::basic_string_view& ref) { return ref.length(); } +inline size_t strLength(const std::basic_string_view& ref) { return ref.length(); } +inline size_t strLength(const std::basic_string_view& ref) { return ref.length(); } } template inline -auto strBegin(S&& str) -> const GetCharTypeT* +auto strBegin(S&& str) { static_assert(IsStringLikeV); return impl::strBegin(std::forward(str)); @@ -222,6 +204,19 @@ size_t strLength(S&& str) static_assert(IsStringLikeV); return impl::strLength(std::forward(str)); } + + +template inline +auto makeStringView(Iterator first, Iterator last) +{ + using CharType = GetCharTypeT; + + return std::basic_string_view(first != last ? &*first : + reinterpret_cast(0x1000), /*Win32 APIs like CompareStringOrdinal() choke on nullptr!*/ + last - first); +} + +template inline auto makeStringView(Iterator first, size_t len) { return makeStringView(first, first + len); } } #endif //STRING_TRAITS_H_813274321443234 -- cgit