diff options
| author | Werner Henze <w.henze@avm.de> | 2020-08-06 11:25:29 +0200 |
|---|---|---|
| committer | Werner Henze <w.henze@avm.de> | 2020-08-06 11:25:29 +0200 |
| commit | 2231d733c6df2f0fe1c9f41460e1997a3f77249a (patch) | |
| tree | 96dccc950adfc0cdcbbed7105ec205924032df3e | |
| parent | f8d9f2e2e3f5e0dce4f49caf3f4b1914dd7607ee (diff) | |
| download | platform_external_Microsoft-GSL-2231d733c6df2f0fe1c9f41460e1997a3f77249a.tar.gz platform_external_Microsoft-GSL-2231d733c6df2f0fe1c9f41460e1997a3f77249a.tar.bz2 platform_external_Microsoft-GSL-2231d733c6df2f0fe1c9f41460e1997a3f77249a.zip | |
zstring_span: fix for Expects, simplify functions
- `s[s.size() - 1]` is wrong for empty `s`, so `Expects(s.size() > 0)`
- no hard coded `'\0'`but `value_type{}`
- hard code `empty()` to return `false`
- simplify `as_string_span`: can never be `empty`
- clarify comment on `ensure_z`
| -rw-r--r-- | include/gsl/string_span | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/include/gsl/string_span b/include/gsl/string_span index ba125b7..506001f 100644 --- a/include/gsl/string_span +++ b/include/gsl/string_span @@ -120,9 +120,8 @@ span<T, dynamic_extent> ensure_sentinel(T* seq, }
//
-// ensure_z - creates a span for a zero terminated strings.
-// Will fail fast if a null-terminator cannot be found before
-// the limit of size_type.
+// 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 <typename CharT>
span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
@@ -395,7 +394,8 @@ public: constexpr basic_zstring_span(impl_type s) : span_(s)
{
// expects a zero-terminated span
- Expects(s[s.size() - 1] == '\0');
+ Expects(s.size() > 0);
+ Expects(s[s.size() - 1] == value_type{});
}
// copy
@@ -410,12 +410,11 @@ public: // move assign
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
{
- const auto sz = span_.size();
- return {span_.data(), sz > 1 ? sz - 1 : 0};
+ return {span_.data(), span_.size() - 1};
}
constexpr string_span_type ensure_z() const { return gsl::ensure_z(span_); }
|
