65 void PrintByteSegmentInObjectTo(
const unsigned char* obj_bytes,
size_t start,
66 size_t count, ostream* os) {
68 for (
size_t i = 0;
i !=
count;
i++) {
69 const size_t j = start +
i;
84 void PrintBytesInObjectToImpl(
const unsigned char* obj_bytes,
size_t count,
87 *os << count <<
"-byte object <";
89 const size_t kThreshold = 132;
90 const size_t kChunkSize = 64;
94 if (count < kThreshold) {
95 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
97 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
100 const size_t resume_pos = (count - kChunkSize + 1)/2*2;
101 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
117 PrintBytesInObjectToImpl(obj_bytes, count, os);
135 return 0x20 <= c && c <= 0x7E;
142 template <
typename Un
signedChar,
typename Char>
144 wchar_t w_c =
static_cast<wchar_t>(
c);
178 *os << static_cast<char>(
c);
181 ostream::fmtflags flags = os->flags();
182 *os <<
"\\x" << std::hex << std::uppercase
183 <<
static_cast<int>(
static_cast<UnsignedChar
>(
c));
202 return PrintAsCharLiteralTo<wchar_t>(
c, os);
210 static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
217 template <
typename Un
signedChar,
typename Char>
220 *os << ((sizeof(c) > 1) ?
"L'" :
"'");
221 const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(
c, os);
229 *os <<
" (" <<
static_cast<int>(
c);
234 if (format ==
kHexEscape || (1 <= c && c <= 9)) {
242 void PrintTo(
unsigned char c, ::std::ostream* os) {
243 PrintCharAndCodeTo<unsigned char>(
c, os);
246 PrintCharAndCodeTo<unsigned char>(
c, os);
252 PrintCharAndCodeTo<wchar_t>(wc, os);
256 *os << std::hex <<
"U+" << std::uppercase << std::setfill(
'0') << std::setw(4)
257 <<
static_cast<uint32_t
>(
c);
264 template <
typename CharType>
270 const CharType* begin,
size_t len, ostream* os) {
271 const char*
const kQuoteBegin =
sizeof(CharType) == 1 ?
"\"" :
"L\"";
273 bool is_previous_hex =
false;
275 for (
size_t index = 0; index < len; ++index) {
276 const CharType cur = begin[index];
277 if (is_previous_hex &&
IsXDigit(cur)) {
281 *os <<
"\" " << kQuoteBegin;
285 if (is_previous_hex) {
295 template <
typename CharType>
301 const CharType* begin,
size_t len, ostream* os) {
309 if (len > 0 && begin[len - 1] ==
'\0') {
319 *os <<
" (no terminating NUL)";
338 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
349 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
355 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
359 #endif // wchar_t is native
363 bool ContainsUnprintableControlCodes(
const char* str,
size_t length) {
364 const unsigned char *s =
reinterpret_cast<const unsigned char *
>(str);
366 for (
size_t i = 0; i < length; i++) {
367 unsigned char ch = *s++;
368 if (std::iscntrl(ch)) {
382 bool IsUTF8TrailByte(
unsigned char t) {
return 0x80 <= t && t<= 0xbf; }
384 bool IsValidUTF8(
const char* str,
size_t length) {
385 const unsigned char *s =
reinterpret_cast<const unsigned char *
>(str);
387 for (
size_t i = 0; i < length;) {
388 unsigned char lead = s[i++];
395 }
else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) {
397 }
else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&
398 IsUTF8TrailByte(s[i]) &&
399 IsUTF8TrailByte(s[i + 1]) &&
401 (lead != 0xe0 || s[
i] >= 0xa0) &&
402 (lead != 0xed || s[i] < 0xa0)) {
404 }
else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&
405 IsUTF8TrailByte(s[i]) &&
406 IsUTF8TrailByte(s[i + 1]) &&
407 IsUTF8TrailByte(s[i + 2]) &&
409 (lead != 0xf0 || s[
i] >= 0x90) &&
410 (lead != 0xf4 || s[i] < 0x90)) {
419 void ConditionalPrintAsText(
const char* str,
size_t length, ostream* os) {
420 if (!ContainsUnprintableControlCodes(str, length) &&
421 IsValidUTF8(str, length)) {
422 *os <<
"\n As Text: \"" << str <<
"\"";
431 ConditionalPrintAsText(s.data(), s.size(), os);
436 #if GTEST_HAS_STD_WSTRING
437 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
440 #endif // GTEST_HAS_STD_WSTRING
void PrintCharAndCodeTo(Char c, ostream *os)
static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream *os)
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)
#define GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
static std::string FormatHexInt(int value)
static CharFormat PrintAsCharLiteralTo(Char c, ostream *os)
#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_
bool IsPrintableAscii(wchar_t c)
void PrintTo(const T &value,::std::ostream *os)
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)