44 #include "gtest/gtest-printers.h"
53 #include <type_traits>
55 #include "gtest/internal/gtest-port.h"
56 #include "src/gtest-internal-inl.h"
69 void PrintByteSegmentInObjectTo(
const unsigned char* obj_bytes,
size_t start,
70 size_t count, ostream* os) {
72 for (
size_t i = 0;
i !=
count;
i++) {
88 void PrintBytesInObjectToImpl(
const unsigned char* obj_bytes,
size_t count,
91 *os <<
count <<
"-byte object <";
93 const size_t kThreshold = 132;
98 if (
count < kThreshold) {
99 PrintByteSegmentInObjectTo(obj_bytes, 0,
count, os);
101 PrintByteSegmentInObjectTo(obj_bytes, 0,
kChunkSize, os);
105 PrintByteSegmentInObjectTo(obj_bytes, resume_pos,
count - resume_pos, os);
114 template <
typename CharType>
115 char32_t ToChar32(CharType
in) {
116 return static_cast<char32_t
>(
131 PrintBytesInObjectToImpl(obj_bytes,
count, os);
153 template <
typename Char>
155 const char32_t u_c = ToChar32(c);
189 *os << static_cast<char>(c);
192 ostream::fmtflags
flags = os->flags();
193 *os <<
"\\x" << std::hex << std::uppercase << static_cast<int>(u_c);
269 template <
typename Char>
281 *os <<
" (" <<
static_cast<int>(c);
302 void PrintTo(char32_t c, ::std::ostream* os) {
303 *os << std::hex <<
"U+" << std::uppercase << std::setfill(
'0') << std::setw(4)
308 #if defined(__SIZEOF_INT128__)
309 void PrintTo(__uint128_t
v, ::std::ostream* os) {
326 while (high != 0 || low != 0) {
334 const uint64_t carry = 6 * high_mod + low % 10;
335 low = low / 10 + high_mod * 1844674407370955161 + carry / 10;
337 char digit =
static_cast<char>(carry % 10);
342 void PrintTo(__int128_t
v, ::std::ostream* os) {
343 __uint128_t uv =
static_cast<__uint128_t
>(
v);
350 #endif // __SIZEOF_INT128__
356 template <
typename CharType>
362 const CharType*
begin,
size_t len, ostream* os) {
364 *os << quote_prefix <<
"\"";
365 bool is_previous_hex =
false;
373 *os <<
"\" " << quote_prefix <<
"\"";
377 if (is_previous_hex) {
387 template <
typename CharType>
393 const CharType*
begin,
size_t len, ostream* os) {
411 *os <<
" (no terminating NUL)";
448 template <
typename Char>
449 void PrintCStringTo(
const Char* s, ostream* os) {
453 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
460 void PrintTo(
const char* s, ostream* os) { PrintCStringTo(s, os); }
463 void PrintTo(
const char8_t* s, ostream* os) { PrintCStringTo(s, os); }
466 void PrintTo(
const char16_t* s, ostream* os) { PrintCStringTo(s, os); }
468 void PrintTo(
const char32_t* s, ostream* os) { PrintCStringTo(s, os); }
476 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
478 void PrintTo(
const wchar_t* s, ostream* os) { PrintCStringTo(s, os); }
479 #endif // wchar_t is native
483 bool ContainsUnprintableControlCodes(
const char*
str,
size_t length) {
484 const unsigned char *s =
reinterpret_cast<const unsigned char *
>(
str);
486 for (
size_t i = 0;
i <
length;
i++) {
487 unsigned char ch = *s++;
488 if (std::iscntrl(
ch)) {
502 bool IsUTF8TrailByte(
unsigned char t) {
return 0x80 <=
t &&
t<= 0xbf; }
505 const unsigned char *
s =
reinterpret_cast<const unsigned char *
>(
str);
507 for (
size_t i = 0;
i <
length;) {
508 unsigned char lead =
s[
i++];
515 }
else if (lead <= 0xdf && (i + 1) <=
length && IsUTF8TrailByte(s[i])) {
517 }
else if (0xe0 <= lead && lead <= 0xef && (i + 2) <=
length &&
518 IsUTF8TrailByte(s[i]) &&
519 IsUTF8TrailByte(s[i + 1]) &&
521 (lead != 0xe0 ||
s[
i] >= 0xa0) &&
522 (lead != 0xed || s[i] < 0xa0)) {
524 }
else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <=
length &&
525 IsUTF8TrailByte(s[i]) &&
526 IsUTF8TrailByte(s[i + 1]) &&
527 IsUTF8TrailByte(s[i + 2]) &&
529 (lead != 0xf0 ||
s[
i] >= 0x90) &&
530 (lead != 0xf4 || s[i] < 0x90)) {
539 void ConditionalPrintAsText(
const char*
str,
size_t length, ostream* os) {
540 if (!ContainsUnprintableControlCodes(
str,
length) &&
542 *os <<
"\n As Text: \"" <<
str <<
"\"";
551 ConditionalPrintAsText(
s.data(),
s.size(), os);
557 void PrintU8StringTo(const ::std::u8string& s, ostream* os) {
570 #if GTEST_HAS_STD_WSTRING
574 #endif // GTEST_HAS_STD_WSTRING