]> git.proxmox.com Git - ceph.git/blame - ceph/src/s3select/rapidjson/thirdparty/gtest/googletest/src/gtest-printers.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googletest / src / gtest-printers.cc
CommitLineData
31f18b77
FG
1// Copyright 2007, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
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
13// distribution.
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.
17//
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.
29//
30// Author: wan@google.com (Zhanyong Wan)
31
1e59de90 32// Google Test - The Google C++ Testing and Mocking Framework
31f18b77
FG
33//
34// This file implements a universal value printer that can print a
35// value of any type T:
36//
37// void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38//
39// It uses the << operator when possible, and prints the bytes in the
40// object otherwise. A user can override its behavior for a class
41// type Foo by defining either operator<<(::std::ostream&, const Foo&)
42// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
43// defines Foo.
44
45#include "gtest/gtest-printers.h"
31f18b77 46#include <stdio.h>
1e59de90 47#include <cctype>
31f18b77
FG
48#include <cwchar>
49#include <ostream> // NOLINT
50#include <string>
51#include "gtest/internal/gtest-port.h"
1e59de90 52#include "src/gtest-internal-inl.h"
31f18b77
FG
53
54namespace testing {
55
56namespace {
57
58using ::std::ostream;
59
60// Prints a segment of bytes in the given object.
61GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
62GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
63GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
64void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
65 size_t count, ostream* os) {
66 char text[5] = "";
67 for (size_t i = 0; i != count; i++) {
68 const size_t j = start + i;
69 if (i != 0) {
70 // Organizes the bytes into groups of 2 for easy parsing by
71 // human.
72 if ((j % 2) == 0)
73 *os << ' ';
74 else
75 *os << '-';
76 }
77 GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
78 *os << text;
79 }
80}
81
82// Prints the bytes in the given value to the given ostream.
83void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
84 ostream* os) {
85 // Tells the user how big the object is.
86 *os << count << "-byte object <";
87
88 const size_t kThreshold = 132;
89 const size_t kChunkSize = 64;
90 // If the object size is bigger than kThreshold, we'll have to omit
91 // some details by printing only the first and the last kChunkSize
92 // bytes.
93 // TODO(wan): let the user control the threshold using a flag.
94 if (count < kThreshold) {
95 PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
96 } else {
97 PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
98 *os << " ... ";
99 // Rounds up to 2-byte boundary.
100 const size_t resume_pos = (count - kChunkSize + 1)/2*2;
101 PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
102 }
103 *os << ">";
104}
105
106} // namespace
107
108namespace internal2 {
109
110// Delegates to PrintBytesInObjectToImpl() to print the bytes in the
111// given object. The delegation simplifies the implementation, which
112// uses the << operator and thus is easier done outside of the
113// ::testing::internal namespace, which contains a << operator that
114// sometimes conflicts with the one in STL.
115void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
116 ostream* os) {
117 PrintBytesInObjectToImpl(obj_bytes, count, os);
118}
119
120} // namespace internal2
121
122namespace internal {
123
124// Depending on the value of a char (or wchar_t), we print it in one
125// of three formats:
126// - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
1e59de90 127// - as a hexadecimal escape sequence (e.g. '\x7F'), or
31f18b77
FG
128// - as a special escape sequence (e.g. '\r', '\n').
129enum CharFormat {
130 kAsIs,
131 kHexEscape,
132 kSpecialEscape
133};
134
135// Returns true if c is a printable ASCII character. We test the
136// value of c directly instead of calling isprint(), which is buggy on
137// Windows Mobile.
138inline bool IsPrintableAscii(wchar_t c) {
139 return 0x20 <= c && c <= 0x7E;
140}
141
142// Prints a wide or narrow char c as a character literal without the
143// quotes, escaping it when necessary; returns how c was formatted.
144// The template argument UnsignedChar is the unsigned version of Char,
145// which is the type of c.
146template <typename UnsignedChar, typename Char>
147static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
148 switch (static_cast<wchar_t>(c)) {
149 case L'\0':
150 *os << "\\0";
151 break;
152 case L'\'':
153 *os << "\\'";
154 break;
155 case L'\\':
156 *os << "\\\\";
157 break;
158 case L'\a':
159 *os << "\\a";
160 break;
161 case L'\b':
162 *os << "\\b";
163 break;
164 case L'\f':
165 *os << "\\f";
166 break;
167 case L'\n':
168 *os << "\\n";
169 break;
170 case L'\r':
171 *os << "\\r";
172 break;
173 case L'\t':
174 *os << "\\t";
175 break;
176 case L'\v':
177 *os << "\\v";
178 break;
179 default:
180 if (IsPrintableAscii(c)) {
181 *os << static_cast<char>(c);
182 return kAsIs;
183 } else {
1e59de90
TL
184 ostream::fmtflags flags = os->flags();
185 *os << "\\x" << std::hex << std::uppercase
186 << static_cast<int>(static_cast<UnsignedChar>(c));
187 os->flags(flags);
31f18b77
FG
188 return kHexEscape;
189 }
190 }
191 return kSpecialEscape;
192}
193
194// Prints a wchar_t c as if it's part of a string literal, escaping it when
195// necessary; returns how c was formatted.
196static CharFormat PrintAsStringLiteralTo(wchar_t c, ostream* os) {
197 switch (c) {
198 case L'\'':
199 *os << "'";
200 return kAsIs;
201 case L'"':
202 *os << "\\\"";
203 return kSpecialEscape;
204 default:
205 return PrintAsCharLiteralTo<wchar_t>(c, os);
206 }
207}
208
209// Prints a char c as if it's part of a string literal, escaping it when
210// necessary; returns how c was formatted.
211static CharFormat PrintAsStringLiteralTo(char c, ostream* os) {
212 return PrintAsStringLiteralTo(
213 static_cast<wchar_t>(static_cast<unsigned char>(c)), os);
214}
215
216// Prints a wide or narrow character c and its code. '\0' is printed
217// as "'\\0'", other unprintable characters are also properly escaped
218// using the standard C++ escape sequence. The template argument
219// UnsignedChar is the unsigned version of Char, which is the type of c.
220template <typename UnsignedChar, typename Char>
221void PrintCharAndCodeTo(Char c, ostream* os) {
222 // First, print c as a literal in the most readable form we can find.
223 *os << ((sizeof(c) > 1) ? "L'" : "'");
224 const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
225 *os << "'";
226
227 // To aid user debugging, we also print c's code in decimal, unless
228 // it's 0 (in which case c was printed as '\\0', making the code
229 // obvious).
230 if (c == 0)
231 return;
232 *os << " (" << static_cast<int>(c);
233
1e59de90 234 // For more convenience, we print c's code again in hexadecimal,
31f18b77
FG
235 // unless c was already printed in the form '\x##' or the code is in
236 // [1, 9].
237 if (format == kHexEscape || (1 <= c && c <= 9)) {
238 // Do nothing.
239 } else {
240 *os << ", 0x" << String::FormatHexInt(static_cast<UnsignedChar>(c));
241 }
242 *os << ")";
243}
244
245void PrintTo(unsigned char c, ::std::ostream* os) {
246 PrintCharAndCodeTo<unsigned char>(c, os);
247}
248void PrintTo(signed char c, ::std::ostream* os) {
249 PrintCharAndCodeTo<unsigned char>(c, os);
250}
251
252// Prints a wchar_t as a symbol if it is printable or as its internal
253// code otherwise and also as its code. L'\0' is printed as "L'\\0'".
254void PrintTo(wchar_t wc, ostream* os) {
255 PrintCharAndCodeTo<wchar_t>(wc, os);
256}
257
258// Prints the given array of characters to the ostream. CharType must be either
259// char or wchar_t.
260// The array starts at begin, the length is len, it may include '\0' characters
261// and may not be NUL-terminated.
262template <typename CharType>
263GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
264GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
265GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
1e59de90 266static CharFormat PrintCharsAsStringTo(
31f18b77
FG
267 const CharType* begin, size_t len, ostream* os) {
268 const char* const kQuoteBegin = sizeof(CharType) == 1 ? "\"" : "L\"";
269 *os << kQuoteBegin;
270 bool is_previous_hex = false;
1e59de90 271 CharFormat print_format = kAsIs;
31f18b77
FG
272 for (size_t index = 0; index < len; ++index) {
273 const CharType cur = begin[index];
274 if (is_previous_hex && IsXDigit(cur)) {
275 // Previous character is of '\x..' form and this character can be
276 // interpreted as another hexadecimal digit in its number. Break string to
277 // disambiguate.
278 *os << "\" " << kQuoteBegin;
279 }
280 is_previous_hex = PrintAsStringLiteralTo(cur, os) == kHexEscape;
1e59de90
TL
281 // Remember if any characters required hex escaping.
282 if (is_previous_hex) {
283 print_format = kHexEscape;
284 }
31f18b77
FG
285 }
286 *os << "\"";
1e59de90 287 return print_format;
31f18b77
FG
288}
289
290// Prints a (const) char/wchar_t array of 'len' elements, starting at address
291// 'begin'. CharType must be either char or wchar_t.
292template <typename CharType>
293GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
294GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
295GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_
296static void UniversalPrintCharArray(
297 const CharType* begin, size_t len, ostream* os) {
298 // The code
299 // const char kFoo[] = "foo";
300 // generates an array of 4, not 3, elements, with the last one being '\0'.
301 //
302 // Therefore when printing a char array, we don't print the last element if
303 // it's '\0', such that the output matches the string literal as it's
304 // written in the source code.
305 if (len > 0 && begin[len - 1] == '\0') {
306 PrintCharsAsStringTo(begin, len - 1, os);
307 return;
308 }
309
310 // If, however, the last element in the array is not '\0', e.g.
311 // const char kFoo[] = { 'f', 'o', 'o' };
312 // we must print the entire array. We also print a message to indicate
313 // that the array is not NUL-terminated.
314 PrintCharsAsStringTo(begin, len, os);
315 *os << " (no terminating NUL)";
316}
317
318// Prints a (const) char array of 'len' elements, starting at address 'begin'.
319void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
320 UniversalPrintCharArray(begin, len, os);
321}
322
323// Prints a (const) wchar_t array of 'len' elements, starting at address
324// 'begin'.
325void UniversalPrintArray(const wchar_t* begin, size_t len, ostream* os) {
326 UniversalPrintCharArray(begin, len, os);
327}
328
329// Prints the given C string to the ostream.
330void PrintTo(const char* s, ostream* os) {
331 if (s == NULL) {
332 *os << "NULL";
333 } else {
334 *os << ImplicitCast_<const void*>(s) << " pointing to ";
335 PrintCharsAsStringTo(s, strlen(s), os);
336 }
337}
338
339// MSVC compiler can be configured to define whar_t as a typedef
340// of unsigned short. Defining an overload for const wchar_t* in that case
341// would cause pointers to unsigned shorts be printed as wide strings,
342// possibly accessing more memory than intended and causing invalid
343// memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
344// wchar_t is implemented as a native type.
345#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
346// Prints the given wide C string to the ostream.
347void PrintTo(const wchar_t* s, ostream* os) {
348 if (s == NULL) {
349 *os << "NULL";
350 } else {
351 *os << ImplicitCast_<const void*>(s) << " pointing to ";
352 PrintCharsAsStringTo(s, std::wcslen(s), os);
353 }
354}
355#endif // wchar_t is native
356
1e59de90
TL
357namespace {
358
359bool ContainsUnprintableControlCodes(const char* str, size_t length) {
360 const unsigned char *s = reinterpret_cast<const unsigned char *>(str);
361
362 for (size_t i = 0; i < length; i++) {
363 unsigned char ch = *s++;
364 if (std::iscntrl(ch)) {
365 switch (ch) {
366 case '\t':
367 case '\n':
368 case '\r':
369 break;
370 default:
371 return true;
372 }
373 }
374 }
375 return false;
376}
377
378bool IsUTF8TrailByte(unsigned char t) { return 0x80 <= t && t<= 0xbf; }
379
380bool IsValidUTF8(const char* str, size_t length) {
381 const unsigned char *s = reinterpret_cast<const unsigned char *>(str);
382
383 for (size_t i = 0; i < length;) {
384 unsigned char lead = s[i++];
385
386 if (lead <= 0x7f) {
387 continue; // single-byte character (ASCII) 0..7F
388 }
389 if (lead < 0xc2) {
390 return false; // trail byte or non-shortest form
391 } else if (lead <= 0xdf && (i + 1) <= length && IsUTF8TrailByte(s[i])) {
392 ++i; // 2-byte character
393 } else if (0xe0 <= lead && lead <= 0xef && (i + 2) <= length &&
394 IsUTF8TrailByte(s[i]) &&
395 IsUTF8TrailByte(s[i + 1]) &&
396 // check for non-shortest form and surrogate
397 (lead != 0xe0 || s[i] >= 0xa0) &&
398 (lead != 0xed || s[i] < 0xa0)) {
399 i += 2; // 3-byte character
400 } else if (0xf0 <= lead && lead <= 0xf4 && (i + 3) <= length &&
401 IsUTF8TrailByte(s[i]) &&
402 IsUTF8TrailByte(s[i + 1]) &&
403 IsUTF8TrailByte(s[i + 2]) &&
404 // check for non-shortest form
405 (lead != 0xf0 || s[i] >= 0x90) &&
406 (lead != 0xf4 || s[i] < 0x90)) {
407 i += 3; // 4-byte character
408 } else {
409 return false;
410 }
411 }
412 return true;
413}
414
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 << "\"";
419 }
420}
421
422} // anonymous namespace
423
31f18b77
FG
424// Prints a ::string object.
425#if GTEST_HAS_GLOBAL_STRING
426void PrintStringTo(const ::string& s, ostream* os) {
1e59de90
TL
427 if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
428 if (GTEST_FLAG(print_utf8)) {
429 ConditionalPrintAsText(s.data(), s.size(), os);
430 }
431 }
31f18b77
FG
432}
433#endif // GTEST_HAS_GLOBAL_STRING
434
435void PrintStringTo(const ::std::string& s, ostream* os) {
1e59de90
TL
436 if (PrintCharsAsStringTo(s.data(), s.size(), os) == kHexEscape) {
437 if (GTEST_FLAG(print_utf8)) {
438 ConditionalPrintAsText(s.data(), s.size(), os);
439 }
440 }
31f18b77
FG
441}
442
443// Prints a ::wstring object.
444#if GTEST_HAS_GLOBAL_WSTRING
445void PrintWideStringTo(const ::wstring& s, ostream* os) {
446 PrintCharsAsStringTo(s.data(), s.size(), os);
447}
448#endif // GTEST_HAS_GLOBAL_WSTRING
449
450#if GTEST_HAS_STD_WSTRING
451void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
452 PrintCharsAsStringTo(s.data(), s.size(), os);
453}
454#endif // GTEST_HAS_STD_WSTRING
455
456} // namespace internal
457
458} // namespace testing