gtest-printers.cc (googletest-release-1.11.0) | : | gtest-printers.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 103 | skipping to change at line 103 | |||
const size_t kChunkSize = 64; | const size_t kChunkSize = 64; | |||
// If the object size is bigger than kThreshold, we'll have to omit | // If the object size is bigger than kThreshold, we'll have to omit | |||
// some details by printing only the first and the last kChunkSize | // some details by printing only the first and the last kChunkSize | |||
// bytes. | // bytes. | |||
if (count < kThreshold) { | if (count < kThreshold) { | |||
PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); | PrintByteSegmentInObjectTo(obj_bytes, 0, count, os); | |||
} else { | } else { | |||
PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); | PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os); | |||
*os << " ... "; | *os << " ... "; | |||
// Rounds up to 2-byte boundary. | // Rounds up to 2-byte boundary. | |||
const size_t resume_pos = (count - kChunkSize + 1)/2*2; | const size_t resume_pos = (count - kChunkSize + 1) / 2 * 2; | |||
PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); | PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os); | |||
} | } | |||
*os << ">"; | *os << ">"; | |||
} | } | |||
// Helpers for widening a character to char32_t. Since the standard does not | // Helpers for widening a character to char32_t. Since the standard does not | |||
// specify if char / wchar_t is signed or unsigned, it is important to first | // specify if char / wchar_t is signed or unsigned, it is important to first | |||
// convert it to the unsigned type of the same width before widening it to | // convert it to the unsigned type of the same width before widening it to | |||
// char32_t. | // char32_t. | |||
template <typename CharType> | template <typename CharType> | |||
skipping to change at line 138 | skipping to change at line 138 | |||
void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, | void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count, | |||
ostream* os) { | ostream* os) { | |||
PrintBytesInObjectToImpl(obj_bytes, count, os); | PrintBytesInObjectToImpl(obj_bytes, count, os); | |||
} | } | |||
// Depending on the value of a char (or wchar_t), we print it in one | // Depending on the value of a char (or wchar_t), we print it in one | |||
// of three formats: | // of three formats: | |||
// - as is if it's a printable ASCII (e.g. 'a', '2', ' '), | // - as is if it's a printable ASCII (e.g. 'a', '2', ' '), | |||
// - as a hexadecimal escape sequence (e.g. '\x7F'), or | // - as a hexadecimal escape sequence (e.g. '\x7F'), or | |||
// - as a special escape sequence (e.g. '\r', '\n'). | // - as a special escape sequence (e.g. '\r', '\n'). | |||
enum CharFormat { | enum CharFormat { kAsIs, kHexEscape, kSpecialEscape }; | |||
kAsIs, | ||||
kHexEscape, | ||||
kSpecialEscape | ||||
}; | ||||
// Returns true if c is a printable ASCII character. We test the | // Returns true if c is a printable ASCII character. We test the | |||
// value of c directly instead of calling isprint(), which is buggy on | // value of c directly instead of calling isprint(), which is buggy on | |||
// Windows Mobile. | // Windows Mobile. | |||
inline bool IsPrintableAscii(char32_t c) { return 0x20 <= c && c <= 0x7E; } | inline bool IsPrintableAscii(char32_t c) { return 0x20 <= c && c <= 0x7E; } | |||
// Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a | // Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a | |||
// character literal without the quotes, escaping it when necessary; returns how | // character literal without the quotes, escaping it when necessary; returns how | |||
// c was formatted. | // c was formatted. | |||
template <typename Char> | template <typename Char> | |||
skipping to change at line 215 | skipping to change at line 211 | |||
*os << "'"; | *os << "'"; | |||
return kAsIs; | return kAsIs; | |||
case L'"': | case L'"': | |||
*os << "\\\""; | *os << "\\\""; | |||
return kSpecialEscape; | return kSpecialEscape; | |||
default: | default: | |||
return PrintAsCharLiteralTo(c, os); | return PrintAsCharLiteralTo(c, os); | |||
} | } | |||
} | } | |||
static const char* GetCharWidthPrefix(char) { | static const char* GetCharWidthPrefix(char) { return ""; } | |||
return ""; | ||||
} | ||||
static const char* GetCharWidthPrefix(signed char) { | static const char* GetCharWidthPrefix(signed char) { return ""; } | |||
return ""; | ||||
} | ||||
static const char* GetCharWidthPrefix(unsigned char) { | static const char* GetCharWidthPrefix(unsigned char) { return ""; } | |||
return ""; | ||||
} | ||||
#ifdef __cpp_char8_t | #ifdef __cpp_char8_t | |||
static const char* GetCharWidthPrefix(char8_t) { | static const char* GetCharWidthPrefix(char8_t) { return "u8"; } | |||
return "u8"; | ||||
} | ||||
#endif | #endif | |||
static const char* GetCharWidthPrefix(char16_t) { | static const char* GetCharWidthPrefix(char16_t) { return "u"; } | |||
return "u"; | ||||
} | ||||
static const char* GetCharWidthPrefix(char32_t) { | static const char* GetCharWidthPrefix(char32_t) { return "U"; } | |||
return "U"; | ||||
} | ||||
static const char* GetCharWidthPrefix(wchar_t) { | static const char* GetCharWidthPrefix(wchar_t) { return "L"; } | |||
return "L"; | ||||
} | ||||
// Prints a char c as if it's part of a string literal, escaping it when | // Prints a char c as if it's part of a string literal, escaping it when | |||
// necessary; returns how c was formatted. | // necessary; returns how c was formatted. | |||
static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { | static CharFormat PrintAsStringLiteralTo(char c, ostream* os) { | |||
return PrintAsStringLiteralTo(ToChar32(c), os); | return PrintAsStringLiteralTo(ToChar32(c), os); | |||
} | } | |||
#ifdef __cpp_char8_t | #ifdef __cpp_char8_t | |||
static CharFormat PrintAsStringLiteralTo(char8_t c, ostream* os) { | static CharFormat PrintAsStringLiteralTo(char8_t c, ostream* os) { | |||
return PrintAsStringLiteralTo(ToChar32(c), os); | return PrintAsStringLiteralTo(ToChar32(c), os); | |||
skipping to change at line 278 | skipping to change at line 260 | |||
template <typename Char> | template <typename Char> | |||
void PrintCharAndCodeTo(Char c, ostream* os) { | void PrintCharAndCodeTo(Char c, ostream* os) { | |||
// First, print c as a literal in the most readable form we can find. | // First, print c as a literal in the most readable form we can find. | |||
*os << GetCharWidthPrefix(c) << "'"; | *os << GetCharWidthPrefix(c) << "'"; | |||
const CharFormat format = PrintAsCharLiteralTo(c, os); | const CharFormat format = PrintAsCharLiteralTo(c, os); | |||
*os << "'"; | *os << "'"; | |||
// To aid user debugging, we also print c's code in decimal, unless | // To aid user debugging, we also print c's code in decimal, unless | |||
// it's 0 (in which case c was printed as '\\0', making the code | // it's 0 (in which case c was printed as '\\0', making the code | |||
// obvious). | // obvious). | |||
if (c == 0) | if (c == 0) return; | |||
return; | ||||
*os << " (" << static_cast<int>(c); | *os << " (" << static_cast<int>(c); | |||
// For more convenience, we print c's code again in hexadecimal, | // For more convenience, we print c's code again in hexadecimal, | |||
// unless c was already printed in the form '\x##' or the code is in | // unless c was already printed in the form '\x##' or the code is in | |||
// [1, 9]. | // [1, 9]. | |||
if (format == kHexEscape || (1 <= c && c <= 9)) { | if (format == kHexEscape || (1 <= c && c <= 9)) { | |||
// Do nothing. | // Do nothing. | |||
} else { | } else { | |||
*os << ", 0x" << String::FormatHexInt(static_cast<int>(c)); | *os << ", 0x" << String::FormatHexInt(static_cast<int>(c)); | |||
} | } | |||
skipping to change at line 306 | skipping to change at line 287 | |||
// Prints a wchar_t as a symbol if it is printable or as its internal | // Prints a wchar_t as a symbol if it is printable or as its internal | |||
// code otherwise and also as its code. L'\0' is printed as "L'\\0'". | // code otherwise and also as its code. L'\0' is printed as "L'\\0'". | |||
void PrintTo(wchar_t wc, ostream* os) { PrintCharAndCodeTo(wc, os); } | void PrintTo(wchar_t wc, ostream* os) { PrintCharAndCodeTo(wc, os); } | |||
// TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well. | // TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well. | |||
void PrintTo(char32_t c, ::std::ostream* os) { | void PrintTo(char32_t c, ::std::ostream* os) { | |||
*os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4) | *os << std::hex << "U+" << std::uppercase << std::setfill('0') << std::setw(4) | |||
<< static_cast<uint32_t>(c); | << static_cast<uint32_t>(c); | |||
} | } | |||
// gcc/clang __{u,}int128_t | ||||
#if defined(__SIZEOF_INT128__) | ||||
void PrintTo(__uint128_t v, ::std::ostream* os) { | ||||
if (v == 0) { | ||||
*os << "0"; | ||||
return; | ||||
} | ||||
// Buffer large enough for ceil(log10(2^128))==39 and the null terminator | ||||
char buf[40]; | ||||
char* p = buf + sizeof(buf); | ||||
// Some configurations have a __uint128_t, but no support for built in | ||||
// division. Do manual long division instead. | ||||
uint64_t high = static_cast<uint64_t>(v >> 64); | ||||
uint64_t low = static_cast<uint64_t>(v); | ||||
*--p = 0; | ||||
while (high != 0 || low != 0) { | ||||
uint64_t high_mod = high % 10; | ||||
high = high / 10; | ||||
// This is the long division algorithm specialized for a divisor of 10 and | ||||
// only two elements. | ||||
// Notable values: | ||||
// 2^64 / 10 == 1844674407370955161 | ||||
// 2^64 % 10 == 6 | ||||
const uint64_t carry = 6 * high_mod + low % 10; | ||||
low = low / 10 + high_mod * 1844674407370955161 + carry / 10; | ||||
char digit = static_cast<char>(carry % 10); | ||||
*--p = '0' + digit; | ||||
} | ||||
*os << p; | ||||
} | ||||
void PrintTo(__int128_t v, ::std::ostream* os) { | ||||
__uint128_t uv = static_cast<__uint128_t>(v); | ||||
if (v < 0) { | ||||
*os << "-"; | ||||
uv = -uv; | ||||
} | ||||
PrintTo(uv, os); | ||||
} | ||||
#endif // __SIZEOF_INT128__ | ||||
// Prints the given array of characters to the ostream. CharType must be either | // Prints the given array of characters to the ostream. CharType must be either | |||
// char, char8_t, char16_t, char32_t, or wchar_t. | // char, char8_t, char16_t, char32_t, or wchar_t. | |||
// The array starts at begin, the length is len, it may include '\0' characters | // The array starts at begin, the length is len, it may include '\0' characters | |||
// and may not be NUL-terminated. | // and may not be NUL-terminated. | |||
template <typename CharType> | template <typename CharType> | |||
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ | GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ | |||
GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ | GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ | |||
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ | GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat | |||
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ | PrintCharsAsStringTo(const CharType* begin, size_t len, ostream* os) { | |||
static CharFormat PrintCharsAsStringTo( | ||||
const CharType* begin, size_t len, ostream* os) { | ||||
const char* const quote_prefix = GetCharWidthPrefix(*begin); | const char* const quote_prefix = GetCharWidthPrefix(*begin); | |||
*os << quote_prefix << "\""; | *os << quote_prefix << "\""; | |||
bool is_previous_hex = false; | bool is_previous_hex = false; | |||
CharFormat print_format = kAsIs; | CharFormat print_format = kAsIs; | |||
for (size_t index = 0; index < len; ++index) { | for (size_t index = 0; index < len; ++index) { | |||
const CharType cur = begin[index]; | const CharType cur = begin[index]; | |||
if (is_previous_hex && IsXDigit(cur)) { | if (is_previous_hex && IsXDigit(cur)) { | |||
// Previous character is of '\x..' form and this character can be | // Previous character is of '\x..' form and this character can be | |||
// interpreted as another hexadecimal digit in its number. Break string to | // interpreted as another hexadecimal digit in its number. Break string to | |||
// disambiguate. | // disambiguate. | |||
skipping to change at line 342 | skipping to change at line 366 | |||
print_format = kHexEscape; | print_format = kHexEscape; | |||
} | } | |||
} | } | |||
*os << "\""; | *os << "\""; | |||
return print_format; | return print_format; | |||
} | } | |||
// Prints a (const) char/wchar_t array of 'len' elements, starting at address | // Prints a (const) char/wchar_t array of 'len' elements, starting at address | |||
// 'begin'. CharType must be either char or wchar_t. | // 'begin'. CharType must be either char or wchar_t. | |||
template <typename CharType> | template <typename CharType> | |||
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ | GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ | |||
GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_ | GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ | |||
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_ | GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static void | |||
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ | UniversalPrintCharArray(const CharType* begin, size_t len, | |||
static void UniversalPrintCharArray( | ostream* os) { | |||
const CharType* begin, size_t len, ostream* os) { | ||||
// The code | // The code | |||
// const char kFoo[] = "foo"; | // const char kFoo[] = "foo"; | |||
// generates an array of 4, not 3, elements, with the last one being '\0'. | // generates an array of 4, not 3, elements, with the last one being '\0'. | |||
// | // | |||
// Therefore when printing a char array, we don't print the last element if | // Therefore when printing a char array, we don't print the last element if | |||
// it's '\0', such that the output matches the string literal as it's | // it's '\0', such that the output matches the string literal as it's | |||
// written in the source code. | // written in the source code. | |||
if (len > 0 && begin[len - 1] == '\0') { | if (len > 0 && begin[len - 1] == '\0') { | |||
PrintCharsAsStringTo(begin, len - 1, os); | PrintCharsAsStringTo(begin, len - 1, os); | |||
return; | return; | |||
skipping to change at line 438 | skipping to change at line 461 | |||
// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when | // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when | |||
// wchar_t is implemented as a native type. | // wchar_t is implemented as a native type. | |||
#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) | #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) | |||
// Prints the given wide C string to the ostream. | // Prints the given wide C string to the ostream. | |||
void PrintTo(const wchar_t* s, ostream* os) { PrintCStringTo(s, os); } | void PrintTo(const wchar_t* s, ostream* os) { PrintCStringTo(s, os); } | |||
#endif // wchar_t is native | #endif // wchar_t is native | |||
namespace { | namespace { | |||
bool ContainsUnprintableControlCodes(const char* str, size_t length) { | bool ContainsUnprintableControlCodes(const char* str, size_t length) { | |||
const unsigned char *s = reinterpret_cast<const unsigned char *>(str); | const unsigned char* s = reinterpret_cast<const unsigned char*>(str); | |||
for (size_t i = 0; i < length; i++) { | for (size_t i = 0; i < length; i++) { | |||
unsigned char ch = *s++; | unsigned char ch = *s++; | |||
if (std::iscntrl(ch)) { | if (std::iscntrl(ch)) { | |||
switch (ch) { | switch (ch) { | |||
case '\t': | case '\t': | |||
case '\n': | case '\n': | |||
case '\r': | case '\r': | |||
break; | break; | |||
default: | default: | |||
return true; | return true; | |||
} | ||||
} | } | |||
} | ||||
} | } | |||
return false; | return false; | |||
} | } | |||
bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t<= 0xbf; } | bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t <= 0xbf; } | |||
bool IsValidUTF8(const char* str, size_t length) { | bool IsValidUTF8(const char* str, size_t length) { | |||
const unsigned char *s = reinterpret_cast<const unsigned char *>(str); | const unsigned char* s = reinterpret_cast<const unsigned char*>(str); | |||
for (size_t i = 0; i < length;) { | for (size_t i = 0; i < length;) { | |||
unsigned char lead = s[i++]; | unsigned char lead = s[i++]; | |||
if (lead <= 0x7f) { | if (lead <= 0x7f) { | |||
continue; // single-byte character (ASCII) 0..7F | continue; // single-byte character (ASCII) 0..7F | |||
} | } | |||
if (lead < 0xc2) { | if (lead < 0xc2) { | |||
return false; // trail byte or non-shortest form | return false; // trail byte or non-shortest form | |||
} else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) { | } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) { | |||
++i; // 2-byte character | ++i; // 2-byte character | |||
} else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length && | } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length && | |||
IsUTF8TrailByte(s[i]) && | IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) && | |||
IsUTF8TrailByte(s[i + 1]) && | ||||
// check for non-shortest form and surrogate | // check for non-shortest form and surrogate | |||
(lead != 0xe0 || s[i] >= 0xa0) && | (lead != 0xe0 || s[i] >= 0xa0) && | |||
(lead != 0xed || s[i] < 0xa0)) { | (lead != 0xed || s[i] < 0xa0)) { | |||
i += 2; // 3-byte character | i += 2; // 3-byte character | |||
} else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length && | } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length && | |||
IsUTF8TrailByte(s[i]) && | IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) && | |||
IsUTF8TrailByte(s[i + 1]) && | ||||
IsUTF8TrailByte(s[i + 2]) && | IsUTF8TrailByte(s[i + 2]) && | |||
// check for non-shortest form | // check for non-shortest form | |||
(lead != 0xf0 || s[i] >= 0x90) && | (lead != 0xf0 || s[i] >= 0x90) && | |||
(lead != 0xf4 || s[i] < 0x90)) { | (lead != 0xf4 || s[i] < 0x90)) { | |||
i += 3; // 4-byte character | i += 3; // 4-byte character | |||
} else { | } else { | |||
return false; | return false; | |||
} | } | |||
} | } | |||
return true; | return true; | |||
skipping to change at line 504 | skipping to change at line 525 | |||
if (!ContainsUnprintableControlCodes(str, length) && | if (!ContainsUnprintableControlCodes(str, length) && | |||
IsValidUTF8(str, length)) { | IsValidUTF8(str, length)) { | |||
*os << "\n As Text: \"" << str << "\""; | *os << "\n As Text: \"" << str << "\""; | |||
} | } | |||
} | } | |||
} // anonymous namespace | } // anonymous namespace | |||
void PrintStringTo(const ::std::string& s, ostream* os) { | void PrintStringTo(const ::std::string& s, ostream* os) { | |||
if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) { | if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) { | |||
if (GTEST_FLAG(print_utf8)) { | if (GTEST_FLAG_GET(print_utf8)) { | |||
ConditionalPrintAsText(s.data(), s.size(), os); | ConditionalPrintAsText(s.data(), s.size(), os); | |||
} | } | |||
} | } | |||
} | } | |||
#ifdef __cpp_char8_t | #ifdef __cpp_char8_t | |||
void PrintU8StringTo(const ::std::u8string& s, ostream* os) { | void PrintU8StringTo(const ::std::u8string& s, ostream* os) { | |||
PrintCharsAsStringTo(s.data(), s.size(), os); | PrintCharsAsStringTo(s.data(), s.size(), os); | |||
} | } | |||
#endif | #endif | |||
End of changes. 22 change blocks. | ||||
51 lines changed or deleted | 72 lines changed or added |