Add support for string_view

This commit is contained in:
Peter Dimov
2021-10-01 15:07:19 +03:00
parent f884833b42
commit e260bb865d
2 changed files with 95 additions and 0 deletions

View File

@ -21,6 +21,9 @@
#include <utility> #include <utility>
#include <cstdio> #include <cstdio>
#include <cstddef> #include <cstddef>
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
# include <string_view>
#endif
namespace boost namespace boost
{ {
@ -447,6 +450,12 @@ inline std::string type_name( tn_identity<std::nullptr_t> )
// strings // strings
template<template<class Ch, class Tr, class A> class L, class Ch> std::string type_name( tn_identity< L<Ch, std::char_traits<Ch>, std::allocator<Ch> > > )
{
std::string tn = sequence_template_name< L<Ch, std::char_traits<Ch>, std::allocator<Ch> > >();
return tn + '<' + type_name( tn_identity<Ch>() ) + '>';
}
inline std::string type_name( tn_identity<std::string> ) inline std::string type_name( tn_identity<std::string> )
{ {
return "std::string"; return "std::string";
@ -484,6 +493,55 @@ inline std::string type_name( tn_identity<std::basic_string<char8_t>> )
#endif #endif
// string views (et al)
template<template<class Ch, class Tr> class L, class Ch> std::string type_name( tn_identity< L<Ch, std::char_traits<Ch> > > )
{
std::string tn = sequence_template_name< L<Ch, std::char_traits<Ch> > >();
return tn + '<' + type_name( tn_identity<Ch>() ) + '>';
}
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
inline std::string type_name( tn_identity<std::string_view> )
{
return "std::string_view";
}
inline std::string type_name( tn_identity<std::wstring_view> )
{
return "std::wstring_view";
}
#if !defined(BOOST_NO_CXX11_CHAR16_T)
inline std::string type_name( tn_identity<std::u16string_view> )
{
return "std::u16string_view";
}
#endif
#if !defined(BOOST_NO_CXX11_CHAR32_T)
inline std::string type_name( tn_identity<std::u32string_view> )
{
return "std::u32string_view";
}
#endif
#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L
inline std::string type_name( tn_identity<std::basic_string_view<char8_t>> )
{
return "std::u8string_view";
}
#endif
#endif
// class templates // class templates
#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)

View File

@ -13,6 +13,7 @@
#include <map> #include <map>
#include <utility> #include <utility>
#include <cstddef> #include <cstddef>
#include <iosfwd>
#if !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET) #if !defined(BOOST_NO_CXX11_HDR_UNORDERED_SET)
# include <unordered_set> # include <unordered_set>
@ -26,6 +27,10 @@
# include <array> # include <array>
#endif #endif
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
# include <string_view>
#endif
// //
#define TEST(...) BOOST_TEST_EQ((boost::core::type_name<__VA_ARGS__>()), std::string(#__VA_ARGS__)) #define TEST(...) BOOST_TEST_EQ((boost::core::type_name<__VA_ARGS__>()), std::string(#__VA_ARGS__))
@ -56,6 +61,10 @@ enum class E2
#endif #endif
struct Ch
{
};
int main() int main()
{ {
TEST(int); TEST(int);
@ -121,6 +130,8 @@ int main()
TEST(std::pair<A, B>); TEST(std::pair<A, B>);
TEST(std::pair<A const*, B*> volatile&); TEST(std::pair<A const*, B*> volatile&);
TEST(std::basic_string<Ch>);
TEST(std::string); TEST(std::string);
TEST(std::wstring); TEST(std::wstring);
@ -181,5 +192,31 @@ int main()
TEST(std::array<std::wstring const*, 0> const&); TEST(std::array<std::wstring const*, 0> const&);
#endif #endif
TEST(std::basic_ostream<char>);
TEST(std::basic_ostream<wchar_t>);
#if !defined(BOOST_NO_CXX17_HDR_STRING_VIEW)
TEST(std::basic_string_view<Ch>);
TEST(std::string_view);
TEST(std::wstring_view);
#if BOOST_CXX_VERSION >= 201100L
TEST(std::u16string_view);
TEST(std::u32string_view);
#endif
#if BOOST_CXX_VERSION >= 202000L
TEST(std::u8string_view);
#endif
#endif
return boost::report_errors(); return boost::report_errors();
} }