diff options
author | Abseil Team <absl-team@google.com> | 2021-03-20 05:24:27 (GMT) |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2021-03-22 19:18:50 (GMT) |
commit | 1a8ecf1813d022cc7914e04564b92decff6161fc (patch) | |
tree | 3ff9941b412d6470e3a37ffc102bd166a4100171 /googletest/include/gtest/gtest-printers.h | |
parent | 3ff1e8b98a3d1d3abc24a5bacb7651c9b32faedd (diff) | |
download | googletest-1a8ecf1813d022cc7914e04564b92decff6161fc.zip googletest-1a8ecf1813d022cc7914e04564b92decff6161fc.tar.gz googletest-1a8ecf1813d022cc7914e04564b92decff6161fc.tar.bz2 |
Googletest export
Print std::u8string, std::u16string, and std::u32string as string literals
Previously, these types were printed as "{ U+123, U+456, U+789 }". However,
printed output in that form is difficult to compare against any literals that
might be defined in code. Instead, just treat these types like std::string
and std::wstring, escaping non-ASCII characters with a hexadecimal escape
sequence.
The tests have also been updated to cover the new functionality: as a bonus,
the tests now also pass with the MSVC toolchain.
Internally, the code has been reorganized to primarily operate in terms of
char32_t, under the assumption that char32_t will always be at least as big
as wchar_t. While that assumption is currently true, perhaps it won't be in
the future...
PiperOrigin-RevId: 364033132
Diffstat (limited to 'googletest/include/gtest/gtest-printers.h')
-rw-r--r-- | googletest/include/gtest/gtest-printers.h | 104 |
1 files changed, 89 insertions, 15 deletions
diff --git a/googletest/include/gtest/gtest-printers.h b/googletest/include/gtest/gtest-printers.h index a7e0a3c..46fe895 100644 --- a/googletest/include/gtest/gtest-printers.h +++ b/googletest/include/gtest/gtest-printers.h @@ -505,24 +505,21 @@ inline void PrintTo(unsigned char* s, ::std::ostream* os) { PrintTo(ImplicitCast_<const void*>(s), os); } #ifdef __cpp_char8_t -inline void PrintTo(const char8_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_<const void*>(s), os); -} +// Overloads for u8 strings. +void PrintTo(const char8_t* s, ::std::ostream* os); inline void PrintTo(char8_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_<const void*>(s), os); + PrintTo(ImplicitCast_<const char8_t*>(s), os); } #endif -inline void PrintTo(const char16_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_<const void*>(s), os); -} +// Overloads for u16 strings. +void PrintTo(const char16_t* s, ::std::ostream* os); inline void PrintTo(char16_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_<const void*>(s), os); -} -inline void PrintTo(const char32_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_<const void*>(s), os); + PrintTo(ImplicitCast_<const char16_t*>(s), os); } +// Overloads for u32 strings. +void PrintTo(const char32_t* s, ::std::ostream* os); inline void PrintTo(char32_t* s, ::std::ostream* os) { - PrintTo(ImplicitCast_<const void*>(s), os); + PrintTo(ImplicitCast_<const char32_t*>(s), os); } // MSVC can be configured to define wchar_t as a typedef of unsigned @@ -558,6 +555,26 @@ inline void PrintTo(const ::std::string& s, ::std::ostream* os) { PrintStringTo(s, os); } +// Overloads for ::std::u8string +#ifdef __cpp_char8_t +GTEST_API_ void PrintU8StringTo(const ::std::u8string& s, ::std::ostream* os); +inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) { + PrintU8StringTo(s, os); +} +#endif + +// Overloads for ::std::u16string +GTEST_API_ void PrintU16StringTo(const ::std::u16string& s, ::std::ostream* os); +inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) { + PrintU16StringTo(s, os); +} + +// Overloads for ::std::u32string +GTEST_API_ void PrintU32StringTo(const ::std::u32string& s, ::std::ostream* os); +inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) { + PrintU32StringTo(s, os); +} + // Overloads for ::std::wstring. #if GTEST_HAS_STD_WSTRING GTEST_API_ void PrintWideStringTo(const ::std::wstring&s, ::std::ostream* os); @@ -805,6 +822,20 @@ void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) { GTEST_API_ void UniversalPrintArray( const char* begin, size_t len, ::std::ostream* os); +#ifdef __cpp_char8_t +// This overload prints a (const) char8_t array compactly. +GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len, + ::std::ostream* os); +#endif + +// This overload prints a (const) char16_t array compactly. +GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len, + ::std::ostream* os); + +// This overload prints a (const) char32_t array compactly. +GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len, + ::std::ostream* os); + // This overload prints a (const) wchar_t array compactly. GTEST_API_ void UniversalPrintArray( const wchar_t* begin, size_t len, ::std::ostream* os); @@ -877,12 +908,55 @@ class UniversalTersePrinter<const char*> { } }; template <> -class UniversalTersePrinter<char*> { +class UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> { +}; + +#ifdef __cpp_char8_t +template <> +class UniversalTersePrinter<const char8_t*> { + public: + static void Print(const char8_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::u8string(str), os); + } + } +}; +template <> +class UniversalTersePrinter<char8_t*> + : public UniversalTersePrinter<const char8_t*> {}; +#endif + +template <> +class UniversalTersePrinter<const char16_t*> { + public: + static void Print(const char16_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::u16string(str), os); + } + } +}; +template <> +class UniversalTersePrinter<char16_t*> + : public UniversalTersePrinter<const char16_t*> {}; + +template <> +class UniversalTersePrinter<const char32_t*> { public: - static void Print(char* str, ::std::ostream* os) { - UniversalTersePrinter<const char*>::Print(str, os); + static void Print(const char32_t* str, ::std::ostream* os) { + if (str == nullptr) { + *os << "NULL"; + } else { + UniversalPrint(::std::u32string(str), os); + } } }; +template <> +class UniversalTersePrinter<char32_t*> + : public UniversalTersePrinter<const char32_t*> {}; #if GTEST_HAS_STD_WSTRING template <> |