/////////////////////////////////////////////////////////////////////////////// // // 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 // for Ensures, Expects #include // for narrow_cast #include // for operator!=, operator==, dynamic_extent #include // for not_null #include // for equal, lexicographical_compare #include // for array #include // for ptrdiff_t, size_t, nullptr_t #include // for PTRDIFF_MAX #include #include // for basic_string, allocator, char_traits #include // 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 using basic_zstring = CharT*; template using czstring = basic_zstring; template using cwzstring = basic_zstring; template using cu16zstring = basic_zstring; template using cu32zstring = basic_zstring; template using zstring = basic_zstring; template using wzstring = basic_zstring; template using u16zstring = basic_zstring; template using u32zstring = basic_zstring; namespace details { template std::ptrdiff_t string_length(const CharT* str, std::ptrdiff_t n) { if (str == nullptr || n <= 0) return 0; const span str_span{str, n}; std::ptrdiff_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 span ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX) { 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 ((cur - seq) < max && *cur != Sentinel) ++cur; Ensures(*cur == Sentinel); return {seq, 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 span ensure_z(CharT* const& sz, std::ptrdiff_t max = PTRDIFF_MAX) { return ensure_sentinel(sz, max); } template span ensure_z(CharT (&sz)[N]) { return ensure_z(&sz[0], narrow_cast(N)); } template span::type, dynamic_extent> ensure_z(Cont& cont) { return ensure_z(cont.data(), narrow_cast(cont.size())); } 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> { }; } // namespace details // // string_span and relatives // template class basic_string_span { public: using element_type = CharT; using pointer = std::add_pointer_t; using reference = std::add_lvalue_reference_t; using const_reference = std::add_lvalue_reference_t>; using impl_type = span; using index_type = typename impl_type::index_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, index_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 constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr)) {} template > constexpr basic_string_span(std::array& arr) noexcept : span_(arr) {} template > constexpr basic_string_span(const std::array& arr) noexcept : span_(arr) {} // Container signature should work for basic_string after C++17 version exists template // GSL_SUPPRESS(bounds.4) // NO-FORMAT: attribute // TODO: parser bug constexpr basic_string_span(std::basic_string& str) : span_(&str[0], narrow_cast(str.length())) {} template constexpr basic_string_span(const std::basic_string& str) : span_(&str[0], str.length()) {} // from containers. Containers must have a pointer type and data() function signatures template ::value && std::is_convertible::value && std::is_convertible().data())>::value>> constexpr basic_string_span(Container& cont) : span_(cont) {} template ::value && std::is_convertible::value && std::is_convertible().data())>::value>> constexpr basic_string_span(const Container& cont) : span_(cont) {} // from string_span template < class OtherValueType, std::ptrdiff_t OtherExtent, class = std::enable_if_t::impl_type, impl_type>::value>> constexpr basic_string_span(basic_string_span other) : span_(other.data(), other.length()) {} template constexpr basic_string_span first() const { return {span_.template first()}; } constexpr basic_string_span first(index_type count) const { return {span_.first(count)}; } template constexpr basic_string_span last() const { return {span_.template last()}; } constexpr basic_string_span last(index_type count) const { return {span_.last(count)}; } template constexpr basic_string_span subspan() const { return {span_.template subspan()}; } constexpr basic_string_span subspan(index_type offset, index_type count = dynamic_extent) const { return {span_.subspan(offset, count)}; } constexpr reference operator[](index_type idx) const { return span_[idx]; } constexpr reference operator()(index_type idx) const { return span_[idx]; } constexpr pointer data() const { return span_.data(); } constexpr index_type length() const noexcept { return span_.size(); } constexpr index_type size() const noexcept { return span_.size(); } constexpr index_type size_bytes() const noexcept { return span_.size_bytes(); } constexpr index_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::ptrdiff_t max) { return {sz, details::string_length(sz, max)}; } template static impl_type remove_z(element_type (&sz)[N]) { 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; template using u16string_span = basic_string_span; template using cu16string_span = basic_string_span; template using u32string_span = basic_string_span; template using cu32string_span = basic_string_span; // // to_string() allow (explicit) conversions from string_span to string // template std::basic_string::type> to_string(basic_string_span view) { return {view.data(), narrow_cast(view.length())}; } template , typename Allocator = std::allocator, typename gCharT, std::ptrdiff_t Extent> std::basic_string to_basic_string(basic_string_span view) { return {view.data(), narrow_cast(view.length())}; } template basic_string_span::value> as_bytes(basic_string_span s) noexcept { GSL_SUPPRESS(type.1) // NO-FORMAT: attribute return {reinterpret_cast(s.data()), s.size_bytes()}; } template ::value>> basic_string_span::value> as_writeable_bytes(basic_string_span s) noexcept { GSL_SUPPRESS(type.1) // NO-FORMAT: attribute return {reinterpret_cast(s.data()), s.size_bytes()}; } // 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; 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 using zstring_span = basic_zstring_span; template using wzstring_span = basic_zstring_span; template using u16zstring_span = basic_zstring_span; template using u32zstring_span = basic_zstring_span; template using czstring_span = basic_zstring_span; template using cwzstring_span = basic_zstring_span; template using cu16zstring_span = basic_zstring_span; template using cu32zstring_span = basic_zstring_span; // operator == template ::value || std::is_convertible>>::value>> bool operator==(const gsl::basic_string_span& one, const T& other) { const gsl::basic_string_span> tmp(other); return std::equal(one.begin(), one.end(), tmp.begin(), tmp.end()); } template ::value && std::is_convertible>>::value>> bool operator==(const T& one, const gsl::basic_string_span& other) { const gsl::basic_string_span> tmp(one); return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end()); } // operator != template , Extent>>::value>> bool operator!=(gsl::basic_string_span one, const T& other) { return !(one == other); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename = std::enable_if_t< std::is_convertible, Extent>>::value && !gsl::details::is_basic_string_span::value>> bool operator!=(const T& one, gsl::basic_string_span other) { return !(one == other); } // operator< template , Extent>>::value>> bool operator<(gsl::basic_string_span one, const T& other) { const gsl::basic_string_span, Extent> tmp(other); return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end()); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename = std::enable_if_t< std::is_convertible, Extent>>::value && !gsl::details::is_basic_string_span::value>> bool operator<(const T& one, gsl::basic_string_span other) { 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 < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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::basic_string_span, Extent> tmp(other); return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end()); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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::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) { return !(other < one); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename = std::enable_if_t< std::is_convertible, Extent>>::value && !gsl::details::is_basic_string_span::value>> bool operator<=(const T& one, gsl::basic_string_span 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::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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) { return !(other < one); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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) { return !(other < one); } #endif // operator> template , Extent>>::value>> bool operator>(gsl::basic_string_span one, const T& other) { return other < one; } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename = std::enable_if_t< std::is_convertible, Extent>>::value && !gsl::details::is_basic_string_span::value>> bool operator>(const T& one, gsl::basic_string_span 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::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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) { return other < one; } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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) { return other < one; } #endif // operator >= template , Extent>>::value>> bool operator>=(gsl::basic_string_span one, const T& other) { return !(one < other); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename = std::enable_if_t< std::is_convertible, Extent>>::value && !gsl::details::is_basic_string_span::value>> bool operator>=(const T& one, gsl::basic_string_span 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::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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) { return !(one < other); } template < typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T, typename DataType = typename T::value_type, typename = std::enable_if_t< !gsl::details::is_span::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) { 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