54 #include <type_traits>
70 void PrintByteSegmentInObjectTo(
const unsigned char* obj_bytes,
size_t start,
71 size_t count, ostream* os) {
73 for (
size_t i = 0;
i !=
count;
i++) {
74 const size_t j = start +
i;
89 void PrintBytesInObjectToImpl(
const unsigned char* obj_bytes,
size_t count,
92 *os << count <<
"-byte object <";
94 const size_t kThreshold = 132;
95 const size_t kChunkSize = 64;
99 if (count < kThreshold) {
100 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
102 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
105 const size_t resume_pos = (count - kChunkSize + 1) / 2 * 2;
106 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
115 template <
typename CharType>
116 char32_t ToChar32(CharType in) {
117 return static_cast<char32_t
>(
118 static_cast<typename std::make_unsigned<CharType>::type
>(in));
132 PrintBytesInObjectToImpl(obj_bytes, count, os);
150 template <
typename Char>
152 const char32_t u_c = ToChar32(c);
186 *os << static_cast<char>(
c);
189 ostream::fmtflags flags = os->flags();
190 *os <<
"\\x" << std::hex << std::uppercase << static_cast<int>(u_c);
219 #ifdef __cpp_lib_char8_t
235 #ifdef __cpp_lib_char8_t
252 template <
typename Char>
263 *os <<
" (" <<
static_cast<int>(
c);
268 if (format ==
kHexEscape || (1 <= c && c <= 9)) {
284 void PrintTo(char32_t c, ::std::ostream* os) {
285 *os << std::hex <<
"U+" << std::uppercase << std::setfill(
'0') << std::setw(4)
286 <<
static_cast<uint32_t
>(
c);
290 #if defined(__SIZEOF_INT128__)
291 void PrintTo(__uint128_t v, ::std::ostream* os) {
299 char*
p = buf +
sizeof(buf);
304 uint64_t high =
static_cast<uint64_t
>(v >> 64);
305 uint64_t low =
static_cast<uint64_t
>(v);
308 while (high != 0 || low != 0) {
309 uint64_t high_mod = high % 10;
316 const uint64_t carry = 6 * high_mod + low % 10;
317 low = low / 10 + high_mod * 1844674407370955161 + carry / 10;
319 char digit =
static_cast<char>(carry % 10);
320 *--p =
static_cast<char>(
'0' + digit);
324 void PrintTo(__int128_t v, ::std::ostream* os) {
325 __uint128_t uv =
static_cast<__uint128_t
>(v);
332 #endif // __SIZEOF_INT128__
338 template <
typename CharType>
344 *os << quote_prefix <<
"\"";
345 bool is_previous_hex =
false;
347 for (
size_t index = 0; index < len; ++index) {
348 const CharType cur = begin[index];
349 if (is_previous_hex &&
IsXDigit(cur)) {
353 *os <<
"\" " << quote_prefix <<
"\"";
357 if (is_previous_hex) {
367 template <
typename CharType>
380 if (len > 0 && begin[len - 1] ==
'\0') {
390 *os <<
" (no terminating NUL)";
398 #ifdef __cpp_lib_char8_t
427 template <
typename Char>
428 void PrintCStringTo(
const Char* s, ostream* os) {
432 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
439 void PrintTo(
const char* s, ostream* os) { PrintCStringTo(s, os); }
441 #ifdef __cpp_lib_char8_t
442 void PrintTo(
const char8_t* s, ostream* os) { PrintCStringTo(s, os); }
445 void PrintTo(
const char16_t* s, ostream* os) { PrintCStringTo(s, os); }
447 void PrintTo(
const char32_t* s, ostream* os) { PrintCStringTo(s, os); }
455 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
457 void PrintTo(
const wchar_t* s, ostream* os) { PrintCStringTo(s, os); }
458 #endif // wchar_t is native
462 bool ContainsUnprintableControlCodes(
const char* str,
size_t length) {
463 const unsigned char* s =
reinterpret_cast<const unsigned char*
>(str);
465 for (
size_t i = 0; i < length; i++) {
466 unsigned char ch = *s++;
467 if (std::iscntrl(ch)) {
481 bool IsUTF8TrailByte(
unsigned char t) {
return 0x80 <= t && t <= 0xbf; }
483 bool IsValidUTF8(
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;) {
487 unsigned char lead = s[i++];
494 }
else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) {
496 }
else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&
497 IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) &&
499 (lead != 0xe0 || s[
i] >= 0xa0) &&
500 (lead != 0xed || s[i] < 0xa0)) {
502 }
else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&
503 IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) &&
504 IsUTF8TrailByte(s[i + 2]) &&
506 (lead != 0xf0 || s[
i] >= 0x90) &&
507 (lead != 0xf4 || s[i] < 0x90)) {
516 void ConditionalPrintAsText(
const char* str,
size_t length, ostream* os) {
517 if (!ContainsUnprintableControlCodes(str, length) &&
518 IsValidUTF8(str, length)) {
519 *os <<
"\n As Text: \"" << str <<
"\"";
528 ConditionalPrintAsText(s.data(), s.size(), os);
533 #ifdef __cpp_lib_char8_t
534 void PrintU8StringTo(const ::std::u8string& s, ostream* os) {
547 #if GTEST_HAS_STD_WSTRING
548 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
551 #endif // GTEST_HAS_STD_WSTRING
GTEST_API_ void PrintU32StringTo(const ::std::u32string &s,::std::ostream *os)
bool IsPrintableAscii(char32_t c)
GTEST_API_ void PrintStringTo(const ::std::string &s,::std::ostream *os)
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ static GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ CharFormat PrintCharsAsStringTo(const CharType *begin, size_t len, ostream *os)
static CharFormat PrintAsCharLiteralTo(Char c, ostream *os)
GTEST_API_ void PrintU16StringTo(const ::std::u16string &s,::std::ostream *os)
#define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
static CharFormat PrintAsStringLiteralTo(char32_t c, ostream *os)
static std::string FormatHexInt(int value)
#define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count,::std::ostream *os)
#define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
void PrintCharAndCodeTo(Char c, ostream *os)
#define GTEST_FLAG_GET(name)
void PrintTo(const T &value,::std::ostream *os)
static const char * GetCharWidthPrefix(char)
void UniversalPrintArray(const T *begin, size_t len,::std::ostream *os)
#define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ static GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ void UniversalPrintCharArray(const CharType *begin, size_t len, ostream *os)