/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// #pragma once #ifndef GSL_STRING_SPAN_H #define GSL_STRING_SPAN_H #include "gsl_assert.h" #include "gsl_util.h" #include "span.h" #include #include #define GSL_IMPL_CONSTEXPR constexpr #define GSL_IMPL_NOEXCEPT noexcept #ifdef _MSC_VER // No MSVC does constexpr fully yet #undef GSL_IMPL_CONSTEXPR #define GSL_IMPL_CONSTEXPR /* nothing */ // VS 2013 workarounds #if _MSC_VER <= 1800 #define GSL_MSVC_HAS_TYPE_DEDUCTION_BUG #define GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE #define GSL_MSVC_NO_CPP14_STD_EQUAL #define GSL_MSVC_NO_DEFAULT_MOVE_CTOR // noexcept is not understood #ifndef GSL_THROW_ON_CONTRACT_VIOLATION #undef GSL_IMPL_NOEXCEPT #define GSL_IMPL_NOEXCEPT /* nothing */ #endif #endif // _MSC_VER <= 1800 #endif // _MSC_VER // In order to test the library, we need it to throw exceptions that we can catch #ifdef GSL_THROW_ON_CONTRACT_VIOLATION #undef GSL_IMPL_NOEXCEPT #define GSL_IMPL_NOEXCEPT /* nothing */ #endif // GSL_THROW_ON_CONTRACT_VIOLATION namespace gsl { // // czstring and wzstring // // These are "tag" typedef's 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 using basic_zstring = CharT*; template using czstring = basic_zstring; template using cwzstring = basic_zstring; template using zstring = basic_zstring; template using wzstring = basic_zstring; // // 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 span ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX) { auto cur = seq; while ((cur - seq) < max && *cur != Sentinel) ++cur; GSL_ENSURES(*cur == Sentinel); return{ seq, cur - seq }; } // // ensure_z - creates a span for a czstring or cwzstring. // Will fail fast if a null-terminator cannot be found before // the limit of size_type. // template inline span ensure_z(T* const & sz, std::ptrdiff_t max = PTRDIFF_MAX) { return ensure_sentinel(sz, max); } // TODO (neilmac) there is probably a better template-magic way to get the const and non-const overloads to share an implementation inline span ensure_z(char* const& sz, std::ptrdiff_t max) { auto len = strnlen(sz, narrow_cast(max)); GSL_ENSURES(sz[len] == 0); return{ sz, static_cast(len) }; } inline span ensure_z(const char* const& sz, std::ptrdiff_t max) { auto len = strnlen(sz, narrow_cast(max)); GSL_ENSURES(sz[len] == 0); return{ sz, static_cast(len) }; } inline span ensure_z(wchar_t* const& sz, std::ptrdiff_t max) { auto len = wcsnlen(sz, narrow_cast(max)); GSL_ENSURES(sz[len] == 0); return{ sz, static_cast(len) }; } inline span ensure_z(const wchar_t* const& sz, std::ptrdiff_t max) { auto len = wcsnlen(sz, narrow_cast(max)); GSL_ENSURES(sz[len] == 0); return{ sz, static_cast(len) }; } template span ensure_z(T(&sz)[N]) { return ensure_z(&sz[0], static_cast(N)); } template span::type, dynamic_range> ensure_z(Cont& cont) { return ensure_z(cont.data(), static_cast(cont.length())); } template class basic_string_span; namespace details { template struct is_basic_string_span_oracle : std::false_type {}; template struct is_basic_string_span_oracle> : std::true_type {}; template struct is_basic_string_span : is_basic_string_span_oracle> {}; template struct length_func {}; template <> struct length_func { std::ptrdiff_t operator()(char* const ptr, std::ptrdiff_t length) GSL_IMPL_NOEXCEPT { return narrow_cast(strnlen(ptr, narrow_cast(length))); } }; template <> struct length_func { std::ptrdiff_t operator()(wchar_t* const ptr, std::ptrdiff_t length) GSL_IMPL_NOEXCEPT { return narrow_cast(wcsnlen(ptr, narrow_cast(length))); } }; template <> struct length_func { std::ptrdiff_t operator()(const char* const ptr, std::ptrdiff_t length) GSL_IMPL_NOEXCEPT { return narrow_cast(strnlen(ptr, narrow_cast(length))); } }; template <> struct length_func { std::ptrdiff_t operator()(const wchar_t* const ptr, std::ptrdiff_t length) GSL_IMPL_NOEXCEPT { return narrow_cast(wcsnlen(ptr, narrow_cast(length))); } }; } // // string_span and relatives // // Note that Extent is always single-dimension only // template class basic_string_span { public: using value_type = CharT; using const_value_type = std::add_const_t; using pointer = std::add_pointer_t; using reference = std::add_lvalue_reference_t; using const_reference = std::add_lvalue_reference_t; using bounds_type = static_bounds; using impl_type = span; using size_type = ptrdiff_t; 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) GSL_IMPL_CONSTEXPR basic_string_span() = default; // copy GSL_IMPL_CONSTEXPR basic_string_span(const basic_string_span& other) = default; // move #ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR GSL_IMPL_CONSTEXPR basic_string_span(basic_string_span&& other) = default; #else GSL_IMPL_CONSTEXPR basic_string_span(basic_string_span&& other) : span_(std::move(other.span_)) {} #endif // assign GSL_IMPL_CONSTEXPR basic_string_span& operator=(const basic_string_span& other) = default; // move assign #ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR GSL_IMPL_CONSTEXPR basic_string_span& operator=(basic_string_span&& other) = default; #else GSL_IMPL_CONSTEXPR basic_string_span& operator=(basic_string_span&& other) { span_ = std::move(other.span_); return *this; } #endif // from nullptr GSL_IMPL_CONSTEXPR basic_string_span(std::nullptr_t ptr) GSL_IMPL_NOEXCEPT : span_(ptr) {} // from nullptr and length GSL_IMPL_CONSTEXPR basic_string_span(std::nullptr_t ptr, size_type length) GSL_IMPL_NOEXCEPT : span_(ptr, length) {} // From static arrays - if 0-terminated, remove 0 from the view // from static arrays and string literals template GSL_IMPL_CONSTEXPR basic_string_span(value_type(&arr)[N]) GSL_IMPL_NOEXCEPT : span_(remove_z(arr)) {} // Those allow 0s within the length, so we do not remove them // from raw data and length GSL_IMPL_CONSTEXPR basic_string_span(pointer ptr, size_type length) GSL_IMPL_NOEXCEPT : span_(ptr, length) {} // from string GSL_IMPL_CONSTEXPR basic_string_span(std::string& s) GSL_IMPL_NOEXCEPT : span_(const_cast(s.data()), narrow_cast(s.length())) {} // from containers. Containers must have .size() and .data() function signatures template ::value && !details::is_basic_string_span::value && !(!std::is_const::value && std::is_const::value) // no converting const containers to non-const span && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > GSL_IMPL_CONSTEXPR basic_string_span(Cont& cont) : span_(cont.data(), cont.size()) {} // disallow creation from temporary containers and strings template ::value && !details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > basic_string_span(Cont&& cont) = delete; #ifndef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE // from span template ::value && std::is_convertible, bounds_type>::value> > GSL_IMPL_CONSTEXPR basic_string_span(span other) GSL_IMPL_NOEXCEPT : span_(other) {} #else // from span GSL_IMPL_CONSTEXPR basic_string_span(span other) GSL_IMPL_NOEXCEPT : span_(other) {} template , value_type>::value>> GSL_IMPL_CONSTEXPR basic_string_span(span, Extent> other) GSL_IMPL_NOEXCEPT : span_(other) {} #endif // from string_span template , typename Dummy = std::enable_if_t::value && std::is_convertible::value> > GSL_IMPL_CONSTEXPR basic_string_span(basic_string_span other) GSL_IMPL_NOEXCEPT : span_(other.data(), other.length()) {} GSL_IMPL_CONSTEXPR bool empty() const GSL_IMPL_NOEXCEPT { return length() == 0; } // first Count elements template GSL_IMPL_CONSTEXPR basic_string_span first() const GSL_IMPL_NOEXCEPT { return{ span_.template first() }; } GSL_IMPL_CONSTEXPR basic_string_span first(size_type count) const GSL_IMPL_NOEXCEPT { return{ span_.first(count) }; } // last Count elements template GSL_IMPL_CONSTEXPR basic_string_span last() const GSL_IMPL_NOEXCEPT { return{ span_.template last() }; } GSL_IMPL_CONSTEXPR basic_string_span last(size_type count) const GSL_IMPL_NOEXCEPT { return{ span_.last(count) }; } // create a subview of Count elements starting from Offset template GSL_IMPL_CONSTEXPR basic_string_span subspan() const GSL_IMPL_NOEXCEPT { return{ span_.template subspan() }; } GSL_IMPL_CONSTEXPR basic_string_span subspan(size_type offset, size_type count = dynamic_range) const GSL_IMPL_NOEXCEPT { return{ span_.subspan(offset, count) }; } GSL_IMPL_CONSTEXPR reference operator[](size_type idx) const GSL_IMPL_NOEXCEPT { return span_[idx]; } GSL_IMPL_CONSTEXPR pointer data() const GSL_IMPL_NOEXCEPT { return span_.data(); } // length of the span in elements GSL_IMPL_CONSTEXPR size_type length() const GSL_IMPL_NOEXCEPT { return span_.size(); } // length of the span in elements GSL_IMPL_CONSTEXPR size_type size() const GSL_IMPL_NOEXCEPT { return span_.size(); } // length of the span in bytes GSL_IMPL_CONSTEXPR size_type size_bytes() const GSL_IMPL_NOEXCEPT { return span_.size_bytes(); } // length of the span in bytes GSL_IMPL_CONSTEXPR size_type length_bytes() const GSL_IMPL_NOEXCEPT { return span_.length_bytes(); } GSL_IMPL_CONSTEXPR iterator begin() const GSL_IMPL_NOEXCEPT { return span_.begin(); } GSL_IMPL_CONSTEXPR iterator end() const GSL_IMPL_NOEXCEPT { return span_.end(); } GSL_IMPL_CONSTEXPR const_iterator cbegin() const GSL_IMPL_NOEXCEPT { return span_.cbegin(); } GSL_IMPL_CONSTEXPR const_iterator cend() const GSL_IMPL_NOEXCEPT { return span_.cend(); } GSL_IMPL_CONSTEXPR reverse_iterator rbegin() const GSL_IMPL_NOEXCEPT { return span_.rbegin(); } GSL_IMPL_CONSTEXPR reverse_iterator rend() const GSL_IMPL_NOEXCEPT { return span_.rend(); } GSL_IMPL_CONSTEXPR const_reverse_iterator crbegin() const GSL_IMPL_NOEXCEPT { return span_.crbegin(); } GSL_IMPL_CONSTEXPR const_reverse_iterator crend() const GSL_IMPL_NOEXCEPT { return span_.crend(); } private: static impl_type remove_z(pointer const& sz, std::ptrdiff_t max) GSL_IMPL_NOEXCEPT { return{ sz, details::length_func()(sz, max)}; } template static impl_type remove_z(value_type(&sz)[N]) GSL_IMPL_NOEXCEPT { return remove_z(&sz[0], narrow_cast(N)); } impl_type span_; }; template using string_span = basic_string_span; template using cstring_span = basic_string_span; template using wstring_span = basic_string_span; template using cwstring_span = basic_string_span; // // to_string() allow (explicit) conversions from string_span to string // #ifndef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG template std::basic_string::type> to_string(basic_string_span view) { return{ view.data(), static_cast(view.length()) }; } #else inline std::string to_string(cstring_span<> view) { return{ view.data(), static_cast(view.length()) }; } inline std::string to_string(string_span<> view) { return{ view.data(), static_cast(view.length()) }; } inline std::wstring to_string(cwstring_span<> view) { return{ view.data(), static_cast(view.length()) }; } inline std::wstring to_string(wstring_span<> view) { return{ view.data(), static_cast(view.length()) }; } #endif // zero-terminated string span, used to convert // zero-terminated spans to legacy strings template class basic_zstring_span { public: using value_type = CharT; using const_value_type = std::add_const_t; using pointer = std::add_pointer_t; using const_pointer = std::add_pointer_t; using zstring_type = basic_zstring; using const_zstring_type = basic_zstring; using impl_type = span; using string_span_type = basic_string_span; GSL_IMPL_CONSTEXPR basic_zstring_span(impl_type span) GSL_IMPL_NOEXCEPT : span_(span) { // expects a zero-terminated span GSL_EXPECTS(span[span.size() - 1] == '\0'); } // copy GSL_IMPL_CONSTEXPR basic_zstring_span(const basic_zstring_span& other) = default; // move #ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR GSL_IMPL_CONSTEXPR basic_zstring_span(basic_zstring_span&& other) = default; #else GSL_IMPL_CONSTEXPR basic_zstring_span(basic_zstring_span&& other) : span_(std::move(other.span_)) {} #endif // assign GSL_IMPL_CONSTEXPR basic_zstring_span& operator=(const basic_zstring_span& other) = default; // move assign #ifndef GSL_MSVC_NO_DEFAULT_MOVE_CTOR GSL_IMPL_CONSTEXPR basic_zstring_span& operator=(basic_zstring_span&& other) = default; #else GSL_IMPL_CONSTEXPR basic_zstring_span& operator=(basic_zstring_span&& other) { span_ = std::move(other.span_); return *this; } #endif GSL_IMPL_CONSTEXPR bool empty() const GSL_IMPL_NOEXCEPT { return span_.size() == 0; } GSL_IMPL_CONSTEXPR string_span_type as_string_span() const GSL_IMPL_NOEXCEPT { return span_.first(span_.size()-1); } GSL_IMPL_CONSTEXPR string_span_type ensure_z() const GSL_IMPL_NOEXCEPT { return gsl::ensure_z(span_); } GSL_IMPL_CONSTEXPR const_zstring_type assume_z() const GSL_IMPL_NOEXCEPT { return span_.data(); } private: impl_type span_; }; template using zstring_span = basic_zstring_span; template using wzstring_span = basic_zstring_span; template using czstring_span = basic_zstring_span; template using cwzstring_span = basic_zstring_span; } // namespace GSL // operator == template , Extent>>::value> > bool operator==(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(other); #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 , Extent>>::value && !gsl::details::is_basic_string_span::value> > bool operator==(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(one); #ifdef GSL_MSVC_NO_CPP14_STD_EQUAL return (tmp.size() == other.size()) && std::equal(tmp.begin(), tmp.end(), other.begin()); #else return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end()); #endif } #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 ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator==(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(other); return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end()); } template ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator==(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(one); return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end()); } #endif // operator != template , Extent>>::value> > bool operator!=(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return !(one == other); } template , Extent>>::value && !gsl::details::is_basic_string_span::value> > bool operator!=(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { 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 ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator!=(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return !(one == other); } template ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator!=(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { return !(one == other); } #endif // operator< template , Extent>>::value> > bool operator<(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(other); return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end()); } template , Extent>>::value && !gsl::details::is_basic_string_span::value> > bool operator<(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, 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 ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator<(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(other); return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end()); } template ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator<(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { gsl::basic_string_span, Extent> tmp(one); return std::lexicographical_compare(tmp.begin(), tmp.end(), other.begin(), other.end()); } #endif // operator <= template , Extent>>::value> > bool operator<=(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return !(other < one); } template , Extent>>::value && !gsl::details::is_basic_string_span::value> > bool operator<=(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { 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 ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator<=(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return !(other < one); } template ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator<=(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { return !(other < one); } #endif // operator> template , Extent>>::value> > bool operator>(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return other < one; } template , Extent>>::value && !gsl::details::is_basic_string_span::value> > bool operator>(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { 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 ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator>(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return other < one; } template ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator>(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { return other < one; } #endif // operator >= template , Extent>>::value> > bool operator>=(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return !(one < other); } template , Extent>>::value && !gsl::details::is_basic_string_span::value> > bool operator>=(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { 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 ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator>=(gsl::basic_string_span one, const T& other) GSL_IMPL_NOEXCEPT { return !(one < other); } template ::value && !gsl::details::is_basic_string_span::value && std::is_convertible::value && std::is_same().size(), *std::declval().data())>, DataType>::value> > bool operator>=(const T& one, gsl::basic_string_span other) GSL_IMPL_NOEXCEPT { return !(one < other); } #endif #undef GSL_IMPL_NOEXCEPT #undef GSL_IMPL_CONSTEXPR // VS 2013 workarounds #if defined(_MSC_VER) and (_MSC_VER <= 1800) #undef GSL_MSVC_HAS_TYPE_DEDUCTION_BUG #undef GSL_MSVC_HAS_SFINAE_SUBSTITUTION_ICE #undef GSL_MSVC_NO_CPP14_STD_EQUAL #undef GSL_MSVC_NO_DEFAULT_MOVE_CTOR #endif // _MSC_VER <= 1800 #endif // GSL_STRING_SPAN_H