44 #include "gtest/gtest-printers.h"
51 #include "gtest/internal/gtest-port.h"
52 #include "src/gtest-internal-inl.h"
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++) {
84 void PrintBytesInObjectToImpl(
const unsigned char* obj_bytes,
size_t count,
87 *os <<
count <<
"-byte object <";
89 const size_t kThreshold = 132;
94 if (
count < kThreshold) {
95 PrintByteSegmentInObjectTo(obj_bytes, 0,
count, os);
97 PrintByteSegmentInObjectTo(obj_bytes, 0,
kChunkSize, os);
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'" :
"'");
229 *os <<
" (" <<
static_cast<int>(c);
242 void PrintTo(
unsigned char c, ::std::ostream* os) {
243 PrintCharAndCodeTo<unsigned char>(c, os);
245 void PrintTo(
signed char c, ::std::ostream* os) {
246 PrintCharAndCodeTo<unsigned char>(c, os);
251 void PrintTo(
wchar_t wc, ostream* os) {
252 PrintCharAndCodeTo<wchar_t>(wc, os);
255 void PrintTo(char32_t c, ::std::ostream* os) {
256 *os << std::hex <<
"U+" << std::uppercase << std::setfill(
'0') << std::setw(4)
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;
281 *os <<
"\" " << kQuoteBegin;
285 if (is_previous_hex) {
295 template <
typename CharType>
301 const CharType*
begin,
size_t len, ostream* os) {
319 *os <<
" (no terminating NUL)";
334 void PrintTo(
const char* s, ostream* os) {
338 *os << ImplicitCast_<const void*>(s) <<
" pointing to ";
349 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
351 void PrintTo(
const wchar_t* s, ostream* os) {
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; }
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) &&
422 *os <<
"\n As Text: \"" <<
str <<
"\"";
431 ConditionalPrintAsText(
s.data(),
s.size(), os);
436 #if GTEST_HAS_STD_WSTRING
440 #endif // GTEST_HAS_STD_WSTRING