From c016bdc77f28891cfc42fc219bd5365e89d3ebda Mon Sep 17 00:00:00 2001 From: Werner Henze <34543625+beinhaerter@users.noreply.github.com> Date: Tue, 27 Dec 2022 16:22:26 +0100 Subject: [PATCH] remove all deprecated string_span stuff (#1074) This solves issue #1070 and removes the class `string_span`. The only content remaining in the header file `gsl/string_span` is the `*zstring` types. This also removes the `string_span_tests.cpp` unit tests as these were only for the deprecated `string_span` class. Co-authored-by: Werner Henze --- include/gsl/string_span | 703 +------------------- tests/CMakeLists.txt | 2 +- tests/string_span_tests.cpp | 1225 ----------------------------------- 3 files changed, 2 insertions(+), 1928 deletions(-) delete mode 100644 tests/string_span_tests.cpp diff --git a/include/gsl/string_span b/include/gsl/string_span index 658cf84..6daf546 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -17,31 +17,9 @@ #ifndef GSL_STRING_SPAN_H #define GSL_STRING_SPAN_H -#include "assert" // for Ensures, Expects -#include "span_ext" // for operator!=, operator==, dynamic_extent -#include "util" // for narrow_cast +#include "span_ext" // for dynamic_extent -#include // for equal, lexicographical_compare -#include // for array #include // for 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 -#pragma warning(disable : 4996) // use of functions & classes marked [[deprecated]] -#endif // _MSC_VER - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" -#endif namespace gsl { @@ -75,685 +53,6 @@ using u16zstring = basic_zstring; using u32zstring = basic_zstring; -namespace details -{ - template - [[deprecated("string_span was removed from the C++ Core Guidelines. For more information, see " - "isocpp/CppCoreGuidelines PR#1680")]] constexpr std::size_t - string_length(const CharT* str, std::size_t n) - { - if (str == nullptr || n == dynamic_extent) return 0; - - const span str_span{str, n}; - - std::size_t len = 0; - while (len < n && str_span[len]) len++; - - return len; - } -} // namespace details - -// -// ensure_sentinel() -// -// Provides a way to obtain an span from a contiguous sequence -// that ends with a (non-inclusive) sentinel value. -// -// Will fail-fast if sentinel cannot be found before max elements are examined. -// -template -[[deprecated("string_span was removed from the C++ Core Guidelines. For more information, see " - "isocpp/CppCoreGuidelines PR#1680")]] constexpr span -ensure_sentinel(T* seq, std::size_t max = static_cast(-1)) -{ - Ensures(seq != nullptr); - - // clang-format off - GSL_SUPPRESS(f.23) // TODO: false positive // TODO: suppress does not work - // clang-format on - auto cur = seq; - Ensures(cur != nullptr); // workaround for removing the warning - - // clang-format off - GSL_SUPPRESS(bounds.1) // TODO: suppress does not work - // clang-format on - while (static_cast(cur - seq) < max && *cur != Sentinel) ++cur; - Ensures(*cur == Sentinel); - return {seq, static_cast(cur - seq)}; -} - -// -// ensure_z - creates a span for a zero terminated strings. The span will not contain the zero -// termination. Will fail fast if a null-terminator cannot be found before the limit of size_type. -// -template -[[deprecated("string_span was removed from the C++ Core Guidelines. For more information, see " - "isocpp/CppCoreGuidelines PR#1680")]] constexpr span -ensure_z(CharT* const& sz, std::size_t max = static_cast(-1)) -{ - return ensure_sentinel(sz, max); -} - -template -constexpr span ensure_z(CharT (&sz)[N]) -{ - return ensure_z(&sz[0], N); -} - -template -[[deprecated( - "string_span was removed from the C++ Core Guidelines. For more information, see " - "isocpp/CppCoreGuidelines PR#1680")]] constexpr span::type, - dynamic_extent> -ensure_z(Cont& cont) -{ - return ensure_z(cont.data(), cont.size()); -} - -template -class [[deprecated("string_span was removed from the C++ Core Guidelines. For more information, " - "see isocpp/CppCoreGuidelines PR#1680")]] basic_string_span; - -namespace details -{ - template - struct [[deprecated( - "string_span was removed from the C++ Core Guidelines. For more information, " - "see isocpp/CppCoreGuidelines PR#1680")]] is_basic_string_span_oracle : std::false_type{}; - - template - struct [[deprecated( - "string_span was removed from the C++ Core Guidelines. For more information, see " - "isocpp/CppCoreGuidelines PR#1680")]] is_basic_string_span_oracle> - : std::true_type{}; - - template - struct [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] is_basic_string_span - : is_basic_string_span_oracle>{}; -} // namespace details - -// -// string_span and relatives -// -template -class [[deprecated("string_span was removed from the C++ Core Guidelines. For more information, " - "see isocpp/CppCoreGuidelines PR#1680")]] basic_string_span -{ -public: - using element_type = CharT; - using value_type = std::remove_cv_t; - 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 size_type = typename impl_type::size_type; - using iterator = typename impl_type::iterator; - using reverse_iterator = typename impl_type::reverse_iterator; - - // default (empty) - constexpr basic_string_span() noexcept = default; - - // copy - constexpr basic_string_span(const basic_string_span& other) noexcept = default; - - // assign - constexpr basic_string_span& operator=(const basic_string_span& other) noexcept = default; - - constexpr basic_string_span(pointer ptr, size_type length) : span_(ptr, length) {} - constexpr basic_string_span(pointer firstElem, pointer lastElem) : span_(firstElem, lastElem) {} - - // From static arrays - if 0-terminated, remove 0 from the view - // All other containers allow 0s within the length, so we do not remove them - template - 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) // TODO: parser bug - constexpr basic_string_span(std::basic_string & str) - : span_(&str[0], 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::size_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(size_type count) const - { - return {span_.first(count)}; - } - - template - constexpr basic_string_span last() const - { - return {span_.template last()}; - } - - constexpr basic_string_span last(size_type count) const - { - return {span_.last(count)}; - } - - template - constexpr basic_string_span subspan() const - { - return {span_.template subspan()}; - } - - constexpr basic_string_span subspan( - size_type offset, size_type count = dynamic_extent) const - { - return {span_.subspan(offset, count)}; - } - - constexpr reference operator[](size_type idx) const { return span_[idx]; } - constexpr reference operator()(size_type idx) const { return span_[idx]; } - - constexpr pointer data() const { return span_.data(); } - - constexpr size_type length() const noexcept { return span_.size(); } - constexpr size_type size() const noexcept { return span_.size(); } - constexpr size_type size_bytes() const noexcept { return span_.size_bytes(); } - constexpr size_type length_bytes() const noexcept { return span_.length_bytes(); } - constexpr bool empty() const noexcept { return size() == 0; } - - constexpr iterator begin() const noexcept { return span_.begin(); } - constexpr iterator end() const noexcept { return span_.end(); } - - constexpr reverse_iterator rbegin() const noexcept { return span_.rbegin(); } - constexpr reverse_iterator rend() const noexcept { return span_.rend(); } - -private: - static constexpr impl_type remove_z(pointer const& sz, std::size_t max) - { - return impl_type(sz, details::string_length(sz, max)); - } - - template - static constexpr impl_type remove_z(element_type(&sz)[N]) - { - return remove_z(&sz[0], N); - } - - impl_type span_; -}; - -template -using string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using cstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using wstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using cwstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using u16string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using cu16string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using u32string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -template -using cu32string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_string_span; - -// -// to_string() allow (explicit) conversions from string_span to string -// - -template -constexpr 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::size_t Extent> -constexpr std::basic_string -to_basic_string(basic_string_span view) -{ - return {view.data(), narrow_cast(view.length())}; -} - -template -constexpr basic_string_span::value> -as_bytes(basic_string_span s) noexcept -{ - // clang-format off - GSL_SUPPRESS(type.1) - // clang-format on - return {reinterpret_cast(s.data()), s.size_bytes()}; -} - -template ::value>> -constexpr basic_string_span::value> -as_writable_bytes(basic_string_span s) noexcept -{ - // clang-format off - GSL_SUPPRESS(type.1) - // clang-format on - return {reinterpret_cast(s.data()), s.size_bytes()}; -} - -// zero-terminated string span, used to convert -// zero-terminated spans to legacy strings -template -class [[deprecated("string_span was removed from the C++ Core Guidelines. For more information, " - "see isocpp/CppCoreGuidelines PR#1680")]] 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.size() > 0); - Expects(s[s.size() - 1] == value_type{}); - } - - // copy - constexpr basic_zstring_span(const basic_zstring_span& other) = default; - - // move - constexpr basic_zstring_span(basic_zstring_span && other) noexcept = default; - - // assign - constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default; - - // move assign - constexpr basic_zstring_span& operator=(basic_zstring_span&& other) noexcept = default; - - constexpr bool empty() const noexcept { return false; } - - constexpr string_span_type as_string_span() const noexcept - { - return {span_.data(), span_.size() - 1}; - } - 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 [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using wzstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using u16zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using u32zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using czstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using cwzstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more " - "information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using cu16zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For " - "more information, see isocpp/CppCoreGuidelines PR#1680")]] = - basic_zstring_span; - -template -using cu32zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For " - "more information, see isocpp/CppCoreGuidelines PR#1680")]] = - 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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::size_t Extent = 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) - -#endif // _MSC_VER - -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif #endif // GSL_STRING_SPAN_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 59a8250..657bde8 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -200,7 +200,7 @@ add_executable(gsl_tests span_ext_tests.cpp span_tests.cpp strict_notnull_tests.cpp - string_span_tests.cpp + utils_tests.cpp ) diff --git a/tests/string_span_tests.cpp b/tests/string_span_tests.cpp deleted file mode 100644 index 45a67d9..0000000 --- a/tests/string_span_tests.cpp +++ /dev/null @@ -1,1225 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// -// Copyright (c) 2015 Microsoft Corporation. All rights reserved. -// -// This code is licensed under the MIT License (MIT). -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -// -/////////////////////////////////////////////////////////////////////////////// - -#include - -#include // for Expects, fail_fast (ptr only) -#include // for owner -#include // for span, dynamic_extent -#include // for basic_string_span, operator==, ensure_z - -#include // for move, find -#include // for size_t -#include // for map -#include // for basic_string, string, char_traits, operat... -#include // for remove_reference<>::type -#include // for vector, allocator - -#include "deathTestCommon.h" - -using namespace gsl; - -// Generic string functions - -namespace generic -{ - -template -auto strlen(const CharT* s) -{ - auto p = s; - while (*p) ++p; - return p - s; -} - -template -auto strnlen(const CharT* s, std::size_t n) -{ - return std::find(s, s + n, CharT{0}) - s; -} - -} // namespace generic - -namespace -{ - -template -T move_wrapper(T&& t) -{ - return std::move(t); -} - -// not used otherwise -#ifdef CONFIRM_COMPILATION_ERRORS - -template -T create() -{ - return T{}; -} - -template -void use(basic_string_span) -{} -#endif - -czstring_span<> CreateTempName(string_span<> span) -{ - Expects(span.size() > 1); - - std::size_t last = 0; - if (span.size() > 4) - { - span[0] = 't'; - span[1] = 'm'; - span[2] = 'p'; - last = 3; - } - span[last] = '\0'; - - auto ret = span.subspan(0, 4); - return {ret}; -} - -cwzstring_span<> CreateTempNameW(wstring_span<> span) -{ - Expects(span.size() > 1); - - std::size_t last = 0; - if (span.size() > 4) - { - span[0] = L't'; - span[1] = L'm'; - span[2] = L'p'; - last = 3; - } - span[last] = L'\0'; - - auto ret = span.subspan(0, 4); - return {ret}; -} - -cu16zstring_span<> CreateTempNameU16(u16string_span<> span) -{ - Expects(span.size() > 1); - - std::size_t last = 0; - if (span.size() > 4) - { - span[0] = u't'; - span[1] = u'm'; - span[2] = u'p'; - last = 3; - } - span[last] = u'\0'; - - auto ret = span.subspan(0, 4); - return {ret}; -} - -cu32zstring_span<> CreateTempNameU32(u32string_span<> span) -{ - Expects(span.size() > 1); - - std::size_t last = 0; - if (span.size() > 4) - { - span[0] = U't'; - span[1] = U'm'; - span[2] = U'p'; - last = 3; - } - span[last] = U'\0'; - - auto ret = span.subspan(0, 4); - return {ret}; -} -} // namespace - -TEST(string_span_tests, TestLiteralConstruction) -{ - cwstring_span<> v = ensure_z(L"Hello"); - EXPECT_TRUE(5 == v.length()); - -#ifdef CONFIRM_COMPILATION_ERRORS - wstring_span<> v2 = ensure0(L"Hello"); -#endif -} - -TEST(string_span_tests, TestConstructFromStdString) -{ - std::string s = "Hello there world"; - cstring_span<> v = s; - EXPECT_TRUE(v.length() == static_cast::size_type>(s.length())); -} - -TEST(string_span_tests, TestConstructFromStdVector) -{ - std::vector vec(5, 'h'); - string_span<> v{vec}; - EXPECT_TRUE(v.length() == static_cast::size_type>(vec.size())); -} - -TEST(string_span_tests, TestStackArrayConstruction) -{ - wchar_t stack_string[] = L"Hello"; - - { - cwstring_span<> v = ensure_z(stack_string); - EXPECT_TRUE(v.length() == 5); - } - - { - cwstring_span<> v = stack_string; - EXPECT_TRUE(v.length() == 5); - } - - { - wstring_span<> v = ensure_z(stack_string); - EXPECT_TRUE(v.length() == 5); - } - - { - wstring_span<> v = stack_string; - EXPECT_TRUE(v.length() == 5); - } -} - -TEST(string_span_tests, TestConstructFromConstCharPointer) -{ - const char* s = "Hello"; - cstring_span<> v = ensure_z(s); - EXPECT_TRUE(v.length() == 5); -} - -TEST(string_span_tests, TestConversionToConst) -{ - char stack_string[] = "Hello"; - string_span<> v = ensure_z(stack_string); - cstring_span<> v2 = v; - EXPECT_TRUE(v.length() == v2.length()); -} - -TEST(string_span_tests, TestConversionFromConst) -{ - char stack_string[] = "Hello"; - cstring_span<> v = ensure_z(stack_string); - (void) v; -#ifdef CONFIRM_COMPILATION_ERRORS - string_span<> v2 = v; - string_span<> v3 = "Hello"; -#endif -} - -TEST(string_span_tests, TestToString) -{ - auto s = gsl::to_string(cstring_span<>{}); - EXPECT_TRUE(s.length() == static_cast(0)); - - char stack_string[] = "Hello"; - cstring_span<> v = ensure_z(stack_string); - auto s2 = gsl::to_string(v); - EXPECT_TRUE(static_cast::size_type>(s2.length()) == v.length()); - EXPECT_TRUE(s2.length() == static_cast(5)); -} - -TEST(string_span_tests, TestToBasicString) -{ - auto s = gsl::to_basic_string, ::std::allocator>( - cstring_span<>{}); - EXPECT_TRUE(s.length() == static_cast(0)); - - char stack_string[] = "Hello"; - cstring_span<> v = ensure_z(stack_string); - auto s2 = gsl::to_basic_string, ::std::allocator>(v); - EXPECT_TRUE(static_cast::size_type>(s2.length()) == v.length()); - EXPECT_TRUE(s2.length() == static_cast(5)); -} - -TEST(string_span_tests, EqualityAndImplicitConstructors) -{ - { - cstring_span<> span = "Hello"; - cstring_span<> span1; - - // comparison to empty span - EXPECT_TRUE(span1 != span); - EXPECT_TRUE(span != span1); - } - - { - cstring_span<> span = "Hello"; - cstring_span<> span1 = "Hello1"; - - // comparison to different span - EXPECT_TRUE(span1 != span); - EXPECT_TRUE(span != span1); - } - - { - cstring_span<> span = "Hello"; - - const char ar[] = {'H', 'e', 'l', 'l', 'o'}; - const char ar1[] = "Hello"; - const char ar2[10] = "Hello"; - const char* ptr = "Hello"; - const std::string str = "Hello"; - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - gsl::span sp = ensure_z("Hello"); - - // comparison to literal - EXPECT_TRUE(span == cstring_span<>("Hello")); - - // comparison to static array with no null termination - EXPECT_TRUE(span == cstring_span<>(ar)); - - // comparison to static array with null at the end - EXPECT_TRUE(span == cstring_span<>(ar1)); - - // comparison to static array with null in the middle - EXPECT_TRUE(span == cstring_span<>(ar2)); - - // comparison to null-terminated c string - EXPECT_TRUE(span == cstring_span<>(ptr, 5)); - - // comparison to string - EXPECT_TRUE(span == cstring_span<>(str)); - - // comparison to vector of charaters with no null termination - EXPECT_TRUE(span == cstring_span<>(vec)); - - // comparison to span - EXPECT_TRUE(span == cstring_span<>(sp)); - - // comparison to string_span - EXPECT_TRUE(span == span); - } - - { - char ar[] = {'H', 'e', 'l', 'l', 'o'}; - - string_span<> span = ar; - - char ar1[] = "Hello"; - char ar2[10] = "Hello"; - char* ptr = ar; - std::string str = "Hello"; - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - gsl::span sp = ensure_z(ar1); - - // comparison to static array with no null termination - EXPECT_TRUE(span == string_span<>(ar)); - - // comparison to static array with null at the end - EXPECT_TRUE(span == string_span<>(ar1)); - - // comparison to static array with null in the middle - EXPECT_TRUE(span == string_span<>(ar2)); - - // comparison to null-terminated c string - EXPECT_TRUE(span == string_span<>(ptr, 5)); - - // comparison to string - EXPECT_TRUE(span == string_span<>(str)); - - // comparison to vector of charaters with no null termination - EXPECT_TRUE(span == string_span<>(vec)); - - // comparison to span - EXPECT_TRUE(span == string_span<>(sp)); - - // comparison to string_span - EXPECT_TRUE(span == span); - } - - { - const char ar[] = {'H', 'e', 'l', 'l', 'o'}; - const char ar1[] = "Hello"; - const char ar2[10] = "Hello"; - const std::string str = "Hello"; - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const gsl::span sp = ensure_z("Hello"); - - cstring_span<> span = "Hello"; - - // const span, const other type - - EXPECT_TRUE(span == "Hello"); - EXPECT_TRUE(span == ar); - EXPECT_TRUE(span == ar1); - EXPECT_TRUE(span == ar2); -#ifdef CONFIRM_COMPILATION_ERRORS - const char* ptr = "Hello"; - EXPECT_TRUE(span == ptr); -#endif - EXPECT_TRUE(span == str); - EXPECT_TRUE(span == vec); - EXPECT_TRUE(span == sp); - - EXPECT_TRUE("Hello" == span); - EXPECT_TRUE(ar == span); - EXPECT_TRUE(ar1 == span); - EXPECT_TRUE(ar2 == span); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(ptr == span); -#endif - EXPECT_TRUE(str == span); - EXPECT_TRUE(vec == span); - EXPECT_TRUE(sp == span); - - // const span, non-const other type - - char _ar[] = {'H', 'e', 'l', 'l', 'o'}; - char _ar1[] = "Hello"; - char _ar2[10] = "Hello"; - char* _ptr = _ar; - std::string _str = "Hello"; - std::vector _vec = {'H', 'e', 'l', 'l', 'o'}; - gsl::span _sp{_ar, 5}; - - EXPECT_TRUE(span == _ar); - EXPECT_TRUE(span == _ar1); - EXPECT_TRUE(span == _ar2); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(span == _ptr); -#endif - EXPECT_TRUE(span == _str); - EXPECT_TRUE(span == _vec); - EXPECT_TRUE(span == _sp); - - EXPECT_TRUE(_ar == span); - EXPECT_TRUE(_ar1 == span); - EXPECT_TRUE(_ar2 == span); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(_ptr == span); -#endif - EXPECT_TRUE(_str == span); - EXPECT_TRUE(_vec == span); - EXPECT_TRUE(_sp == span); - - string_span<> _span{_ptr, 5}; - - // non-const span, non-const other type - - EXPECT_TRUE(_span == _ar); - EXPECT_TRUE(_span == _ar1); - EXPECT_TRUE(_span == _ar2); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(_span == _ptr); -#endif - EXPECT_TRUE(_span == _str); - EXPECT_TRUE(_span == _vec); - EXPECT_TRUE(_span == _sp); - - EXPECT_TRUE(_ar == _span); - EXPECT_TRUE(_ar1 == _span); - EXPECT_TRUE(_ar2 == _span); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(_ptr == _span); -#endif - EXPECT_TRUE(_str == _span); - EXPECT_TRUE(_vec == _span); - EXPECT_TRUE(_sp == _span); - - // non-const span, const other type - - EXPECT_TRUE(_span == "Hello"); - EXPECT_TRUE(_span == ar); - EXPECT_TRUE(_span == ar1); - EXPECT_TRUE(_span == ar2); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(_span == ptr); -#endif - EXPECT_TRUE(_span == str); - EXPECT_TRUE(_span == vec); - EXPECT_TRUE(_span == sp); - - EXPECT_TRUE("Hello" == _span); - EXPECT_TRUE(ar == _span); - EXPECT_TRUE(ar1 == _span); - EXPECT_TRUE(ar2 == _span); -#ifdef CONFIRM_COMPILATION_ERRORS - EXPECT_TRUE(ptr == _span); -#endif - EXPECT_TRUE(str == _span); - EXPECT_TRUE(vec == _span); - EXPECT_TRUE(sp == _span); - - // two spans - - EXPECT_TRUE(_span == span); - EXPECT_TRUE(span == _span); - } - - { - std::vector str1 = {'H', 'e', 'l', 'l', 'o'}; - cstring_span<> span1 = str1; - std::vector str2 = std::move(str1); - cstring_span<> span2 = str2; - - // comparison of spans from the same vector before and after move (ok) - EXPECT_TRUE(span1 == span2); - } -} - -TEST(string_span_tests, ComparisonAndImplicitConstructors) -{ - { - cstring_span<> span = "Hello"; - - const char ar[] = {'H', 'e', 'l', 'l', 'o'}; - const char ar1[] = "Hello"; - const char ar2[10] = "Hello"; - const char* ptr = "Hello"; - const std::string str = "Hello"; - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - - // comparison to literal - EXPECT_TRUE(span < cstring_span<>("Helloo")); - EXPECT_TRUE(span > cstring_span<>("Hell")); - - // comparison to static array with no null termination - EXPECT_TRUE(span >= cstring_span<>(ar)); - - // comparison to static array with null at the end - EXPECT_TRUE(span <= cstring_span<>(ar1)); - - // comparison to static array with null in the middle - EXPECT_TRUE(span >= cstring_span<>(ar2)); - - // comparison to null-terminated c string - EXPECT_TRUE(span <= cstring_span<>(ptr, 5)); - - // comparison to string - EXPECT_TRUE(span >= cstring_span<>(str)); - - // comparison to vector of charaters with no null termination - EXPECT_TRUE(span <= cstring_span<>(vec)); - } - - { - char ar[] = {'H', 'e', 'l', 'l', 'o'}; - - string_span<> span = ar; - - char larr[] = "Hell"; - char rarr[] = "Helloo"; - - char ar1[] = "Hello"; - char ar2[10] = "Hello"; - char* ptr = ar; - std::string str = "Hello"; - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - - // comparison to static array with no null termination - EXPECT_TRUE(span <= string_span<>(ar)); - EXPECT_TRUE(span < string_span<>(rarr)); - EXPECT_TRUE(span > string_span<>(larr)); - - // comparison to static array with null at the end - EXPECT_TRUE(span >= string_span<>(ar1)); - - // comparison to static array with null in the middle - EXPECT_TRUE(span <= string_span<>(ar2)); - - // comparison to null-terminated c string - EXPECT_TRUE(span >= string_span<>(ptr, 5)); - - // comparison to string - EXPECT_TRUE(span <= string_span<>(str)); - - // comparison to vector of charaters with no null termination - EXPECT_TRUE(span >= string_span<>(vec)); - } -} - -TEST(string_span_tests, ConstrutorsEnsureZ) -{ - // remove z from literals - { - cstring_span<> sp = "hello"; - EXPECT_TRUE(sp.length() == 5); - } - - // take the string as is - { - auto str = std::string("hello"); - cstring_span<> sp = str; - EXPECT_TRUE(sp.length() == 5); - } - - // ensure z on c strings - { - gsl::owner ptr = new char[3]; - - ptr[0] = 'a'; - ptr[1] = 'b'; - ptr[2] = '\0'; - - string_span<> span = ensure_z(ptr); - EXPECT_TRUE(span.length() == 2); - - delete[] ptr; - } -} - -TEST(string_span_tests, Constructors) -{ - // creating cstring_span - - // from span of a final extent - { - span sp = "Hello"; - cstring_span<> span = sp; - EXPECT_TRUE(span.length() == 6); - } - -// from const span of a final extent to non-const string_span -#ifdef CONFIRM_COMPILATION_ERRORS - { - span sp = "Hello"; - string_span<> span = sp; - EXPECT_TRUE(span.length() == 6); - } -#endif - -// from string temporary -#ifdef CONFIRM_COMPILATION_ERRORS - { - cstring_span<> span = std::string("Hello"); - } -#endif - - // default - { - cstring_span<> span; - EXPECT_TRUE(span.length() == 0); - } - - // from string literal - { - cstring_span<> span = "Hello"; - EXPECT_TRUE(span.length() == 5); - } - - // from const static array - { - const char ar[] = {'H', 'e', 'l', 'l', 'o'}; - cstring_span<> span = ar; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const static array - { - char ar[] = {'H', 'e', 'l', 'l', 'o'}; - cstring_span<> span = ar; - EXPECT_TRUE(span.length() == 5); - } - - // from const ptr and length - { - const char* ptr = "Hello"; - cstring_span<> span{ptr, 5}; - EXPECT_TRUE(span.length() == 5); - } - - // from const ptr and length, include 0 - { - const char* ptr = "Hello"; - cstring_span<> span{ptr, 6}; - EXPECT_TRUE(span.length() == 6); - } - - // from const ptr and length, 0 inside - { - const char* ptr = "He\0lo"; - cstring_span<> span{ptr, 5}; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const ptr and length - { - char ar[] = {'H', 'e', 'l', 'l', 'o'}; - char* ptr = ar; - cstring_span<> span{ptr, 5}; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const ptr and length, 0 inside - { - char ar[] = {'H', 'e', '\0', 'l', 'o'}; - char* ptr = ar; - cstring_span<> span{ptr, 5}; - EXPECT_TRUE(span.length() == 5); - } - - // from const string - { - const std::string str = "Hello"; - const cstring_span<> span = str; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const string - { - std::string str = "Hello"; - const cstring_span<> span = str; - EXPECT_TRUE(span.length() == 5); - } - - // from const vector - { - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const cstring_span<> span = vec; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const vector - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const cstring_span<> span = vec; - EXPECT_TRUE(span.length() == 5); - } - - // from const span - { - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const span inner = vec; - const cstring_span<> span = inner; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const span - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const span inner = vec; - const cstring_span<> span = inner; - EXPECT_TRUE(span.length() == 5); - } - - // from const string_span - { - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const cstring_span<> tmp = vec; - const cstring_span<> span = tmp; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const string_span - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - string_span<> tmp = vec; - cstring_span<> span = tmp; - EXPECT_TRUE(span.length() == 5); - } - - // creating string_span - - // from string literal - { -#ifdef CONFIRM_COMPILATION_ERRORS - string_span<> span = "Hello"; -#endif - } - - // from const static array - { -#ifdef CONFIRM_COMPILATION_ERRORS - const char ar[] = {'H', 'e', 'l', 'l', 'o'}; - string_span<> span = ar; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from non-const static array - { - char ar[] = {'H', 'e', 'l', 'l', 'o'}; - string_span<> span = ar; - EXPECT_TRUE(span.length() == 5); - } - - // from const ptr and length - { -#ifdef CONFIRM_COMPILATION_ERRORS - const char* ptr = "Hello"; - string_span<> span{ptr, 5}; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from non-const ptr and length - { - char ar[] = {'H', 'e', 'l', 'l', 'o'}; - char* ptr = ar; - string_span<> span{ptr, 5}; - EXPECT_TRUE(span.length() == 5); - } - - // from const string - { -#ifdef CONFIRM_COMPILATION_ERRORS - const std::string str = "Hello"; - string_span<> span = str; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from non-const string - { - std::string str = "Hello"; - string_span<> span = str; - EXPECT_TRUE(span.length() == 5); - } - - // from const vector - { -#ifdef CONFIRM_COMPILATION_ERRORS - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - string_span<> span = vec; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from non-const vector - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - string_span<> span = vec; - EXPECT_TRUE(span.length() == 5); - } - - // from const span - { -#ifdef CONFIRM_COMPILATION_ERRORS - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const span inner = vec; - string_span<> span = inner; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from non-const span - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - span inner = vec; - string_span<> span = inner; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const span of non-const data from const vector - { -#ifdef CONFIRM_COMPILATION_ERRORS - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const span inner = vec; - string_span<> span = inner; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from const string_span - { -#ifdef CONFIRM_COMPILATION_ERRORS - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - cstring_span<> tmp = vec; - string_span<> span = tmp; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from non-const string_span - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const string_span<> tmp = vec; - const string_span<> span = tmp; - EXPECT_TRUE(span.length() == 5); - } - - // from non-const string_span from const vector - { -#ifdef CONFIRM_COMPILATION_ERRORS - const std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - string_span<> tmp = vec; - string_span<> span = tmp; - EXPECT_TRUE(span.length() == 5); -#endif - } - - // from const string_span of non-const data - { - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - const string_span<> tmp = vec; - const string_span<> span = tmp; - EXPECT_TRUE(span.length() == 5); - } -} - -TEST(string_span_tests, MoveConstructors) -{ - // move string_span - { - cstring_span<> span = "Hello"; - const auto span1 = std::move(span); - EXPECT_TRUE(span1.length() == 5); - } - { - cstring_span<> span = "Hello"; - const auto span1 = move_wrapper(std::move(span)); - EXPECT_TRUE(span1.length() == 5); - } - { - cstring_span<> span = "Hello"; - const auto span1 = move_wrapper(std::move(span)); - EXPECT_TRUE(span1.length() == 5); - } - - // move span - { - span span = ensure_z("Hello"); - const cstring_span<> span1 = std::move(span); - EXPECT_TRUE(span1.length() == 5); - } - { - span span = ensure_z("Hello"); - const cstring_span<> span2 = move_wrapper(std::move(span)); - EXPECT_TRUE(span2.length() == 5); - } - - // move string - { -#ifdef CONFIRM_COMPILATION_ERRORS - std::string str = "Hello"; - string_span<> span = std::move(str); - EXPECT_TRUE(span.length() == 5); -#endif - } - { -#ifdef CONFIRM_COMPILATION_ERRORS - std::string str = "Hello"; - string_span<> span = move_wrapper(std::move(str)); - EXPECT_TRUE(span.length() == 5); -#endif - } - { -#ifdef CONFIRM_COMPILATION_ERRORS - use(create()); -#endif - } - - // move container - { -#ifdef CONFIRM_COMPILATION_ERRORS - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - string_span<> span = std::move(vec); - EXPECT_TRUE(span.length() == 5); -#endif - } - { -#ifdef CONFIRM_COMPILATION_ERRORS - std::vector vec = {'H', 'e', 'l', 'l', 'o'}; - string_span<> span = move_wrapper>(std::move(vec)); - EXPECT_TRUE(span.length() == 5); -#endif - } - { -#ifdef CONFIRM_COMPILATION_ERRORS - use(create>()); -#endif - } -} - -TEST(string_span_tests, Conversion) -{ -#ifdef CONFIRM_COMPILATION_ERRORS - cstring_span<> span = "Hello"; - cwstring_span<> wspan{span}; - EXPECT_TRUE(wspan.length() == 5); -#endif -} - -TEST(string_span_tests, zstring) -{ - const auto terminateHandler = std::set_terminate([] { - std::cerr << "Expected Death. zstring"; - std::abort(); - }); - const auto expected = GetExpectedDeathString(terminateHandler); - - // create zspan from zero terminated string - { - char buf[1]; - buf[0] = '\0'; - - zstring_span<> zspan({buf, 1}); - - EXPECT_TRUE(generic::strlen(zspan.assume_z()) == 0); - EXPECT_TRUE(zspan.as_string_span().size() == 0); - EXPECT_TRUE(zspan.ensure_z().size() == 0); - } - - // create zspan from non-zero terminated string - { - char buf[1]; - buf[0] = 'a'; - - auto workaround_macro = [&]() { const zstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), expected); - } - - // usage scenario: create zero-terminated temp file name and pass to a legacy API - { - char buf[10]; - - auto name = CreateTempName({buf, 10}); - if (!name.empty()) - { - czstring str = name.assume_z(); - EXPECT_TRUE(generic::strlen(str) == 3); - EXPECT_TRUE(*(str + 3) == '\0'); - } - } -} - -TEST(string_span_tests, wzstring) -{ - const auto terminateHandler = std::set_terminate([] { - std::cerr << "Expected Death. wzstring"; - std::abort(); - }); - const auto expected = GetExpectedDeathString(terminateHandler); - - // create zspan from zero terminated string - { - wchar_t buf[1]; - buf[0] = L'\0'; - - wzstring_span<> zspan({buf, 1}); - - EXPECT_TRUE(generic::strnlen(zspan.assume_z(), 1) == 0); - EXPECT_TRUE(zspan.as_string_span().size() == 0); - EXPECT_TRUE(zspan.ensure_z().size() == 0); - } - - // create zspan from non-zero terminated string - { - wchar_t buf[1]; - buf[0] = L'a'; - - const auto workaround_macro = [&]() { const wzstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), expected); - } - - // usage scenario: create zero-terminated temp file name and pass to a legacy API - { - wchar_t buf[10]; - - const auto name = CreateTempNameW({buf, 10}); - if (!name.empty()) - { - cwzstring str = name.assume_z(); - EXPECT_TRUE(generic::strnlen(str, 10) == 3); - EXPECT_TRUE(*(str + 3) == L'\0'); - } - } -} - -TEST(string_span_tests, u16zstring) -{ - const auto terminateHandler = std::set_terminate([] { - std::cerr << "Expected Death. u16zstring"; - std::abort(); - }); - const auto expected = GetExpectedDeathString(terminateHandler); - - // create zspan from zero terminated string - { - char16_t buf[1]; - buf[0] = L'\0'; - - u16zstring_span<> zspan({buf, 1}); - - EXPECT_TRUE(generic::strnlen(zspan.assume_z(), 1) == 0); - EXPECT_TRUE(zspan.as_string_span().size() == 0); - EXPECT_TRUE(zspan.ensure_z().size() == 0); - } - - // create zspan from non-zero terminated string - { - char16_t buf[1]; - buf[0] = u'a'; - - const auto workaround_macro = [&]() { const u16zstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), expected); - } - - // usage scenario: create zero-terminated temp file name and pass to a legacy API - { - char16_t buf[10]; - - const auto name = CreateTempNameU16({buf, 10}); - if (!name.empty()) - { - cu16zstring str = name.assume_z(); - EXPECT_TRUE(generic::strnlen(str, 10) == 3); - EXPECT_TRUE(*(str + 3) == L'\0'); - } - } -} - -TEST(string_span_tests, u32zstring) -{ - const auto terminateHandler = std::set_terminate([] { - std::cerr << "Expected Death. u31zstring"; - std::abort(); - }); - const auto expected = GetExpectedDeathString(terminateHandler); - - // create zspan from zero terminated string - { - char32_t buf[1]; - buf[0] = L'\0'; - - u32zstring_span<> zspan({buf, 1}); - - EXPECT_TRUE(generic::strnlen(zspan.assume_z(), 1) == 0); - EXPECT_TRUE(zspan.as_string_span().size() == 0); - EXPECT_TRUE(zspan.ensure_z().size() == 0); - } - - // create zspan from non-zero terminated string - { - char32_t buf[1]; - buf[0] = u'a'; - - const auto workaround_macro = [&]() { const u32zstring_span<> zspan({buf, 1}); }; - EXPECT_DEATH(workaround_macro(), expected); - } - - // usage scenario: create zero-terminated temp file name and pass to a legacy API - { - char32_t buf[10]; - - const auto name = CreateTempNameU32({buf, 10}); - if (!name.empty()) - { - cu32zstring str = name.assume_z(); - EXPECT_TRUE(generic::strnlen(str, 10) == 3); - EXPECT_TRUE(*(str + 3) == L'\0'); - } - } -} - -TEST(string_span_tests, Issue305) -{ - std::map, int> foo = {{"foo", 0}, {"bar", 1}}; - EXPECT_TRUE(foo["foo"] == 0); - EXPECT_TRUE(foo["bar"] == 1); -} - -TEST(string_span_tests, char16_t_type) -{ - gsl::cu16string_span<> ss1 = gsl::ensure_z(u"abc"); - EXPECT_TRUE(ss1.size() == 3); - EXPECT_TRUE(ss1.size_bytes() == 6); - - std::u16string s1 = gsl::to_string(ss1); - EXPECT_TRUE(s1 == u"abc"); - - std::u16string s2 = u"abc"; - gsl::u16string_span<> ss2 = s2; - EXPECT_TRUE(ss2.size() == 3); - - gsl::u16string_span<> ss3 = ss2.subspan(1, 1); - EXPECT_TRUE(ss3.size() == 1); - EXPECT_TRUE(ss3[0] == u'b'); - - char16_t buf[4]{u'a', u'b', u'c', u'\0'}; - gsl::u16string_span<> ss4{buf, 4}; - EXPECT_TRUE(ss4[3] == u'\0'); - - gsl::cu16zstring_span<> ss5(u"abc"); - EXPECT_TRUE((ss5.as_string_span().size()) == 3); - - gsl::cu16string_span<> ss6 = ss5.as_string_span(); - EXPECT_TRUE(ss6 == ss1); - - std::vector v7 = {u'a', u'b', u'c'}; - gsl::cu16string_span<> ss7{v7}; - EXPECT_TRUE(ss7 == ss1); - - gsl::cu16string_span<> ss8 = gsl::ensure_z(u"abc"); - gsl::cu16string_span<> ss9 = gsl::ensure_z(u"abc"); - EXPECT_TRUE(ss8 == ss9); - - ss9 = gsl::ensure_z(u"abd"); - EXPECT_TRUE(ss8 < ss9); - EXPECT_TRUE(ss8 <= ss9); - EXPECT_TRUE(ss8 != ss9); -} - -TEST(string_span_tests, char32_t_type) -{ - gsl::cu32string_span<> ss1 = gsl::ensure_z(U"abc"); - EXPECT_TRUE(ss1.size() == 3); - EXPECT_TRUE(ss1.size_bytes() == 12); - - std::u32string s1 = gsl::to_string(ss1); - EXPECT_TRUE(s1 == U"abc"); - - std::u32string s2 = U"abc"; - gsl::u32string_span<> ss2 = s2; - EXPECT_TRUE(ss2.size() == 3); - - gsl::u32string_span<> ss3 = ss2.subspan(1, 1); - EXPECT_TRUE(ss3.size() == 1); - EXPECT_TRUE(ss3[0] == U'b'); - - char32_t buf[4]{U'a', U'b', U'c', U'\0'}; - gsl::u32string_span<> ss4{buf, 4}; - EXPECT_TRUE(ss4[3] == u'\0'); - - gsl::cu32zstring_span<> ss5(U"abc"); - EXPECT_TRUE(ss5.as_string_span().size() == 3); - - gsl::cu32string_span<> ss6 = ss5.as_string_span(); - EXPECT_TRUE(ss6 == ss1); - - gsl::cu32string_span<> ss8 = gsl::ensure_z(U"abc"); - gsl::cu32string_span<> ss9 = gsl::ensure_z(U"abc"); - EXPECT_TRUE(ss8 == ss9); - - ss9 = gsl::ensure_z(U"abd"); - EXPECT_TRUE(ss8 < ss9); - EXPECT_TRUE(ss8 <= ss9); - EXPECT_TRUE(ss8 != ss9); -} - -TEST(string_span_tests, as_bytes) -{ - cwzstring_span<> v(L"qwerty"); - const auto s = v.as_string_span(); - const auto bs = as_bytes(s); - EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); - EXPECT_TRUE(bs.size() == s.size_bytes()); -} - -TEST(string_span_tests, as_writable_bytes) -{ - wchar_t buf[]{L"qwerty"}; - wzstring_span<> v(buf); - const auto s = v.as_string_span(); - const auto bs = as_writable_bytes(s); - EXPECT_TRUE(static_cast(bs.data()) == static_cast(s.data())); - EXPECT_TRUE(bs.size() == s.size_bytes()); -}