mirror of
https://github.com/microsoft/GSL.git
synced 2024-11-03 17:56:43 -05:00
Fixed operators and constructors for string_span
This commit is contained in:
parent
ace63c5a9d
commit
87c5daa6c4
@ -1,17 +1,17 @@
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (c) 2015 Microsoft Corporation. All rights reserved.
|
||||
//
|
||||
// This code is licensed under the MIT License (MIT).
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
@ -243,17 +243,17 @@ public:
|
||||
// move assign
|
||||
constexpr basic_string_span& operator=(basic_string_span&& other) = default;
|
||||
|
||||
// from nullptr
|
||||
constexpr basic_string_span(std::nullptr_t ptr) noexcept
|
||||
: span_(ptr)
|
||||
{}
|
||||
|
||||
// from nullptr and length
|
||||
constexpr basic_string_span(std::nullptr_t ptr, size_type length) noexcept
|
||||
: span_(ptr, length)
|
||||
{}
|
||||
|
||||
// For pointers and static arrays - if 0-terminated, remove 0 from the view
|
||||
|
||||
// from raw data and length
|
||||
constexpr basic_string_span(pointer ptr, size_type length) noexcept
|
||||
: span_(remove_z(ptr, length))
|
||||
{}
|
||||
// From static arrays - if 0-terminated, remove 0 from the view
|
||||
|
||||
// from static arrays and string literals
|
||||
template<size_t N>
|
||||
@ -263,6 +263,11 @@ public:
|
||||
|
||||
// Those allow 0s within the length, so we do not remove them
|
||||
|
||||
// from raw data and length
|
||||
constexpr basic_string_span(pointer ptr, size_type length) noexcept
|
||||
: span_(ptr, length)
|
||||
{}
|
||||
|
||||
// from string
|
||||
constexpr basic_string_span(std::string& s) noexcept
|
||||
: span_(&(s.at(0)), narrow_cast<std::ptrdiff_t>(s.length()))
|
||||
@ -523,32 +528,126 @@ template <size_t Max = dynamic_range>
|
||||
using wzstring_builder = basic_zstring_builder<wchar_t, Max>;
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range>
|
||||
bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
|
||||
// operator ==
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
|
||||
>
|
||||
bool operator==(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
return std::equal(one.begin(), one.end(), other.begin(), other.end());
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
|
||||
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range>
|
||||
bool operator<(const gsl::basic_string_span<CharT, Extent>& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
|
||||
&& !details::is_basic_string_span<T>::value>
|
||||
>
|
||||
bool operator==(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
|
||||
return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
|
||||
}
|
||||
|
||||
// operator !=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
|
||||
>
|
||||
bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
return !(one == other);
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
|
||||
&& !details::is_basic_string_span<T>::value>
|
||||
>
|
||||
bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
return !(one == other);
|
||||
}
|
||||
|
||||
// operator<
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
|
||||
>
|
||||
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
|
||||
return std::lexicographical_compare(one.begin(), one.end(), other.begin(), other.end());
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range>
|
||||
bool operator<=(const gsl::basic_string_span<CharT, Extent>& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
|
||||
&& !details::is_basic_string_span<T>::value>
|
||||
>
|
||||
bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
|
||||
return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
|
||||
}
|
||||
|
||||
// operator <=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
|
||||
>
|
||||
bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
return !(other < one);
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range>
|
||||
bool operator>(const gsl::basic_string_span<CharT, Extent>& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
|
||||
&& !details::is_basic_string_span<T>::value>
|
||||
>
|
||||
bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
return !(other < one);
|
||||
}
|
||||
|
||||
// operator>
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
|
||||
>
|
||||
bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
return other < one;
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range>
|
||||
bool operator>=(const gsl::basic_string_span<CharT, Extent>& one, const gsl::basic_string_span<CharT, Extent>& other) noexcept
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
|
||||
&& !details::is_basic_string_span<T>::value>
|
||||
>
|
||||
bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
return other < one;
|
||||
}
|
||||
|
||||
// operator >=
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
|
||||
>
|
||||
bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
|
||||
{
|
||||
return !(one < other);
|
||||
}
|
||||
|
||||
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
|
||||
typename Dummy = std::enable_if_t<
|
||||
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
|
||||
&& !details::is_basic_string_span<T>::value>
|
||||
>
|
||||
bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
|
||||
{
|
||||
return !(one < other);
|
||||
}
|
||||
|
@ -14,11 +14,10 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
#include <UnitTest++/UnitTest++.h>
|
||||
#include <cstdlib>
|
||||
#include <string_span.h>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
using namespace std;
|
||||
using namespace gsl;
|
||||
@ -126,6 +125,7 @@ SUITE(string_span_tests)
|
||||
const char* ptr = "Hello";
|
||||
const std::string str = "Hello";
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
gsl::span<const char> sp = ensure_z("Hello");
|
||||
|
||||
// comparison to literal
|
||||
CHECK(span == cstring_span<>("Hello"));
|
||||
@ -148,6 +148,12 @@ SUITE(string_span_tests)
|
||||
// comparison to vector of charaters with no null termination
|
||||
CHECK(span == cstring_span<>(vec));
|
||||
|
||||
// comparison to span
|
||||
CHECK(span == cstring_span<>(sp));
|
||||
|
||||
// comparison to string_span
|
||||
CHECK(span == span);
|
||||
|
||||
// comparison of the original data to string
|
||||
CHECK(span.data() == std::string("Hello"));
|
||||
}
|
||||
@ -162,6 +168,7 @@ SUITE(string_span_tests)
|
||||
char* ptr = ar;
|
||||
std::string str = "Hello";
|
||||
std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
gsl::span<char> sp = ensure_z(ar1);
|
||||
|
||||
// comparison to static array with no null termination
|
||||
CHECK(span == string_span<>(ar));
|
||||
@ -180,52 +187,133 @@ SUITE(string_span_tests)
|
||||
|
||||
// comparison to vector of charaters with no null termination
|
||||
CHECK(span == string_span<>(vec));
|
||||
|
||||
// comparison to span
|
||||
CHECK(span == string_span<>(sp));
|
||||
|
||||
// comparison to string_span
|
||||
CHECK(span == span);
|
||||
}
|
||||
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
|
||||
{
|
||||
const char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
const char ar1[] = "Hello";
|
||||
const char ar2[10] = "Hello";
|
||||
const std::string str = "Hello";
|
||||
const std::vector<char> vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
gsl::span<const char> sp = ensure_z("Hello");
|
||||
|
||||
cstring_span<> span = "Hello";
|
||||
|
||||
// const span, const other type
|
||||
|
||||
CHECK(span == "Hello");
|
||||
CHECK(span == ar);
|
||||
CHECK(span == ar1);
|
||||
CHECK(span == ar2);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
const char* ptr = "Hello";
|
||||
CHECK(span == ptr);
|
||||
#endif
|
||||
CHECK(span == str);
|
||||
CHECK(span == vec);
|
||||
CHECK(span == sp);
|
||||
|
||||
CHECK("Hello" == span);
|
||||
CHECK(ar == span);
|
||||
CHECK(ar1 == span);
|
||||
CHECK(ar2 == span);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(ptr == span);
|
||||
#endif
|
||||
CHECK(str == span);
|
||||
CHECK(vec == span);
|
||||
CHECK(sp == span);
|
||||
|
||||
// const span, non-const other type
|
||||
|
||||
char _ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char _ar1[] = "Hello";
|
||||
char _ar2[10] = "Hello";
|
||||
char* _ptr = _ar1;
|
||||
char* _ptr = _ar;
|
||||
std::string _str = "Hello";
|
||||
std::vector<char> _vec = { 'H', 'e', 'l', 'l', 'o' };
|
||||
gsl::span<char> _sp{ _ar, 5 };
|
||||
|
||||
CHECK(span == _ar);
|
||||
CHECK(span == _ar1);
|
||||
CHECK(span == _ar2);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(span == _ptr);
|
||||
#endif
|
||||
CHECK(span == _str);
|
||||
CHECK(span == _vec);
|
||||
CHECK(span == _sp);
|
||||
|
||||
string_span<> _span{ _ptr };
|
||||
CHECK(_ar == span);
|
||||
CHECK(_ar1 == span);
|
||||
CHECK(_ar2 == span);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(_ptr == span);
|
||||
#endif
|
||||
CHECK(_str == span);
|
||||
CHECK(_vec == span);
|
||||
CHECK(_sp == span);
|
||||
|
||||
string_span<> _span{ _ptr, 5 };
|
||||
|
||||
// non-const span, non-const other type
|
||||
|
||||
CHECK(_span == _ar);
|
||||
CHECK(_span == _ar1);
|
||||
CHECK(_span == _ar2);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(_span == _ptr);
|
||||
#endif
|
||||
CHECK(_span == _str);
|
||||
CHECK(_span == _vec);
|
||||
CHECK(_span == _sp);
|
||||
|
||||
CHECK(_ar == _span);
|
||||
CHECK(_ar1 == _span);
|
||||
CHECK(_ar2 == _span);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(_ptr == _span);
|
||||
#endif
|
||||
CHECK(_str == _span);
|
||||
CHECK(_vec == _span);
|
||||
CHECK(_sp == _span);
|
||||
|
||||
// non-const span, const other type
|
||||
|
||||
CHECK(_span == "Hello");
|
||||
CHECK(_span == ar);
|
||||
CHECK(_span == ar1);
|
||||
CHECK(_span == ar2);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(_span == ptr);
|
||||
#endif
|
||||
CHECK(_span == str);
|
||||
CHECK(_span == vec);
|
||||
}
|
||||
CHECK(_span == sp);
|
||||
|
||||
CHECK("Hello" == _span);
|
||||
CHECK(ar == _span);
|
||||
CHECK(ar1 == _span);
|
||||
CHECK(ar2 == _span);
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
CHECK(ptr == _span);
|
||||
#endif
|
||||
CHECK(str == _span);
|
||||
CHECK(vec == _span);
|
||||
CHECK(sp == _span);
|
||||
|
||||
// two spans
|
||||
|
||||
CHECK(_span == span);
|
||||
CHECK(span == _span);
|
||||
}
|
||||
|
||||
{
|
||||
std::vector<char> str1 = { 'H', 'e', 'l', 'l', 'o' };
|
||||
@ -238,7 +326,6 @@ SUITE(string_span_tests)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
TEST(ComparisonAndImplicitConstructors)
|
||||
{
|
||||
{
|
||||
@ -310,7 +397,7 @@ SUITE(string_span_tests)
|
||||
CHECK(span >= string_span<>(vec));
|
||||
}
|
||||
}
|
||||
TEST(EnzureRemoveZ)
|
||||
TEST(ConstrutorsEnsureZ)
|
||||
{
|
||||
// remove z from literals
|
||||
{
|
||||
@ -351,6 +438,18 @@ SUITE(string_span_tests)
|
||||
}
|
||||
#endif
|
||||
|
||||
// default
|
||||
{
|
||||
cstring_span<> span;
|
||||
CHECK(span.length() == 0);
|
||||
}
|
||||
|
||||
// from nullptr
|
||||
{
|
||||
cstring_span<> span(nullptr);
|
||||
CHECK(span.length() == 0);
|
||||
}
|
||||
|
||||
// from string literal
|
||||
{
|
||||
cstring_span<> span = "Hello";
|
||||
@ -378,14 +477,34 @@ SUITE(string_span_tests)
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const ptr and length
|
||||
// from const ptr and length, include 0
|
||||
{
|
||||
// does not compile with GCC (ISO standard does not allow converting string literals to char*)
|
||||
#ifdef CONFIRM_COMPILATION_ERRORS
|
||||
char* ptr = "Hello";
|
||||
const char* ptr = "Hello";
|
||||
cstring_span<> span{ ptr, 6 };
|
||||
CHECK(span.length() == 6);
|
||||
}
|
||||
|
||||
// from const ptr and length, 0 inside
|
||||
{
|
||||
const char* ptr = "He\0lo";
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const ptr and length
|
||||
{
|
||||
char ar[] = { 'H', 'e', 'l', 'l', 'o' };
|
||||
char* ptr = ar;
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
CHECK(span.length() == 5);
|
||||
}
|
||||
|
||||
// from non-const ptr and length, 0 inside
|
||||
{
|
||||
char ar[] = { 'H', 'e', '\0', 'l', 'o' };
|
||||
char* ptr = ar;
|
||||
cstring_span<> span{ ptr, 5 };
|
||||
CHECK(span.length() == 5);
|
||||
#endif
|
||||
}
|
||||
|
||||
// from const string
|
||||
|
Loading…
Reference in New Issue
Block a user