44 #include "gtest/gtest-printers.h"
50 #include "gtest/internal/gtest-port.h"
51 #include "src/gtest-internal-inl.h"
64 void PrintByteSegmentInObjectTo(
const unsigned char* obj_bytes,
size_t start,
65 size_t count, ostream* os) {
67 for (
size_t i = 0;
i !=
count;
i++) {
83 void PrintBytesInObjectToImpl(
const unsigned char* obj_bytes,
size_t count,
86 *os <<
count <<
"-byte object <";
88 const size_t kThreshold = 132;
93 if (
count < kThreshold) {
94 PrintByteSegmentInObjectTo(obj_bytes, 0,
count, os);
96 PrintByteSegmentInObjectTo(obj_bytes, 0,
kChunkSize, os);
100 PrintByteSegmentInObjectTo(obj_bytes, resume_pos,
count - resume_pos, os);
107 namespace internal2 {
116 PrintBytesInObjectToImpl(obj_bytes,
count, os);
138 return 0x20 <= c && c <= 0x7E;
145 template <
typename Un
signedChar,
typename Char>
147 wchar_t w_c =
static_cast<wchar_t>(c);
181 *os << static_cast<char>(c);
184 ostream::fmtflags
flags = os->flags();
185 *os <<
"\\x" << std::hex << std::uppercase
186 <<
static_cast<int>(
static_cast<UnsignedChar
>(c));
205 return PrintAsCharLiteralTo<wchar_t>(c, os);
213 static_cast<wchar_t>(
static_cast<unsigned char>(c)), os);
220 template <
typename Un
signedChar,
typename Char>
223 *os << ((
sizeof(c) > 1) ?
"L'" :
"'");
232 *os <<
" (" <<
static_cast<int>(c);
245 void PrintTo(
unsigned char c, ::std::ostream* os) {
246 PrintCharAndCodeTo<unsigned char>(c, os);
248 void PrintTo(
signed char c, ::std::ostream* os) {
249 PrintCharAndCodeTo<unsigned char>(c, os);
255 PrintCharAndCodeTo<wchar_t>(wc, os);
262 template <
typename CharType>
268 const CharType*
begin,
size_t len, ostream* os) {
269 const char*
const kQuoteBegin =
sizeof(CharType) == 1 ?
"\"" :
"L\"";
271 bool is_previous_hex =
false;
279 *os <<
"\" " << kQuoteBegin;
283 if (is_previous_hex) {
293 template <
typename CharType>
299 const CharType*
begin,
size_t len, ostream* os) {
317 *os <<
" (no terminating NUL)";
336 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
347 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
353 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
357 #endif // wchar_t is native
361 bool ContainsUnprintableControlCodes(
const char*
str,
size_t length) {
362 const unsigned char *s =
reinterpret_cast<const unsigned char *
>(
str);
364 for (
size_t i = 0;
i <
length;
i++) {
365 unsigned char ch = *s++;
366 if (std::iscntrl(
ch)) {
380 bool IsUTF8TrailByte(
unsigned char t) {
return 0x80 <=
t &&
t<= 0xbf; }
383 const unsigned char *
s =
reinterpret_cast<const unsigned char *
>(
str);
385 for (
size_t i = 0;
i <
length;) {
386 unsigned char lead =
s[
i++];
393 }
else if (lead <= 0xdf && (i + 1) <=
length && IsUTF8TrailByte(s[i])) {
395 }
else if (0xe0 <= lead && lead <= 0xef && (i + 2) <=
length &&
396 IsUTF8TrailByte(s[i]) &&
397 IsUTF8TrailByte(s[i + 1]) &&
399 (lead != 0xe0 ||
s[
i] >= 0xa0) &&
400 (lead != 0xed || s[i] < 0xa0)) {
402 }
else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <=
length &&
403 IsUTF8TrailByte(s[i]) &&
404 IsUTF8TrailByte(s[i + 1]) &&
405 IsUTF8TrailByte(s[i + 2]) &&
407 (lead != 0xf0 ||
s[
i] >= 0x90) &&
408 (lead != 0xf4 || s[i] < 0x90)) {
417 void ConditionalPrintAsText(
const char*
str,
size_t length, ostream* os) {
418 if (!ContainsUnprintableControlCodes(
str,
length) &&
420 *os <<
"\n As Text: \"" <<
str <<
"\"";
429 ConditionalPrintAsText(s.data(), s.size(), os);
434 #if GTEST_HAS_STD_WSTRING
438 #endif // GTEST_HAS_STD_WSTRING