Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-28 10:15:47

0001 // Copyright 2007, Google Inc.
0002 // All rights reserved.
0003 //
0004 // Redistribution and use in source and binary forms, with or without
0005 // modification, are permitted provided that the following conditions are
0006 // met:
0007 //
0008 //     * Redistributions of source code must retain the above copyright
0009 // notice, this list of conditions and the following disclaimer.
0010 //     * Redistributions in binary form must reproduce the above
0011 // copyright notice, this list of conditions and the following disclaimer
0012 // in the documentation and/or other materials provided with the
0013 // distribution.
0014 //     * Neither the name of Google Inc. nor the names of its
0015 // contributors may be used to endorse or promote products derived from
0016 // this software without specific prior written permission.
0017 //
0018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0029 
0030 // Google Test - The Google C++ Testing and Mocking Framework
0031 //
0032 // This file implements a universal value printer that can print a
0033 // value of any type T:
0034 //
0035 //   void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
0036 //
0037 // A user can teach this function how to print a class type T by
0038 // defining either operator<<() or PrintTo() in the namespace that
0039 // defines T.  More specifically, the FIRST defined function in the
0040 // following list will be used (assuming T is defined in namespace
0041 // foo):
0042 //
0043 //   1. foo::PrintTo(const T&, ostream*)
0044 //   2. operator<<(ostream&, const T&) defined in either foo or the
0045 //      global namespace.
0046 // * Prefer AbslStringify(..) to operator<<(..), per https://abseil.io/tips/215.
0047 // * Define foo::PrintTo(..) if the type already has AbslStringify(..), but an
0048 //   alternative presentation in test results is of interest.
0049 //
0050 // However if T is an STL-style container then it is printed element-wise
0051 // unless foo::PrintTo(const T&, ostream*) is defined. Note that
0052 // operator<<() is ignored for container types.
0053 //
0054 // If none of the above is defined, it will print the debug string of
0055 // the value if it is a protocol buffer, or print the raw bytes in the
0056 // value otherwise.
0057 //
0058 // To aid debugging: when T is a reference type, the address of the
0059 // value is also printed; when T is a (const) char pointer, both the
0060 // pointer value and the NUL-terminated string it points to are
0061 // printed.
0062 //
0063 // We also provide some convenient wrappers:
0064 //
0065 //   // Prints a value to a string.  For a (const or not) char
0066 //   // pointer, the NUL-terminated string (but not the pointer) is
0067 //   // printed.
0068 //   std::string ::testing::PrintToString(const T& value);
0069 //
0070 //   // Prints a value tersely: for a reference type, the referenced
0071 //   // value (but not the address) is printed; for a (const or not) char
0072 //   // pointer, the NUL-terminated string (but not the pointer) is
0073 //   // printed.
0074 //   void ::testing::internal::UniversalTersePrint(const T& value, ostream*);
0075 //
0076 //   // Prints value using the type inferred by the compiler.  The difference
0077 //   // from UniversalTersePrint() is that this function prints both the
0078 //   // pointer and the NUL-terminated string for a (const or not) char pointer.
0079 //   void ::testing::internal::UniversalPrint(const T& value, ostream*);
0080 //
0081 //   // Prints the fields of a tuple tersely to a string vector, one
0082 //   // element for each field. Tuple support must be enabled in
0083 //   // gtest-port.h.
0084 //   std::vector<string> UniversalTersePrintTupleFieldsToStrings(
0085 //       const Tuple& value);
0086 //
0087 // Known limitation:
0088 //
0089 // The print primitives print the elements of an STL-style container
0090 // using the compiler-inferred type of *iter where iter is a
0091 // const_iterator of the container.  When const_iterator is an input
0092 // iterator but not a forward iterator, this inferred type may not
0093 // match value_type, and the print output may be incorrect.  In
0094 // practice, this is rarely a problem as for most containers
0095 // const_iterator is a forward iterator.  We'll fix this if there's an
0096 // actual need for it.  Note that this fix cannot rely on value_type
0097 // being defined as many user-defined container types don't have
0098 // value_type.
0099 
0100 // IWYU pragma: private, include "gtest/gtest.h"
0101 // IWYU pragma: friend gtest/.*
0102 // IWYU pragma: friend gmock/.*
0103 
0104 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0105 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_
0106 
0107 #include <any>
0108 #include <functional>
0109 #include <memory>
0110 #include <optional>
0111 #include <ostream>  // NOLINT
0112 #include <sstream>
0113 #include <string>
0114 #include <string_view>
0115 #include <tuple>
0116 #include <type_traits>
0117 #include <typeinfo>
0118 #include <utility>
0119 #include <variant>
0120 #include <vector>
0121 
0122 #ifdef GTEST_HAS_ABSL
0123 #include "absl/strings/has_absl_stringify.h"
0124 #include "absl/strings/str_cat.h"
0125 #endif  // GTEST_HAS_ABSL
0126 #include "gtest/internal/gtest-internal.h"
0127 #include "gtest/internal/gtest-port.h"
0128 
0129 #if GTEST_INTERNAL_HAS_STD_SPAN
0130 #include <span>  // NOLINT
0131 #endif           // GTEST_INTERNAL_HAS_STD_SPAN
0132 
0133 #if GTEST_INTERNAL_HAS_COMPARE_LIB
0134 #include <compare>  // NOLINT
0135 #endif              // GTEST_INTERNAL_HAS_COMPARE_LIB
0136 
0137 namespace testing {
0138 
0139 // Definitions in the internal* namespaces are subject to change without notice.
0140 // DO NOT USE THEM IN USER CODE!
0141 namespace internal {
0142 
0143 template <typename T>
0144 void UniversalPrint(const T& value, ::std::ostream* os);
0145 
0146 template <typename T>
0147 struct IsStdSpan {
0148   static constexpr bool value = false;
0149 };
0150 
0151 #if GTEST_INTERNAL_HAS_STD_SPAN
0152 template <typename E>
0153 struct IsStdSpan<std::span<E>> {
0154   static constexpr bool value = true;
0155 };
0156 #endif  // GTEST_INTERNAL_HAS_STD_SPAN
0157 
0158 // Used to print an STL-style container when the user doesn't define
0159 // a PrintTo() for it.
0160 //
0161 // NOTE: Since std::span does not have const_iterator until C++23, it would
0162 // fail IsContainerTest before C++23. However, IsContainerTest only uses
0163 // the presence of const_iterator to avoid treating iterators as containers
0164 // because of iterator::iterator. Which means std::span satisfies the *intended*
0165 // condition of IsContainerTest.
0166 struct ContainerPrinter {
0167   template <typename T,
0168             typename = typename std::enable_if<
0169                 ((sizeof(IsContainerTest<T>(0)) == sizeof(IsContainer)) &&
0170                  !IsRecursiveContainer<T>::value) ||
0171                 IsStdSpan<T>::value>::type>
0172   static void PrintValue(const T& container, std::ostream* os) {
0173     const size_t kMaxCount = 32;  // The maximum number of elements to print.
0174     *os << '{';
0175     size_t count = 0;
0176     for (auto&& elem : container) {
0177       if (count > 0) {
0178         *os << ',';
0179         if (count == kMaxCount) {  // Enough has been printed.
0180           *os << " ...";
0181           break;
0182         }
0183       }
0184       *os << ' ';
0185       // We cannot call PrintTo(elem, os) here as PrintTo() doesn't
0186       // handle `elem` being a native array.
0187       internal::UniversalPrint(elem, os);
0188       ++count;
0189     }
0190 
0191     if (count > 0) {
0192       *os << ' ';
0193     }
0194     *os << '}';
0195   }
0196 };
0197 
0198 // Used to print a pointer that is neither a char pointer nor a member
0199 // pointer, when the user doesn't define PrintTo() for it.  (A member
0200 // variable pointer or member function pointer doesn't really point to
0201 // a location in the address space.  Their representation is
0202 // implementation-defined.  Therefore they will be printed as raw
0203 // bytes.)
0204 struct FunctionPointerPrinter {
0205   template <typename T, typename = typename std::enable_if<
0206                             std::is_function<T>::value>::type>
0207   static void PrintValue(T* p, ::std::ostream* os) {
0208     if (p == nullptr) {
0209       *os << "NULL";
0210     } else {
0211       // T is a function type, so '*os << p' doesn't do what we want
0212       // (it just prints p as bool).  We want to print p as a const
0213       // void*.
0214       *os << reinterpret_cast<const void*>(p);
0215     }
0216   }
0217 };
0218 
0219 struct PointerPrinter {
0220   template <typename T>
0221   static void PrintValue(T* p, ::std::ostream* os) {
0222     if (p == nullptr) {
0223       *os << "NULL";
0224     } else {
0225       // T is not a function type.  We just call << to print p,
0226       // relying on ADL to pick up user-defined << for their pointer
0227       // types, if any.
0228       *os << p;
0229     }
0230   }
0231 };
0232 
0233 namespace internal_stream_operator_without_lexical_name_lookup {
0234 
0235 // The presence of an operator<< here will terminate lexical scope lookup
0236 // straight away (even though it cannot be a match because of its argument
0237 // types). Thus, the two operator<< calls in StreamPrinter will find only ADL
0238 // candidates.
0239 struct LookupBlocker {};
0240 void operator<<(LookupBlocker, LookupBlocker);
0241 
0242 struct StreamPrinter {
0243   template <typename T,
0244             // Don't accept member pointers here. We'd print them via implicit
0245             // conversion to bool, which isn't useful.
0246             typename = typename std::enable_if<
0247                 !std::is_member_pointer<T>::value>::type>
0248   // Only accept types for which we can find a streaming operator via
0249   // ADL (possibly involving implicit conversions).
0250   // (Use SFINAE via return type, because it seems GCC < 12 doesn't handle name
0251   // lookup properly when we do it in the template parameter list.)
0252   static auto PrintValue(const T& value, ::std::ostream* os)
0253       -> decltype((void)(*os << value)) {
0254     // Call streaming operator found by ADL, possibly with implicit conversions
0255     // of the arguments.
0256     *os << value;
0257   }
0258 };
0259 
0260 }  // namespace internal_stream_operator_without_lexical_name_lookup
0261 
0262 struct ProtobufPrinter {
0263   // We print a protobuf using its ShortDebugString() when the string
0264   // doesn't exceed this many characters; otherwise we print it using
0265   // DebugString() for better readability.
0266   static const size_t kProtobufOneLinerMaxLength = 50;
0267 
0268   template <typename T,
0269             typename = typename std::enable_if<
0270                 internal::HasDebugStringAndShortDebugString<T>::value>::type>
0271   static void PrintValue(const T& value, ::std::ostream* os) {
0272     std::string pretty_str = value.ShortDebugString();
0273     if (pretty_str.length() > kProtobufOneLinerMaxLength) {
0274       pretty_str = "\n" + value.DebugString();
0275     }
0276     *os << ("<" + pretty_str + ">");
0277   }
0278 };
0279 
0280 struct ConvertibleToIntegerPrinter {
0281   // Since T has no << operator or PrintTo() but can be implicitly
0282   // converted to BiggestInt, we print it as a BiggestInt.
0283   //
0284   // Most likely T is an enum type (either named or unnamed), in which
0285   // case printing it as an integer is the desired behavior.  In case
0286   // T is not an enum, printing it as an integer is the best we can do
0287   // given that it has no user-defined printer.
0288   static void PrintValue(internal::BiggestInt value, ::std::ostream* os) {
0289     *os << value;
0290   }
0291 };
0292 
0293 struct ConvertibleToStringViewPrinter {
0294   static void PrintValue(internal::StringView value, ::std::ostream* os) {
0295     internal::UniversalPrint(value, os);
0296   }
0297 };
0298 
0299 #ifdef GTEST_HAS_ABSL
0300 struct ConvertibleToAbslStringifyPrinter {
0301   template <typename T,
0302             typename = typename std::enable_if<
0303                 absl::HasAbslStringify<T>::value>::type>  // NOLINT
0304   static void PrintValue(const T& value, ::std::ostream* os) {
0305     *os << absl::StrCat(value);
0306   }
0307 };
0308 #endif  // GTEST_HAS_ABSL
0309 
0310 // Prints the given number of bytes in the given object to the given
0311 // ostream.
0312 GTEST_API_ void PrintBytesInObjectTo(const unsigned char* obj_bytes,
0313                                      size_t count, ::std::ostream* os);
0314 struct RawBytesPrinter {
0315   // SFINAE on `sizeof` to make sure we have a complete type.
0316   template <typename T, size_t = sizeof(T)>
0317   static void PrintValue(const T& value, ::std::ostream* os) {
0318     PrintBytesInObjectTo(
0319         static_cast<const unsigned char*>(
0320             // Load bearing cast to void* to support iOS
0321             reinterpret_cast<const void*>(std::addressof(value))),
0322         sizeof(value), os);
0323   }
0324 };
0325 
0326 struct FallbackPrinter {
0327   template <typename T>
0328   static void PrintValue(const T&, ::std::ostream* os) {
0329     *os << "(incomplete type)";
0330   }
0331 };
0332 
0333 // Try every printer in order and return the first one that works.
0334 template <typename T, typename E, typename Printer, typename... Printers>
0335 struct FindFirstPrinter : FindFirstPrinter<T, E, Printers...> {};
0336 
0337 template <typename T, typename Printer, typename... Printers>
0338 struct FindFirstPrinter<
0339     T, decltype(Printer::PrintValue(std::declval<const T&>(), nullptr)),
0340     Printer, Printers...> {
0341   using type = Printer;
0342 };
0343 
0344 // Select the best printer in the following order:
0345 //  - Print containers (they have begin/end/etc).
0346 //  - Print function pointers.
0347 //  - Print object pointers.
0348 //  - Print protocol buffers.
0349 //  - Use the stream operator, if available.
0350 //  - Print types convertible to BiggestInt.
0351 //  - Print types convertible to StringView, if available.
0352 //  - Fallback to printing the raw bytes of the object.
0353 template <typename T>
0354 void PrintWithFallback(const T& value, ::std::ostream* os) {
0355   using Printer = typename FindFirstPrinter<
0356       T, void, ContainerPrinter, FunctionPointerPrinter, PointerPrinter,
0357       ProtobufPrinter,
0358 #ifdef GTEST_HAS_ABSL
0359       ConvertibleToAbslStringifyPrinter,
0360 #endif  // GTEST_HAS_ABSL
0361       internal_stream_operator_without_lexical_name_lookup::StreamPrinter,
0362       ConvertibleToIntegerPrinter, ConvertibleToStringViewPrinter,
0363       RawBytesPrinter, FallbackPrinter>::type;
0364   Printer::PrintValue(value, os);
0365 }
0366 
0367 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a
0368 // value of type ToPrint that is an operand of a comparison assertion
0369 // (e.g. ASSERT_EQ).  OtherOperand is the type of the other operand in
0370 // the comparison, and is used to help determine the best way to
0371 // format the value.  In particular, when the value is a C string
0372 // (char pointer) and the other operand is an STL string object, we
0373 // want to format the C string as a string, since we know it is
0374 // compared by value with the string object.  If the value is a char
0375 // pointer but the other operand is not an STL string object, we don't
0376 // know whether the pointer is supposed to point to a NUL-terminated
0377 // string, and thus want to print it as a pointer to be safe.
0378 //
0379 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
0380 
0381 // The default case.
0382 template <typename ToPrint, typename OtherOperand>
0383 class [[nodiscard]] FormatForComparison {
0384  public:
0385   static ::std::string Format(const ToPrint& value) {
0386     return ::testing::PrintToString(value);
0387   }
0388 };
0389 
0390 // Array.
0391 template <typename ToPrint, size_t N, typename OtherOperand>
0392 class [[nodiscard]] FormatForComparison<ToPrint[N], OtherOperand> {
0393  public:
0394   static ::std::string Format(const ToPrint* value) {
0395     return FormatForComparison<const ToPrint*, OtherOperand>::Format(value);
0396   }
0397 };
0398 
0399 // By default, print C string as pointers to be safe, as we don't know
0400 // whether they actually point to a NUL-terminated string.
0401 
0402 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType)                \
0403   template <typename OtherOperand>                                      \
0404   class FormatForComparison<CharType*, OtherOperand> {                  \
0405    public:                                                              \
0406     static ::std::string Format(CharType* value) {                      \
0407       return ::testing::PrintToString(static_cast<const void*>(value)); \
0408     }                                                                   \
0409   }
0410 
0411 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char);
0412 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char);
0413 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t);
0414 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t);
0415 #ifdef __cpp_lib_char8_t
0416 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char8_t);
0417 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char8_t);
0418 #endif
0419 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char16_t);
0420 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char16_t);
0421 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char32_t);
0422 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char32_t);
0423 
0424 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_
0425 
0426 // If a C string is compared with an STL string object, we know it's meant
0427 // to point to a NUL-terminated string, and thus can print it as a string.
0428 
0429 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \
0430   template <>                                                            \
0431   class FormatForComparison<CharType*, OtherStringType> {                \
0432    public:                                                               \
0433     static ::std::string Format(CharType* value) {                       \
0434       return ::testing::PrintToString(value);                            \
0435     }                                                                    \
0436   }
0437 
0438 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string);
0439 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string);
0440 #ifdef __cpp_lib_char8_t
0441 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char8_t, ::std::u8string);
0442 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char8_t, ::std::u8string);
0443 #endif
0444 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char16_t, ::std::u16string);
0445 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char16_t, ::std::u16string);
0446 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char32_t, ::std::u32string);
0447 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char32_t, ::std::u32string);
0448 
0449 #if GTEST_HAS_STD_WSTRING
0450 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring);
0451 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring);
0452 #endif
0453 
0454 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_
0455 
0456 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc)
0457 // operand to be used in a failure message.  The type (but not value)
0458 // of the other operand may affect the format.  This allows us to
0459 // print a char* as a raw pointer when it is compared against another
0460 // char* or void*, and print it as a C string when it is compared
0461 // against an std::string object, for example.
0462 //
0463 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM.
0464 template <typename T1, typename T2>
0465 std::string FormatForComparisonFailureMessage(const T1& value,
0466                                               const T2& /* other_operand */) {
0467   return FormatForComparison<T1, T2>::Format(value);
0468 }
0469 
0470 // UniversalPrinter<T>::Print(value, ostream_ptr) prints the given
0471 // value to the given ostream.  The caller must ensure that
0472 // 'ostream_ptr' is not NULL, or the behavior is undefined.
0473 //
0474 // We define UniversalPrinter as a class template (as opposed to a
0475 // function template), as we need to partially specialize it for
0476 // reference types, which cannot be done with function templates.
0477 template <typename T>
0478 class [[nodiscard]] UniversalPrinter;
0479 
0480 // Prints the given value using the << operator if it has one;
0481 // otherwise prints the bytes in it.  This is what
0482 // UniversalPrinter<T>::Print() does when PrintTo() is not specialized
0483 // or overloaded for type T.
0484 //
0485 // A user can override this behavior for a class type Foo by defining
0486 // an overload of PrintTo() in the namespace where Foo is defined.  We
0487 // give the user this option as sometimes defining a << operator for
0488 // Foo is not desirable (e.g. the coding style may prevent doing it,
0489 // or there is already a << operator but it doesn't do what the user
0490 // wants).
0491 template <typename T>
0492 void PrintTo(const T& value, ::std::ostream* os) {
0493   internal::PrintWithFallback(value, os);
0494 }
0495 
0496 // The following list of PrintTo() overloads tells
0497 // UniversalPrinter<T>::Print() how to print standard types (built-in
0498 // types, strings, plain arrays, and pointers).
0499 
0500 // Overloads for various char types.
0501 GTEST_API_ void PrintTo(unsigned char c, ::std::ostream* os);
0502 GTEST_API_ void PrintTo(signed char c, ::std::ostream* os);
0503 inline void PrintTo(char c, ::std::ostream* os) {
0504   // When printing a plain char, we always treat it as unsigned.  This
0505   // way, the output won't be affected by whether the compiler thinks
0506   // char is signed or not.
0507   PrintTo(static_cast<unsigned char>(c), os);
0508 }
0509 
0510 // Overloads for other simple built-in types.
0511 inline void PrintTo(bool x, ::std::ostream* os) {
0512   *os << (x ? "true" : "false");
0513 }
0514 
0515 // Overload for wchar_t type.
0516 // Prints a wchar_t as a symbol if it is printable or as its internal
0517 // code otherwise and also as its decimal code (except for L'\0').
0518 // The L'\0' char is printed as "L'\\0'". The decimal code is printed
0519 // as signed integer when wchar_t is implemented by the compiler
0520 // as a signed type and is printed as an unsigned integer when wchar_t
0521 // is implemented as an unsigned type.
0522 GTEST_API_ void PrintTo(wchar_t wc, ::std::ostream* os);
0523 
0524 GTEST_API_ void PrintTo(char32_t c, ::std::ostream* os);
0525 inline void PrintTo(char16_t c, ::std::ostream* os) {
0526   // TODO(b/418738869): Incorrect for values not representing valid codepoints.
0527   // Also see https://github.com/google/googletest/issues/4762.
0528   PrintTo(static_cast<char32_t>(c), os);
0529 }
0530 #ifdef __cpp_lib_char8_t
0531 inline void PrintTo(char8_t c, ::std::ostream* os) {
0532   // TODO(b/418738869): Incorrect for values not representing valid codepoints.
0533   // Also see https://github.com/google/googletest/issues/4762.
0534   PrintTo(static_cast<char32_t>(c), os);
0535 }
0536 #endif
0537 
0538 // gcc/clang __{u,}int128_t
0539 #if defined(__SIZEOF_INT128__)
0540 GTEST_API_ void PrintTo(__uint128_t v, ::std::ostream* os);
0541 GTEST_API_ void PrintTo(__int128_t v, ::std::ostream* os);
0542 #endif  // __SIZEOF_INT128__
0543 
0544 // The default resolution used to print floating-point values uses only
0545 // 6 digits, which can be confusing if a test compares two values whose
0546 // difference lies in the 7th digit.  So we'd like to print out numbers
0547 // in full precision.
0548 // However if the value is something simple like 1.1, full will print a
0549 // long string like 1.100000001 due to floating-point numbers not using
0550 // a base of 10.  This routiune returns an appropriate resolution for a
0551 // given floating-point number, that is, 6 if it will be accurate, or a
0552 // max_digits10 value (full precision) if it won't,  for values between
0553 // 0.0001 and one million.
0554 // It does this by computing what those digits would be (by multiplying
0555 // by an appropriate power of 10), then dividing by that power again to
0556 // see if gets the original value back.
0557 // A similar algorithm applies for values larger than one million; note
0558 // that for those values, we must divide to get a six-digit number, and
0559 // then multiply to possibly get the original value again.
0560 template <typename FloatType>
0561 int AppropriateResolution(FloatType val) {
0562   int full = std::numeric_limits<FloatType>::max_digits10;
0563   if (val < 0) val = -val;
0564 
0565 #ifdef __GNUC__
0566 #pragma GCC diagnostic push
0567 #pragma GCC diagnostic ignored "-Wfloat-equal"
0568 #endif
0569   if (val < 1000000) {
0570     FloatType mulfor6 = 1e10;
0571     // Without these static casts, the template instantiation for float would
0572     // fail to compile when -Wdouble-promotion is enabled, as the arithmetic and
0573     // comparison logic would promote floats to doubles.
0574     if (val >= static_cast<FloatType>(100000.0)) {  // 100,000 to 999,999
0575       mulfor6 = 1.0;
0576     } else if (val >= static_cast<FloatType>(10000.0)) {
0577       mulfor6 = 1e1;
0578     } else if (val >= static_cast<FloatType>(1000.0)) {
0579       mulfor6 = 1e2;
0580     } else if (val >= static_cast<FloatType>(100.0)) {
0581       mulfor6 = 1e3;
0582     } else if (val >= static_cast<FloatType>(10.0)) {
0583       mulfor6 = 1e4;
0584     } else if (val >= static_cast<FloatType>(1.0)) {
0585       mulfor6 = 1e5;
0586     } else if (val >= static_cast<FloatType>(0.1)) {
0587       mulfor6 = 1e6;
0588     } else if (val >= static_cast<FloatType>(0.01)) {
0589       mulfor6 = 1e7;
0590     } else if (val >= static_cast<FloatType>(0.001)) {
0591       mulfor6 = 1e8;
0592     } else if (val >= static_cast<FloatType>(0.0001)) {
0593       mulfor6 = 1e9;
0594     }
0595     if (static_cast<FloatType>(static_cast<int32_t>(
0596             val * mulfor6 + (static_cast<FloatType>(0.5)))) /
0597             mulfor6 ==
0598         val)
0599       return 6;
0600   } else if (val < static_cast<FloatType>(1e10)) {
0601     FloatType divfor6 = static_cast<FloatType>(1.0);
0602     if (val >= static_cast<FloatType>(1e9)) {  // 1,000,000,000 to 9,999,999,999
0603       divfor6 = 10000;
0604     } else if (val >=
0605                static_cast<FloatType>(1e8)) {  // 100,000,000 to 999,999,999
0606       divfor6 = 1000;
0607     } else if (val >=
0608                static_cast<FloatType>(1e7)) {  // 10,000,000 to 99,999,999
0609       divfor6 = 100;
0610     } else if (val >= static_cast<FloatType>(1e6)) {  // 1,000,000 to 9,999,999
0611       divfor6 = 10;
0612     }
0613     if (static_cast<FloatType>(static_cast<int32_t>(
0614             val / divfor6 + (static_cast<FloatType>(0.5)))) *
0615             divfor6 ==
0616         val)
0617       return 6;
0618   }
0619 #ifdef __GNUC__
0620 #pragma GCC diagnostic pop
0621 #endif
0622   return full;
0623 }
0624 
0625 inline void PrintTo(float f, ::std::ostream* os) {
0626   auto old_precision = os->precision();
0627   os->precision(AppropriateResolution(f));
0628   *os << f;
0629   os->precision(old_precision);
0630 }
0631 
0632 inline void PrintTo(double d, ::std::ostream* os) {
0633   auto old_precision = os->precision();
0634   os->precision(AppropriateResolution(d));
0635   *os << d;
0636   os->precision(old_precision);
0637 }
0638 
0639 // Overloads for C strings.
0640 GTEST_API_ void PrintTo(const char* s, ::std::ostream* os);
0641 inline void PrintTo(char* s, ::std::ostream* os) {
0642   PrintTo(ImplicitCast_<const char*>(s), os);
0643 }
0644 
0645 // signed/unsigned char is often used for representing binary data, so
0646 // we print pointers to it as void* to be safe.
0647 inline void PrintTo(const signed char* s, ::std::ostream* os) {
0648   PrintTo(ImplicitCast_<const void*>(s), os);
0649 }
0650 inline void PrintTo(signed char* s, ::std::ostream* os) {
0651   PrintTo(ImplicitCast_<const void*>(s), os);
0652 }
0653 inline void PrintTo(const unsigned char* s, ::std::ostream* os) {
0654   PrintTo(ImplicitCast_<const void*>(s), os);
0655 }
0656 inline void PrintTo(unsigned char* s, ::std::ostream* os) {
0657   PrintTo(ImplicitCast_<const void*>(s), os);
0658 }
0659 #ifdef __cpp_lib_char8_t
0660 // Overloads for u8 strings.
0661 GTEST_API_ void PrintTo(const char8_t* s, ::std::ostream* os);
0662 inline void PrintTo(char8_t* s, ::std::ostream* os) {
0663   PrintTo(ImplicitCast_<const char8_t*>(s), os);
0664 }
0665 #endif
0666 // Overloads for u16 strings.
0667 GTEST_API_ void PrintTo(const char16_t* s, ::std::ostream* os);
0668 inline void PrintTo(char16_t* s, ::std::ostream* os) {
0669   PrintTo(ImplicitCast_<const char16_t*>(s), os);
0670 }
0671 // Overloads for u32 strings.
0672 GTEST_API_ void PrintTo(const char32_t* s, ::std::ostream* os);
0673 inline void PrintTo(char32_t* s, ::std::ostream* os) {
0674   PrintTo(ImplicitCast_<const char32_t*>(s), os);
0675 }
0676 
0677 // MSVC can be configured to define wchar_t as a typedef of unsigned
0678 // short.  It defines _NATIVE_WCHAR_T_DEFINED when wchar_t is a native
0679 // type.  When wchar_t is a typedef, defining an overload for const
0680 // wchar_t* would cause unsigned short* be printed as a wide string,
0681 // possibly causing invalid memory accesses.
0682 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
0683 // Overloads for wide C strings
0684 GTEST_API_ void PrintTo(const wchar_t* s, ::std::ostream* os);
0685 inline void PrintTo(wchar_t* s, ::std::ostream* os) {
0686   PrintTo(ImplicitCast_<const wchar_t*>(s), os);
0687 }
0688 #endif
0689 
0690 // Overload for C arrays.  Multi-dimensional arrays are printed
0691 // properly.
0692 
0693 // Prints the given number of elements in an array, without printing
0694 // the curly braces.
0695 template <typename T>
0696 void PrintRawArrayTo(const T a[], size_t count, ::std::ostream* os) {
0697   UniversalPrint(a[0], os);
0698   for (size_t i = 1; i != count; i++) {
0699     *os << ", ";
0700     UniversalPrint(a[i], os);
0701   }
0702 }
0703 
0704 // Overloads for ::std::string and std::string_view
0705 GTEST_API_ void PrintStringTo(std::string_view s, ::std::ostream* os);
0706 inline void PrintTo(const ::std::string& s, ::std::ostream* os) {
0707   PrintStringTo(s, os);
0708 }
0709 inline void PrintTo(std::string_view s, ::std::ostream* os) {
0710   PrintStringTo(s, os);
0711 }
0712 
0713 // Overloads for ::std::u8string and ::std::u8string_view
0714 #ifdef __cpp_lib_char8_t
0715 GTEST_API_ void PrintU8StringTo(::std::u8string_view s, ::std::ostream* os);
0716 inline void PrintTo(const ::std::u8string& s, ::std::ostream* os) {
0717   PrintU8StringTo(s, os);
0718 }
0719 inline void PrintTo(::std::u8string_view s, ::std::ostream* os) {
0720   PrintU8StringTo(s, os);
0721 }
0722 #endif
0723 
0724 // Overloads for ::std::u16string and ::std::u16string_view
0725 GTEST_API_ void PrintU16StringTo(::std::u16string_view s, ::std::ostream* os);
0726 inline void PrintTo(const ::std::u16string& s, ::std::ostream* os) {
0727   PrintU16StringTo(s, os);
0728 }
0729 inline void PrintTo(::std::u16string_view s, ::std::ostream* os) {
0730   PrintU16StringTo(s, os);
0731 }
0732 
0733 // Overloads for ::std::u32string and ::std::u32string_view
0734 GTEST_API_ void PrintU32StringTo(::std::u32string_view s, ::std::ostream* os);
0735 inline void PrintTo(const ::std::u32string& s, ::std::ostream* os) {
0736   PrintU32StringTo(s, os);
0737 }
0738 inline void PrintTo(::std::u32string_view s, ::std::ostream* os) {
0739   PrintU32StringTo(s, os);
0740 }
0741 
0742 // Overloads for ::std::wstring and ::std::wstring_view
0743 #if GTEST_HAS_STD_WSTRING
0744 GTEST_API_ void PrintWideStringTo(::std::wstring_view s, ::std::ostream* os);
0745 inline void PrintTo(const ::std::wstring& s, ::std::ostream* os) {
0746   PrintWideStringTo(s, os);
0747 }
0748 inline void PrintTo(::std::wstring_view s, ::std::ostream* os) {
0749   PrintWideStringTo(s, os);
0750 }
0751 #endif  // GTEST_HAS_STD_WSTRING
0752 
0753 // Overload for internal::StringView. Needed for build configurations where
0754 // internal::StringView is an alias for absl::string_view, but absl::string_view
0755 // is a distinct type from std::string_view.
0756 template <int&... ExplicitArgumentBarrier, typename T = internal::StringView,
0757           std::enable_if_t<!std::is_same_v<T, std::string_view>, int> = 0>
0758 inline void PrintTo(internal::StringView sp, ::std::ostream* os) {
0759   PrintStringTo(sp, os);
0760 }
0761 
0762 inline void PrintTo(std::nullptr_t, ::std::ostream* os) { *os << "(nullptr)"; }
0763 
0764 #if GTEST_HAS_RTTI
0765 inline void PrintTo(const std::type_info& info, std::ostream* os) {
0766   *os << internal::GetTypeName(info);
0767 }
0768 #endif  // GTEST_HAS_RTTI
0769 
0770 template <typename T>
0771 void PrintTo(std::reference_wrapper<T> ref, ::std::ostream* os) {
0772   UniversalPrinter<T&>::Print(ref.get(), os);
0773 }
0774 
0775 inline const void* VoidifyPointer(const void* p) { return p; }
0776 inline const void* VoidifyPointer(volatile const void* p) {
0777   return const_cast<const void*>(p);
0778 }
0779 
0780 template <typename T, typename Ptr>
0781 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, char) {
0782   if (ptr == nullptr) {
0783     *os << "(nullptr)";
0784   } else {
0785     // We can't print the value. Just print the pointer..
0786     *os << "(" << (VoidifyPointer)(ptr.get()) << ")";
0787   }
0788 }
0789 template <typename T, typename Ptr,
0790           typename = typename std::enable_if<!std::is_void<T>::value &&
0791                                              !std::is_array<T>::value>::type>
0792 void PrintSmartPointer(const Ptr& ptr, std::ostream* os, int) {
0793   if (ptr == nullptr) {
0794     *os << "(nullptr)";
0795   } else {
0796     *os << "(ptr = " << (VoidifyPointer)(ptr.get()) << ", value = ";
0797     UniversalPrinter<T>::Print(*ptr, os);
0798     *os << ")";
0799   }
0800 }
0801 
0802 template <typename T, typename D>
0803 void PrintTo(const std::unique_ptr<T, D>& ptr, std::ostream* os) {
0804   (PrintSmartPointer<T>)(ptr, os, 0);
0805 }
0806 
0807 template <typename T>
0808 void PrintTo(const std::shared_ptr<T>& ptr, std::ostream* os) {
0809   (PrintSmartPointer<T>)(ptr, os, 0);
0810 }
0811 
0812 #if GTEST_INTERNAL_HAS_COMPARE_LIB
0813 template <typename T>
0814 void PrintOrderingHelper(T ordering, std::ostream* os) {
0815   if (ordering == T::less) {
0816     *os << "(less)";
0817   } else if (ordering == T::greater) {
0818     *os << "(greater)";
0819   } else if (ordering == T::equivalent) {
0820     *os << "(equivalent)";
0821   } else {
0822     *os << "(unknown ordering)";
0823   }
0824 }
0825 
0826 inline void PrintTo(std::strong_ordering ordering, std::ostream* os) {
0827   if (ordering == std::strong_ordering::equal) {
0828     *os << "(equal)";
0829   } else {
0830     PrintOrderingHelper(ordering, os);
0831   }
0832 }
0833 
0834 inline void PrintTo(std::partial_ordering ordering, std::ostream* os) {
0835   if (ordering == std::partial_ordering::unordered) {
0836     *os << "(unordered)";
0837   } else {
0838     PrintOrderingHelper(ordering, os);
0839   }
0840 }
0841 
0842 inline void PrintTo(std::weak_ordering ordering, std::ostream* os) {
0843   PrintOrderingHelper(ordering, os);
0844 }
0845 #endif
0846 
0847 // Helper function for printing a tuple.  T must be instantiated with
0848 // a tuple type.
0849 template <typename T>
0850 void PrintTupleTo(const T&, std::integral_constant<size_t, 0>,
0851                   ::std::ostream*) {}
0852 
0853 template <typename T, size_t I>
0854 void PrintTupleTo(const T& t, std::integral_constant<size_t, I>,
0855                   ::std::ostream* os) {
0856   PrintTupleTo(t, std::integral_constant<size_t, I - 1>(), os);
0857   GTEST_INTENTIONAL_CONST_COND_PUSH_()
0858   if (I > 1) {
0859     GTEST_INTENTIONAL_CONST_COND_POP_()
0860     *os << ", ";
0861   }
0862   UniversalPrinter<std::tuple_element_t<I - 1, T>>::Print(std::get<I - 1>(t),
0863                                                           os);
0864 }
0865 
0866 template <typename... Types>
0867 void PrintTo(const ::std::tuple<Types...>& t, ::std::ostream* os) {
0868   *os << "(";
0869   PrintTupleTo(t, std::integral_constant<size_t, sizeof...(Types)>(), os);
0870   *os << ")";
0871 }
0872 
0873 // Overload for std::pair.
0874 template <typename T1, typename T2>
0875 void PrintTo(const ::std::pair<T1, T2>& value, ::std::ostream* os) {
0876   *os << '(';
0877   // We cannot use UniversalPrint(value.first, os) here, as T1 may be
0878   // a reference type.  The same for printing value.second.
0879   UniversalPrinter<T1>::Print(value.first, os);
0880   *os << ", ";
0881   UniversalPrinter<T2>::Print(value.second, os);
0882   *os << ')';
0883 }
0884 
0885 // Implements printing a non-reference type T by letting the compiler
0886 // pick the right overload of PrintTo() for T.
0887 template <typename T>
0888 class [[nodiscard]] UniversalPrinter {
0889  public:
0890   // MSVC warns about adding const to a function type, so we want to
0891   // disable the warning.
0892   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
0893 
0894   // Note: we deliberately don't call this PrintTo(), as that name
0895   // conflicts with ::testing::internal::PrintTo in the body of the
0896   // function.
0897   static void Print(const T& value, ::std::ostream* os) {
0898     // By default, ::testing::internal::PrintTo() is used for printing
0899     // the value.
0900     //
0901     // Thanks to Koenig look-up, if T is a class and has its own
0902     // PrintTo() function defined in its namespace, that function will
0903     // be visible here.  Since it is more specific than the generic ones
0904     // in ::testing::internal, it will be picked by the compiler in the
0905     // following statement - exactly what we want.
0906     PrintTo(value, os);
0907   }
0908 
0909   GTEST_DISABLE_MSC_WARNINGS_POP_()
0910 };
0911 
0912 // Remove any const-qualifiers before passing a type to UniversalPrinter.
0913 template <typename T>
0914 class [[nodiscard]] UniversalPrinter<const T> : public UniversalPrinter<T> {};
0915 
0916 // Printer for std::any
0917 template <>
0918 class [[nodiscard]] UniversalPrinter<std::any> {
0919  public:
0920   static void Print(const std::any& value, ::std::ostream* os) {
0921     if (value.has_value()) {
0922       *os << "value of type " << GetTypeName(value);
0923     } else {
0924       *os << "no value";
0925     }
0926   }
0927 
0928  private:
0929   static std::string GetTypeName(const std::any& value) {
0930 #if GTEST_HAS_RTTI
0931     return internal::GetTypeName(value.type());
0932 #else
0933     static_cast<void>(value);  // possibly unused
0934     return "<unknown_type>";
0935 #endif  // GTEST_HAS_RTTI
0936   }
0937 };
0938 
0939 // Printer for std::optional
0940 template <typename T>
0941 class [[nodiscard]] UniversalPrinter<std::optional<T>> {
0942  public:
0943   static void Print(const std::optional<T>& value, ::std::ostream* os) {
0944     if (!value) {
0945       UniversalPrint(std::nullopt, os);
0946     } else {
0947       *os << '(';
0948       UniversalPrint(*value, os);
0949       *os << ')';
0950     }
0951   }
0952 };
0953 
0954 template <>
0955 class [[nodiscard]] UniversalPrinter<std::nullopt_t> {
0956  public:
0957   static void Print(std::nullopt_t, ::std::ostream* os) { *os << "(nullopt)"; }
0958 };
0959 
0960 struct UniversalPrinterVisitor {
0961   template <typename T>
0962   void operator()(const T& arg) const {
0963     *os << "'" << GetTypeName<T>() << "(index = " << index << ")' with value ";
0964     UniversalPrint(arg, os);
0965   }
0966   ::std::ostream* os;
0967   std::size_t index;
0968 };
0969 
0970 // Printer for std::variant
0971 template <typename... T>
0972 class [[nodiscard]] UniversalPrinter<std::variant<T...>> {
0973  public:
0974   static void Print(const std::variant<T...>& value, ::std::ostream* os) {
0975     if (value.valueless_by_exception()) {
0976       *os << "(valueless)";
0977     } else {
0978       *os << '(';
0979       std::visit(UniversalPrinterVisitor{os, value.index()}, value);
0980       *os << ')';
0981     }
0982   }
0983 };
0984 
0985 // Printer for std::monostate
0986 template <>
0987 class [[nodiscard]] UniversalPrinter<std::monostate> {
0988  public:
0989   static void Print(std::monostate, ::std::ostream* os) {
0990     *os << "(monostate)";
0991   }
0992 };
0993 
0994 // UniversalPrintArray(begin, len, os) prints an array of 'len'
0995 // elements, starting at address 'begin'.
0996 template <typename T>
0997 void UniversalPrintArray(const T* begin, size_t len, ::std::ostream* os) {
0998   if (len == 0) {
0999     *os << "{}";
1000   } else {
1001     *os << "{ ";
1002     const size_t kThreshold = 18;
1003     const size_t kChunkSize = 8;
1004     // If the array has more than kThreshold elements, we'll have to
1005     // omit some details by printing only the first and the last
1006     // kChunkSize elements.
1007     if (len <= kThreshold) {
1008       PrintRawArrayTo(begin, len, os);
1009     } else {
1010       PrintRawArrayTo(begin, kChunkSize, os);
1011       *os << ", ..., ";
1012       PrintRawArrayTo(begin + len - kChunkSize, kChunkSize, os);
1013     }
1014     *os << " }";
1015   }
1016 }
1017 // This overload prints a (const) char array compactly.
1018 GTEST_API_ void UniversalPrintArray(const char* begin, size_t len,
1019                                     ::std::ostream* os);
1020 
1021 #ifdef __cpp_lib_char8_t
1022 // This overload prints a (const) char8_t array compactly.
1023 GTEST_API_ void UniversalPrintArray(const char8_t* begin, size_t len,
1024                                     ::std::ostream* os);
1025 #endif
1026 
1027 // This overload prints a (const) char16_t array compactly.
1028 GTEST_API_ void UniversalPrintArray(const char16_t* begin, size_t len,
1029                                     ::std::ostream* os);
1030 
1031 // This overload prints a (const) char32_t array compactly.
1032 GTEST_API_ void UniversalPrintArray(const char32_t* begin, size_t len,
1033                                     ::std::ostream* os);
1034 
1035 // This overload prints a (const) wchar_t array compactly.
1036 GTEST_API_ void UniversalPrintArray(const wchar_t* begin, size_t len,
1037                                     ::std::ostream* os);
1038 
1039 // Implements printing an array type T[N].
1040 template <typename T, size_t N>
1041 class [[nodiscard]] UniversalPrinter<T[N]> {
1042  public:
1043   // Prints the given array, omitting some elements when there are too
1044   // many.
1045   static void Print(const T (&a)[N], ::std::ostream* os) {
1046     UniversalPrintArray(a, N, os);
1047   }
1048 };
1049 
1050 // Implements printing a reference type T&.
1051 template <typename T>
1052 class [[nodiscard]] UniversalPrinter<T&> {
1053  public:
1054   // MSVC warns about adding const to a function type, so we want to
1055   // disable the warning.
1056   GTEST_DISABLE_MSC_WARNINGS_PUSH_(4180)
1057 
1058   static void Print(const T& value, ::std::ostream* os) {
1059     // Prints the address of the value.  We use reinterpret_cast here
1060     // as static_cast doesn't compile when T is a function type.
1061     *os << "@" << reinterpret_cast<const void*>(&value) << " ";
1062 
1063     // Then prints the value itself.
1064     UniversalPrint(value, os);
1065   }
1066 
1067   GTEST_DISABLE_MSC_WARNINGS_POP_()
1068 };
1069 
1070 // Prints a value tersely: for a reference type, the referenced value
1071 // (but not the address) is printed; for a (const) char pointer, the
1072 // NUL-terminated string (but not the pointer) is printed.
1073 
1074 template <typename T>
1075 class [[nodiscard]] UniversalTersePrinter {
1076  public:
1077   static void Print(const T& value, ::std::ostream* os) {
1078     UniversalPrint(value, os);
1079   }
1080 };
1081 template <typename T>
1082 class [[nodiscard]] UniversalTersePrinter<T&> {
1083  public:
1084   static void Print(const T& value, ::std::ostream* os) {
1085     UniversalPrint(value, os);
1086   }
1087 };
1088 template <typename T>
1089 class [[nodiscard]] UniversalTersePrinter<std::reference_wrapper<T>> {
1090  public:
1091   static void Print(std::reference_wrapper<T> value, ::std::ostream* os) {
1092     UniversalTersePrinter<T>::Print(value.get(), os);
1093   }
1094 };
1095 template <typename T, size_t N>
1096 class [[nodiscard]] UniversalTersePrinter<T[N]> {
1097  public:
1098   static void Print(const T (&value)[N], ::std::ostream* os) {
1099     UniversalPrinter<T[N]>::Print(value, os);
1100   }
1101 };
1102 template <>
1103 class [[nodiscard]] UniversalTersePrinter<const char*> {
1104  public:
1105   static void Print(const char* str, ::std::ostream* os) {
1106     if (str == nullptr) {
1107       *os << "NULL";
1108     } else {
1109       UniversalPrint(std::string(str), os);
1110     }
1111   }
1112 };
1113 template <>
1114 class [[nodiscard]]
1115 UniversalTersePrinter<char*> : public UniversalTersePrinter<const char*> {};
1116 
1117 #ifdef __cpp_lib_char8_t
1118 template <>
1119 class [[nodiscard]] UniversalTersePrinter<const char8_t*> {
1120  public:
1121   static void Print(const char8_t* str, ::std::ostream* os) {
1122     if (str == nullptr) {
1123       *os << "NULL";
1124     } else {
1125       UniversalPrint(::std::u8string(str), os);
1126     }
1127   }
1128 };
1129 template <>
1130 class [[nodiscard]] UniversalTersePrinter<char8_t*>
1131     : public UniversalTersePrinter<const char8_t*> {};
1132 #endif
1133 
1134 template <>
1135 class [[nodiscard]] UniversalTersePrinter<const char16_t*> {
1136  public:
1137   static void Print(const char16_t* str, ::std::ostream* os) {
1138     if (str == nullptr) {
1139       *os << "NULL";
1140     } else {
1141       UniversalPrint(::std::u16string(str), os);
1142     }
1143   }
1144 };
1145 template <>
1146 class [[nodiscard]] UniversalTersePrinter<char16_t*>
1147     : public UniversalTersePrinter<const char16_t*> {};
1148 
1149 template <>
1150 class [[nodiscard]] UniversalTersePrinter<const char32_t*> {
1151  public:
1152   static void Print(const char32_t* str, ::std::ostream* os) {
1153     if (str == nullptr) {
1154       *os << "NULL";
1155     } else {
1156       UniversalPrint(::std::u32string(str), os);
1157     }
1158   }
1159 };
1160 template <>
1161 class [[nodiscard]] UniversalTersePrinter<char32_t*>
1162     : public UniversalTersePrinter<const char32_t*> {};
1163 
1164 #if GTEST_HAS_STD_WSTRING
1165 template <>
1166 class [[nodiscard]] UniversalTersePrinter<const wchar_t*> {
1167  public:
1168   static void Print(const wchar_t* str, ::std::ostream* os) {
1169     if (str == nullptr) {
1170       *os << "NULL";
1171     } else {
1172       UniversalPrint(::std::wstring(str), os);
1173     }
1174   }
1175 };
1176 #endif
1177 
1178 template <>
1179 class [[nodiscard]] UniversalTersePrinter<wchar_t*> {
1180  public:
1181   static void Print(wchar_t* str, ::std::ostream* os) {
1182     UniversalTersePrinter<const wchar_t*>::Print(str, os);
1183   }
1184 };
1185 
1186 template <typename T>
1187 void UniversalTersePrint(const T& value, ::std::ostream* os) {
1188   UniversalTersePrinter<T>::Print(value, os);
1189 }
1190 
1191 // Prints a value using the type inferred by the compiler.  The
1192 // difference between this and UniversalTersePrint() is that for a
1193 // (const) char pointer, this prints both the pointer and the
1194 // NUL-terminated string.
1195 template <typename T>
1196 void UniversalPrint(const T& value, ::std::ostream* os) {
1197   // A workarond for the bug in VC++ 7.1 that prevents us from instantiating
1198   // UniversalPrinter with T directly.
1199   typedef T T1;
1200   UniversalPrinter<T1>::Print(value, os);
1201 }
1202 
1203 typedef ::std::vector<::std::string> Strings;
1204 
1205 // Tersely prints the first N fields of a tuple to a string vector,
1206 // one element for each field.
1207 template <typename Tuple>
1208 void TersePrintPrefixToStrings(const Tuple&, std::integral_constant<size_t, 0>,
1209                                Strings*) {}
1210 template <typename Tuple, size_t I>
1211 void TersePrintPrefixToStrings(const Tuple& t,
1212                                std::integral_constant<size_t, I>,
1213                                Strings* strings) {
1214   TersePrintPrefixToStrings(t, std::integral_constant<size_t, I - 1>(),
1215                             strings);
1216   ::std::stringstream ss;
1217   UniversalTersePrint(std::get<I - 1>(t), &ss);
1218   strings->push_back(ss.str());
1219 }
1220 
1221 // Prints the fields of a tuple tersely to a string vector, one
1222 // element for each field.  See the comment before
1223 // UniversalTersePrint() for how we define "tersely".
1224 template <typename Tuple>
1225 Strings UniversalTersePrintTupleFieldsToStrings(const Tuple& value) {
1226   Strings result;
1227   TersePrintPrefixToStrings(
1228       value, std::integral_constant<size_t, std::tuple_size_v<Tuple>>(),
1229       &result);
1230   return result;
1231 }
1232 
1233 }  // namespace internal
1234 
1235 template <typename T>
1236 ::std::string PrintToString(const T& value) {
1237   ::std::stringstream ss;
1238   internal::UniversalTersePrinter<T>::Print(value, &ss);
1239   return ss.str();
1240 }
1241 
1242 }  // namespace testing
1243 
1244 // Include any custom printer added by the local installation.
1245 // We must include this header at the end to make sure it can use the
1246 // declarations from this file.
1247 #include "gtest/internal/custom/gtest-printers.h"
1248 
1249 #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_PRINTERS_H_