Merge pull request #850 from beinhaerter/zstring_ctor

zstring_span: fix for Expects, simplify functions
This commit is contained in:
Jordan Maples [MSFT] 2020-08-06 10:28:02 -07:00 committed by GitHub
commit 63379b7935
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -120,9 +120,8 @@ span<T, dynamic_extent> ensure_sentinel(T* seq,
} }
// //
// ensure_z - creates a span for a zero terminated strings. // 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 // Will fail fast if a null-terminator cannot be found before the limit of size_type.
// the limit of size_type.
// //
template <typename CharT> template <typename CharT>
span<CharT, dynamic_extent> ensure_z(CharT* const& sz, span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
@ -395,7 +394,8 @@ public:
constexpr basic_zstring_span(impl_type s) : span_(s) constexpr basic_zstring_span(impl_type s) : span_(s)
{ {
// expects a zero-terminated span // expects a zero-terminated span
Expects(s[s.size() - 1] == '\0'); Expects(s.size() > 0);
Expects(s[s.size() - 1] == value_type{});
} }
// copy // copy
@ -410,12 +410,11 @@ public:
// move assign // move assign
constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default; constexpr basic_zstring_span& operator=(basic_zstring_span&& other) = default;
constexpr bool empty() const noexcept { return span_.size() == 0; } constexpr bool empty() const noexcept { return false; }
constexpr string_span_type as_string_span() const noexcept constexpr string_span_type as_string_span() const noexcept
{ {
const auto sz = span_.size(); return {span_.data(), span_.size() - 1};
return {span_.data(), sz > 1 ? sz - 1 : 0};
} }
constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); } constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); }