GSL/include/gsl/string_span

722 lines
27 KiB
Plaintext
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
///////////////////////////////////////////////////////////////////////////////
#ifndef GSL_STRING_SPAN_H
#define GSL_STRING_SPAN_H
2015-09-24 21:08:34 -04:00
#include <gsl/gsl_assert> // for Ensures, Expects
#include <gsl/gsl_util> // for narrow_cast
#include <gsl/span> // for operator!=, operator==, dynamic_extent
#include <algorithm> // for equal, lexicographical_compare
#include <array> // for array
#include <cstddef> // for ptrdiff_t, size_t, nullptr_t
#include <cstdint> // for PTRDIFF_MAX
2015-08-20 21:09:14 -04:00
#include <cstring>
#include <string> // for basic_string, allocator, char_traits
#include <type_traits> // for declval, is_convertible, enable_if_t, add_...
2015-08-20 21:09:14 -04:00
Clang cl (#762) * Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit 7263f3289e6e835c32fc7800d0c2d4e58bd21178. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits: bda3d6a428e5d19e97375b2e575b0f51ff1b4bc0 97062933acfb6428535c0fdcab5d94371ce72bfc 0f4fb04bac9c2d091005b791294237250dbe0668 1b0c19afd154f4ffc5ef793014c1bc2324534fd0 a5739ea5f0cd7633527b5e5d3b16a9e3ade7149f * Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit 6fe1a4203501145ab4350b9152ecc11f3a30e49a. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces 3c1c0794dd9a29908cc7c38f591bd3c7b4929677 * AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit 2238c4760e86feebb2a18620b77312bd01055f61. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
2019-01-15 13:27:34 -05:00
#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
2015-08-20 21:09:14 -04:00
{
//
// czstring and wzstring
//
// These are "tag" typedefs 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.
//
2016-07-20 16:17:47 -04:00
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
using basic_zstring = CharT*;
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
using czstring = basic_zstring<const char, Extent>;
2015-08-20 21:09:14 -04:00
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
using cwzstring = basic_zstring<const wchar_t, Extent>;
2015-08-20 21:09:14 -04:00
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
using cu16zstring = basic_zstring<const char16_t, Extent>;
2015-08-20 21:09:14 -04:00
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
using cu32zstring = basic_zstring<const char32_t, Extent>;
2015-08-20 21:09:14 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
using zstring = basic_zstring<char, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using wzstring = basic_zstring<wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u16zstring = basic_zstring<char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u32zstring = basic_zstring<char32_t, Extent>;
namespace details
{
template <class CharT>
std::ptrdiff_t string_length(const CharT* str, std::ptrdiff_t n)
{
if (str == nullptr || n <= 0) return 0;
const span<const CharT> str_span{str, n};
std::ptrdiff_t len = 0;
while (len < n && str_span[len]) len++;
return len;
}
} // namespace details
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.
//
2016-07-20 16:17:47 -04:00
template <typename T, const T Sentinel>
span<T, dynamic_extent> ensure_sentinel(T* seq, std::ptrdiff_t max = PTRDIFF_MAX)
2015-08-20 21:09:14 -04:00
{
Ensures(seq != nullptr);
GSL_SUPPRESS(f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
2015-08-20 21:09:14 -04:00
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);
2016-07-20 16:17:47 -04:00
return {seq, cur - seq};
2015-08-20 21:09:14 -04:00
}
//
// ensure_z - creates a span for a zero terminated strings.
2015-08-20 21:09:14 -04:00
// 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::ptrdiff_t max = PTRDIFF_MAX)
2015-08-20 21:09:14 -04:00
{
return ensure_sentinel<CharT, CharT(0)>(sz, max);
2015-08-20 21:09:14 -04:00
}
template <typename CharT, std::size_t N>
span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
2016-07-20 16:17:47 -04:00
{
return ensure_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
2016-07-20 16:17:47 -04:00
}
2015-08-20 21:09:14 -04:00
2016-07-20 16:17:47 -04:00
template <class Cont>
span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
ensure_z(Cont& cont)
2015-08-20 21:09:14 -04:00
{
return ensure_z(cont.data(), narrow_cast<std::ptrdiff_t>(cont.size()));
2015-08-20 21:09:14 -04:00
}
2016-07-20 16:17:47 -04:00
template <typename CharT, std::ptrdiff_t>
2015-11-30 15:24:00 -05:00
class basic_string_span;
namespace details {
2015-11-30 15:24:00 -05:00
template <typename T>
struct is_basic_string_span_oracle : std::false_type
2016-07-20 16:17:47 -04:00
{
};
2015-11-30 15:24:00 -05:00
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
2016-07-20 16:17:47 -04:00
{
};
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>>
2016-07-20 16:17:47 -04:00
{
};
} // namespace details
2015-11-30 15:24:00 -05:00
//
// string_span and relatives
//
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
2015-11-30 15:24:00 -05:00
class basic_string_span
{
2015-12-04 20:03:19 -05:00
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>;
2015-11-30 15:24:00 -05:00
using index_type = typename impl_type::index_type;
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;
using size_type = index_type;
2015-11-24 02:05:31 -05:00
// default (empty)
constexpr basic_string_span() noexcept = default;
2015-11-30 15:24:00 -05:00
// copy
constexpr basic_string_span(const basic_string_span& other) noexcept = default;
2015-11-30 15:24:00 -05:00
2015-11-24 02:05:31 -05:00
// assign
constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default;
2015-11-24 02:05:31 -05:00
constexpr basic_string_span(pointer ptr, index_type length) : span_(ptr, length) {}
constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {}
2015-11-30 15:24:00 -05:00
// 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
2017-02-13 15:11:45 -05:00
template <std::size_t N>
constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
{}
2016-07-26 22:19:47 -04:00
2017-02-13 15:11:45 -05:00
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)
{}
2017-02-13 15:11:45 -05:00
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], narrow_cast<std::ptrdiff_t>(str.length()))
{}
2015-11-30 15:24:00 -05:00
template <class Traits, class Allocator>
constexpr basic_string_span(const std::basic_string<element_type, Traits, Allocator>& str)
2016-07-26 22:19:47 -04:00
: span_(&str[0], str.length())
{}
2015-11-30 15:24:00 -05:00
// 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 &&
2016-07-26 22:19:47 -04:00
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr basic_string_span(Container& cont) : span_(cont)
{}
2015-11-30 15:24:00 -05:00
template <class Container,
class = std::enable_if_t<
!details::is_basic_string_span<Container>::value &&
std::is_convertible<typename Container::pointer, pointer>::value &&
2016-07-26 22:19:47 -04:00
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr basic_string_span(const Container& cont) : span_(cont)
{}
2015-11-24 02:05:31 -05:00
2015-11-30 15:24:00 -05:00
// from string_span
2016-07-26 22:19:47 -04:00
template <
class OtherValueType, std::ptrdiff_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)
2015-11-24 02:05:31 -05:00
: span_(other.data(), other.length())
{}
2015-11-24 02:05:31 -05:00
template <index_type Count>
2016-07-26 22:19:47 -04:00
constexpr basic_string_span<element_type, Count> first() const
2015-11-30 15:24:00 -05:00
{
2016-07-20 16:17:47 -04:00
return {span_.template first<Count>()};
2015-11-30 15:24:00 -05:00
}
2016-07-27 16:48:29 -04:00
constexpr basic_string_span<element_type, dynamic_extent> first(index_type count) const
2015-11-30 15:24:00 -05:00
{
2016-07-20 16:17:47 -04:00
return {span_.first(count)};
2015-11-30 15:24:00 -05:00
}
template <index_type Count>
2016-07-27 16:48:29 -04:00
constexpr basic_string_span<element_type, Count> last() const
2015-11-30 15:24:00 -05:00
{
2016-07-20 16:17:47 -04:00
return {span_.template last<Count>()};
2015-11-30 15:24:00 -05:00
}
2016-07-26 22:19:47 -04:00
constexpr basic_string_span<element_type, dynamic_extent> last(index_type count) const
2015-11-30 15:24:00 -05:00
{
2016-07-20 16:17:47 -04:00
return {span_.last(count)};
2015-11-30 15:24:00 -05:00
}
template <index_type Offset, index_type Count>
2016-07-26 22:19:47 -04:00
constexpr basic_string_span<element_type, Count> subspan() const
2015-11-30 15:24:00 -05:00
{
2016-07-20 16:17:47 -04:00
return {span_.template subspan<Offset, Count>()};
2015-11-30 15:24:00 -05:00
}
constexpr basic_string_span<element_type, dynamic_extent>
2016-07-26 22:19:47 -04:00
subspan(index_type offset, index_type count = dynamic_extent) const
2015-11-30 15:24:00 -05:00
{
2016-07-20 16:17:47 -04:00
return {span_.subspan(offset, count)};
2015-11-30 15:24:00 -05:00
}
constexpr reference operator[](index_type idx) const { return span_[idx]; }
constexpr reference operator()(index_type idx) const { return span_[idx]; }
2015-11-30 15:24:00 -05:00
constexpr pointer data() const { return span_.data(); }
2015-11-30 15:24:00 -05:00
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; }
2015-11-30 15:24:00 -05:00
constexpr iterator begin() const noexcept { return span_.begin(); }
constexpr iterator end() const noexcept { return span_.end(); }
2016-07-26 22:19:47 -04:00
constexpr const_iterator cbegin() const noexcept { return span_.cbegin(); }
constexpr const_iterator cend() const noexcept { return span_.cend(); }
2016-07-26 22:19:47 -04:00
constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); }
constexpr reverse_iterator rend() const noexcept { return span_.rend(); }
2015-11-30 15:24:00 -05:00
constexpr const_reverse_iterator crbegin() const noexcept { return span_.crbegin(); }
constexpr const_reverse_iterator crend() const noexcept { return span_.crend(); }
2015-11-30 15:24:00 -05:00
private:
2016-07-26 22:19:47 -04:00
static impl_type remove_z(pointer const& sz, std::ptrdiff_t max)
2015-11-24 02:05:31 -05:00
{
return {sz, details::string_length(sz, max)};
2015-11-24 02:05:31 -05:00
}
2017-02-13 15:11:45 -05:00
template <std::size_t N>
2016-07-26 22:19:47 -04:00
static impl_type remove_z(element_type (&sz)[N])
2015-11-24 02:05:31 -05:00
{
return remove_z(&sz[0], narrow_cast<std::ptrdiff_t>(N));
}
impl_type span_;
2015-11-30 15:24:00 -05:00
};
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
2015-11-30 15:24:00 -05:00
using string_span = basic_string_span<char, Extent>;
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
2015-11-30 15:24:00 -05:00
using cstring_span = basic_string_span<const char, Extent>;
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
2015-11-30 15:24:00 -05:00
using wstring_span = basic_string_span<wchar_t, Extent>;
2016-07-20 16:17:47 -04:00
template <std::ptrdiff_t Extent = dynamic_extent>
2015-11-30 15:24:00 -05:00
using cwstring_span = basic_string_span<const wchar_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u16string_span = basic_string_span<char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cu16string_span = basic_string_span<const char16_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using u32string_span = basic_string_span<char32_t, Extent>;
template <std::ptrdiff_t Extent = dynamic_extent>
using cu32string_span = basic_string_span<const char32_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
//
template <typename CharT, std::ptrdiff_t Extent>
2016-07-20 16:17:47 -04:00
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::ptrdiff_t Extent>
2016-09-04 02:29:10 -04:00
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())};
2016-09-04 02:29:10 -04:00
}
template <class ElementType, std::ptrdiff_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::ptrdiff_t Extent,
class = std::enable_if_t<!std::is_const<ElementType>::value>>
basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writeable_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
2016-07-20 16:17:47 -04:00
template <typename CharT, std::ptrdiff_t Extent = dynamic_extent>
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
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');
}
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
// move
2016-02-06 13:46:40 -05:00
constexpr basic_zstring_span(basic_zstring_span&& other) = default;
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
// move assign
2016-02-06 13:46:40 -05:00
constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
constexpr bool empty() const noexcept { return span_.size() == 0; }
2016-02-06 13:46:40 -05:00
constexpr string_span_type as_string_span() const noexcept
2016-07-20 16:17:47 -04:00
{
const auto sz = span_.size();
return {span_.data(), sz > 1 ? sz - 1 : 0};
2016-07-20 16:17:47 -04:00
}
constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); }
2016-02-06 13:46:40 -05:00
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_extent>
using zstring_span = basic_zstring_span<char, Max>;
2015-08-20 21:09:14 -04:00
template <std::ptrdiff_t Max = dynamic_extent>
using wzstring_span = basic_zstring_span<wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using u16zstring_span = basic_zstring_span<char16_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using u32zstring_span = basic_zstring_span<char32_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using czstring_span = basic_zstring_span<const char, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
template <std::ptrdiff_t Max = dynamic_extent>
using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
// operator ==
template <class CharT, std::ptrdiff_t Extent, class T,
2016-07-26 22:19:47 -04:00
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());
}
2016-07-26 22:19:47 -04:00
template <class CharT, std::ptrdiff_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)
2015-12-04 20:03:19 -05:00
{
const gsl::basic_string_span<std::add_const_t<CharT>> tmp(one);
2015-12-04 20:03:19 -05:00
return std::equal(tmp.begin(), tmp.end(), other.begin(), other.end());
}
// operator !=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2016-07-20 16:17:47 -04:00
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);
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
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)
2015-12-04 20:03:19 -05:00
{
return !(one == other);
}
// operator<
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2016-07-20 16:17:47 -04:00
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)
2015-11-30 15:24:00 -05:00
{
const 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
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
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());
}
2016-07-20 16:17:47 -04:00
#ifndef _MSC_VER
2015-12-04 20:03:19 -05:00
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
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
gsl::basic_string_span<std::add_const_t<CharT>, Extent> tmp(other);
return std::lexicographical_compare(one.begin(), one.end(), tmp.begin(), tmp.end());
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
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_extent, typename T,
2016-07-20 16:17:47 -04:00
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);
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
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)
2015-12-04 20:03:19 -05:00
{
return !(other < one);
}
2016-07-20 16:17:47 -04:00
#ifndef _MSC_VER
2015-12-04 20:03:19 -05:00
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
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
return !(other < one);
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
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_extent, typename T,
2016-07-20 16:17:47 -04:00
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;
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
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)
2015-11-30 15:24:00 -05:00
{
return other < one;
}
2016-07-20 16:17:47 -04:00
#ifndef _MSC_VER
2015-12-04 20:03:19 -05:00
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
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
return other < one;
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
return other < one;
}
#endif
// operator >=
template <typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2016-07-20 16:17:47 -04:00
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);
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
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)
2015-11-30 15:24:00 -05:00
{
return !(one < other);
}
2015-11-24 02:05:31 -05:00
2016-07-20 16:17:47 -04:00
#ifndef _MSC_VER
2015-12-04 20:03:19 -05:00
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
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
return !(one < other);
}
2016-07-20 16:17:47 -04:00
template <
typename CharT, std::ptrdiff_t Extent = gsl::dynamic_extent, typename T,
2015-12-04 20:03:19 -05:00
typename DataType = typename T::value_type,
typename = std::enable_if_t<
2016-07-20 16:17:47 -04:00
!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)
2015-12-04 20:03:19 -05:00
{
return !(one < other);
}
#endif
} // namespace gsl
2015-12-04 20:03:19 -05:00
Clang cl (#762) * Added c++17 test configurations for clang5.0 and clang6.0 * Fix #739 correct cppcorecheck warnings for clang-cl * Add clang-cl configurations * Corrections Appveyor; Temporarily disable msvc compilation for faster testing (#741) * Add path to clang-cl.exe (#741) * Escape backslash in path (#741) * Update vcpkg (#741) * Check vcpkg version; try without building vcpkg; use latest clang-cl from path (#741) * Fix blocks in ps script (#741) * Try accessing APPVEYOR_BUILD_FOLDER variable (#471) * Update span size() bug confirmation test for GCC 6.5 (#741) * MSVC flags to Clang-cl; disable c++98-compat and undefined macro warnings (#741) * Suppress clang warning on tests (missing-prototypes) (#741) * Fix clang warning -Wnewline-eof (#741) * Fix clang warning -Wdouble-promotion (#741) * Set linker explicitly * Clean condition statement * For Clang, fallback to the AppVeyor installed version of CMake * Fix clang warning -Wmissing-variable-declarations * Fallback to the MSVC linker until vcpkg has been updated * Revert "Fallback to the MSVC linker until vcpkg has been updated" This reverts commit 7263f3289e6e835c32fc7800d0c2d4e58bd21178. * Fix clang warning -Wunused-member-function * Fix clang warning -Wmissing-noreturn * Fix clang warning -Winvalid-noreturn on Windows * Add macro block end comment on large #if blocks * Workaround: fallback to mscv link.exe * Workaround: get msvc paths into PowerShell through intermediate file * Workaround: fix, remove "PATH=" from text * Workaround: try with full-path; and return user PATH * Workaround: fix, escape backslashes * Revert all since "Workaround: fallback to mscv link.exe" did not work on AppVeyor This reverts the commits: bda3d6a428e5d19e97375b2e575b0f51ff1b4bc0 97062933acfb6428535c0fdcab5d94371ce72bfc 0f4fb04bac9c2d091005b791294237250dbe0668 1b0c19afd154f4ffc5ef793014c1bc2324534fd0 a5739ea5f0cd7633527b5e5d3b16a9e3ade7149f * Suppress output of git pull; remove vcpkg from cache * Re-enable AppVeyor builds for all platforms * Correct typo Co-Authored-By: Farwaykorse <Farwaykorse@users.noreply.github.com> * Add Clang-cl 7.0.0 to the supported platforms * Revert "Fix clang warning -Wunused-member-function" This reverts commit 6fe1a4203501145ab4350b9152ecc11f3a30e49a. * Fix or locally suppress clang warning -Wunused-member-function * format touched code and correct comment * git pull --quiet * fix logic error in workaround * fix missing bracket * Suppress output of mkdir * Replace MSBuild with Ninja * Suppress output of 7z * Add architecture flags for Clang * Drop workaround for lld-link * 7-zip Overwrite and Alternative output suppression without suppressing errors Replaces 3c1c0794dd9a29908cc7c38f591bd3c7b4929677 * AppVeyor setup and CMake before build * reorder compiler configuration * remove unnecessary * remove -fno-strict-aliasing * remove -Wsign-conversion, since before v4.0 part of -Wconversion * -Wctor-dtor-privacy is GCC only * remove -Woverloaded-virtual part of -Wmost, part of -Wall * add -Wmissing-noreturn * remove the pragmas for -Wunused-member-function * Re-add MSBuild generator on AppVeyor * Print CMake commands * Add MSBuild toolset selection * Separate Architecture setting * clang-cl: add -Weverything * clang-cl -Wno-c++98-compat * clang-cl -Wno-c++98-compat-pedantic * clang-cl -Wno-missing-prototypes * clang-cl C++14 -Wno-unused-member-function * clang-cl -Wundef __GNUC__ * clang++: add -Weverything * clang++ -Wno-c++98-compat * clang++ -Wno-c++98-compat-pedantic * clang++ -Wno-missing-prototypes * clang++ -Wno-weak-vtables * clang++ C++14 -Wno-unused-member-function * clang++ fix -Wundef _MSC_VER * clang++ -Wno-padded * clang++ solve -Wdeprecated * Add AppleClang compiler target Since CMake v3.0 use of Clang for both is deprecated * clang++ v5.0 C++17 -Wno-undefined-func-template * Add VS2015 + LLVM/clang-cl to AppVeyor * Do not disable constexpr when compiling with clang-cl on Windows * Clean-up clang-only warnings (now under -Weverything) * Revert "Fix clang warning -Winvalid-noreturn on Windows" This reverts commit 2238c4760e86feebb2a18620b77312bd01055f61. * Suppress -Winvalid-noreturn for the MS STL noexception workaround * CMake: put preprocessor definition in target_compile_definitions * Solve compiler warning C4668: __GNUC__ not defined
2019-01-15 13:27:34 -05:00
#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
2015-11-30 15:24:00 -05:00
#endif // GSL_STRING_SPAN_H