diff --git a/include/string_span.h b/include/string_span.h index 44a3f56..c9a4dc9 100644 --- a/include/string_span.h +++ b/include/string_span.h @@ -34,8 +34,9 @@ #if _MSC_VER <= 1800 #define GSL_MSVC_HAS_TYPE_DEDUCTION_BUG -#define GSL_MSVC2013_ICE_WHEN_USING_DUMMY_TEMPLATE_PARAMETER -#define GSL_MSVC2013_EQUAL_ALGORITHM_IS_NOT_CPP14 +#define GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE +#define GSL_MSVC_NO_CPP14_STD_EQUAL +#define GSL_MSVC_NO_DEFAULT_MOVE_CTOR // noexcept is not understood #ifndef GSL_THROW_ON_CONTRACT_VIOLATION @@ -237,27 +238,26 @@ public: constexpr basic_string_span(const basic_string_span& other) = default; // move -#ifdef GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT - constexpr basic_string_span(basic_string_span&& other) - : span_(std::move(other.span_)) - { - } -#else +#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR constexpr basic_string_span(basic_string_span&& other) = default; +#else + constexpr basic_string_span(basic_string_span&& other) + : span_(std::move(other.span_)) + {} #endif // assign constexpr basic_string_span& operator=(const basic_string_span& other) = default; // move assign -#ifdef GSL_MSVC_NO_SUPPORT_FOR_MOVE_CTOR_DEFAULT - constexpr basic_string_span& operator=(basic_string_span&& other) - { - span_ = std::move(other.span_); - return *this; - } -#else +#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR constexpr basic_string_span& operator=(basic_string_span&& other) = default; +#else + constexpr basic_string_span& operator=(basic_string_span&& other) + { + span_ = std::move(other.span_); + return *this; + } #endif // from nullptr @@ -290,74 +290,6 @@ public: : span_(const_cast(s.data()), narrow_cast(s.length())) {} -#ifdef GSL_MSVC2013_ICE_WHEN_USING_DUMMY_TEMPLATE_PARAMETER - template< - typename Cont, - typename DataType = typename Cont::value_type - > - constexpr basic_string_span( - Cont& cont, - std::enable_if_t< - !details::is_span::value - && !details::is_basic_string_span::value - && !(!std::is_const::value && std::is_const::value) - && std::is_convertible::value - && std::is_same().size(), *std::declval().data())>, DataType>::value - >* = nullptr - ) - : span_(cont.data(), cont.size()) - {} - - // disallow creation from temporary containers and strings - template< - typename Cont, - typename DataType = typename Cont::value_type - > - explicit basic_string_span( - Cont&& cont - , - std::enable_if_t< - !details::is_span::value - && !details::is_basic_string_span::value - && std::is_convertible::value - && std::is_same().size(), *std::declval().data())>, DataType>::value - >* = nullptr - ) = delete; - - // from span - template< - typename OtherValueType, - std::ptrdiff_t... OtherDimensions, - typename OtherBounds = static_bounds - > - constexpr basic_string_span( - span other, - typename std::enable_if< - std::is_convertible::value && - std::is_convertible::value - >::type* = nullptr - ) noexcept - : span_(other) - {} - - // from string_span - template< - typename OtherValueType, - std::ptrdiff_t OtherExtent, - typename OtherBounds = static_bounds - > - constexpr basic_string_span( - basic_string_span other, - std::enable_if_t< - std::is_convertible::value - && std::is_convertible::value - >* = nullptr - ) noexcept - : span_(other.data(), other.length()) - {} - -#else // GSL_MSVC2013_ICE_WHEN_USING_DUMMY_TEMPLATE_PARAMETER - // from containers. Containers must have .size() and .data() function signatures template ::value @@ -379,14 +311,27 @@ public: > basic_string_span(Cont&& cont) = delete; +#ifndef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE // from span template , - typename Dummy = std::enable_if_t::value && std::is_convertible::value> + typename Dummy = std::enable_if_t< + std::is_convertible::value + && std::is_convertible, bounds_type>::value> > constexpr basic_string_span(span other) noexcept : span_(other) {} +#else + // from span + constexpr basic_string_span(span other) noexcept + : span_(other) + {} + + template , value_type>::value>> + constexpr basic_string_span(span, Extent> other) noexcept + : span_(other) + {} +#endif // from string_span template other) noexcept : span_(other.data(), other.length()) {} -#endif // GSL_MSVC2013_ICE_WHEN_USING_DUMMY_TEMPLATE_PARAMETER constexpr bool empty() const noexcept { @@ -556,22 +500,22 @@ std::basic_string::type> to_string(basic_strin inline std::string to_string(cstring_span<> view) { - return{ view.data(), view.length() }; + return{ view.data(), static_cast(view.length()) }; } inline std::string to_string(string_span<> view) { - return{ view.data(), view.length() }; + return{ view.data(), static_cast(view.length()) }; } inline std::wstring to_string(cwstring_span<> view) { - return{ view.data(), view.length() }; + return{ view.data(), static_cast(view.length()) }; } inline std::wstring to_string(wstring_span<> view) { - return{ view.data(), view.length() }; + return{ view.data(), static_cast(view.length()) }; } #endif @@ -622,10 +566,8 @@ template one, const T& other) noexcept { gsl::basic_string_span, Extent> tmp(other); -#ifdef GSL_MSVC2013_EQUAL_ALGORITHM_IS_NOT_CPP14 - if (std::distance(one.begin(), one.end()) != std::distance(tmp.begin(), tmp.end())) - return false; - return std::equal(one.begin(), one.end(), tmp.begin()); +#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL + return (one.size() == tmp.size()) && std::equal(one.begin(), one.end(), tmp.begin()); #else return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end()); #endif @@ -639,10 +581,8 @@ template other) noexcept { gsl::basic_string_span, Extent> tmp(one); -#ifdef GSL_MSVC2013_EQUAL_ALGORITHM_IS_NOT_CPP14 - if (std::distance(tmp.begin(), tmp.end()) != std::distance(other.begin(), other.end())) - return false; - return std::equal(tmp.begin(), tmp.end(), other.begin()); +#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL + return (tmp.size() == other.size()) && std::equal(tmp.begin(), tmp.end(), other.begin()); #else return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end()); #endif @@ -959,9 +899,10 @@ bool operator>=(const T& one, gsl::basic_string_span other) noexc #pragma pop_macro("noexcept") #endif // GSL_THROW_ON_CONTRACT_VIOLATION -#undef GSL_MSVC2013_EQUAL_ALGORITHM_IS_NOT_CPP14 -#undef GSL_MSVC2013_ICE_WHEN_USING_DUMMY_TEMPLATE_PARAMETER #undef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG +#undef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE +#define GSL_MSVC_NO_CPP14_STD_EQUAL +#undef GSL_MSVC_NO_DEFAULT_MOVE_CTOR #endif // _MSC_VER <= 1800 #endif // _MSC_VER @@ -975,5 +916,4 @@ bool operator>=(const T& one, gsl::basic_string_span other) noexc #endif #endif // GSL_THROW_ON_CONTRACT_VIOLATION - #endif // GSL_STRING_SPAN_H diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp index 18fda26..88d125a 100644 --- a/tests/string_span_tests.cpp +++ b/tests/string_span_tests.cpp @@ -29,7 +29,6 @@ SUITE(string_span_tests) TEST(TestLiteralConstruction) { cwstring_span<> v = ensure_z(L"Hello"); - CHECK(5 == v.length()); #ifdef CONFIRM_COMPILATION_ERRORS @@ -116,6 +115,24 @@ SUITE(string_span_tests) TEST(EqualityAndImplicitConstructors) { + { + cstring_span<> span = "Hello"; + cstring_span<> span1; + + // comparison to empty span + CHECK(span1 != span); + CHECK(span != span1); + } + + { + cstring_span<> span = "Hello"; + cstring_span<> span1 = "Hello1"; + + // comparison to different span + CHECK(span1 != span); + CHECK(span != span1); + } + { cstring_span<> span = "Hello"; @@ -153,9 +170,6 @@ SUITE(string_span_tests) // comparison to string_span CHECK(span == span); - - // comparison of the original data to string - CHECK(span.data() == std::string("Hello")); } { @@ -431,6 +445,22 @@ SUITE(string_span_tests) { // creating cstring_span + // from span of a final extent + { + span sp = "Hello"; + cstring_span<> span = sp; + CHECK(span.length() == 6); + } + + // from const span of a final extent to non-const string_span +#ifdef CONFIRM_COMPILATION_ERRORS + { + span sp = "Hello"; + string_span<> span = sp; + CHECK(span.length() == 6); + } +#endif + // from string temporary #ifdef CONFIRM_COMPILATION_ERRORS { @@ -794,7 +824,7 @@ SUITE(string_span_tests) TEST(Conversion) { -#ifdef CONFIRM_COMPPILATION_ERRORS +#ifdef CONFIRM_COMPILATION_ERRORS cstring_span<> span = "Hello"; cwstring_span<> wspan{ span }; CHECK(wspan.length() == 5);