diff options
author | Jordan Maples [MSFT] <49793787+JordanMaples@users.noreply.github.com> | 2020-04-15 20:00:55 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-15 20:00:55 +0300 |
commit | b43855631afdb9f7ccd4b56ed05330a8e3817af1 (patch) | |
tree | e669d6b074721dbbc830294343e3a29b799e2655 | |
parent | 044849d6fa0cdeb47a3ea51ee02f557b7684e312 (diff) | |
parent | 9cb376c0508475c8aa8be015ef266558232f9583 (diff) |
Merge pull request #861 from JordanMaples/dev/jomaples/lwg3320v3.0.0
implement LWG3320
-rw-r--r-- | include/gsl/span | 24 | ||||
-rw-r--r-- | include/gsl/span_ext | 396 | ||||
-rw-r--r-- | include/gsl/string_span | 1440 | ||||
-rw-r--r-- | tests/span_compatibility_tests.cpp | 79 | ||||
-rw-r--r-- | tests/span_ext_tests.cpp | 720 | ||||
-rw-r--r-- | tests/span_tests.cpp | 151 |
6 files changed, 1274 insertions, 1536 deletions
diff --git a/include/gsl/span b/include/gsl/span index 566f731..c011d76 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -403,9 +403,7 @@ public: using difference_type = std::ptrdiff_t; using iterator = details::span_iterator<ElementType>; - using const_iterator = details::span_iterator<const ElementType>; using reverse_iterator = std::reverse_iterator<iterator>; - using const_reverse_iterator = std::reverse_iterator<const_iterator>; #if defined(GSL_USE_STATIC_CONSTEXPR_WORKAROUND) static constexpr const size_type extent{Extent}; @@ -594,31 +592,9 @@ public: return {data, endData, endData}; } - constexpr const_iterator cbegin() const noexcept - { - const auto data = storage_.data(); - return {data, data + size(), data}; - } - - constexpr const_iterator cend() const noexcept - { - const auto data = storage_.data(); - const auto endData = data + storage_.size(); - return {data, endData, endData}; - } - constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } - constexpr const_reverse_iterator crbegin() const noexcept - { - return const_reverse_iterator{cend()}; - } - constexpr const_reverse_iterator crend() const noexcept - { - return const_reverse_iterator{cbegin()}; - } - #ifdef _MSC_VER // Tell MSVC how to unwrap spans in range-based-for constexpr pointer _Unchecked_begin() const noexcept { return data(); } diff --git a/include/gsl/span_ext b/include/gsl/span_ext index b466df8..1d375fd 100644 --- a/include/gsl/span_ext +++ b/include/gsl/span_ext @@ -1,198 +1,198 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// 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. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef GSL_SPAN_EXT_H -#define GSL_SPAN_EXT_H - -/////////////////////////////////////////////////////////////////////////////// -// -// File: span_ext -// Purpose: continue offering features that have been cut from the official -// implementation of span. -// While modernizing gsl::span a number of features needed to be removed to -// be compliant with the design of std::span -// -/////////////////////////////////////////////////////////////////////////////// - - -#include <gsl/gsl_util> // for narrow_cast, narrow -#include <gsl/span> // for span - -#include <algorithm> // for lexicographical_compare -#include <cstddef> // for ptrdiff_t, size_t -#include <utility> - -namespace gsl -{ - -// [span.comparison], span comparison operators -template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent> -constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r) -{ - return std::equal(l.begin(), l.end(), r.begin(), r.end()); -} - -template <class ElementType, std::size_t Extent> -constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r) -{ - return !(l == r); -} - -template <class ElementType, std::size_t Extent> -constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r) -{ - return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end()); -} - -template <class ElementType, std::size_t Extent> -constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r) -{ - return !(l > r); -} - -template <class ElementType, std::size_t Extent> -constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r) -{ - return r < l; -} - -template <class ElementType, std::size_t Extent> -constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r) -{ - return !(l < r); -} - -// -// make_span() - Utility functions for creating spans -// -template <class ElementType> -constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::size_type count) -{ - return span<ElementType>(ptr, count); -} - -template <class ElementType> -constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem) -{ - return span<ElementType>(firstElem, lastElem); -} - -template <class ElementType, std::size_t N> -constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept -{ - return span<ElementType, N>(arr); -} - -template <class Container> -constexpr span<typename Container::value_type> make_span(Container& cont) -{ - return span<typename Container::value_type>(cont); -} - -template <class Container> -constexpr span<const typename Container::value_type> make_span(const Container& cont) -{ - return span<const typename Container::value_type>(cont); -} - -template <class Ptr> -constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count) -{ - return span<typename Ptr::element_type>(cont, count); -} - -template <class Ptr> -constexpr span<typename Ptr::element_type> make_span(Ptr& cont) -{ - return span<typename Ptr::element_type>(cont); -} - -// Specialization of gsl::at for span -template <class ElementType, std::size_t Extent> -constexpr ElementType& at(span<ElementType, Extent> s, index i) -{ - // No bounds checking here because it is done in span::operator[] called below - Ensures(i >= 0); - return s[narrow_cast<std::size_t>(i)]; -} - -// [span.obs] Free observer functions -template <class ElementType, std::size_t Extent> -constexpr std::ptrdiff_t ssize(const span<ElementType, Extent>& s) noexcept -{ - return static_cast<std::ptrdiff_t>(s.size()); -} - -// [span.iter] Free functions for begin/end functions -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::iterator -begin(const span<ElementType, Extent>& s) noexcept -{ - return s.begin(); -} - -template <class ElementType, std::size_t Extent = dynamic_extent> -constexpr typename span<ElementType, Extent>::iterator -end(const span<ElementType, Extent>& s) noexcept -{ - return s.end(); -} - -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::const_iterator -cbegin(const span<ElementType, Extent>& s) noexcept -{ - return s.cbegin(); -} - -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::const_iterator -cend(const span<ElementType, Extent>& s) noexcept -{ - return s.cend(); -} - -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::reverse_iterator -rbegin(const span<ElementType, Extent>& s) noexcept -{ - return s.rbegin(); -} - -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::reverse_iterator -rend(const span<ElementType, Extent>& s) noexcept -{ - return s.rend(); -} - -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::const_reverse_iterator -crbegin(const span<ElementType, Extent>& s) noexcept -{ - return s.crbegin(); -} - -template <class ElementType, std::size_t Extent> -constexpr typename span<ElementType, Extent>::const_reverse_iterator -crend(const span<ElementType, Extent>& s) noexcept -{ - return s.crend(); -} - -} // namespace gsl - -#endif // GSL_SPAN_EXT_H +///////////////////////////////////////////////////////////////////////////////
+//
+// 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef GSL_SPAN_EXT_H
+#define GSL_SPAN_EXT_H
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// File: span_ext
+// Purpose: continue offering features that have been cut from the official
+// implementation of span.
+// While modernizing gsl::span a number of features needed to be removed to
+// be compliant with the design of std::span
+//
+///////////////////////////////////////////////////////////////////////////////
+
+
+#include <gsl/gsl_util> // for narrow_cast, narrow
+#include <gsl/span> // for span
+
+#include <algorithm> // for lexicographical_compare
+#include <cstddef> // for ptrdiff_t, size_t
+#include <utility>
+
+namespace gsl
+{
+
+// [span.comparison], span comparison operators
+template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
+constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
+{
+ return std::equal(l.begin(), l.end(), r.begin(), r.end());
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
+{
+ return !(l == r);
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
+{
+ return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
+{
+ return !(l > r);
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
+{
+ return r < l;
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
+{
+ return !(l < r);
+}
+
+//
+// make_span() - Utility functions for creating spans
+//
+template <class ElementType>
+constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::size_type count)
+{
+ return span<ElementType>(ptr, count);
+}
+
+template <class ElementType>
+constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
+{
+ return span<ElementType>(firstElem, lastElem);
+}
+
+template <class ElementType, std::size_t N>
+constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
+{
+ return span<ElementType, N>(arr);
+}
+
+template <class Container>
+constexpr span<typename Container::value_type> make_span(Container& cont)
+{
+ return span<typename Container::value_type>(cont);
+}
+
+template <class Container>
+constexpr span<const typename Container::value_type> make_span(const Container& cont)
+{
+ return span<const typename Container::value_type>(cont);
+}
+
+template <class Ptr>
+constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
+{
+ return span<typename Ptr::element_type>(cont, count);
+}
+
+template <class Ptr>
+constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
+{
+ return span<typename Ptr::element_type>(cont);
+}
+
+// Specialization of gsl::at for span
+template <class ElementType, std::size_t Extent>
+constexpr ElementType& at(span<ElementType, Extent> s, index i)
+{
+ // No bounds checking here because it is done in span::operator[] called below
+ Ensures(i >= 0);
+ return s[narrow_cast<std::size_t>(i)];
+}
+
+// [span.obs] Free observer functions
+template <class ElementType, std::size_t Extent>
+constexpr std::ptrdiff_t ssize(const span<ElementType, Extent>& s) noexcept
+{
+ return static_cast<std::ptrdiff_t>(s.size());
+}
+
+// [span.iter] Free functions for begin/end functions
+template <class ElementType, std::size_t Extent>
+constexpr typename span<ElementType, Extent>::iterator
+begin(const span<ElementType, Extent>& s) noexcept
+{
+ return s.begin();
+}
+
+template <class ElementType, std::size_t Extent = dynamic_extent>
+constexpr typename span<ElementType, Extent>::iterator
+end(const span<ElementType, Extent>& s) noexcept
+{
+ return s.end();
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr typename span<ElementType, Extent>::reverse_iterator
+rbegin(const span<ElementType, Extent>& s) noexcept
+{
+ return s.rbegin();
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr typename span<ElementType, Extent>::reverse_iterator
+rend(const span<ElementType, Extent>& s) noexcept
+{
+ return s.rend();
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr typename span<ElementType, Extent>::iterator
+cbegin(const span<ElementType, Extent>& s) noexcept
+{
+ return s.begin();
+}
+
+template <class ElementType, std::size_t Extent = dynamic_extent>
+constexpr typename span<ElementType, Extent>::iterator
+cend(const span<ElementType, Extent>& s) noexcept
+{
+ return s.end();
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr typename span<ElementType, Extent>::reverse_iterator
+crbegin(const span<ElementType, Extent>& s) noexcept
+{
+ return s.rbegin();
+}
+
+template <class ElementType, std::size_t Extent>
+constexpr typename span<ElementType, Extent>::reverse_iterator
+crend(const span<ElementType, Extent>& s) noexcept
+{
+ return s.rend();
+}
+
+} // namespace gsl
+
+#endif // GSL_SPAN_EXT_H
diff --git a/include/gsl/string_span b/include/gsl/string_span index 2207291..b96c9ff 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -1,724 +1,716 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// 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. -// -/////////////////////////////////////////////////////////////////////////////// - -#ifndef GSL_STRING_SPAN_H -#define GSL_STRING_SPAN_H - -#include <gsl/gsl_assert> // for Ensures, Expects -#include <gsl/gsl_util> // for narrow_cast -#include <gsl/span_ext> // for operator!=, operator==, dynamic_extent - -#include <algorithm> // for equal, lexicographical_compare -#include <array> // for array -#include <cstddef> // for size_t, nullptr_t -#include <cstdint> // for PTRDIFF_MAX -#include <cstring> -#include <string> // for basic_string, allocator, char_traits -#include <type_traits> // for declval, is_convertible, enable_if_t, add_... - -#if defined(_MSC_VER) && !defined(__clang__) -#pragma warning(push) - -// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool. -#pragma warning(disable : 26446) // TODO: bug in parser - attributes and templates -#pragma warning(disable : 26481) // TODO: suppress does not work inside templates sometimes - -#if _MSC_VER < 1910 -#pragma push_macro("constexpr") -#define constexpr /*constexpr*/ - -#endif // _MSC_VER < 1910 -#endif // _MSC_VER - -namespace gsl -{ -// -// czstring and wzstring -// -// These are "tag" typedefs for C-style strings (i.e. null-terminated character arrays) -// that allow static analysis to help find bugs. -// -// There are no additional features/semantics that we can find a way to add inside the -// type system for these types that will not either incur significant runtime costs or -// (sometimes needlessly) break existing programs when introduced. -// - -template <typename CharT, std::size_t Extent = dynamic_extent> -using basic_zstring = CharT*; - -template <std::size_t Extent = dynamic_extent> -using czstring = basic_zstring<const char, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cwzstring = basic_zstring<const wchar_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cu16zstring = basic_zstring<const char16_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cu32zstring = basic_zstring<const char32_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using zstring = basic_zstring<char, Extent>; - -template <std::size_t Extent = dynamic_extent> -using wzstring = basic_zstring<wchar_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using u16zstring = basic_zstring<char16_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using u32zstring = basic_zstring<char32_t, Extent>; - -namespace details -{ - template <class CharT> - std::size_t string_length(const CharT* str, std::size_t n) - { - if (str == nullptr || n == dynamic_extent) return 0; - - const span<const CharT> str_span{str, n}; - - std::size_t len = 0; - while (len < n && str_span[len]) len++; - - return len; - } -} // namespace details - -// -// ensure_sentinel() -// -// Provides a way to obtain an span from a contiguous sequence -// that ends with a (non-inclusive) sentinel value. -// -// Will fail-fast if sentinel cannot be found before max elements are examined. -// -template <typename T, const T Sentinel> -span<T, dynamic_extent> ensure_sentinel(T* seq, - std::size_t max = static_cast<std::size_t>(-1)) -{ - Ensures(seq != nullptr); - - GSL_SUPPRESS( - f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work - auto cur = seq; - Ensures(cur != nullptr); // workaround for removing the warning - - GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute // TODO: suppress does not work - while (static_cast<std::size_t>(cur - seq) < max && *cur != Sentinel) ++cur; - Ensures(*cur == Sentinel); - return {seq, static_cast<std::size_t>(cur - seq)}; -} - -// -// ensure_z - creates a span for a zero terminated strings. -// Will fail fast if a null-terminator cannot be found before -// the limit of size_type. -// -template <typename CharT> -span<CharT, dynamic_extent> ensure_z(CharT* const& sz, - std::size_t max = static_cast<std::size_t>(-1)) -{ - return ensure_sentinel<CharT, CharT(0)>(sz, max); -} - -template <typename CharT, std::size_t N> -span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N]) -{ - return ensure_z(&sz[0], N); -} - -template <class Cont> -span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent> -ensure_z(Cont& cont) -{ - return ensure_z(cont.data(), cont.size()); -} - -template <typename CharT, std::size_t> -class basic_string_span; - -namespace details -{ - template <typename T> - struct is_basic_string_span_oracle : std::false_type - { - }; - - template <typename CharT, std::size_t Extent> - struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type - { - }; - - template <typename T> - struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>> - { - }; -} // namespace details - -// -// string_span and relatives -// -template <typename CharT, std::size_t Extent = dynamic_extent> -class basic_string_span -{ -public: - using element_type = CharT; - using value_type = std::remove_cv_t<element_type>; - using pointer = std::add_pointer_t<element_type>; - using reference = std::add_lvalue_reference_t<element_type>; - using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>; - using impl_type = span<element_type, Extent>; - - using size_type = typename impl_type::size_type; - using iterator = typename impl_type::iterator; - using const_iterator = typename impl_type::const_iterator; - using reverse_iterator = typename impl_type::reverse_iterator; - using const_reverse_iterator = typename impl_type::const_reverse_iterator; - - // default (empty) - constexpr basic_string_span() noexcept = default; - - // copy - constexpr basic_string_span(const basic_string_span& other) noexcept = default; - - // assign - constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default; - - constexpr basic_string_span(pointer ptr, size_type length) : span_(ptr, length) {} - constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {} - - // From static arrays - if 0-terminated, remove 0 from the view - // All other containers allow 0s within the length, so we do not remove them - template <std::size_t N> - constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr)) - {} - - template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>> - constexpr basic_string_span(std::array<ArrayElementType, N>& arr) noexcept : span_(arr) - {} - - template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>> - constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) noexcept : span_(arr) - {} - - // Container signature should work for basic_string after C++17 version exists - template <class Traits, class Allocator> - // GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug - constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str) - : span_(&str[0], str.length()) - {} - - template <class Traits, class Allocator> - constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str) - : span_(&str[0], str.length()) - {} - - // from containers. Containers must have a pointer type and data() function signatures - template <class Container, - class = std::enable_if_t< - !details::is_basic_string_span<Container>::value && - std::is_convertible<typename Container::pointer, pointer>::value && - std::is_convertible<typename Container::pointer, - decltype(std::declval<Container>().data())>::value>> - constexpr basic_string_span(Container& cont) : span_(cont) - {} - - template <class Container, - class = std::enable_if_t< - !details::is_basic_string_span<Container>::value && - std::is_convertible<typename Container::pointer, pointer>::value && - std::is_convertible<typename Container::pointer, - decltype(std::declval<Container>().data())>::value>> - constexpr basic_string_span(const Container& cont) : span_(cont) - {} - - // from string_span - template < - class OtherValueType, std::size_t OtherExtent, - class = std::enable_if_t<std::is_convertible< - typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>> - constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other) - : span_(other.data(), other.length()) - {} - - template <size_type Count> - constexpr basic_string_span<element_type, Count> first() const - { - return {span_.template first<Count>()}; - } - - constexpr basic_string_span<element_type, dynamic_extent> first(size_type count) const - { - return {span_.first(count)}; - } - - template <size_type Count> - constexpr basic_string_span<element_type, Count> last() const - { - return {span_.template last<Count>()}; - } - - constexpr basic_string_span<element_type, dynamic_extent> last(size_type count) const - { - return {span_.last(count)}; - } - - template <size_type Offset, size_type Count> - constexpr basic_string_span<element_type, Count> subspan() const - { - return {span_.template subspan<Offset, Count>()}; - } - - constexpr basic_string_span<element_type, dynamic_extent> - subspan(size_type offset, size_type count = dynamic_extent) const - { - return {span_.subspan(offset, count)}; - } - - constexpr reference operator[](size_type idx) const { return span_[idx]; } - constexpr reference operator()(size_type idx) const { return span_[idx]; } - - constexpr pointer data() const { return span_.data(); } - - constexpr size_type length() const noexcept { return span_.size(); } - constexpr size_type size() const noexcept { return span_.size(); } - constexpr size_type size_bytes() const noexcept { return span_.size_bytes(); } - constexpr size_type length_bytes() const noexcept { return span_.length_bytes(); } - constexpr bool empty() const noexcept { return size() == 0; } - - constexpr iterator begin() const noexcept { return span_.begin(); } - constexpr iterator end() const noexcept { return span_.end(); } - - constexpr const_iterator cbegin() const noexcept { return span_.cbegin(); } - constexpr const_iterator cend() const noexcept { return span_.cend(); } - - constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); } - constexpr reverse_iterator rend() const noexcept { return span_.rend(); } - - constexpr const_reverse_iterator crbegin() const noexcept { return span_.crbegin(); } - constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); } - -private: - static impl_type remove_z(pointer const& sz, std::size_t max) - { - return {sz, details::string_length(sz, max)}; - } - - template <std::size_t N> - static impl_type remove_z(element_type (&sz)[N]) - { - return remove_z(&sz[0], N); - } - - impl_type span_; -}; - -template <std::size_t Extent = dynamic_extent> -using string_span = basic_string_span<char, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cstring_span = basic_string_span<const char, Extent>; - -template <std::size_t Extent = dynamic_extent> -using wstring_span = basic_string_span<wchar_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cwstring_span = basic_string_span<const wchar_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using u16string_span = basic_string_span<char16_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cu16string_span = basic_string_span<const char16_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using u32string_span = basic_string_span<char32_t, Extent>; - -template <std::size_t Extent = dynamic_extent> -using cu32string_span = basic_string_span<const char32_t, Extent>; - -// -// to_string() allow (explicit) conversions from string_span to string -// - -template <typename CharT, std::size_t Extent> -std::basic_string<typename std::remove_const<CharT>::type> -to_string(basic_string_span<CharT, Extent> view) -{ - return {view.data(), narrow_cast<std::size_t>(view.length())}; -} - -template <typename CharT, typename Traits = typename std::char_traits<CharT>, - typename Allocator = std::allocator<CharT>, typename gCharT, std::size_t Extent> -std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view) -{ - return {view.data(), narrow_cast<std::size_t>(view.length())}; -} - -template <class ElementType, std::size_t Extent> -basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value> -as_bytes(basic_string_span<ElementType, Extent> s) noexcept -{ - GSL_SUPPRESS(type.1) // NO-FORMAT: attribute - return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()}; -} - -template <class ElementType, std::size_t Extent, - class = std::enable_if_t<!std::is_const<ElementType>::value>> -basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value> -as_writable_bytes(basic_string_span<ElementType, Extent> s) noexcept -{ - GSL_SUPPRESS(type.1) // NO-FORMAT: attribute - return {reinterpret_cast<byte*>(s.data()), s.size_bytes()}; -} - -// zero-terminated string span, used to convert -// zero-terminated spans to legacy strings -template <typename CharT, std::size_t Extent = dynamic_extent> -class basic_zstring_span -{ -public: - using value_type = CharT; - using const_value_type = std::add_const_t<CharT>; - - using pointer = std::add_pointer_t<value_type>; - using const_pointer = std::add_pointer_t<const_value_type>; - - using zstring_type = basic_zstring<value_type, Extent>; - using const_zstring_type = basic_zstring<const_value_type, Extent>; - - using impl_type = span<value_type, Extent>; - using string_span_type = basic_string_span<value_type, Extent>; - - constexpr basic_zstring_span(impl_type s) : span_(s) - { - // expects a zero-terminated span - Expects(s[s.size() - 1] == '\0'); - } - - // copy - constexpr basic_zstring_span(const basic_zstring_span& other) = default; - - // move - constexpr basic_zstring_span(basic_zstring_span&& other) = default; - - // assign - constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default; - - // move assign - constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default; - - constexpr bool empty() const noexcept { return span_.size() == 0; } - - constexpr string_span_type as_string_span() const noexcept - { - const auto sz = span_.size(); - return {span_.data(), sz > 1 ? sz - 1 : 0}; - } - constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); } - - constexpr const_zstring_type assume_z() const noexcept { return span_.data(); } - -private: - impl_type span_; -}; - -template <std::size_t Max = dynamic_extent> -using zstring_span = basic_zstring_span<char, Max>; - -template <std::size_t Max = dynamic_extent> -using wzstring_span = basic_zstring_span<wchar_t, Max>; - -template <std::size_t Max = dynamic_extent> -using u16zstring_span = basic_zstring_span<char16_t, Max>; - -template <std::size_t Max = dynamic_extent> -using u32zstring_span = basic_zstring_span<char32_t, Max>; - -template <std::size_t Max = dynamic_extent> -using czstring_span = basic_zstring_span<const char, Max>; - -template <std::size_t Max = dynamic_extent> -using cwzstring_span = basic_zstring_span<const wchar_t, Max>; - -template <std::size_t Max = dynamic_extent> -using cu16zstring_span = basic_zstring_span<const char16_t, Max>; - -template <std::size_t Max = dynamic_extent> -using cu32zstring_span = basic_zstring_span<const char32_t, Max>; - -// operator == -template <class CharT, std::size_t Extent, class T, - class = std::enable_if_t< - details::is_basic_string_span<T>::value || - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>> -bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other) -{ - const gsl::basic_string_span<std::add_const_t<CharT>> tmp(other); - return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end()); -} - -template <class CharT, std::size_t Extent, class T, - class = std::enable_if_t< - !details::is_basic_string_span<T>::value && - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>> -bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other) -{ - const gsl::basic_string_span<std::add_const_t<CharT>> tmp(one); - return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end()); -} - -// operator != -template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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) -{ - return !(one == other); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename = std::enable_if_t< - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && - !gsl::details::is_basic_string_span<T>::value>> -bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return !(one == other); -} - -// operator< -template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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) -{ - const gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other); - return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end()); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename = std::enable_if_t< - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && - !gsl::details::is_basic_string_span<T>::value>> -bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one); - return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end()); -} - -#ifndef _MSC_VER - -// VS treats temp and const containers as convertible to basic_string_span, -// so the cases below are already covered by the previous operators - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) -{ - gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other); - return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end()); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one); - return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end()); -} -#endif - -// operator <= -template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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) -{ - return !(other < one); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename = std::enable_if_t< - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && - !gsl::details::is_basic_string_span<T>::value>> -bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return !(other < one); -} - -#ifndef _MSC_VER - -// VS treats temp and const containers as convertible to basic_string_span, -// so the cases below are already covered by the previous operators - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) -{ - return !(other < one); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return !(other < one); -} -#endif - -// operator> -template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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) -{ - return other < one; -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename = std::enable_if_t< - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && - !gsl::details::is_basic_string_span<T>::value>> -bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return other < one; -} - -#ifndef _MSC_VER - -// VS treats temp and const containers as convertible to basic_string_span, -// so the cases below are already covered by the previous operators - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) -{ - return other < one; -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return other < one; -} -#endif - -// operator >= -template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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) -{ - return !(one < other); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename = std::enable_if_t< - std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value && - !gsl::details::is_basic_string_span<T>::value>> -bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return !(one < other); -} - -#ifndef _MSC_VER - -// VS treats temp and const containers as convertible to basic_string_span, -// so the cases below are already covered by the previous operators - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) -{ - return !(one < other); -} - -template < - typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T, - typename DataType = typename T::value_type, - typename = std::enable_if_t< - !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value && - std::is_convertible<DataType*, CharT*>::value && - std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>, - DataType>::value>> -bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) -{ - return !(one < other); -} -#endif -} // namespace gsl - -#if defined(_MSC_VER) && !defined(__clang__) -#pragma warning(pop) - -#if _MSC_VER < 1910 -#undef constexpr -#pragma pop_macro("constexpr") - -#endif // _MSC_VER < 1910 -#endif // _MSC_VER - -#endif // GSL_STRING_SPAN_H +///////////////////////////////////////////////////////////////////////////////
+//
+// 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef GSL_STRING_SPAN_H
+#define GSL_STRING_SPAN_H
+
+#include <gsl/gsl_assert> // for Ensures, Expects
+#include <gsl/gsl_util> // for narrow_cast
+#include <gsl/span_ext> // for operator!=, operator==, dynamic_extent
+
+#include <algorithm> // for equal, lexicographical_compare
+#include <array> // for array
+#include <cstddef> // for size_t, nullptr_t
+#include <cstdint> // for PTRDIFF_MAX
+#include <cstring>
+#include <string> // for basic_string, allocator, char_traits
+#include <type_traits> // for declval, is_convertible, enable_if_t, add_...
+
+#if defined(_MSC_VER) && !defined(__clang__)
+#pragma warning(push)
+
+// Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
+#pragma warning(disable : 26446) // TODO: bug in parser - attributes and templates
+#pragma warning(disable : 26481) // TODO: suppress does not work inside templates sometimes
+
+#if _MSC_VER < 1910
+#pragma push_macro("constexpr")
+#define constexpr /*constexpr*/
+
+#endif // _MSC_VER < 1910
+#endif // _MSC_VER
+
+namespace gsl
+{
+//
+// czstring and wzstring
+//
+// These are "tag" typedefs for C-style strings (i.e. null-terminated character arrays)
+// that allow static analysis to help find bugs.
+//
+// There are no additional features/semantics that we can find a way to add inside the
+// type system for these types that will not either incur significant runtime costs or
+// (sometimes needlessly) break existing programs when introduced.
+//
+
+template <typename CharT, std::size_t Extent = dynamic_extent>
+using basic_zstring = CharT*;
+
+template <std::size_t Extent = dynamic_extent>
+using czstring = basic_zstring<const char, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cwzstring = basic_zstring<const wchar_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cu16zstring = basic_zstring<const char16_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cu32zstring = basic_zstring<const char32_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using zstring = basic_zstring<char, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using wzstring = basic_zstring<wchar_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using u16zstring = basic_zstring<char16_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using u32zstring = basic_zstring<char32_t, Extent>;
+
+namespace details
+{
+ template <class CharT>
+ std::size_t string_length(const CharT* str, std::size_t n)
+ {
+ if (str == nullptr || n == dynamic_extent) return 0;
+
+ const span<const CharT> str_span{str, n};
+
+ std::size_t len = 0;
+ while (len < n && str_span[len]) len++;
+
+ return len;
+ }
+} // namespace details
+
+//
+// ensure_sentinel()
+//
+// Provides a way to obtain an span from a contiguous sequence
+// that ends with a (non-inclusive) sentinel value.
+//
+// Will fail-fast if sentinel cannot be found before max elements are examined.
+//
+template <typename T, const T Sentinel>
+span<T, dynamic_extent> ensure_sentinel(T* seq,
+ std::size_t max = static_cast<std::size_t>(-1))
+{
+ Ensures(seq != nullptr);
+
+ GSL_SUPPRESS(
+ f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
+ auto cur = seq;
+ Ensures(cur != nullptr); // workaround for removing the warning
+
+ GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute // TODO: suppress does not work
+ while (static_cast<std::size_t>(cur - seq) < max && *cur != Sentinel) ++cur;
+ Ensures(*cur == Sentinel);
+ return {seq, static_cast<std::size_t>(cur - seq)};
+}
+
+//
+// ensure_z - creates a span for a zero terminated strings.
+// Will fail fast if a null-terminator cannot be found before
+// the limit of size_type.
+//
+template <typename CharT>
+span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
+ std::size_t max = static_cast<std::size_t>(-1))
+{
+ return ensure_sentinel<CharT, CharT(0)>(sz, max);
+}
+
+template <typename CharT, std::size_t N>
+span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
+{
+ return ensure_z(&sz[0], N);
+}
+
+template <class Cont>
+span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
+ensure_z(Cont& cont)
+{
+ return ensure_z(cont.data(), cont.size());
+}
+
+template <typename CharT, std::size_t>
+class basic_string_span;
+
+namespace details
+{
+ template <typename T>
+ struct is_basic_string_span_oracle : std::false_type
+ {
+ };
+
+ template <typename CharT, std::size_t Extent>
+ struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
+ {
+ };
+
+ template <typename T>
+ struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>>
+ {
+ };
+} // namespace details
+
+//
+// string_span and relatives
+//
+template <typename CharT, std::size_t Extent = dynamic_extent>
+class basic_string_span
+{
+public:
+ using element_type = CharT;
+ using value_type = std::remove_cv_t<element_type>;
+ using pointer = std::add_pointer_t<element_type>;
+ using reference = std::add_lvalue_reference_t<element_type>;
+ using const_reference = std::add_lvalue_reference_t<std::add_const_t<element_type>>;
+ using impl_type = span<element_type, Extent>;
+
+ using size_type = typename impl_type::size_type;
+ using iterator = typename impl_type::iterator;
+ using reverse_iterator = typename impl_type::reverse_iterator;
+
+ // default (empty)
+ constexpr basic_string_span() noexcept = default;
+
+ // copy
+ constexpr basic_string_span(const basic_string_span& other) noexcept = default;
+
+ // assign
+ constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default;
+
+ constexpr basic_string_span(pointer ptr, size_type length) : span_(ptr, length) {}
+ constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
+
+ // From static arrays - if 0-terminated, remove 0 from the view
+ // All other containers allow 0s within the length, so we do not remove them
+ template <std::size_t N>
+ constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
+ {}
+
+ template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
+ constexpr basic_string_span(std::array<ArrayElementType, N>& arr) noexcept : span_(arr)
+ {}
+
+ template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
+ constexpr basic_string_span(const std::array<ArrayElementType, N>& arr) noexcept : span_(arr)
+ {}
+
+ // Container signature should work for basic_string after C++17 version exists
+ template <class Traits, class Allocator>
+ // GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug
+ constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator>& str)
+ : span_(&str[0], str.length())
+ {}
+
+ template <class Traits, class Allocator>
+ constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str)
+ : span_(&str[0], str.length())
+ {}
+
+ // from containers. Containers must have a pointer type and data() function signatures
+ template <class Container,
+ class = std::enable_if_t<
+ !details::is_basic_string_span<Container>::value &&
+ std::is_convertible<typename Container::pointer, pointer>::value &&
+ std::is_convertible<typename Container::pointer,
+ decltype(std::declval<Container>().data())>::value>>
+ constexpr basic_string_span(Container& cont) : span_(cont)
+ {}
+
+ template <class Container,
+ class = std::enable_if_t<
+ !details::is_basic_string_span<Container>::value &&
+ std::is_convertible<typename Container::pointer, pointer>::value &&
+ std::is_convertible<typename Container::pointer,
+ decltype(std::declval<Container>().data())>::value>>
+ constexpr basic_string_span(const Container& cont) : span_(cont)
+ {}
+
+ // from string_span
+ template <
+ class OtherValueType, std::size_t OtherExtent,
+ class = std::enable_if_t<std::is_convertible<
+ typename basic_string_span<OtherValueType, OtherExtent>::impl_type, impl_type>::value>>
+ constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other)
+ : span_(other.data(), other.length())
+ {}
+
+ template <size_type Count>
+ constexpr basic_string_span<element_type, Count> first() const
+ {
+ return {span_.template first<Count>()};
+ }
+
+ constexpr basic_string_span<element_type, dynamic_extent> first(size_type count) const
+ {
+ return {span_.first(count)};
+ }
+
+ template <size_type Count>
+ constexpr basic_string_span<element_type, Count> last() const
+ {
+ return {span_.template last<Count>()};
+ }
+
+ constexpr basic_string_span<element_type, dynamic_extent> last(size_type count) const
+ {
+ return {span_.last(count)};
+ }
+
+ template <size_type Offset, size_type Count>
+ constexpr basic_string_span<element_type, Count> subspan() const
+ {
+ return {span_.template subspan<Offset, Count>()};
+ }
+
+ constexpr basic_string_span<element_type, dynamic_extent>
+ subspan(size_type offset, size_type count = dynamic_extent) const
+ {
+ return {span_.subspan(offset, count)};
+ }
+
+ constexpr reference operator[](size_type idx) const { return span_[idx]; }
+ constexpr reference operator()(size_type idx) const { return span_[idx]; }
+
+ constexpr pointer data() const { return span_.data(); }
+
+ constexpr size_type length() const noexcept { return span_.size(); }
+ constexpr size_type size() const noexcept { return span_.size(); }
+ constexpr size_type size_bytes() const noexcept { return span_.size_bytes(); }
+ constexpr size_type length_bytes() const noexcept { return span_.length_bytes(); }
+ constexpr bool empty() const noexcept { return size() == 0; }
+
+ constexpr iterator begin() const noexcept { return span_.begin(); }
+ constexpr iterator end() const noexcept { return span_.end(); }
+
+ constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); }
+ constexpr reverse_iterator rend() const noexcept { return span_.rend(); }
+
+private:
+ static impl_type remove_z(pointer const& sz, std::size_t max)
+ {
+ return {sz, details::string_length(sz, max)};
+ }
+
+ template <std::size_t N>
+ static impl_type remove_z(element_type (&sz)[N])
+ {
+ return remove_z(&sz[0], N);
+ }
+
+ impl_type span_;
+};
+
+template <std::size_t Extent = dynamic_extent>
+using string_span = basic_string_span<char, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cstring_span = basic_string_span<const char, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using wstring_span = basic_string_span<wchar_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cwstring_span = basic_string_span<const wchar_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using u16string_span = basic_string_span<char16_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cu16string_span = basic_string_span<const char16_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using u32string_span = basic_string_span<char32_t, Extent>;
+
+template <std::size_t Extent = dynamic_extent>
+using cu32string_span = basic_string_span<const char32_t, Extent>;
+
+//
+// to_string() allow (explicit) conversions from string_span to string
+//
+
+template <typename CharT, std::size_t Extent>
+std::basic_string<typename std::remove_const<CharT>::type>
+to_string(basic_string_span<CharT, Extent> view)
+{
+ return {view.data(), narrow_cast<std::size_t>(view.length())};
+}
+
+template <typename CharT, typename Traits = typename std::char_traits<CharT>,
+ typename Allocator = std::allocator<CharT>, typename gCharT, std::size_t Extent>
+std::basic_string<CharT, Traits, Allocator> to_basic_string(basic_string_span<gCharT, Extent> view)
+{
+ return {view.data(), narrow_cast<std::size_t>(view.length())};
+}
+
+template <class ElementType, std::size_t Extent>
+basic_string_span<const byte, details::calculate_byte_size<ElementType, Extent>::value>
+as_bytes(basic_string_span<ElementType, Extent> s) noexcept
+{
+ GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
+}
+
+template <class ElementType, std::size_t Extent,
+ class = std::enable_if_t<!std::is_const<ElementType>::value>>
+basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
+as_writable_bytes(basic_string_span<ElementType, Extent> s) noexcept
+{
+ GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
+ return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
+}
+
+// zero-terminated string span, used to convert
+// zero-terminated spans to legacy strings
+template <typename CharT, std::size_t Extent = dynamic_extent>
+class basic_zstring_span
+{
+public:
+ using value_type = CharT;
+ using const_value_type = std::add_const_t<CharT>;
+
+ using pointer = std::add_pointer_t<value_type>;
+ using const_pointer = std::add_pointer_t<const_value_type>;
+
+ using zstring_type = basic_zstring<value_type, Extent>;
+ using const_zstring_type = basic_zstring<const_value_type, Extent>;
+
+ using impl_type = span<value_type, Extent>;
+ using string_span_type = basic_string_span<value_type, Extent>;
+
+ constexpr basic_zstring_span(impl_type s) : span_(s)
+ {
+ // expects a zero-terminated span
+ Expects(s[s.size() - 1] == '\0');
+ }
+
+ // copy
+ constexpr basic_zstring_span(const basic_zstring_span& other) = default;
+
+ // move
+ constexpr basic_zstring_span(basic_zstring_span&& other) = default;
+
+ // assign
+ constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default;
+
+ // move assign
+ constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
+
+ constexpr bool empty() const noexcept { return span_.size() == 0; }
+
+ constexpr string_span_type as_string_span() const noexcept
+ {
+ const auto sz = span_.size();
+ return {span_.data(), sz > 1 ? sz - 1 : 0};
+ }
+ constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); }
+
+ constexpr const_zstring_type assume_z() const noexcept { return span_.data(); }
+
+private:
+ impl_type span_;
+};
+
+template <std::size_t Max = dynamic_extent>
+using zstring_span = basic_zstring_span<char, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using wzstring_span = basic_zstring_span<wchar_t, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using u16zstring_span = basic_zstring_span<char16_t, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using u32zstring_span = basic_zstring_span<char32_t, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using czstring_span = basic_zstring_span<const char, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
+
+template <std::size_t Max = dynamic_extent>
+using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
+
+// operator ==
+template <class CharT, std::size_t Extent, class T,
+ class = std::enable_if_t<
+ details::is_basic_string_span<T>::value ||
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
+bool operator==(const gsl::basic_string_span<CharT, Extent>& one, const T& other)
+{
+ const gsl::basic_string_span<std::add_const_t<CharT>> tmp(other);
+ return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
+}
+
+template <class CharT, std::size_t Extent, class T,
+ class = std::enable_if_t<
+ !details::is_basic_string_span<T>::value &&
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>>>::value>>
+bool operator==(const T& one, const gsl::basic_string_span<CharT, Extent>& other)
+{
+ const gsl::basic_string_span<std::add_const_t<CharT>> tmp(one);
+ return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
+}
+
+// operator !=
+template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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)
+{
+ return !(one == other);
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename = std::enable_if_t<
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
+ !gsl::details::is_basic_string_span<T>::value>>
+bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return !(one == other);
+}
+
+// operator<
+template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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)
+{
+ const gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
+ return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename = std::enable_if_t<
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
+ !gsl::details::is_basic_string_span<T>::value>>
+bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
+ return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
+}
+
+#ifndef _MSC_VER
+
+// VS treats temp and const containers as convertible to basic_string_span,
+// so the cases below are already covered by the previous operators
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other)
+{
+ gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
+ return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
+ return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
+}
+#endif
+
+// operator <=
+template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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)
+{
+ return !(other < one);
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename = std::enable_if_t<
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
+ !gsl::details::is_basic_string_span<T>::value>>
+bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return !(other < one);
+}
+
+#ifndef _MSC_VER
+
+// VS treats temp and const containers as convertible to basic_string_span,
+// so the cases below are already covered by the previous operators
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other)
+{
+ return !(other < one);
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return !(other < one);
+}
+#endif
+
+// operator>
+template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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)
+{
+ return other < one;
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename = std::enable_if_t<
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
+ !gsl::details::is_basic_string_span<T>::value>>
+bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return other < one;
+}
+
+#ifndef _MSC_VER
+
+// VS treats temp and const containers as convertible to basic_string_span,
+// so the cases below are already covered by the previous operators
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other)
+{
+ return other < one;
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return other < one;
+}
+#endif
+
+// operator >=
+template <typename CharT, std::size_t Extent = gsl::dynamic_extent, 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)
+{
+ return !(one < other);
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename = std::enable_if_t<
+ std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value &&
+ !gsl::details::is_basic_string_span<T>::value>>
+bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return !(one < other);
+}
+
+#ifndef _MSC_VER
+
+// VS treats temp and const containers as convertible to basic_string_span,
+// so the cases below are already covered by the previous operators
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other)
+{
+ return !(one < other);
+}
+
+template <
+ typename CharT, std::size_t Extent = gsl::dynamic_extent, typename T,
+ typename DataType = typename T::value_type,
+ typename = std::enable_if_t<
+ !gsl::details::is_span<T>::value && !gsl::details::is_basic_string_span<T>::value &&
+ std::is_convertible<DataType*, CharT*>::value &&
+ std::is_same<std::decay_t<decltype(std::declval<T>().size(), *std::declval<T>().data())>,
+ DataType>::value>>
+bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
+{
+ return !(one < other);
+}
+#endif
+} // namespace gsl
+
+#if defined(_MSC_VER) && !defined(__clang__)
+#pragma warning(pop)
+
+#if _MSC_VER < 1910
+#undef constexpr
+#pragma pop_macro("constexpr")
+
+#endif // _MSC_VER < 1910
+#endif // _MSC_VER
+
+#endif // GSL_STRING_SPAN_H
diff --git a/tests/span_compatibility_tests.cpp b/tests/span_compatibility_tests.cpp index ab29748..bebfb77 100644 --- a/tests/span_compatibility_tests.cpp +++ b/tests/span_compatibility_tests.cpp @@ -387,12 +387,8 @@ TEST(span_compatibility_tests, assertion_tests) static_assert(noexcept(sp_dyn.data()), "noexcept(sp_dyn.data())"); static_assert(noexcept(sp_dyn.begin()), "noexcept(sp_dyn.begin())"); static_assert(noexcept(sp_dyn.end()), "noexcept(sp_dyn.end())"); - static_assert(noexcept(sp_dyn.cbegin()), "noexcept(sp_dyn.cbegin())"); - static_assert(noexcept(sp_dyn.cend()), "noexcept(sp_dyn.cend())"); static_assert(noexcept(sp_dyn.rbegin()), "noexcept(sp_dyn.rbegin())"); static_assert(noexcept(sp_dyn.rend()), "noexcept(sp_dyn.rend())"); - static_assert(noexcept(sp_dyn.crbegin()), "noexcept(sp_dyn.crbegin())"); - static_assert(noexcept(sp_dyn.crend()), "noexcept(sp_dyn.crend())"); static_assert(noexcept(sp_nine.size()), "noexcept(sp_nine.size())"); static_assert(noexcept(sp_nine.size_bytes()), "noexcept(sp_nine.size_bytes())"); @@ -403,12 +399,8 @@ TEST(span_compatibility_tests, assertion_tests) static_assert(noexcept(sp_nine.data()), "noexcept(sp_nine.data())"); static_assert(noexcept(sp_nine.begin()), "noexcept(sp_nine.begin())"); static_assert(noexcept(sp_nine.end()), "noexcept(sp_nine.end())"); - static_assert(noexcept(sp_nine.cbegin()), "noexcept(sp_nine.cbegin())"); - static_assert(noexcept(sp_nine.cend()), "noexcept(sp_nine.cend())"); static_assert(noexcept(sp_nine.rbegin()), "noexcept(sp_nine.rbegin())"); static_assert(noexcept(sp_nine.rend()), "noexcept(sp_nine.rend())"); - static_assert(noexcept(sp_nine.crbegin()), "noexcept(sp_nine.crbegin())"); - static_assert(noexcept(sp_nine.crend()), "noexcept(sp_nine.crend())"); EXPECT_TRUE(sp_dyn.size() == 9); EXPECT_TRUE(sp_nine.size() == 9); @@ -446,24 +438,12 @@ TEST(span_compatibility_tests, assertion_tests) EXPECT_TRUE(sp_dyn.end()[-2] == 80); EXPECT_TRUE(sp_nine.end()[-2] == 80); - EXPECT_TRUE(*sp_dyn.cbegin() == 10); - EXPECT_TRUE(*sp_nine.cbegin() == 10); - - EXPECT_TRUE(sp_dyn.cend()[-2] == 80); - EXPECT_TRUE(sp_nine.cend()[-2] == 80); - EXPECT_TRUE(*sp_dyn.rbegin() == 90); EXPECT_TRUE(*sp_nine.rbegin() == 90); EXPECT_TRUE(sp_dyn.rend()[-2] == 20); EXPECT_TRUE(sp_nine.rend()[-2] == 20); - EXPECT_TRUE(*sp_dyn.crbegin() == 90); - EXPECT_TRUE(*sp_nine.crbegin() == 90); - - EXPECT_TRUE(sp_dyn.crend()[-2] == 20); - EXPECT_TRUE(sp_nine.crend()[-2] == 20); - static_assert(is_same<decltype(sp_dyn.begin()), gsl::span<int>::iterator>::value, "is_same<decltype(sp_dyn.begin()), gsl::span<int>::iterator>::value"); static_assert(is_same<decltype(sp_nine.begin()), gsl::span<int, 9>::iterator>::value, @@ -472,16 +452,6 @@ TEST(span_compatibility_tests, assertion_tests) "is_same<decltype(sp_dyn.end()), gsl::span<int>::iterator>::value"); static_assert(is_same<decltype(sp_nine.end()), gsl::span<int, 9>::iterator>::value, "is_same<decltype(sp_nine.end()), gsl::span<int, 9>::iterator>::value"); - static_assert(is_same<decltype(sp_dyn.cbegin()), gsl::span<int>::const_iterator>::value, - "is_same<decltype(sp_dyn.cbegin()), gsl::span<int>::const_iterator>::value"); - static_assert( - is_same<decltype(sp_nine.cbegin()), gsl::span<int, 9>::const_iterator>::value, - "is_same<decltype(sp_nine.cbegin()), gsl::span<int, 9>::const_iterator>::value"); - static_assert(is_same<decltype(sp_dyn.cend()), gsl::span<int>::const_iterator>::value, - "is_same<decltype(sp_dyn.cend()), gsl::span<int>::const_iterator>::value"); - static_assert( - is_same<decltype(sp_nine.cend()), gsl::span<int, 9>::const_iterator>::value, - "is_same<decltype(sp_nine.cend()), gsl::span<int, 9>::const_iterator>::value"); static_assert( is_same<decltype(sp_dyn.rbegin()), gsl::span<int>::reverse_iterator>::value, "is_same<decltype(sp_dyn.rbegin()), gsl::span<int>::reverse_iterator>::value"); @@ -493,19 +463,6 @@ TEST(span_compatibility_tests, assertion_tests) static_assert( is_same<decltype(sp_nine.rend()), gsl::span<int, 9>::reverse_iterator>::value, "is_same<decltype(sp_nine.rend()), gsl::span<int, 9>::reverse_iterator>::value"); - static_assert( - is_same<decltype(sp_dyn.crbegin()), gsl::span<int>::const_reverse_iterator>::value, - "is_same<decltype(sp_dyn.crbegin()), gsl::span<int>::const_reverse_iterator>::value"); - static_assert( - is_same<decltype(sp_nine.crbegin()), gsl::span<int, 9>::const_reverse_iterator>::value, - "is_same<decltype(sp_nine.crbegin()), gsl::span<int, " - "9>::const_reverse_iterator>::value"); - static_assert( - is_same<decltype(sp_dyn.crend()), gsl::span<int>::const_reverse_iterator>::value, - "is_same<decltype(sp_dyn.crend()), gsl::span<int>::const_reverse_iterator>::value"); - static_assert( - is_same<decltype(sp_nine.crend()), gsl::span<int, 9>::const_reverse_iterator>::value, - "is_same<decltype(sp_nine.crend()), gsl::span<int, 9>::const_reverse_iterator>::value"); } { int sequence[9]{10, 20, 30, 40, 50, 60, 70, 80, 90}; @@ -647,89 +604,53 @@ static_assert(std::is_same<gsl::span<const int, 3>::const_reference, const int&> static_assert(std::is_same<std::iterator_traits<gsl::span<int>::iterator>::pointer, int*>::value, "span<int>::iterator's pointer should be int*"); static_assert( - std::is_same<std::iterator_traits<gsl::span<int>::const_iterator>::pointer, const int*>::value, - "span<int>::const_iterator's pointer should be const int*"); -static_assert( std::is_same<gsl::span<int>::reverse_iterator, std::reverse_iterator<gsl::span<int>::iterator>>::value, "span<int>::reverse_iterator should equal std::reverse_iterator<span<int>::iterator>"); -static_assert(std::is_same<gsl::span<int>::const_reverse_iterator, - std::reverse_iterator<gsl::span<int>::const_iterator>>::value, - "span<int>::const_reverse_iterator should equal " - "std::reverse_iterator<span<int>::const_iterator>"); static_assert(std::is_same<std::iterator_traits<gsl::span<int, 3>::iterator>::pointer, int*>::value, "span<int, 3>::iterator's pointer should be int*"); -static_assert(std::is_same<std::iterator_traits<gsl::span<int, 3>::const_iterator>::pointer, - const int*>::value, - "span<int, 3>::const_iterator's pointer should be const int*"); static_assert( std::is_same<gsl::span<int, 3>::reverse_iterator, std::reverse_iterator<gsl::span<int, 3>::iterator>>::value, "span<int, 3>::reverse_iterator should equal std::reverse_iterator<span<int, 3>::iterator>"); -static_assert(std::is_same<gsl::span<int, 3>::const_reverse_iterator, - std::reverse_iterator<gsl::span<int, 3>::const_iterator>>::value, - "span<int, 3>::const_reverse_iterator should equal std::reverse_iterator<span<int, " - "3>::const_iterator>"); static_assert( std::is_same<std::iterator_traits<gsl::span<const int>::iterator>::pointer, const int*>::value, "span<const int>::iterator's pointer should be int*"); -static_assert(std::is_same<std::iterator_traits<gsl::span<const int>::const_iterator>::pointer, - const int*>::value, - "span<const int>::const_iterator's pointer should be const int*"); static_assert(std::is_same<gsl::span<const int>::reverse_iterator, std::reverse_iterator<gsl::span<const int>::iterator>>::value, "span<const int>::reverse_iterator should equal std::reverse_iterator<span<const " "int>::iterator>"); -static_assert(std::is_same<gsl::span<const int>::const_reverse_iterator, - std::reverse_iterator<gsl::span<const int>::const_iterator>>::value, - "span<const int>::const_reverse_iterator should equal " - "std::reverse_iterator<span<const int>::const_iterator>"); static_assert(std::is_same<std::iterator_traits<gsl::span<const int, 3>::iterator>::pointer, const int*>::value, "span<const int, 3>::iterator's pointer should be int*"); -static_assert(std::is_same<std::iterator_traits<gsl::span<const int, 3>::const_iterator>::pointer, - const int*>::value, - "span<const int, 3>::const_iterator's pointer should be const int*"); static_assert(std::is_same<gsl::span<const int, 3>::reverse_iterator, std::reverse_iterator<gsl::span<const int, 3>::iterator>>::value, "span<const int, 3>::reverse_iterator should equal std::reverse_iterator<span<const " "int, 3>::iterator>"); -static_assert(std::is_same<gsl::span<const int, 3>::const_reverse_iterator, - std::reverse_iterator<gsl::span<const int, 3>::const_iterator>>::value, - "span<const int, 3>::const_reverse_iterator should equal " - "std::reverse_iterator<span<const int, 3>::const_iterator>"); // copyability assertions static_assert(std::is_trivially_copyable<gsl::span<int>>::value, "span<int> should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<int>::iterator>::value, "span<int>::iterator should be trivially copyable"); -static_assert(std::is_trivially_copyable<gsl::span<int>::const_iterator>::value, - "span<int>::const_iterator should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<int, 3>>::value, "span<int, 3> should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<int, 3>::iterator>::value, "span<int, 3>::iterator should be trivially copyable"); -static_assert(std::is_trivially_copyable<gsl::span<int, 3>::const_iterator>::value, - "span<int, 3>::const_iterator should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<const int>>::value, "span<const int> should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<const int>::iterator>::value, "span<const int>::iterator should be trivially copyable"); -static_assert(std::is_trivially_copyable<gsl::span<const int>::const_iterator>::value, - "span<const int>::const_iterator should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<const int, 3>>::value, "span<const int, 3> should be trivially copyable"); static_assert(std::is_trivially_copyable<gsl::span<const int, 3>::iterator>::value, "span<const int, 3>::iterator should be trivially copyable"); -static_assert(std::is_trivially_copyable<gsl::span<const int, 3>::const_iterator>::value, - "span<const int, 3>::const_iterator should be trivially copyable"); // nothrow constructible assertions static_assert(std::is_nothrow_constructible<gsl::span<int>, int*, std::size_t>::value, diff --git a/tests/span_ext_tests.cpp b/tests/span_ext_tests.cpp index bd256a8..d413e03 100644 --- a/tests/span_ext_tests.cpp +++ b/tests/span_ext_tests.cpp @@ -1,360 +1,360 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// 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. -// -/////////////////////////////////////////////////////////////////////////////// - -#include <gtest/gtest.h> - -#include <gsl/gsl_util> // for narrow_cast, at -#include <gsl/span_ext> // for operator==, operator!=, make_span - -#include <array> // for array -#include <iostream> // for cerr -#include <vector> // for vector - -using namespace std; -using namespace gsl; - -namespace -{ -static constexpr char deathstring[] = "Expected Death"; -} // namespace - -TEST(span_ext_test, make_span_from_pointer_length_constructor) -{ - std::set_terminate([] { - std::cerr << "Expected Death. from_pointer_length_constructor"; - std::abort(); - }); - int arr[4] = {1, 2, 3, 4}; - - { - auto s = make_span(&arr[0], 2); - EXPECT_TRUE(s.size() == 2); - EXPECT_TRUE(s.data() == &arr[0]); - EXPECT_TRUE(s[0] == 1); - EXPECT_TRUE(s[1] == 2); - } - - { - int* p = nullptr; - auto s = make_span(p, narrow_cast<span<int>::size_type>(0)); - EXPECT_TRUE(s.size() == 0); - EXPECT_TRUE(s.data() == nullptr); - } - - { - int* p = nullptr; - auto workaround_macro = [=]() { make_span(p, 2); }; - EXPECT_DEATH(workaround_macro(), deathstring); - } -} - -TEST(span_ext_test, make_span_from_pointer_pointer_construction) -{ - int arr[4] = {1, 2, 3, 4}; - - { - auto s = make_span(&arr[0], &arr[2]); - EXPECT_TRUE(s.size() == 2); - EXPECT_TRUE(s.data() == &arr[0]); - EXPECT_TRUE(s[0] == 1); - EXPECT_TRUE(s[1] == 2); - } - - { - auto s = make_span(&arr[0], &arr[0]); - EXPECT_TRUE(s.size() == 0); - EXPECT_TRUE(s.data() == &arr[0]); - } - - { - int* p = nullptr; - auto s = make_span(p, p); - EXPECT_TRUE(s.size() == 0); - EXPECT_TRUE(s.data() == nullptr); - } -} - -TEST(span_ext_test, make_span_from_array_constructor) - { - int arr[5] = {1, 2, 3, 4, 5}; - int arr2d[2][3] = {1, 2, 3, 4, 5, 6}; - int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; - - { - const auto s = make_span(arr); - EXPECT_TRUE(s.size() == 5); - EXPECT_TRUE(s.data() == std::addressof(arr[0])); - } - - { - const auto s = make_span(std::addressof(arr2d[0]), 1); - EXPECT_TRUE(s.size() == 1); - EXPECT_TRUE(s.data() == std::addressof(arr2d[0])); - } - - { - const auto s = make_span(std::addressof(arr3d[0]), 1); - EXPECT_TRUE(s.size() == 1); - EXPECT_TRUE(s.data() == std::addressof(arr3d[0])); - } - } - - TEST(span_ext_test, make_span_from_dynamic_array_constructor) - { - double(*arr)[3][4] = new double[100][3][4]; - - { - auto s = make_span(&arr[0][0][0], 10); - EXPECT_TRUE(s.size() == 10); - EXPECT_TRUE(s.data() == &arr[0][0][0]); - } - - delete[] arr; - } - - TEST(span_ext_test, make_span_from_std_array_constructor) - { - std::array<int, 4> arr = {1, 2, 3, 4}; - - { - auto s = make_span(arr); - EXPECT_TRUE(s.size() == arr.size()); - EXPECT_TRUE(s.data() == arr.data()); - } - - // This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590 - { - span<int> s1 = make_span(arr); - - static span<int> s2; - s2 = s1; - - #if defined(__GNUC__) && __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && \ - __GNUC_PATCHLEVEL__ == 0 && defined(__OPTIMIZE__) - // Known to be broken in gcc 6.4 and 6.5 with optimizations - // Issue in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116 - EXPECT_TRUE(s1.size() == 4); - EXPECT_TRUE(s2.size() == 0); - #else - EXPECT_TRUE(s1.size() == s2.size()); - #endif - } - } - - TEST(span_ext_test, make_span_from_const_std_array_constructor) - { - const std::array<int, 4> arr = {1, 2, 3, 4}; - - { - auto s = make_span(arr); - EXPECT_TRUE(s.size() == arr.size()); - EXPECT_TRUE(s.data() == arr.data()); - } - } - - TEST(span_ext_test, make_span_from_std_array_const_constructor) - { - std::array<const int, 4> arr = {1, 2, 3, 4}; - - { - auto s = make_span(arr); - EXPECT_TRUE(s.size() == arr.size()); - EXPECT_TRUE(s.data() == arr.data()); - } - } - - TEST(span_ext_test, make_span_from_container_constructor) - { - std::vector<int> v = {1, 2, 3}; - const std::vector<int> cv = v; - - { - auto s = make_span(v); - EXPECT_TRUE(s.size() == v.size()); - EXPECT_TRUE(s.data() == v.data()); - - auto cs = make_span(cv); - EXPECT_TRUE(cs.size() == cv.size()); - EXPECT_TRUE(cs.data() == cv.data()); - } - } - - TEST(span_test, interop_with_gsl_at) - { - int arr[5] = {1, 2, 3, 4, 5}; - span<int> s{arr}; - EXPECT_TRUE(at(s, 0) == 1); - EXPECT_TRUE(at(s, 1) == 2); - } - - TEST(span_ext_test, iterator_free_functions) - { - int a[] = {1, 2, 3, 4}; - span<int> s{a}; - - EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value)); - EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value)); - - EXPECT_TRUE((std::is_same<decltype(s.cbegin()), decltype(cbegin(s))>::value)); - EXPECT_TRUE((std::is_same<decltype(s.cend()), decltype(cend(s))>::value)); - - EXPECT_TRUE((std::is_same<decltype(s.rbegin()), decltype(rbegin(s))>::value)); - EXPECT_TRUE((std::is_same<decltype(s.rend()), decltype(rend(s))>::value)); - - EXPECT_TRUE((std::is_same<decltype(s.crbegin()), decltype(crbegin(s))>::value)); - EXPECT_TRUE((std::is_same<decltype(s.crend()), decltype(crend(s))>::value)); - - EXPECT_TRUE(s.begin() == begin(s)); - EXPECT_TRUE(s.end() == end(s)); - - EXPECT_TRUE(s.cbegin() == cbegin(s)); - EXPECT_TRUE(s.cend() == cend(s)); - - EXPECT_TRUE(s.rbegin() == rbegin(s)); - EXPECT_TRUE(s.rend() == rend(s)); - - EXPECT_TRUE(s.crbegin() == crbegin(s)); - EXPECT_TRUE(s.crend() == crend(s)); - } - - TEST(span_ext_test, ssize_free_function) - { - int a[] = {1, 2, 3, 4}; - span<int> s{a}; - - EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value)); - EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s))); - } - - TEST(span_ext_test, comparison_operators) - { - { - span<int> s1; - span<int> s2; - EXPECT_TRUE(s1 == s2); - EXPECT_FALSE(s1 != s2); - EXPECT_FALSE(s1 < s2); - EXPECT_TRUE(s1 <= s2); - EXPECT_FALSE(s1 > s2); - EXPECT_TRUE(s1 >= s2); - EXPECT_TRUE(s2 == s1); - EXPECT_FALSE(s2 != s1); - EXPECT_FALSE(s2 != s1); - EXPECT_TRUE(s2 <= s1); - EXPECT_FALSE(s2 > s1); - EXPECT_TRUE(s2 >= s1); - } - - { - int arr[] = {2, 1}; - span<int> s1 = arr; - span<int> s2 = arr; - - EXPECT_TRUE(s1 == s2); - EXPECT_FALSE(s1 != s2); - EXPECT_FALSE(s1 < s2); - EXPECT_TRUE(s1 <= s2); - EXPECT_FALSE(s1 > s2); - EXPECT_TRUE(s1 >= s2); - EXPECT_TRUE(s2 == s1); - EXPECT_FALSE(s2 != s1); - EXPECT_FALSE(s2 < s1); - EXPECT_TRUE(s2 <= s1); - EXPECT_FALSE(s2 > s1); - EXPECT_TRUE(s2 >= s1); - } - - { - int arr[] = {2, 1}; // bigger - - span<int> s1; - span<int> s2 = arr; - - EXPECT_TRUE(s1 != s2); - EXPECT_TRUE(s2 != s1); - EXPECT_FALSE(s1 == s2); - EXPECT_FALSE(s2 == s1); - EXPECT_TRUE(s1 < s2); - EXPECT_FALSE(s2 < s1); - EXPECT_TRUE(s1 <= s2); - EXPECT_FALSE(s2 <= s1); - EXPECT_TRUE(s2 > s1); - EXPECT_FALSE(s1 > s2); - EXPECT_TRUE(s2 >= s1); - EXPECT_FALSE(s1 >= s2); - } - - { - int arr1[] = {1, 2}; - int arr2[] = {1, 2}; - span<int> s1 = arr1; - span<int> s2 = arr2; - - EXPECT_TRUE(s1 == s2); - EXPECT_FALSE(s1 != s2); - EXPECT_FALSE(s1 < s2); - EXPECT_TRUE(s1 <= s2); - EXPECT_FALSE(s1 > s2); - EXPECT_TRUE(s1 >= s2); - EXPECT_TRUE(s2 == s1); - EXPECT_FALSE(s2 != s1); - EXPECT_FALSE(s2 < s1); - EXPECT_TRUE(s2 <= s1); - EXPECT_FALSE(s2 > s1); - EXPECT_TRUE(s2 >= s1); - } - - { - int arr[] = {1, 2, 3}; - - span<int> s1 = {&arr[0], 2}; // shorter - span<int> s2 = arr; // longer - - EXPECT_TRUE(s1 != s2); - EXPECT_TRUE(s2 != s1); - EXPECT_FALSE(s1 == s2); - EXPECT_FALSE(s2 == s1); - EXPECT_TRUE(s1 < s2); - EXPECT_FALSE(s2 < s1); - EXPECT_TRUE(s1 <= s2); - EXPECT_FALSE(s2 <= s1); - EXPECT_TRUE(s2 > s1); - EXPECT_FALSE(s1 > s2); - EXPECT_TRUE(s2 >= s1); - EXPECT_FALSE(s1 >= s2); - } - - { - int arr1[] = {1, 2}; // smaller - int arr2[] = {2, 1}; // bigger - - span<int> s1 = arr1; - span<int> s2 = arr2; - - EXPECT_TRUE(s1 != s2); - EXPECT_TRUE(s2 != s1); - EXPECT_FALSE(s1 == s2); - EXPECT_FALSE(s2 == s1); - EXPECT_TRUE(s1 < s2); - EXPECT_FALSE(s2 < s1); - EXPECT_TRUE(s1 <= s2); - EXPECT_FALSE(s2 <= s1); - EXPECT_TRUE(s2 > s1); - EXPECT_FALSE(s1 > s2); - EXPECT_TRUE(s2 >= s1); - EXPECT_FALSE(s1 >= s2); - } - } +///////////////////////////////////////////////////////////////////////////////
+//
+// 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.
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#include <gtest/gtest.h>
+
+#include <gsl/gsl_util> // for narrow_cast, at
+#include <gsl/span_ext> // for operator==, operator!=, make_span
+
+#include <array> // for array
+#include <iostream> // for cerr
+#include <vector> // for vector
+
+using namespace std;
+using namespace gsl;
+
+namespace
+{
+static constexpr char deathstring[] = "Expected Death";
+} // namespace
+
+TEST(span_ext_test, make_span_from_pointer_length_constructor)
+{
+ std::set_terminate([] {
+ std::cerr << "Expected Death. from_pointer_length_constructor";
+ std::abort();
+ });
+ int arr[4] = {1, 2, 3, 4};
+
+ {
+ auto s = make_span(&arr[0], 2);
+ EXPECT_TRUE(s.size() == 2);
+ EXPECT_TRUE(s.data() == &arr[0]);
+ EXPECT_TRUE(s[0] == 1);
+ EXPECT_TRUE(s[1] == 2);
+ }
+
+ {
+ int* p = nullptr;
+ auto s = make_span(p, narrow_cast<span<int>::size_type>(0));
+ EXPECT_TRUE(s.size() == 0);
+ EXPECT_TRUE(s.data() == nullptr);
+ }
+
+ {
+ int* p = nullptr;
+ auto workaround_macro = [=]() { make_span(p, 2); };
+ EXPECT_DEATH(workaround_macro(), deathstring);
+ }
+}
+
+TEST(span_ext_test, make_span_from_pointer_pointer_construction)
+{
+ int arr[4] = {1, 2, 3, 4};
+
+ {
+ auto s = make_span(&arr[0], &arr[2]);
+ EXPECT_TRUE(s.size() == 2);
+ EXPECT_TRUE(s.data() == &arr[0]);
+ EXPECT_TRUE(s[0] == 1);
+ EXPECT_TRUE(s[1] == 2);
+ }
+
+ {
+ auto s = make_span(&arr[0], &arr[0]);
+ EXPECT_TRUE(s.size() == 0);
+ EXPECT_TRUE(s.data() == &arr[0]);
+ }
+
+ {
+ int* p = nullptr;
+ auto s = make_span(p, p);
+ EXPECT_TRUE(s.size() == 0);
+ EXPECT_TRUE(s.data() == nullptr);
+ }
+}
+
+TEST(span_ext_test, make_span_from_array_constructor)
+ {
+ int arr[5] = {1, 2, 3, 4, 5};
+ int arr2d[2][3] = {1, 2, 3, 4, 5, 6};
+ int arr3d[2][3][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+
+ {
+ const auto s = make_span(arr);
+ EXPECT_TRUE(s.size() == 5);
+ EXPECT_TRUE(s.data() == std::addressof(arr[0]));
+ }
+
+ {
+ const auto s = make_span(std::addressof(arr2d[0]), 1);
+ EXPECT_TRUE(s.size() == 1);
+ EXPECT_TRUE(s.data() == std::addressof(arr2d[0]));
+ }
+
+ {
+ const auto s = make_span(std::addressof(arr3d[0]), 1);
+ EXPECT_TRUE(s.size() == 1);
+ EXPECT_TRUE(s.data() == std::addressof(arr3d[0]));
+ }
+ }
+
+ TEST(span_ext_test, make_span_from_dynamic_array_constructor)
+ {
+ double(*arr)[3][4] = new double[100][3][4];
+
+ {
+ auto s = make_span(&arr[0][0][0], 10);
+ EXPECT_TRUE(s.size() == 10);
+ EXPECT_TRUE(s.data() == &arr[0][0][0]);
+ }
+
+ delete[] arr;
+ }
+
+ TEST(span_ext_test, make_span_from_std_array_constructor)
+ {
+ std::array<int, 4> arr = {1, 2, 3, 4};
+
+ {
+ auto s = make_span(arr);
+ EXPECT_TRUE(s.size() == arr.size());
+ EXPECT_TRUE(s.data() == arr.data());
+ }
+
+ // This test checks for the bug found in gcc 6.1, 6.2, 6.3, 6.4, 6.5 7.1, 7.2, 7.3 - issue #590
+ {
+ span<int> s1 = make_span(arr);
+
+ static span<int> s2;
+ s2 = s1;
+
+ #if defined(__GNUC__) && __GNUC__ == 6 && (__GNUC_MINOR__ == 4 || __GNUC_MINOR__ == 5) && \
+ __GNUC_PATCHLEVEL__ == 0 && defined(__OPTIMIZE__)
+ // Known to be broken in gcc 6.4 and 6.5 with optimizations
+ // Issue in gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83116
+ EXPECT_TRUE(s1.size() == 4);
+ EXPECT_TRUE(s2.size() == 0);
+ #else
+ EXPECT_TRUE(s1.size() == s2.size());
+ #endif
+ }
+ }
+
+ TEST(span_ext_test, make_span_from_const_std_array_constructor)
+ {
+ const std::array<int, 4> arr = {1, 2, 3, 4};
+
+ {
+ auto s = make_span(arr);
+ EXPECT_TRUE(s.size() == arr.size());
+ EXPECT_TRUE(s.data() == arr.data());
+ }
+ }
+
+ TEST(span_ext_test, make_span_from_std_array_const_constructor)
+ {
+ std::array<const int, 4> arr = {1, 2, 3, 4};
+
+ {
+ auto s = make_span(arr);
+ EXPECT_TRUE(s.size() == arr.size());
+ EXPECT_TRUE(s.data() == arr.data());
+ }
+ }
+
+ TEST(span_ext_test, make_span_from_container_constructor)
+ {
+ std::vector<int> v = {1, 2, 3};
+ const std::vector<int> cv = v;
+
+ {
+ auto s = make_span(v);
+ EXPECT_TRUE(s.size() == v.size());
+ EXPECT_TRUE(s.data() == v.data());
+
+ auto cs = make_span(cv);
+ EXPECT_TRUE(cs.size() == cv.size());
+ EXPECT_TRUE(cs.data() == cv.data());
+ }
+ }
+
+ TEST(span_test, interop_with_gsl_at)
+ {
+ int arr[5] = {1, 2, 3, 4, 5};
+ span<int> s{arr};
+ EXPECT_TRUE(at(s, 0) == 1);
+ EXPECT_TRUE(at(s, 1) == 2);
+ }
+
+ TEST(span_ext_test, iterator_free_functions)
+ {
+ int a[] = {1, 2, 3, 4};
+ span<int> s{a};
+
+ EXPECT_TRUE((std::is_same<decltype(s.begin()), decltype(begin(s))>::value));
+ EXPECT_TRUE((std::is_same<decltype(s.end()), decltype(end(s))>::value));
+
+ EXPECT_TRUE((std::is_same<decltype(std::cbegin(s)), decltype(cbegin(s))>::value));
+ EXPECT_TRUE((std::is_same<decltype(std::cend(s)), decltype(cend(s))>::value));
+
+ EXPECT_TRUE((std::is_same<decltype(s.rbegin()), decltype(rbegin(s))>::value));
+ EXPECT_TRUE((std::is_same<decltype(s.rend()), decltype(rend(s))>::value));
+
+ EXPECT_TRUE((std::is_same<decltype(std::crbegin(s)), decltype(crbegin(s))>::value));
+ EXPECT_TRUE((std::is_same<decltype(std::crend(s)), decltype(crend(s))>::value));
+
+ EXPECT_TRUE(s.begin() == begin(s));
+ EXPECT_TRUE(s.end() == end(s));
+
+ EXPECT_TRUE(s.rbegin() == rbegin(s));
+ EXPECT_TRUE(s.rend() == rend(s));
+
+ EXPECT_TRUE(s.begin() == cbegin(s));
+ EXPECT_TRUE(s.end() == cend(s));
+
+ EXPECT_TRUE(s.rbegin() == crbegin(s));
+ EXPECT_TRUE(s.rend() == crend(s));
+ }
+
+ TEST(span_ext_test, ssize_free_function)
+ {
+ int a[] = {1, 2, 3, 4};
+ span<int> s{a};
+
+ EXPECT_FALSE((std::is_same<decltype(s.size()), decltype(ssize(s))>::value));
+ EXPECT_TRUE(s.size() == static_cast<std::size_t>(ssize(s)));
+ }
+
+ TEST(span_ext_test, comparison_operators)
+ {
+ {
+ span<int> s1;
+ span<int> s2;
+ EXPECT_TRUE(s1 == s2);
+ EXPECT_FALSE(s1 != s2);
+ EXPECT_FALSE(s1 < s2);
+ EXPECT_TRUE(s1 <= s2);
+ EXPECT_FALSE(s1 > s2);
+ EXPECT_TRUE(s1 >= s2);
+ EXPECT_TRUE(s2 == s1);
+ EXPECT_FALSE(s2 != s1);
+ EXPECT_FALSE(s2 != s1);
+ EXPECT_TRUE(s2 <= s1);
+ EXPECT_FALSE(s2 > s1);
+ EXPECT_TRUE(s2 >= s1);
+ }
+
+ {
+ int arr[] = {2, 1};
+ span<int> s1 = arr;
+ span<int> s2 = arr;
+
+ EXPECT_TRUE(s1 == s2);
+ EXPECT_FALSE(s1 != s2);
+ EXPECT_FALSE(s1 < s2);
+ EXPECT_TRUE(s1 <= s2);
+ EXPECT_FALSE(s1 > s2);
+ EXPECT_TRUE(s1 >= s2);
+ EXPECT_TRUE(s2 == s1);
+ EXPECT_FALSE(s2 != s1);
+ EXPECT_FALSE(s2 < s1);
+ EXPECT_TRUE(s2 <= s1);
+ EXPECT_FALSE(s2 > s1);
+ EXPECT_TRUE(s2 >= s1);
+ }
+
+ {
+ int arr[] = {2, 1}; // bigger
+
+ span<int> s1;
+ span<int> s2 = arr;
+
+ EXPECT_TRUE(s1 != s2);
+ EXPECT_TRUE(s2 != s1);
+ EXPECT_FALSE(s1 == s2);
+ EXPECT_FALSE(s2 == s1);
+ EXPECT_TRUE(s1 < s2);
+ EXPECT_FALSE(s2 < s1);
+ EXPECT_TRUE(s1 <= s2);
+ EXPECT_FALSE(s2 <= s1);
+ EXPECT_TRUE(s2 > s1);
+ EXPECT_FALSE(s1 > s2);
+ EXPECT_TRUE(s2 >= s1);
+ EXPECT_FALSE(s1 >= s2);
+ }
+
+ {
+ int arr1[] = {1, 2};
+ int arr2[] = {1, 2};
+ span<int> s1 = arr1;
+ span<int> s2 = arr2;
+
+ EXPECT_TRUE(s1 == s2);
+ EXPECT_FALSE(s1 != s2);
+ EXPECT_FALSE(s1 < s2);
+ EXPECT_TRUE(s1 <= s2);
+ EXPECT_FALSE(s1 > s2);
+ EXPECT_TRUE(s1 >= s2);
+ EXPECT_TRUE(s2 == s1);
+ EXPECT_FALSE(s2 != s1);
+ EXPECT_FALSE(s2 < s1);
+ EXPECT_TRUE(s2 <= s1);
+ EXPECT_FALSE(s2 > s1);
+ EXPECT_TRUE(s2 >= s1);
+ }
+
+ {
+ int arr[] = {1, 2, 3};
+
+ span<int> s1 = {&arr[0], 2}; // shorter
+ span<int> s2 = arr; // longer
+
+ EXPECT_TRUE(s1 != s2);
+ EXPECT_TRUE(s2 != s1);
+ EXPECT_FALSE(s1 == s2);
+ EXPECT_FALSE(s2 == s1);
+ EXPECT_TRUE(s1 < s2);
+ EXPECT_FALSE(s2 < s1);
+ EXPECT_TRUE(s1 <= s2);
+ EXPECT_FALSE(s2 <= s1);
+ EXPECT_TRUE(s2 > s1);
+ EXPECT_FALSE(s1 > s2);
+ EXPECT_TRUE(s2 >= s1);
+ EXPECT_FALSE(s1 >= s2);
+ }
+
+ {
+ int arr1[] = {1, 2}; // smaller
+ int arr2[] = {2, 1}; // bigger
+
+ span<int> s1 = arr1;
+ span<int> s2 = arr2;
+
+ EXPECT_TRUE(s1 != s2);
+ EXPECT_TRUE(s2 != s1);
+ EXPECT_FALSE(s1 == s2);
+ EXPECT_FALSE(s2 == s1);
+ EXPECT_TRUE(s1 < s2);
+ EXPECT_FALSE(s2 < s1);
+ EXPECT_TRUE(s1 <= s2);
+ EXPECT_FALSE(s2 <= s1);
+ EXPECT_TRUE(s2 > s1);
+ EXPECT_FALSE(s1 > s2);
+ EXPECT_TRUE(s2 >= s1);
+ EXPECT_FALSE(s1 >= s2);
+ }
+ }
diff --git a/tests/span_tests.cpp b/tests/span_tests.cpp index 689fff9..c5d978e 100644 --- a/tests/span_tests.cpp +++ b/tests/span_tests.cpp @@ -864,34 +864,6 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(it1 == it2); } - TEST(span_test, const_iterator_default_init) - { - span<int>::const_iterator it1; - span<int>::const_iterator it2; - EXPECT_TRUE(it1 == it2); - } - - TEST(span_test, iterator_conversions) - { - span<int>::iterator badIt; - span<int>::const_iterator badConstIt; - EXPECT_TRUE(badIt == badConstIt); - - int a[] = {1, 2, 3, 4}; - span<int> s = a; - - auto it = s.begin(); - auto cit = s.cbegin(); - - EXPECT_TRUE(it == cit); - EXPECT_TRUE(cit == it); - - span<int>::const_iterator cit2 = it; - EXPECT_TRUE(cit2 == cit); - - span<int>::const_iterator cit3 = it + 4; - EXPECT_TRUE(cit3 == s.cend()); - } TEST(span_test, iterator_comparisons) { int a[] = {1, 2, 3, 4}; @@ -899,15 +871,8 @@ TEST(span_test, from_array_constructor) span<int> s = a; span<int>::iterator it = s.begin(); auto it2 = it + 1; - span<int>::const_iterator cit = s.cbegin(); - EXPECT_TRUE(it == cit); - EXPECT_TRUE(cit == it); EXPECT_TRUE(it == it); - EXPECT_TRUE(cit == cit); - EXPECT_TRUE(cit == s.begin()); - EXPECT_TRUE(s.begin() == cit); - EXPECT_TRUE(s.cbegin() == cit); EXPECT_TRUE(it == s.begin()); EXPECT_TRUE(s.begin() == it); @@ -916,26 +881,16 @@ TEST(span_test, from_array_constructor) EXPECT_TRUE(it != s.end()); EXPECT_TRUE(it2 != s.end()); EXPECT_TRUE(s.end() != it); - EXPECT_TRUE(it2 != cit); - EXPECT_TRUE(cit != it2); EXPECT_TRUE(it < it2); EXPECT_TRUE(it <= it2); EXPECT_TRUE(it2 <= s.end()); EXPECT_TRUE(it < s.end()); - EXPECT_TRUE(it <= cit); - EXPECT_TRUE(cit <= it); - EXPECT_TRUE(cit < it2); - EXPECT_TRUE(cit <= it2); - EXPECT_TRUE(cit < s.end()); - EXPECT_TRUE(cit <= s.end()); EXPECT_TRUE(it2 > it); EXPECT_TRUE(it2 >= it); EXPECT_TRUE(s.end() > it2); EXPECT_TRUE(s.end() >= it2); - EXPECT_TRUE(it2 > cit); - EXPECT_TRUE(it2 >= cit); } } @@ -1019,63 +974,6 @@ TEST(span_test, from_array_constructor) } } - TEST(span_test, cbegin_cend) - { - std::set_terminate([] { - std::cerr << "Expected Death. cbegin_cend"; - std::abort(); - }); - { - int a[] = {1, 2, 3, 4}; - span<int> s = a; - - span<int>::const_iterator cit = s.cbegin(); - span<int>::const_iterator cit2 = std::cbegin(s); - EXPECT_TRUE(cit == cit2); - - cit = s.cend(); - cit2 = std::cend(s); - EXPECT_TRUE(cit == cit2); - } - - { - int a[] = {1, 2, 3, 4}; - span<int> s = a; - - auto it = s.cbegin(); - auto first = it; - EXPECT_TRUE(it == first); - EXPECT_TRUE(*it == 1); - - auto beyond = s.cend(); - EXPECT_TRUE(it != beyond); - EXPECT_DEATH(*beyond, deathstring); - - EXPECT_TRUE(beyond - first == 4); - EXPECT_TRUE(first - first == 0); - EXPECT_TRUE(beyond - beyond == 0); - - ++it; - EXPECT_TRUE(it - first == 1); - EXPECT_TRUE(*it == 2); - EXPECT_TRUE(beyond - it == 3); - - int last = 0; - it = first; - EXPECT_TRUE(it == first); - while (it != s.cend()) - { - EXPECT_TRUE(*it == last + 1); - - last = *it; - ++it; - } - - EXPECT_TRUE(it == beyond); - EXPECT_TRUE(it - beyond == 0); - } - } - TEST(span_test, rbegin_rend) { std::set_terminate([] { @@ -1125,55 +1023,6 @@ TEST(span_test, from_array_constructor) } } - TEST(span_test, crbegin_crend) - { - std::set_terminate([] { - std::cerr << "Expected Death. crbegin_crend"; - std::abort(); - }); - { - int a[] = {1, 2, 3, 4}; - span<int> s = a; - - auto it = s.crbegin(); - auto first = it; - EXPECT_TRUE(it == first); - EXPECT_TRUE(*it == 4); - - auto beyond = s.crend(); - EXPECT_TRUE(it != beyond); -#if (__cplusplus > 201402L) - EXPECT_DEATH([[maybe_unused]] auto _ = *beyond, deathstring); -#else - EXPECT_DEATH(auto _ = *beyond, deathstring); -#endif - - EXPECT_TRUE(beyond - first == 4); - EXPECT_TRUE(first - first == 0); - EXPECT_TRUE(beyond - beyond == 0); - - std::cout << *first << std::endl; - ++it; - EXPECT_TRUE(it - s.crbegin() == 1); - EXPECT_TRUE(*it == 3); - EXPECT_TRUE(beyond - it == 3); - - it = first; - EXPECT_TRUE(it == first); - int last = 5; - while (it != s.crend()) - { - EXPECT_TRUE(*it == last - 1); - last = *it; - - ++it; - } - - EXPECT_TRUE(it == beyond); - EXPECT_TRUE(it - beyond == 0); - } - } - TEST(span_test, as_bytes) { std::set_terminate([] { |