64void PrintByteSegmentInObjectTo(
const unsigned char* obj_bytes,
size_t start,
67 for (
size_t i = 0; i !=
count; i++) {
68 const size_t j =
start + i;
83void PrintBytesInObjectToImpl(
const unsigned char* obj_bytes,
size_t count,
86 *
os <<
count <<
"-byte object <";
88 const size_t kThreshold = 132;
89 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);
139 return 0x20 <= c && c <= 0x7E;
146template <
typename Un
signedChar,
typename Char>
147static CharFormat PrintAsCharLiteralTo(Char c, ostream*
os) {
148 switch (
static_cast<wchar_t>(c)) {
184 ostream::fmtflags
flags =
os->flags();
185 *
os <<
"\\x" << std::hex << std::uppercase
186 <<
static_cast<int>(
static_cast<UnsignedChar
>(c));
196static CharFormat PrintAsStringLiteralTo(
wchar_t c, ostream*
os) {
205 return PrintAsCharLiteralTo<wchar_t>(c,
os);
211static CharFormat PrintAsStringLiteralTo(
char c, ostream*
os) {
212 return PrintAsStringLiteralTo(
213 static_cast<wchar_t>(
static_cast<unsigned char>(c)),
os);
220template <
typename Un
signedChar,
typename Char>
223 *
os << ((
sizeof(c) > 1) ?
"L'" :
"'");
232 *
os <<
" (" <<
static_cast<int>(c);
262template <
typename CharType>
267 const CharType* begin,
size_t len, ostream*
os) {
268 const char*
const kQuoteBegin =
sizeof(CharType) == 1 ?
"\"" :
"L\"";
270 bool is_previous_hex =
false;
272 for (
size_t index = 0; index <
len; ++index) {
273 const CharType cur = begin[index];
274 if (is_previous_hex &&
IsXDigit(cur)) {
278 *
os <<
"\" " << kQuoteBegin;
280 is_previous_hex = PrintAsStringLiteralTo(cur,
os) ==
kHexEscape;
282 if (is_previous_hex) {
292template <
typename CharType>
296static void UniversalPrintCharArray(
297 const CharType* begin,
size_t len, ostream*
os) {
305 if (
len > 0 && begin[
len - 1] ==
'\0') {
306 PrintCharsAsStringTo(begin,
len - 1,
os);
314 PrintCharsAsStringTo(begin,
len,
os);
315 *
os <<
" (no terminating NUL)";
320 UniversalPrintCharArray(begin,
len,
os);
326 UniversalPrintCharArray(begin,
len,
os);
335 PrintCharsAsStringTo(
s, strlen(
s),
os);
345#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
352 PrintCharsAsStringTo(
s, std::wcslen(
s),
os);
359bool ContainsUnprintableControlCodes(
const char* str,
size_t length) {
360 const unsigned char *
s =
reinterpret_cast<const unsigned char *
>(str);
362 for (
size_t i = 0; i < length; i++) {
363 unsigned char ch = *
s++;
364 if (std::iscntrl(ch)) {
378bool IsUTF8TrailByte(
unsigned char t) {
return 0x80 <= t && t<= 0xbf; }
380bool IsValidUTF8(
const char* str,
size_t length) {
381 const unsigned char *
s =
reinterpret_cast<const unsigned char *
>(
str);
383 for (
size_t i = 0; i < length;) {
384 unsigned char lead =
s[i++];
391 }
else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(
s[i])) {
393 }
else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&
394 IsUTF8TrailByte(
s[i]) &&
395 IsUTF8TrailByte(
s[i + 1]) &&
397 (lead != 0xe0 ||
s[i] >= 0xa0) &&
398 (lead != 0xed ||
s[i] < 0xa0)) {
400 }
else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&
401 IsUTF8TrailByte(
s[i]) &&
402 IsUTF8TrailByte(
s[i + 1]) &&
403 IsUTF8TrailByte(
s[i + 2]) &&
405 (lead != 0xf0 ||
s[i] >= 0x90) &&
406 (lead != 0xf4 ||
s[i] < 0x90)) {
415void ConditionalPrintAsText(
const char* str,
size_t length, ostream*
os) {
416 if (!ContainsUnprintableControlCodes(str, length) &&
417 IsValidUTF8(str, length)) {
418 *
os <<
"\n As Text: \"" <<
str <<
"\"";
425#if GTEST_HAS_GLOBAL_STRING
429 ConditionalPrintAsText(
s.data(),
s.size(),
os);
438 ConditionalPrintAsText(
s.data(),
s.size(),
os);
444#if GTEST_HAS_GLOBAL_WSTRING
445void PrintWideStringTo(const ::wstring&
s, ostream*
os) {
446 PrintCharsAsStringTo(
s.data(),
s.size(),
os);
450#if GTEST_HAS_STD_WSTRING
451void PrintWideStringTo(const ::std::wstring&
s, ostream*
os) {
452 PrintCharsAsStringTo(
s.data(),
s.size(),
os);
static std::string FormatHexInt(int value)
#define GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
#define GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
#define GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
GTEST_API_ void PrintBytesInObjectTo(const unsigned char *obj_bytes, size_t count, ::std::ostream *os)
void PrintCharAndCodeTo(Char c, ostream *os)
bool IsPrintableAscii(wchar_t c)
GTEST_API_ void PrintStringTo(const ::std::string &s, ::std::ostream *os)
void PrintTo(const ReferenceWrapper< T > &ref, ::std::ostream *os)
void UniversalPrintArray(const T *begin, size_t len, ::std::ostream *os)