Merge pull request #931 from JordanMaples/deprecate-string_span

Deprecate string_span
This commit is contained in:
Jordan Maples [MSFT] 2020-10-02 12:36:09 -07:00 committed by GitHub
commit bd23bdc0ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 162 additions and 83 deletions

View File

@ -27,27 +27,8 @@ Feature | Supported? | Description
[**1. Views**][cg-views] | |
owner | ☑ | an alias for a raw pointer
not_null | ☑ | restricts a pointer / smart pointer to hold non-null values
strict_not_null | ☑ | a stricter version of `not_null` with explicit constructors
span | ☑ | a view over a contiguous sequence of memory. Based on the standardized verison of `std::span`, however `gsl::span` enforces bounds checking. See the [wiki](https://github.com/microsoft/GSL/wiki/gsl::span-and-std::span) for additional information.
span_p | ☐ | spans a range starting from a pointer to the first place for which the predicate is true
basic_zstring | ☑ | a pointer to a C-string (zero-terminated array) with a templated char type
zstring | ☑ | an alias to `basic_zstring` with a char type of char
czstring | ☑ | an alias to `basic_zstring` with a char type of const char
wzstring | ☑ | an alias to `basic_zstring` with a char type of wchar_t
cwzstring | ☑ | an alias to `basic_zstring` with a char type of const wchar_t
u16zstring | ☑ | an alias to `basic_zstring` with a char type of char16_t
cu16zstring | ☑ | an alias to `basic_zstring` with a char type of const char16_t
u32zstring | ☑ | an alias to `basic_zstring` with a char type of char32_t
cu32zstring | ☑ | an alias to `basic_zstring` with a char type of const char32_t
basic_string_span | ☑ | like `span` but for strings with a templated char type
string_span | ☑ | an alias to `basic_string_span` with a char type of char
cstring_span | ☑ | an alias to `basic_string_span` with a char type of const char
wstring_span | ☑ | an alias to `basic_string_span` with a char type of wchar_t
cwstring_span | ☑ | an alias to `basic_string_span` with a char type of const wchar_t
u16string_span | ☑ | an alias to `basic_string_span` with a char type of char16_t
cu16string_span | ☑ | an alias to `basic_string_span` with a char type of const char16_t
u32string_span | ☑ | an alias to `basic_string_span` with a char type of char32_t
cu32string_span | ☑ | an alias to `basic_string_span` with a char type of const char32_t
[**2. Owners**][cg-owners] | |
unique_ptr | ☑ | an alias to `std::unique_ptr`
shared_ptr | ☑ | an alias to `std::shared_ptr`
@ -70,11 +51,30 @@ narrow_cast | ☑ | a narrowing cast for values an
narrowing_error | ☑ | a custom exception type thrown by `narrow()`
[**5. Concepts**][cg-concepts] | ☐ |
## The following features do not exist in C++ Core Guidelines:
## The following features do not exist in or have been removed from the C++ Core Guidelines:
Feature | Supported? | Description
-----------------------------------|:----------:|-------------
multi_span | ☐ | Deprecated. Support for this type has been discontinued.
strict_not_null | ☑ | A stricter version of `not_null` with explicit constructors
multi_span | ☐ | Deprecated. Multi-dimensional span.
strided_span | ☐ | Deprecated. Support for this type has been discontinued.
basic_zstring | ☐ | Deprecated. A pointer to a C-string (zero-terminated array) with a templated char type
zstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of char
czstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of const char
wzstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of wchar_t
cwzstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of const wchar_t
u16zstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of char16_t
cu16zstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of const char16_t
u32zstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of char32_t
cu32zstring | ☐ | Deprecated. An alias to `basic_zstring` with a char type of const char32_t
basic_string_span | ☐ | Deprecated. Like `span` but for strings with a templated char type
string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of char
cstring_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const char
wstring_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of wchar_t
cwstring_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const wchar_t
u16string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of char16_t
cu16string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const char16_t
u32string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of char32_t
cu32string_span | ☐ | Deprecated. An alias to `basic_string_span` with a char type of const char32_t
This is based on [CppCoreGuidelines semi-specification](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gsl-guidelines-support-library).

View File

@ -35,9 +35,14 @@
// 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
{
//
@ -52,36 +57,55 @@ namespace gsl
//
template <typename CharT, std::size_t Extent = dynamic_extent>
using basic_zstring = CharT*;
using basic_zstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] = CharT*;
template <std::size_t Extent = dynamic_extent>
using czstring = basic_zstring<const char, Extent>;
using czstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<const char, Extent>;
template <std::size_t Extent = dynamic_extent>
using cwzstring = basic_zstring<const wchar_t, Extent>;
using cwzstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<const wchar_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using cu16zstring = basic_zstring<const char16_t, Extent>;
using cu16zstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<const char16_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using cu32zstring = basic_zstring<const char32_t, Extent>;
using cu32zstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<const char32_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using zstring = basic_zstring<char, Extent>;
using zstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<char, Extent>;
template <std::size_t Extent = dynamic_extent>
using wzstring = basic_zstring<wchar_t, Extent>;
using wzstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<wchar_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using u16zstring = basic_zstring<char16_t, Extent>;
using u16zstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<char16_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using u32zstring = basic_zstring<char32_t, Extent>;
using u32zstring [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring<char32_t, Extent>;
namespace details
{
template <class CharT>
constexpr std::size_t string_length(const CharT* str, std::size_t n)
[[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;
@ -103,29 +127,34 @@ namespace details
// Will fail-fast if sentinel cannot be found before max elements are examined.
//
template <typename T, const T Sentinel>
constexpr span<T, dynamic_extent> ensure_sentinel(T* seq,
std::size_t max = static_cast<std::size_t>(-1))
[[deprecated("string_span was removed from the C++ Core Guidelines. For more information, see "
"isocpp/CppCoreGuidelines PR#1680")]] constexpr span<T, dynamic_extent>
ensure_sentinel(T* seq, std::size_t max = static_cast<std::size_t>(-1))
{
Ensures(seq != nullptr);
GSL_SUPPRESS(
f.23) // NO-FORMAT: attribute // TODO: false positive // TODO: suppress does not work
// 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
GSL_SUPPRESS(bounds.1) // NO-FORMAT: attribute // TODO: suppress does not work
// clang-format off
GSL_SUPPRESS(bounds.1) // TODO: suppress does not work
// clang-format on
while (static_cast<std::size_t>(cur - seq) < max && *cur != Sentinel) ++cur;
Ensures(*cur == Sentinel);
return {seq, static_cast<std::size_t>(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.
// 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>
constexpr span<CharT, dynamic_extent> ensure_z(CharT* const& sz,
std::size_t max = static_cast<std::size_t>(-1))
[[deprecated("string_span was removed from the C++ Core Guidelines. For more information, see "
"isocpp/CppCoreGuidelines PR#1680")]] constexpr span<CharT, dynamic_extent>
ensure_z(CharT* const& sz, std::size_t max = static_cast<std::size_t>(-1))
{
return ensure_sentinel<CharT, CharT(0)>(sz, max);
}
@ -137,38 +166,48 @@ constexpr span<CharT, dynamic_extent> ensure_z(CharT (&sz)[N])
}
template <class Cont>
constexpr span<typename std::remove_pointer<typename Cont::pointer>::type, dynamic_extent>
[[deprecated(
"string_span was removed from the C++ Core Guidelines. For more information, see "
"isocpp/CppCoreGuidelines PR#1680")]] constexpr span<typename std::
remove_pointer<
typename Cont::pointer>::type,
dynamic_extent>
ensure_z(Cont& cont)
{
return ensure_z(cont.data(), cont.size());
}
template <typename CharT, std::size_t>
class basic_string_span;
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 <typename T>
struct is_basic_string_span_oracle : std::false_type
{
};
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 <typename CharT, std::size_t Extent>
struct is_basic_string_span_oracle<basic_string_span<CharT, Extent>> : std::true_type
{
};
struct [[deprecated(
"string_span was removed from the C++ Core Guidelines. For more information, see "
"isocpp/CppCoreGuidelines PR#1680")]] is_basic_string_span_oracle<basic_string_span<CharT,
Extent>>
: std::true_type{};
template <typename T>
struct is_basic_string_span : is_basic_string_span_oracle<std::remove_cv_t<T>>
{
};
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<std::remove_cv_t<T>>{};
} // namespace details
//
// string_span and relatives
//
template <typename CharT, std::size_t Extent = dynamic_extent>
class basic_string_span
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;
@ -197,11 +236,11 @@ public:
// 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 <std::size_t N>
constexpr basic_string_span(element_type (&arr)[N]) : span_(remove_z(arr))
constexpr basic_string_span(element_type(&arr)[N]) : span_(remove_z(arr))
{}
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)
constexpr basic_string_span(std::array<ArrayElementType, N> & arr) noexcept : span_(arr)
{}
template <std::size_t N, class ArrayElementType = std::remove_const_t<element_type>>
@ -210,8 +249,8 @@ public:
// 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)
// GSL_SUPPRESS(bounds.4) // TODO: parser bug
constexpr basic_string_span(std::basic_string<element_type, Traits, Allocator> & str)
: span_(&str[0], str.length())
{}
@ -227,7 +266,7 @@ public:
std::is_convertible<typename Container::pointer, pointer>::value &&
std::is_convertible<typename Container::pointer,
decltype(std::declval<Container>().data())>::value>>
constexpr basic_string_span(Container& cont) : span_(cont)
constexpr basic_string_span(Container & cont) : span_(cont)
{}
template <class Container,
@ -276,8 +315,8 @@ public:
return {span_.template subspan<Offset, Count>()};
}
constexpr basic_string_span<element_type, dynamic_extent>
subspan(size_type offset, size_type count = dynamic_extent) const
constexpr basic_string_span<element_type, dynamic_extent> subspan(
size_type offset, size_type count = dynamic_extent) const
{
return {span_.subspan(offset, count)};
}
@ -306,7 +345,7 @@ private:
}
template <std::size_t N>
static constexpr impl_type remove_z(element_type (&sz)[N])
static constexpr impl_type remove_z(element_type(&sz)[N])
{
return remove_z(&sz[0], N);
}
@ -315,28 +354,44 @@ private:
};
template <std::size_t Extent = dynamic_extent>
using string_span = basic_string_span<char, Extent>;
using string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<char, Extent>;
template <std::size_t Extent = dynamic_extent>
using cstring_span = basic_string_span<const char, Extent>;
using cstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<const char, Extent>;
template <std::size_t Extent = dynamic_extent>
using wstring_span = basic_string_span<wchar_t, Extent>;
using wstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<wchar_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using cwstring_span = basic_string_span<const wchar_t, Extent>;
using cwstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<const wchar_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using u16string_span = basic_string_span<char16_t, Extent>;
using u16string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<char16_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using cu16string_span = basic_string_span<const char16_t, Extent>;
using cu16string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<const char16_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using u32string_span = basic_string_span<char32_t, Extent>;
using u32string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<char32_t, Extent>;
template <std::size_t Extent = dynamic_extent>
using cu32string_span = basic_string_span<const char32_t, Extent>;
using cu32string_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_string_span<const char32_t, Extent>;
//
// to_string() allow (explicit) conversions from string_span to string
@ -361,7 +416,9 @@ template <class ElementType, std::size_t Extent>
constexpr 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
// clang-format off
GSL_SUPPRESS(type.1)
// clang-format on
return {reinterpret_cast<const byte*>(s.data()), s.size_bytes()};
}
@ -370,14 +427,17 @@ template <class ElementType, std::size_t Extent,
constexpr basic_string_span<byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writable_bytes(basic_string_span<ElementType, Extent> s) noexcept
{
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format off
GSL_SUPPRESS(type.1)
// clang-format on
return {reinterpret_cast<byte*>(s.data()), s.size_bytes()};
}
// zero-terminated string span, used to convert
// zero-terminated spans to legacy strings
template <typename CharT, std::size_t Extent = dynamic_extent>
class basic_zstring_span
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;
@ -403,7 +463,7 @@ public:
constexpr basic_zstring_span(const basic_zstring_span& other) = default;
// move
constexpr basic_zstring_span(basic_zstring_span&& other) = default;
constexpr basic_zstring_span(basic_zstring_span && other) = default;
// assign
constexpr basic_zstring_span& operator=(const basic_zstring_span& other) = default;
@ -426,28 +486,44 @@ private:
};
template <std::size_t Max = dynamic_extent>
using zstring_span = basic_zstring_span<char, Max>;
using zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<char, Max>;
template <std::size_t Max = dynamic_extent>
using wzstring_span = basic_zstring_span<wchar_t, Max>;
using wzstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<wchar_t, Max>;
template <std::size_t Max = dynamic_extent>
using u16zstring_span = basic_zstring_span<char16_t, Max>;
using u16zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<char16_t, Max>;
template <std::size_t Max = dynamic_extent>
using u32zstring_span = basic_zstring_span<char32_t, Max>;
using u32zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<char32_t, Max>;
template <std::size_t Max = dynamic_extent>
using czstring_span = basic_zstring_span<const char, Max>;
using czstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<const char, Max>;
template <std::size_t Max = dynamic_extent>
using cwzstring_span = basic_zstring_span<const wchar_t, Max>;
using cwzstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For more "
"information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<const wchar_t, Max>;
template <std::size_t Max = dynamic_extent>
using cu16zstring_span = basic_zstring_span<const char16_t, Max>;
using cu16zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For "
"more information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<const char16_t, Max>;
template <std::size_t Max = dynamic_extent>
using cu32zstring_span = basic_zstring_span<const char32_t, Max>;
using cu32zstring_span [[deprecated("string_span was removed from the C++ Core Guidelines. For "
"more information, see isocpp/CppCoreGuidelines PR#1680")]] =
basic_zstring_span<const char32_t, Max>;
// operator ==
template <class CharT, std::size_t Extent, class T,
@ -703,4 +779,7 @@ bool operator>=(const T& one, gsl::basic_string_span<CharT, Extent> other)
#endif // _MSC_VER
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic pop
#endif
#endif // GSL_STRING_SPAN_H