GSL/include/string_span.h

964 lines
31 KiB
C
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////
//
// 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.
//
2015-08-20 21:09:14 -04:00
///////////////////////////////////////////////////////////////////////////////
#pragma once
#ifndef GSL_STRING_SPAN_H
#define GSL_STRING_SPAN_H
2015-09-24 21:08:34 -04:00
#include "gsl_assert.h"
2015-11-24 02:05:31 -05:00
#include "gsl_util.h"
#include "multi_span.h"
2015-08-20 21:09:14 -04:00
#include <cstring>
#include <string>
2015-08-20 21:09:14 -04:00
#ifdef _MSC_VER
2015-11-30 15:24:00 -05:00
// No MSVC does constexpr fully yet
#pragma push_macro("constexpr")
#define constexpr /* nothing */
// VS 2013 workarounds
#if _MSC_VER <= 1800
2015-11-24 02:05:31 -05:00
#define GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
2015-12-14 14:36:20 -05:00
#define GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE
#define GSL_MSVC_NO_CPP14_STD_EQUAL
#define GSL_MSVC_NO_DEFAULT_MOVE_CTOR
2015-11-24 02:05:31 -05:00
// noexcept is not understood
2015-11-30 15:24:00 -05:00
#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
#pragma push_macro("noexcept")
2015-11-24 02:05:31 -05:00
#define noexcept /* nothing */
2015-11-30 15:24:00 -05:00
#endif
#endif // _MSC_VER <= 1800
#endif // _MSC_VER
2015-11-30 15:24:00 -05:00
// In order to test the library, we need it to throw exceptions that we can catch
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION
#ifdef _MSC_VER
#pragma push_macro("noexcept")
#endif
2015-11-24 02:05:31 -05:00
#define noexcept /* nothing */
2015-11-30 15:24:00 -05:00
2015-11-24 02:05:31 -05:00
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
2015-11-30 15:24:00 -05:00
namespace gsl
2015-08-20 21:09:14 -04:00
{
//
// czstring and wzstring
//
2015-11-24 02:05:31 -05:00
// These are "tag" typedef's for C-style strings (i.e. null-terminated character arrays)
2015-08-20 21:09:14 -04:00
// 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::ptrdiff_t Extent = dynamic_range>
using basic_zstring = CharT*;
template<std::ptrdiff_t Extent = dynamic_range>
using czstring = basic_zstring<const char, Extent>;
2015-08-20 21:09:14 -04:00
template<std::ptrdiff_t Extent = dynamic_range>
using cwzstring = basic_zstring<const wchar_t, Extent>;
2015-08-20 21:09:14 -04:00
template<std::ptrdiff_t Extent = dynamic_range>
using zstring = basic_zstring<char, Extent>;
2015-08-20 21:09:14 -04:00
template<std::ptrdiff_t Extent = dynamic_range>
using wzstring = basic_zstring<wchar_t, Extent>;
2015-08-20 21:09:14 -04:00
//
2015-11-24 02:05:31 -05:00
// ensure_sentinel()
2015-08-20 21:09:14 -04:00
//
// Provides a way to obtain an span from a contiguous sequence
2015-08-20 21:09:14 -04:00
// that ends with a (non-inclusive) sentinel value.
//
// Will fail-fast if sentinel cannot be found before max elements are examined.
//
2015-11-23 16:24:08 -05:00
template<typename T, const T Sentinel>
2016-02-24 13:29:29 -05:00
multi_span<T, dynamic_range> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX)
2015-08-20 21:09:14 -04:00
{
auto cur = seq;
while ((cur - seq) < max && *cur != Sentinel) ++cur;
Ensures(*cur == Sentinel);
return{ seq, cur - seq };
2015-08-20 21:09:14 -04:00
}
//
2015-11-24 02:05:31 -05:00
// ensure_z - creates a span for a czstring or cwzstring.
2015-08-20 21:09:14 -04:00
// Will fail fast if a null-terminator cannot be found before
// the limit of size_type.
//
2015-11-23 16:24:08 -05:00
template<typename T>
2016-02-24 13:29:29 -05:00
inline multi_span<T, dynamic_range> ensure_z(T* const & sz, std::ptrdiff_t max = PTRDIFF_MAX)
2015-08-20 21:09:14 -04:00
{
return ensure_sentinel<T, 0>(sz, max);
2015-08-20 21:09:14 -04:00
}
// TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation
2016-02-24 13:29:29 -05:00
inline multi_span<char, dynamic_range> ensure_z(char* const& sz, std::ptrdiff_t max)
2015-08-20 21:09:14 -04:00
{
auto len = strnlen(sz, narrow_cast<size_t>(max));
Ensures(sz[len] == 0);
return{ sz, static_cast<std::ptrdiff_t>(len) };
2015-08-20 21:09:14 -04:00
}
2016-02-24 13:29:29 -05:00
inline multi_span<const char, dynamic_range> ensure_z(const char* const& sz, std::ptrdiff_t max)
2015-08-20 21:09:14 -04:00
{
auto len = strnlen(sz, narrow_cast<size_t>(max));
2015-11-24 02:05:31 -05:00
Ensures(sz[len] == 0);
return{ sz, static_cast<std::ptrdiff_t>(len) };
2015-08-20 21:09:14 -04:00
}
2016-02-24 13:29:29 -05:00
inline multi_span<wchar_t, dynamic_range> ensure_z(wchar_t* const& sz, std::ptrdiff_t max)
2015-08-20 21:09:14 -04:00
{
auto len = wcsnlen(sz, narrow_cast<size_t>(max));
2015-11-24 02:05:31 -05:00
Ensures(sz[len] == 0);
return{ sz, static_cast<std::ptrdiff_t>(len) };
2015-08-20 21:09:14 -04:00
}
2016-02-24 13:29:29 -05:00
inline multi_span<const wchar_t, dynamic_range> ensure_z(const wchar_t* const& sz, std::ptrdiff_t max)
2015-08-20 21:09:14 -04:00
{
auto len = wcsnlen(sz, narrow_cast<size_t>(max));
2015-11-24 02:05:31 -05:00
Ensures(sz[len] == 0);
return{ sz, static_cast<std::ptrdiff_t>(len) };
2015-08-20 21:09:14 -04:00
}
2015-11-23 16:24:08 -05:00
template<typename T, size_t N>
2016-02-24 13:29:29 -05:00
multi_span<T, dynamic_range> ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], static_cast<std::ptrdiff_t>(N)); }
2015-08-20 21:09:14 -04:00
template<class Cont>
2016-02-24 13:29:29 -05:00
multi_span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_range> ensure_z(Cont& cont)
2015-08-20 21:09:14 -04:00
{
return ensure_z(cont.data(), static_cast<std::ptrdiff_t>(cont.length()));
2015-08-20 21:09:14 -04:00
}
2015-11-24 02:05:31 -05:00
template<typename CharT, std::ptrdiff_t>
2015-11-30 15:24:00 -05:00
class basic_string_span;
namespace details
{
template <typename T>
struct is_basic_string_span_oracle : std::false_type
{};
2015-11-24 02:05:31 -05:00
template <typename CharT, std::ptrdiff_t Extent>
struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
2015-11-30 15:24:00 -05:00
{};
template <typename T>
struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>>
{};
2015-11-24 02:05:31 -05:00
template <typename T>
struct length_func
{};
template <>
struct length_func<char>
{
std::ptrdiff_t operator()(char* const ptr, std::ptrdiff_t length) noexcept
{
return narrow_cast<std::ptrdiff_t>(strnlen(ptr, narrow_cast<size_t>(length)));
2015-11-24 02:05:31 -05:00
}
};
template <>
struct length_func<wchar_t>
{
std::ptrdiff_t operator()(wchar_t* const ptr, std::ptrdiff_t length) noexcept
{
return narrow_cast<std::ptrdiff_t>(wcsnlen(ptr, narrow_cast<size_t>(length)));
2015-11-24 02:05:31 -05:00
}
};
template <>
struct length_func<const char>
{
std::ptrdiff_t operator()(const char* const ptr, std::ptrdiff_t length) noexcept
{
return narrow_cast<std::ptrdiff_t>(strnlen(ptr, narrow_cast<size_t>(length)));
2015-11-24 02:05:31 -05:00
}
};
template <>
struct length_func<const wchar_t>
{
std::ptrdiff_t operator()(const wchar_t* const ptr, std::ptrdiff_t length) noexcept
{
return narrow_cast<std::ptrdiff_t>(wcsnlen(ptr, narrow_cast<size_t>(length)));
2015-11-24 02:05:31 -05:00
}
};
2015-11-30 15:24:00 -05:00
}
2015-11-24 02:05:31 -05:00
2015-11-30 15:24:00 -05:00
//
// string_span and relatives
//
// Note that Extent is always single-dimension only
//
2015-11-23 16:24:08 -05:00
template <typename CharT, std::ptrdiff_t Extent = dynamic_range>
2015-11-30 15:24:00 -05:00
class basic_string_span
{
2015-12-04 20:03:19 -05:00
public:
2015-11-30 15:24:00 -05:00
using value_type = CharT;
using const_value_type = std::add_const_t<value_type>;
using pointer = std::add_pointer_t<value_type>;
using reference = std::add_lvalue_reference_t<value_type>;
using const_reference = std::add_lvalue_reference_t<const_value_type>;
using bounds_type = static_bounds<Extent>;
2016-02-24 13:29:29 -05:00
using impl_type = multi_span<value_type, Extent>;
2015-11-30 15:24:00 -05:00
using size_type = ptrdiff_t;
2015-11-24 02:05:31 -05:00
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() = default;
2015-11-30 15:24:00 -05:00
// copy
2015-11-24 02:05:31 -05:00
constexpr basic_string_span(const basic_string_span& other) = default;
2015-11-30 15:24:00 -05:00
// move
2015-12-14 14:36:20 -05:00
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
2015-11-24 02:05:31 -05:00
constexpr basic_string_span(basic_string_span&& other) = default;
2015-12-14 14:36:20 -05:00
#else
constexpr basic_string_span(basic_string_span&& other)
: span_(std::move(other.span_))
{}
#endif
2015-11-24 02:05:31 -05:00
// assign
constexpr basic_string_span& operator=(const basic_string_span& other) = default;
// move assign
2015-12-14 14:36:20 -05:00
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
2015-11-24 02:05:31 -05:00
constexpr basic_string_span& operator=(basic_string_span&& other) = default;
2015-12-14 14:36:20 -05:00
#else
constexpr basic_string_span& operator=(basic_string_span&& other)
{
span_ = std::move(other.span_);
return *this;
}
#endif
2015-11-30 15:24:00 -05:00
// from nullptr
constexpr basic_string_span(std::nullptr_t ptr) noexcept
: span_(ptr)
{}
2015-11-30 15:24:00 -05:00
// from nullptr and length
2015-11-23 16:24:08 -05:00
constexpr basic_string_span(std::nullptr_t ptr, size_type length) noexcept
2015-11-24 02:05:31 -05:00
: span_(ptr, length)
2015-11-30 15:24:00 -05:00
{}
// From static arrays - if 0-terminated, remove 0 from the view
2015-11-30 15:24:00 -05:00
// from static arrays and string literals
template<size_t N>
constexpr basic_string_span(value_type(&arr)[N]) noexcept
2015-11-24 02:05:31 -05:00
: span_(remove_z(arr))
2015-11-30 15:24:00 -05:00
{}
2015-11-24 02:05:31 -05:00
// Those allow 0s within the length, so we do not remove them
2015-11-30 15:24:00 -05:00
// from raw data and length
constexpr basic_string_span(pointer ptr, size_type length) noexcept
: span_(ptr, length)
{}
2015-11-24 02:05:31 -05:00
// from string
2015-11-30 15:24:00 -05:00
constexpr basic_string_span(std::string& s) noexcept
: span_(const_cast<pointer>(s.data()), narrow_cast<std::ptrdiff_t>(s.length()))
2015-11-30 15:24:00 -05:00
{}
2015-11-24 02:05:31 -05:00
// from containers. Containers must have .size() and .data() function signatures
2015-11-30 15:24:00 -05:00
template <typename Cont, typename DataType = typename Cont::value_type,
typename Dummy = std::enable_if_t<!details::is_span<Cont>::value
2015-11-24 02:05:31 -05:00
&& !details::is_basic_string_span<Cont>::value
2015-11-30 15:24:00 -05:00
&& !(!std::is_const<value_type>::value && std::is_const<Cont>::value) // no converting const containers to non-const span
&& std::is_convertible<DataType*, value_type*>::value
&& std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
>
constexpr basic_string_span(Cont& cont)
2015-11-24 02:05:31 -05:00
: span_(cont.data(), cont.size())
2015-11-30 15:24:00 -05:00
{}
2015-11-24 02:05:31 -05:00
// disallow creation from temporary containers and strings
template <typename Cont, typename DataType = typename Cont::value_type,
typename Dummy = std::enable_if_t<!details::is_span<Cont>::value
&& !details::is_basic_string_span<Cont>::value
&& std::is_convertible<DataType*, value_type*>::value
&& std::is_same<std::decay_t<decltype(std::declval<Cont>().size(), *std::declval<Cont>().data())>, DataType>::value>
>
basic_string_span(Cont&& cont) = delete;
2015-12-14 14:36:20 -05:00
#ifndef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE
2015-11-30 15:24:00 -05:00
// from span
template <typename OtherValueType, std::ptrdiff_t OtherExtent,
2015-12-14 14:36:20 -05:00
typename Dummy = std::enable_if_t<
std::is_convertible<OtherValueType*, value_type*>::value
&& std::is_convertible<static_bounds<OtherExtent>, bounds_type>::value>
2015-11-30 15:24:00 -05:00
>
2016-02-24 13:29:29 -05:00
constexpr basic_string_span(multi_span<OtherValueType, OtherExtent> other) noexcept
2015-11-24 02:05:31 -05:00
: span_(other)
2015-11-30 15:24:00 -05:00
{}
2015-12-14 14:36:20 -05:00
#else
// from span
2016-02-24 13:29:29 -05:00
constexpr basic_string_span(multi_span<value_type, Extent> other) noexcept
2015-12-14 14:36:20 -05:00
: span_(other)
{}
template <typename Dummy = std::enable_if_t<!std::is_same<std::remove_const_t<value_type>, value_type>::value>>
2016-02-24 13:29:29 -05:00
constexpr basic_string_span(multi_span<std::remove_const_t<value_type>, Extent> other) noexcept
2015-12-14 14:36:20 -05:00
: span_(other)
{}
#endif
2015-11-30 15:24:00 -05:00
// from string_span
template <typename OtherValueType, std::ptrdiff_t OtherExtent,
typename OtherBounds = static_bounds<OtherExtent>,
typename Dummy = std::enable_if_t<std::is_convertible<OtherValueType*, value_type*>::value && std::is_convertible<OtherBounds, bounds_type>::value>
>
2015-11-24 02:05:31 -05:00
constexpr basic_string_span(basic_string_span<OtherValueType, OtherExtent> other) noexcept
: span_(other.data(), other.length())
2015-11-30 15:24:00 -05:00
{}
2015-11-24 02:05:31 -05:00
constexpr bool empty() const noexcept
{
return length() == 0;
}
// first Count elements
2015-11-30 15:24:00 -05:00
template<size_type Count>
constexpr basic_string_span<value_type, Count> first() const noexcept
{
2015-11-24 02:05:31 -05:00
return{ span_.template first<Count>() };
2015-11-30 15:24:00 -05:00
}
constexpr basic_string_span<value_type, dynamic_range> first(size_type count) const noexcept
{
2015-11-24 02:05:31 -05:00
return{ span_.first(count) };
2015-11-30 15:24:00 -05:00
}
2015-11-24 02:05:31 -05:00
// last Count elements
2015-11-30 15:24:00 -05:00
template<size_type Count>
constexpr basic_string_span<value_type, Count> last() const noexcept
{
2015-11-24 02:05:31 -05:00
return{ span_.template last<Count>() };
2015-11-30 15:24:00 -05:00
}
constexpr basic_string_span<value_type, dynamic_range> last(size_type count) const noexcept
{
2015-11-24 02:05:31 -05:00
return{ span_.last(count) };
2015-11-30 15:24:00 -05:00
}
2015-12-01 16:52:01 -05:00
// create a subview of Count elements starting from Offset
2015-11-30 15:24:00 -05:00
template<size_type Offset, size_type Count>
2015-12-01 16:52:01 -05:00
constexpr basic_string_span<value_type, Count> subspan() const noexcept
2015-11-30 15:24:00 -05:00
{
2015-12-01 16:52:01 -05:00
return{ span_.template subspan<Offset, Count>() };
2015-11-30 15:24:00 -05:00
}
2015-12-01 16:52:01 -05:00
constexpr basic_string_span<value_type, dynamic_range> subspan(size_type offset, size_type count = dynamic_range) const noexcept
2015-11-30 15:24:00 -05:00
{
2015-12-01 16:52:01 -05:00
return{ span_.subspan(offset, count) };
2015-11-30 15:24:00 -05:00
}
2015-11-24 02:05:31 -05:00
constexpr reference operator[](size_type idx) const noexcept
2015-11-30 15:24:00 -05:00
{
2015-11-24 02:05:31 -05:00
return span_[idx];
2015-11-30 15:24:00 -05:00
}
constexpr pointer data() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.data();
2015-11-30 15:24:00 -05:00
}
2015-12-01 16:52:01 -05:00
// length of the span in elements
2015-11-30 15:24:00 -05:00
constexpr size_type length() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.size();
2015-11-30 15:24:00 -05:00
}
2015-12-01 16:52:01 -05:00
// length of the span in elements
2015-11-30 15:24:00 -05:00
constexpr size_type size() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.size();
2015-11-30 15:24:00 -05:00
}
2015-12-01 16:52:01 -05:00
// length of the span in bytes
constexpr size_type size_bytes() const noexcept
2015-11-30 15:24:00 -05:00
{
2015-12-01 16:52:01 -05:00
return span_.size_bytes();
}
// length of the span in bytes
constexpr size_type length_bytes() const noexcept
{
return span_.length_bytes();
2015-11-30 15:24:00 -05:00
}
constexpr iterator begin() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.begin();
2015-11-30 15:24:00 -05:00
}
constexpr iterator end() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.end();
2015-11-30 15:24:00 -05:00
}
constexpr const_iterator cbegin() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.cbegin();
2015-11-30 15:24:00 -05:00
}
constexpr const_iterator cend() const noexcept
{
return span_.cend();
2015-11-30 15:24:00 -05:00
}
constexpr reverse_iterator rbegin() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.rbegin();
2015-11-30 15:24:00 -05:00
}
constexpr reverse_iterator rend() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.rend();
2015-11-30 15:24:00 -05:00
}
constexpr const_reverse_iterator crbegin() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.crbegin();
2015-11-30 15:24:00 -05:00
}
constexpr const_reverse_iterator crend() const noexcept
{
2015-11-24 02:05:31 -05:00
return span_.crend();
2015-11-30 15:24:00 -05:00
}
private:
2015-11-24 02:05:31 -05:00
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max) noexcept
{
return{ sz, details::length_func<value_type>()(sz, max)};
}
template<size_t N>
static impl_type remove_z(value_type(&sz)[N]) noexcept
{
return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
}
impl_type span_;
2015-11-30 15:24:00 -05:00
};
template<std::ptrdiff_t Extent = dynamic_range>
using string_span = basic_string_span<char, Extent>;
template<std::ptrdiff_t Extent = dynamic_range>
using cstring_span = basic_string_span<const char, Extent>;
template<std::ptrdiff_t Extent = dynamic_range>
using wstring_span = basic_string_span<wchar_t, Extent>;
template<std::ptrdiff_t Extent = dynamic_range>
using cwstring_span = basic_string_span<const wchar_t, Extent>;
2015-08-20 21:09:14 -04:00
//
// to_string() allow (explicit) conversions from string_span to string
2015-08-20 21:09:14 -04:00
//
2015-11-24 02:05:31 -05:00
#ifndef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
2015-11-23 16:24:08 -05:00
template<typename CharT, ptrdiff_t Extent>
std::basic_string<typename std::remove_const<CharT>::type> to_string(basic_string_span<CharT, Extent> view)
{
return{ view.data(), static_cast<size_t>(view.length()) };
}
#else
2015-11-05 12:56:50 -05:00
inline std::string to_string(cstring_span<> view)
{
2015-12-14 14:36:20 -05:00
return{ view.data(), static_cast<size_t>(view.length()) };
}
2015-11-05 12:56:50 -05:00
inline std::string to_string(string_span<> view)
{
2015-12-14 14:36:20 -05:00
return{ view.data(), static_cast<size_t>(view.length()) };
}
2015-11-05 12:56:50 -05:00
inline std::wstring to_string(cwstring_span<> view)
{
2015-12-14 14:36:20 -05:00
return{ view.data(), static_cast<size_t>(view.length()) };
}
2015-11-05 12:56:50 -05:00
inline std::wstring to_string(wstring_span<> view)
2015-08-20 21:09:14 -04:00
{
2015-12-14 14:36:20 -05:00
return{ view.data(), static_cast<size_t>(view.length()) };
2015-08-20 21:09:14 -04:00
}
2015-11-24 02:05:31 -05:00
#endif
2015-08-20 21:09:14 -04:00
// zero-terminated string span, used to convert
// zero-terminated spans to legacy strings
2016-02-06 13:46:40 -05:00
template<typename CharT, std::ptrdiff_t Extent = dynamic_range>
class basic_zstring_span
2015-08-20 21:09:14 -04:00
{
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>;
2015-08-20 21:09:14 -04:00
using zstring_type = basic_zstring<value_type, Extent>;
using const_zstring_type = basic_zstring<const_value_type, Extent>;
2015-08-20 21:09:14 -04:00
2016-02-24 13:29:29 -05:00
using impl_type = multi_span<value_type, Extent>;
using string_span_type = basic_string_span<value_type, Extent>;
2016-02-24 13:29:29 -05:00
constexpr basic_zstring_span(impl_type multi_span) noexcept
: span_(multi_span)
{
// expects a zero-terminated span
2016-02-24 13:29:29 -05:00
Expects(multi_span[multi_span.size() - 1] == '\0');
}
2015-08-20 21:09:14 -04:00
2016-02-06 13:46:40 -05:00
// copy
constexpr basic_zstring_span(const basic_zstring_span& other) = default;
2015-08-20 21:09:14 -04:00
2016-02-06 13:46:40 -05:00
// move
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
constexpr basic_zstring_span(basic_zstring_span&& other) = default;
#else
constexpr basic_zstring_span(basic_zstring_span&& other)
: span_(std::move(other.span_))
{}
#endif
2015-08-20 21:09:14 -04:00
2016-02-06 13:46:40 -05:00
// assign
constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default;
2015-08-20 21:09:14 -04:00
2016-02-06 13:46:40 -05:00
// move assign
#ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
#else
constexpr basic_zstring_span& operator=(basic_zstring_span&& other)
{
span_ = std::move(other.span_);
return *this;
}
#endif
constexpr bool empty() const noexcept { return span_.size() == 0; }
constexpr string_span_type as_string_span() const noexcept { return span_.first(span_.size()-1); }
constexpr string_span_type ensure_z() const noexcept { return gsl::ensure_z(span_); }
constexpr const_zstring_type assume_z() const noexcept { return span_.data(); }
2015-08-20 21:09:14 -04:00
private:
2016-02-06 13:46:40 -05:00
impl_type span_;
2015-08-20 21:09:14 -04:00
};
template <std::ptrdiff_t Max = dynamic_range>
using zstring_span = basic_zstring_span<char, Max>;
2015-08-20 21:09:14 -04:00
template <std::ptrdiff_t Max = dynamic_range>
using wzstring_span = basic_zstring_span<wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_range>
using czstring_span = basic_zstring_span<const char, Max>;
template <std::ptrdiff_t Max = dynamic_range>
using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
} // namespace GSL
2015-09-24 21:08:34 -04:00
// operator ==
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
>
bool operator==(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
2015-12-14 14:36:20 -05:00
#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
return (one.size() == tmp.size()) && std::equal(one.begin(), one.end(), tmp.begin());
#else
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
#endif
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename Dummy = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
2015-12-04 20:03:19 -05:00
&& !gsl::details::is_basic_string_span<T>::value>
>
bool operator==(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
2015-12-14 14:36:20 -05:00
#ifdef GSL_MSVC_NO_CPP14_STD_EQUAL
return (tmp.size() == other.size()) && std::equal(tmp.begin(), tmp.end(), other.begin());
#else
2015-12-04 20:03:19 -05:00
return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
#endif
2015-12-04 20:03:19 -05:00
}
#ifndef _MSC_VER
2015-12-07 18:30:00 -05:00
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
2015-12-04 20:03:19 -05:00
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end());
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
}
2015-12-04 20:03:19 -05:00
#endif
// operator !=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
>
bool operator!=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
{
return !(one == other);
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename Dummy = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
2015-12-04 20:03:19 -05:00
&& !gsl::details::is_basic_string_span<T>::value>
>
bool operator!=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
{
return !(one == other);
}
#ifndef _MSC_VER
2015-12-07 18:30:00 -05:00
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
2015-12-04 20:03:19 -05:00
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
return !(one == other);
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
2015-11-23 16:24:08 -05:00
{
return !(one == other);
2015-11-23 16:24:08 -05:00
}
2015-12-04 20:03:19 -05:00
#endif
2015-11-30 15:24:00 -05:00
// operator<
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
>
bool operator<(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
2015-11-30 15:24:00 -05:00
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
2015-12-04 20:03:19 -05:00
return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
2015-11-30 15:24:00 -05:00
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename Dummy = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
2015-12-04 20:03:19 -05:00
&& !gsl::details::is_basic_string_span<T>::value>
>
bool operator<(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
}
2015-12-04 20:03:19 -05:00
#ifndef _MSC_VER
2015-12-07 18:30:00 -05:00
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
2015-12-04 20:03:19 -05:00
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
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::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(one);
return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end());
}
#endif
// operator <=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
>
bool operator<=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
{
return !(other < one);
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename Dummy = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
2015-12-04 20:03:19 -05:00
&& !gsl::details::is_basic_string_span<T>::value>
>
bool operator<=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
{
return !(other < one);
}
#ifndef _MSC_VER
2015-12-07 18:30:00 -05:00
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
2015-12-04 20:03:19 -05:00
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
return !(other < one);
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
2015-11-30 15:24:00 -05:00
{
return !(other < one);
}
2015-12-04 20:03:19 -05:00
#endif
2015-11-30 15:24:00 -05:00
// operator>
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
>
bool operator>(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
{
return other < one;
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename Dummy = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
2015-12-04 20:03:19 -05:00
&& !gsl::details::is_basic_string_span<T>::value>
>
bool operator>(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
2015-11-30 15:24:00 -05:00
{
return other < one;
}
2015-12-04 20:03:19 -05:00
#ifndef _MSC_VER
2015-12-07 18:30:00 -05:00
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
2015-12-04 20:03:19 -05:00
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
return other < one;
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
return other < one;
}
#endif
// operator >=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value>
>
bool operator>=(gsl::basic_string_span<CharT, Extent> one, const T& other) noexcept
{
return !(one < other);
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename Dummy = std::enable_if_t<
std::is_convertible<T, gsl::basic_string_span<std::add_const_t<CharT>, Extent>>::value
2015-12-04 20:03:19 -05:00
&& !gsl::details::is_basic_string_span<T>::value>
>
bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other) noexcept
2015-11-30 15:24:00 -05:00
{
return !(one < other);
}
2015-11-24 02:05:31 -05:00
2015-12-04 20:03:19 -05:00
#ifndef _MSC_VER
2015-12-07 18:30:00 -05:00
// VS treats temp and const containers as convertible to basic_string_span,
// so the cases below are already covered by the previous operators
2015-12-04 20:03:19 -05:00
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
return !(one < other);
}
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_range, typename T,
typename DataType = typename T::value_type,
typename Dummy = 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) noexcept
{
return !(one < other);
}
#endif
// VS 2013 workarounds
#ifdef _MSC_VER
2015-11-30 15:24:00 -05:00
#undef constexpr
#pragma pop_macro("constexpr")
#if _MSC_VER <= 1800
2015-11-30 15:24:00 -05:00
#ifndef GSL_THROW_ON_CONTRACT_VIOLATION
#undef noexcept
#pragma pop_macro("noexcept")
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
#undef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG
2015-12-14 14:36:20 -05:00
#undef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE
#undef GSL_MSVC_NO_CPP14_STD_EQUAL
2015-12-14 14:36:20 -05:00
#undef GSL_MSVC_NO_DEFAULT_MOVE_CTOR
#endif // _MSC_VER <= 1800
#endif // _MSC_VER
2015-11-24 02:05:31 -05:00
#if defined(GSL_THROW_ON_CONTRACT_VIOLATION)
2015-11-30 15:24:00 -05:00
#undef noexcept
#ifdef _MSC_VER
#pragma pop_macro("noexcept")
#endif
#endif // GSL_THROW_ON_CONTRACT_VIOLATION
#endif // GSL_STRING_SPAN_H