1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Google Test - The Google C++ Testing and Mocking Framework
33 // This file implements a universal value printer that can print a
34 // value of any type T:
36 // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38 // It uses the << operator when possible, and prints the bytes in the
39 // object otherwise. A user can override its behavior for a class
40 // type Foo by defining either operator<<(::std::ostream&, const Foo&)
41 // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
44 #include "gtest/gtest-printers.h"
48 #include <ostream> // NOLINT
50 #include "gtest/internal/gtest-port.h"
51 #include "src/gtest-internal-inl.h"
59 // Prints a segment of bytes in the given object.
60 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
61 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
62 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
63 void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
64 size_t count, ostream* os) {
66 for (size_t i = 0; i != count; i++) {
67 const size_t j = start + i;
69 // Organizes the bytes into groups of 2 for easy parsing by
76 GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
81 // Prints the bytes in the given value to the given ostream.
82 void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
84 // Tells the user how big the object is.
85 *os << count << "-byte object <";
87 const size_t kThreshold = 132;
88 const size_t kChunkSize = 64;
89 // If the object size is bigger than kThreshold, we'll have to omit
90 // some details by printing only the first and the last kChunkSize
92 // FIXME: let the user control the threshold using a flag.
93 if (count < kThreshold) {
94 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
96 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
98 // Rounds up to 2-byte boundary.
99 const size_t resume_pos = (count - kChunkSize + 1)/2*2;
100 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
107 namespace internal2 {
109 // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
110 // given object. The delegation simplifies the implementation, which
111 // uses the << operator and thus is easier done outside of the
112 // ::testing::internal namespace, which contains a << operator that
113 // sometimes conflicts with the one in STL.
114 void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
116 PrintBytesInObjectToImpl(obj_bytes, count, os);
119 } // namespace internal2
123 // Depending on the value of a char (or wchar_t), we print it in one
125 // - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
126 // - as a hexadecimal escape sequence (e.g. '\x7F'), or
127 // - as a special escape sequence (e.g. '\r', '\n').
134 // Returns true if c is a printable ASCII character. We test the
135 // value of c directly instead of calling isprint(), which is buggy on
137 inline bool IsPrintableAscii(wchar_t c) {
138 return 0x20 <= c && c <= 0x7E;
141 // Prints a wide or narrow char c as a character literal without the
142 // quotes, escaping it when necessary; returns how c was formatted.
143 // The template argument UnsignedChar is the unsigned version of Char,
144 // which is the type of c.
145 template <typename UnsignedChar, typename Char>
146 static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
147 switch (static_cast<wchar_t>(c)) {
179 if (IsPrintableAscii(c)) {
180 *os << static_cast<char>(c);
183 ostream::fmtflags flags = os->flags();
184 *os << "\\x" << std::hex << std::uppercase
185 << static_cast<int>(static_cast<UnsignedChar>(c));
190 return kSpecialEscape;
193 // Prints a wchar_t c as if it's part of a string literal, escaping it when
194 // necessary; returns how c was formatted.
195 static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {
202 return kSpecialEscape;
204 return PrintAsCharLiteralTo<wchar_t>(c, os);
208 // Prints a char c as if it's part of a string literal, escaping it when
209 // necessary; returns how c was formatted.
210 static CharFormat PrintAsStringLiteralTo(char c, ostream* os) {
211 return PrintAsStringLiteralTo(
212 static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
215 // Prints a wide or narrow character c and its code. '\0' is printed
216 // as "'\\0'", other unprintable characters are also properly escaped
217 // using the standard C++ escape sequence. The template argument
218 // UnsignedChar is the unsigned version of Char, which is the type of c.
219 template <typename UnsignedChar, typename Char>
220 void PrintCharAndCodeTo(Char c, ostream* os) {
221 // First, print c as a literal in the most readable form we can find.
222 *os << ((sizeof(c) > 1) ? "L'" : "'");
223 const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
226 // To aid user debugging, we also print c's code in decimal, unless
227 // it's 0 (in which case c was printed as '\\0', making the code
231 *os << " (" << static_cast<int>(c);
233 // For more convenience, we print c's code again in hexadecimal,
234 // unless c was already printed in the form '\x##' or the code is in
236 if (format == kHexEscape || (1 <= c && c <= 9)) {
239 *os << ", 0x" << String::FormatHexInt(static_cast<UnsignedChar>(c));
244 void PrintTo(unsigned char c, ::std::ostream* os) {
245 PrintCharAndCodeTo<unsigned char>(c, os);
247 void PrintTo(signed char c, ::std::ostream* os) {
248 PrintCharAndCodeTo<unsigned char>(c, os);
251 // Prints a wchar_t as a symbol if it is printable or as its internal
252 // code otherwise and also as its code. L'\0' is printed as "L'\\0'".
253 void PrintTo(wchar_t wc, ostream* os) {
254 PrintCharAndCodeTo<wchar_t>(wc, os);
257 // Prints the given array of characters to the ostream. CharType must be either
259 // The array starts at begin, the length is len, it may include '\0' characters
260 // and may not be NUL-terminated.
261 template <typename CharType>
262 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
263 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
264 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
265 static CharFormat PrintCharsAsStringTo(
266 const CharType* begin, size_t len, ostream* os) {
267 const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\"";
269 bool is_previous_hex = false;
270 CharFormat print_format = kAsIs;
271 for (size_t index = 0; index < len; ++index) {
272 const CharType cur = begin[index];
273 if (is_previous_hex && IsXDigit(cur)) {
274 // Previous character is of '\x..' form and this character can be
275 // interpreted as another hexadecimal digit in its number. Break string to
277 *os << "\" " << kQuoteBegin;
279 is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;
280 // Remember if any characters required hex escaping.
281 if (is_previous_hex) {
282 print_format = kHexEscape;
289 // Prints a (const) char/wchar_t array of 'len' elements, starting at address
290 // 'begin'. CharType must be either char or wchar_t.
291 template <typename CharType>
292 GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
293 GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
294 GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
295 static void UniversalPrintCharArray(
296 const CharType* begin, size_t len, ostream* os) {
298 // const char kFoo[] = "foo";
299 // generates an array of 4, not 3, elements, with the last one being '\0'.
301 // Therefore when printing a char array, we don't print the last element if
302 // it's '\0', such that the output matches the string literal as it's
303 // written in the source code.
304 if (len > 0 && begin[len - 1] == '\0') {
305 PrintCharsAsStringTo(begin, len - 1, os);
309 // If, however, the last element in the array is not '\0', e.g.
310 // const char kFoo[] = { 'f', 'o', 'o' };
311 // we must print the entire array. We also print a message to indicate
312 // that the array is not NUL-terminated.
313 PrintCharsAsStringTo(begin, len, os);
314 *os << " (no terminating NUL)";
317 // Prints a (const) char array of 'len' elements, starting at address 'begin'.
318 void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
319 UniversalPrintCharArray(begin, len, os);
322 // Prints a (const) wchar_t array of 'len' elements, starting at address
324 void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
325 UniversalPrintCharArray(begin, len, os);
328 // Prints the given C string to the ostream.
329 void PrintTo(const char* s, ostream* os) {
333 *os << ImplicitCast_<const void*>(s) << " pointing to ";
334 PrintCharsAsStringTo(s, strlen(s), os);
338 // MSVC compiler can be configured to define whar_t as a typedef
339 // of unsigned short. Defining an overload for const wchar_t* in that case
340 // would cause pointers to unsigned shorts be printed as wide strings,
341 // possibly accessing more memory than intended and causing invalid
342 // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
343 // wchar_t is implemented as a native type.
344 #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
345 // Prints the given wide C string to the ostream.
346 void PrintTo(const wchar_t* s, ostream* os) {
350 *os << ImplicitCast_<const void*>(s) << " pointing to ";
351 PrintCharsAsStringTo(s, std::wcslen(s), os);
354 #endif // wchar_t is native
358 bool ContainsUnprintableControlCodes(const char* str, size_t length) {
359 const unsigned char *s = reinterpret_cast<const unsigned char *>(str);
361 for (size_t i = 0; i < length; i++) {
362 unsigned char ch = *s++;
363 if (std::iscntrl(ch)) {
377 bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t<= 0xbf; }
379 bool IsValidUTF8(const char* str, size_t length) {
380 const unsigned char *s = reinterpret_cast<const unsigned char *>(str);
382 for (size_t i = 0; i < length;) {
383 unsigned char lead = s[i++];
386 continue; // single-byte character (ASCII) 0..7F
389 return false; // trail byte or non-shortest form
390 } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) {
391 ++i; // 2-byte character
392 } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&
393 IsUTF8TrailByte(s[i]) &&
394 IsUTF8TrailByte(s[i + 1]) &&
395 // check for non-shortest form and surrogate
396 (lead != 0xe0 || s[i] >= 0xa0) &&
397 (lead != 0xed || s[i] < 0xa0)) {
398 i += 2; // 3-byte character
399 } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&
400 IsUTF8TrailByte(s[i]) &&
401 IsUTF8TrailByte(s[i + 1]) &&
402 IsUTF8TrailByte(s[i + 2]) &&
403 // check for non-shortest form
404 (lead != 0xf0 || s[i] >= 0x90) &&
405 (lead != 0xf4 || s[i] < 0x90)) {
406 i += 3; // 4-byte character
414 void ConditionalPrintAsText(const char* str, size_t length, ostream* os) {
415 if (!ContainsUnprintableControlCodes(str, length) &&
416 IsValidUTF8(str, length)) {
417 *os << "\n As Text: \"" << str << "\"";
421 } // anonymous namespace
423 // Prints a ::string object.
424 #if GTEST_HAS_GLOBAL_STRING
425 void PrintStringTo(const ::string& s, ostream* os) {
426 if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
427 if (GTEST_FLAG(print_utf8)) {
428 ConditionalPrintAsText(s.data(), s.size(), os);
432 #endif // GTEST_HAS_GLOBAL_STRING
434 void PrintStringTo(const ::std::string& s, ostream* os) {
435 if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
436 if (GTEST_FLAG(print_utf8)) {
437 ConditionalPrintAsText(s.data(), s.size(), os);
442 // Prints a ::wstring object.
443 #if GTEST_HAS_GLOBAL_WSTRING
444 void PrintWideStringTo(const ::wstring& s, ostream* os) {
445 PrintCharsAsStringTo(s.data(), s.size(), os);
447 #endif // GTEST_HAS_GLOBAL_WSTRING
449 #if GTEST_HAS_STD_WSTRING
450 void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
451 PrintCharsAsStringTo(s.data(), s.size(), os);
453 #endif // GTEST_HAS_STD_WSTRING
455 } // namespace internal
457 } // namespace testing