1*9880d681SAndroid Build Coastguard Worker // Copyright 2007, Google Inc.
2*9880d681SAndroid Build Coastguard Worker // All rights reserved.
3*9880d681SAndroid Build Coastguard Worker //
4*9880d681SAndroid Build Coastguard Worker // Redistribution and use in source and binary forms, with or without
5*9880d681SAndroid Build Coastguard Worker // modification, are permitted provided that the following conditions are
6*9880d681SAndroid Build Coastguard Worker // met:
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker // * Redistributions of source code must retain the above copyright
9*9880d681SAndroid Build Coastguard Worker // notice, this list of conditions and the following disclaimer.
10*9880d681SAndroid Build Coastguard Worker // * Redistributions in binary form must reproduce the above
11*9880d681SAndroid Build Coastguard Worker // copyright notice, this list of conditions and the following disclaimer
12*9880d681SAndroid Build Coastguard Worker // in the documentation and/or other materials provided with the
13*9880d681SAndroid Build Coastguard Worker // distribution.
14*9880d681SAndroid Build Coastguard Worker // * Neither the name of Google Inc. nor the names of its
15*9880d681SAndroid Build Coastguard Worker // contributors may be used to endorse or promote products derived from
16*9880d681SAndroid Build Coastguard Worker // this software without specific prior written permission.
17*9880d681SAndroid Build Coastguard Worker //
18*9880d681SAndroid Build Coastguard Worker // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19*9880d681SAndroid Build Coastguard Worker // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*9880d681SAndroid Build Coastguard Worker // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*9880d681SAndroid Build Coastguard Worker // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22*9880d681SAndroid Build Coastguard Worker // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*9880d681SAndroid Build Coastguard Worker // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*9880d681SAndroid Build Coastguard Worker // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*9880d681SAndroid Build Coastguard Worker // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*9880d681SAndroid Build Coastguard Worker // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*9880d681SAndroid Build Coastguard Worker // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*9880d681SAndroid Build Coastguard Worker // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*9880d681SAndroid Build Coastguard Worker //
30*9880d681SAndroid Build Coastguard Worker // Author: [email protected] (Zhanyong Wan)
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker // Google Test - The Google C++ Testing Framework
33*9880d681SAndroid Build Coastguard Worker //
34*9880d681SAndroid Build Coastguard Worker // This file implements a universal value printer that can print a
35*9880d681SAndroid Build Coastguard Worker // value of any type T:
36*9880d681SAndroid Build Coastguard Worker //
37*9880d681SAndroid Build Coastguard Worker // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr);
38*9880d681SAndroid Build Coastguard Worker //
39*9880d681SAndroid Build Coastguard Worker // It uses the << operator when possible, and prints the bytes in the
40*9880d681SAndroid Build Coastguard Worker // object otherwise. A user can override its behavior for a class
41*9880d681SAndroid Build Coastguard Worker // type Foo by defining either operator<<(::std::ostream&, const Foo&)
42*9880d681SAndroid Build Coastguard Worker // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
43*9880d681SAndroid Build Coastguard Worker // defines Foo.
44*9880d681SAndroid Build Coastguard Worker
45*9880d681SAndroid Build Coastguard Worker #include "gtest/gtest-printers.h"
46*9880d681SAndroid Build Coastguard Worker #include <ctype.h>
47*9880d681SAndroid Build Coastguard Worker #include <stdio.h>
48*9880d681SAndroid Build Coastguard Worker #include <ostream> // NOLINT
49*9880d681SAndroid Build Coastguard Worker #include <string>
50*9880d681SAndroid Build Coastguard Worker #include "gtest/internal/gtest-port.h"
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker namespace testing {
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker namespace {
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker using ::std::ostream;
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker #if GTEST_OS_WINDOWS_MOBILE // Windows CE does not define _snprintf_s.
59*9880d681SAndroid Build Coastguard Worker # define snprintf _snprintf
60*9880d681SAndroid Build Coastguard Worker #elif _MSC_VER >= 1400 // VC 8.0 and later deprecate snprintf and _snprintf.
61*9880d681SAndroid Build Coastguard Worker # define snprintf _snprintf_s
62*9880d681SAndroid Build Coastguard Worker #elif _MSC_VER
63*9880d681SAndroid Build Coastguard Worker # define snprintf _snprintf
64*9880d681SAndroid Build Coastguard Worker #endif // GTEST_OS_WINDOWS_MOBILE
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker // Prints a segment of bytes in the given object.
PrintByteSegmentInObjectTo(const unsigned char * obj_bytes,size_t start,size_t count,ostream * os)67*9880d681SAndroid Build Coastguard Worker void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start,
68*9880d681SAndroid Build Coastguard Worker size_t count, ostream* os) {
69*9880d681SAndroid Build Coastguard Worker char text[5] = "";
70*9880d681SAndroid Build Coastguard Worker for (size_t i = 0; i != count; i++) {
71*9880d681SAndroid Build Coastguard Worker const size_t j = start + i;
72*9880d681SAndroid Build Coastguard Worker if (i != 0) {
73*9880d681SAndroid Build Coastguard Worker // Organizes the bytes into groups of 2 for easy parsing by
74*9880d681SAndroid Build Coastguard Worker // human.
75*9880d681SAndroid Build Coastguard Worker if ((j % 2) == 0)
76*9880d681SAndroid Build Coastguard Worker *os << ' ';
77*9880d681SAndroid Build Coastguard Worker else
78*9880d681SAndroid Build Coastguard Worker *os << '-';
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker snprintf(text, sizeof(text), "%02X", obj_bytes[j]);
81*9880d681SAndroid Build Coastguard Worker *os << text;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker }
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker // Prints the bytes in the given value to the given ostream.
PrintBytesInObjectToImpl(const unsigned char * obj_bytes,size_t count,ostream * os)86*9880d681SAndroid Build Coastguard Worker void PrintBytesInObjectToImpl(const unsigned char* obj_bytes, size_t count,
87*9880d681SAndroid Build Coastguard Worker ostream* os) {
88*9880d681SAndroid Build Coastguard Worker // Tells the user how big the object is.
89*9880d681SAndroid Build Coastguard Worker *os << count << "-byte object <";
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker const size_t kThreshold = 132;
92*9880d681SAndroid Build Coastguard Worker const size_t kChunkSize = 64;
93*9880d681SAndroid Build Coastguard Worker // If the object size is bigger than kThreshold, we'll have to omit
94*9880d681SAndroid Build Coastguard Worker // some details by printing only the first and the last kChunkSize
95*9880d681SAndroid Build Coastguard Worker // bytes.
96*9880d681SAndroid Build Coastguard Worker // TODO(wan): let the user control the threshold using a flag.
97*9880d681SAndroid Build Coastguard Worker if (count < kThreshold) {
98*9880d681SAndroid Build Coastguard Worker PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
99*9880d681SAndroid Build Coastguard Worker } else {
100*9880d681SAndroid Build Coastguard Worker PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
101*9880d681SAndroid Build Coastguard Worker *os << " ... ";
102*9880d681SAndroid Build Coastguard Worker // Rounds up to 2-byte boundary.
103*9880d681SAndroid Build Coastguard Worker const size_t resume_pos = (count - kChunkSize + 1)/2*2;
104*9880d681SAndroid Build Coastguard Worker PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker *os << ">";
107*9880d681SAndroid Build Coastguard Worker }
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker } // namespace
110*9880d681SAndroid Build Coastguard Worker
111*9880d681SAndroid Build Coastguard Worker namespace internal2 {
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker // Delegates to PrintBytesInObjectToImpl() to print the bytes in the
114*9880d681SAndroid Build Coastguard Worker // given object. The delegation simplifies the implementation, which
115*9880d681SAndroid Build Coastguard Worker // uses the << operator and thus is easier done outside of the
116*9880d681SAndroid Build Coastguard Worker // ::testing::internal namespace, which contains a << operator that
117*9880d681SAndroid Build Coastguard Worker // sometimes conflicts with the one in STL.
PrintBytesInObjectTo(const unsigned char * obj_bytes,size_t count,ostream * os)118*9880d681SAndroid Build Coastguard Worker void PrintBytesInObjectTo(const unsigned char* obj_bytes, size_t count,
119*9880d681SAndroid Build Coastguard Worker ostream* os) {
120*9880d681SAndroid Build Coastguard Worker PrintBytesInObjectToImpl(obj_bytes, count, os);
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker } // namespace internal2
124*9880d681SAndroid Build Coastguard Worker
125*9880d681SAndroid Build Coastguard Worker namespace internal {
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker // Depending on the value of a char (or wchar_t), we print it in one
128*9880d681SAndroid Build Coastguard Worker // of three formats:
129*9880d681SAndroid Build Coastguard Worker // - as is if it's a printable ASCII (e.g. 'a', '2', ' '),
130*9880d681SAndroid Build Coastguard Worker // - as a hexadecimal escape sequence (e.g. '\x7F'), or
131*9880d681SAndroid Build Coastguard Worker // - as a special escape sequence (e.g. '\r', '\n').
132*9880d681SAndroid Build Coastguard Worker enum CharFormat {
133*9880d681SAndroid Build Coastguard Worker kAsIs,
134*9880d681SAndroid Build Coastguard Worker kHexEscape,
135*9880d681SAndroid Build Coastguard Worker kSpecialEscape
136*9880d681SAndroid Build Coastguard Worker };
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker // Returns true if c is a printable ASCII character. We test the
139*9880d681SAndroid Build Coastguard Worker // value of c directly instead of calling isprint(), which is buggy on
140*9880d681SAndroid Build Coastguard Worker // Windows Mobile.
IsPrintableAscii(wchar_t c)141*9880d681SAndroid Build Coastguard Worker inline bool IsPrintableAscii(wchar_t c) {
142*9880d681SAndroid Build Coastguard Worker return 0x20 <= c && c <= 0x7E;
143*9880d681SAndroid Build Coastguard Worker }
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker // Prints a wide or narrow char c as a character literal without the
146*9880d681SAndroid Build Coastguard Worker // quotes, escaping it when necessary; returns how c was formatted.
147*9880d681SAndroid Build Coastguard Worker // The template argument UnsignedChar is the unsigned version of Char,
148*9880d681SAndroid Build Coastguard Worker // which is the type of c.
149*9880d681SAndroid Build Coastguard Worker template <typename UnsignedChar, typename Char>
PrintAsCharLiteralTo(Char c,ostream * os)150*9880d681SAndroid Build Coastguard Worker static CharFormat PrintAsCharLiteralTo(Char c, ostream* os) {
151*9880d681SAndroid Build Coastguard Worker switch (static_cast<wchar_t>(c)) {
152*9880d681SAndroid Build Coastguard Worker case L'\0':
153*9880d681SAndroid Build Coastguard Worker *os << "\\0";
154*9880d681SAndroid Build Coastguard Worker break;
155*9880d681SAndroid Build Coastguard Worker case L'\'':
156*9880d681SAndroid Build Coastguard Worker *os << "\\'";
157*9880d681SAndroid Build Coastguard Worker break;
158*9880d681SAndroid Build Coastguard Worker case L'\\':
159*9880d681SAndroid Build Coastguard Worker *os << "\\\\";
160*9880d681SAndroid Build Coastguard Worker break;
161*9880d681SAndroid Build Coastguard Worker case L'\a':
162*9880d681SAndroid Build Coastguard Worker *os << "\\a";
163*9880d681SAndroid Build Coastguard Worker break;
164*9880d681SAndroid Build Coastguard Worker case L'\b':
165*9880d681SAndroid Build Coastguard Worker *os << "\\b";
166*9880d681SAndroid Build Coastguard Worker break;
167*9880d681SAndroid Build Coastguard Worker case L'\f':
168*9880d681SAndroid Build Coastguard Worker *os << "\\f";
169*9880d681SAndroid Build Coastguard Worker break;
170*9880d681SAndroid Build Coastguard Worker case L'\n':
171*9880d681SAndroid Build Coastguard Worker *os << "\\n";
172*9880d681SAndroid Build Coastguard Worker break;
173*9880d681SAndroid Build Coastguard Worker case L'\r':
174*9880d681SAndroid Build Coastguard Worker *os << "\\r";
175*9880d681SAndroid Build Coastguard Worker break;
176*9880d681SAndroid Build Coastguard Worker case L'\t':
177*9880d681SAndroid Build Coastguard Worker *os << "\\t";
178*9880d681SAndroid Build Coastguard Worker break;
179*9880d681SAndroid Build Coastguard Worker case L'\v':
180*9880d681SAndroid Build Coastguard Worker *os << "\\v";
181*9880d681SAndroid Build Coastguard Worker break;
182*9880d681SAndroid Build Coastguard Worker default:
183*9880d681SAndroid Build Coastguard Worker if (IsPrintableAscii(c)) {
184*9880d681SAndroid Build Coastguard Worker *os << static_cast<char>(c);
185*9880d681SAndroid Build Coastguard Worker return kAsIs;
186*9880d681SAndroid Build Coastguard Worker } else {
187*9880d681SAndroid Build Coastguard Worker *os << String::Format("\\x%X", static_cast<UnsignedChar>(c));
188*9880d681SAndroid Build Coastguard Worker return kHexEscape;
189*9880d681SAndroid Build Coastguard Worker }
190*9880d681SAndroid Build Coastguard Worker }
191*9880d681SAndroid Build Coastguard Worker return kSpecialEscape;
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker // Prints a char c as if it's part of a string literal, escaping it when
195*9880d681SAndroid Build Coastguard Worker // necessary; returns how c was formatted.
PrintAsWideStringLiteralTo(wchar_t c,ostream * os)196*9880d681SAndroid Build Coastguard Worker static CharFormat PrintAsWideStringLiteralTo(wchar_t c, ostream* os) {
197*9880d681SAndroid Build Coastguard Worker switch (c) {
198*9880d681SAndroid Build Coastguard Worker case L'\'':
199*9880d681SAndroid Build Coastguard Worker *os << "'";
200*9880d681SAndroid Build Coastguard Worker return kAsIs;
201*9880d681SAndroid Build Coastguard Worker case L'"':
202*9880d681SAndroid Build Coastguard Worker *os << "\\\"";
203*9880d681SAndroid Build Coastguard Worker return kSpecialEscape;
204*9880d681SAndroid Build Coastguard Worker default:
205*9880d681SAndroid Build Coastguard Worker return PrintAsCharLiteralTo<wchar_t>(c, os);
206*9880d681SAndroid Build Coastguard Worker }
207*9880d681SAndroid Build Coastguard Worker }
208*9880d681SAndroid Build Coastguard Worker
209*9880d681SAndroid Build Coastguard Worker // Prints a char c as if it's part of a string literal, escaping it when
210*9880d681SAndroid Build Coastguard Worker // necessary; returns how c was formatted.
PrintAsNarrowStringLiteralTo(char c,ostream * os)211*9880d681SAndroid Build Coastguard Worker static CharFormat PrintAsNarrowStringLiteralTo(char c, ostream* os) {
212*9880d681SAndroid Build Coastguard Worker return PrintAsWideStringLiteralTo(static_cast<unsigned char>(c), os);
213*9880d681SAndroid Build Coastguard Worker }
214*9880d681SAndroid Build Coastguard Worker
215*9880d681SAndroid Build Coastguard Worker // Prints a wide or narrow character c and its code. '\0' is printed
216*9880d681SAndroid Build Coastguard Worker // as "'\\0'", other unprintable characters are also properly escaped
217*9880d681SAndroid Build Coastguard Worker // using the standard C++ escape sequence. The template argument
218*9880d681SAndroid Build Coastguard Worker // UnsignedChar is the unsigned version of Char, which is the type of c.
219*9880d681SAndroid Build Coastguard Worker template <typename UnsignedChar, typename Char>
PrintCharAndCodeTo(Char c,ostream * os)220*9880d681SAndroid Build Coastguard Worker void PrintCharAndCodeTo(Char c, ostream* os) {
221*9880d681SAndroid Build Coastguard Worker // First, print c as a literal in the most readable form we can find.
222*9880d681SAndroid Build Coastguard Worker *os << ((sizeof(c) > 1) ? "L'" : "'");
223*9880d681SAndroid Build Coastguard Worker const CharFormat format = PrintAsCharLiteralTo<UnsignedChar>(c, os);
224*9880d681SAndroid Build Coastguard Worker *os << "'";
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker // To aid user debugging, we also print c's code in decimal, unless
227*9880d681SAndroid Build Coastguard Worker // it's 0 (in which case c was printed as '\\0', making the code
228*9880d681SAndroid Build Coastguard Worker // obvious).
229*9880d681SAndroid Build Coastguard Worker if (c == 0)
230*9880d681SAndroid Build Coastguard Worker return;
231*9880d681SAndroid Build Coastguard Worker *os << " (" << String::Format("%d", c).c_str();
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker // For more convenience, we print c's code again in hexadecimal,
234*9880d681SAndroid Build Coastguard Worker // unless c was already printed in the form '\x##' or the code is in
235*9880d681SAndroid Build Coastguard Worker // [1, 9].
236*9880d681SAndroid Build Coastguard Worker if (format == kHexEscape || (1 <= c && c <= 9)) {
237*9880d681SAndroid Build Coastguard Worker // Do nothing.
238*9880d681SAndroid Build Coastguard Worker } else {
239*9880d681SAndroid Build Coastguard Worker *os << String::Format(", 0x%X",
240*9880d681SAndroid Build Coastguard Worker static_cast<UnsignedChar>(c)).c_str();
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker *os << ")";
243*9880d681SAndroid Build Coastguard Worker }
244*9880d681SAndroid Build Coastguard Worker
PrintTo(unsigned char c,::std::ostream * os)245*9880d681SAndroid Build Coastguard Worker void PrintTo(unsigned char c, ::std::ostream* os) {
246*9880d681SAndroid Build Coastguard Worker PrintCharAndCodeTo<unsigned char>(c, os);
247*9880d681SAndroid Build Coastguard Worker }
PrintTo(signed char c,::std::ostream * os)248*9880d681SAndroid Build Coastguard Worker void PrintTo(signed char c, ::std::ostream* os) {
249*9880d681SAndroid Build Coastguard Worker PrintCharAndCodeTo<unsigned char>(c, os);
250*9880d681SAndroid Build Coastguard Worker }
251*9880d681SAndroid Build Coastguard Worker
252*9880d681SAndroid Build Coastguard Worker // Prints a wchar_t as a symbol if it is printable or as its internal
253*9880d681SAndroid Build Coastguard Worker // code otherwise and also as its code. L'\0' is printed as "L'\\0'".
PrintTo(wchar_t wc,ostream * os)254*9880d681SAndroid Build Coastguard Worker void PrintTo(wchar_t wc, ostream* os) {
255*9880d681SAndroid Build Coastguard Worker PrintCharAndCodeTo<wchar_t>(wc, os);
256*9880d681SAndroid Build Coastguard Worker }
257*9880d681SAndroid Build Coastguard Worker
258*9880d681SAndroid Build Coastguard Worker // Prints the given array of characters to the ostream.
259*9880d681SAndroid Build Coastguard Worker // The array starts at *begin, the length is len, it may include '\0' characters
260*9880d681SAndroid Build Coastguard Worker // and may not be null-terminated.
PrintCharsAsStringTo(const char * begin,size_t len,ostream * os)261*9880d681SAndroid Build Coastguard Worker static void PrintCharsAsStringTo(const char* begin, size_t len, ostream* os) {
262*9880d681SAndroid Build Coastguard Worker *os << "\"";
263*9880d681SAndroid Build Coastguard Worker bool is_previous_hex = false;
264*9880d681SAndroid Build Coastguard Worker for (size_t index = 0; index < len; ++index) {
265*9880d681SAndroid Build Coastguard Worker const char cur = begin[index];
266*9880d681SAndroid Build Coastguard Worker if (is_previous_hex && IsXDigit(cur)) {
267*9880d681SAndroid Build Coastguard Worker // Previous character is of '\x..' form and this character can be
268*9880d681SAndroid Build Coastguard Worker // interpreted as another hexadecimal digit in its number. Break string to
269*9880d681SAndroid Build Coastguard Worker // disambiguate.
270*9880d681SAndroid Build Coastguard Worker *os << "\" \"";
271*9880d681SAndroid Build Coastguard Worker }
272*9880d681SAndroid Build Coastguard Worker is_previous_hex = PrintAsNarrowStringLiteralTo(cur, os) == kHexEscape;
273*9880d681SAndroid Build Coastguard Worker }
274*9880d681SAndroid Build Coastguard Worker *os << "\"";
275*9880d681SAndroid Build Coastguard Worker }
276*9880d681SAndroid Build Coastguard Worker
277*9880d681SAndroid Build Coastguard Worker // Prints a (const) char array of 'len' elements, starting at address 'begin'.
UniversalPrintArray(const char * begin,size_t len,ostream * os)278*9880d681SAndroid Build Coastguard Worker void UniversalPrintArray(const char* begin, size_t len, ostream* os) {
279*9880d681SAndroid Build Coastguard Worker PrintCharsAsStringTo(begin, len, os);
280*9880d681SAndroid Build Coastguard Worker }
281*9880d681SAndroid Build Coastguard Worker
282*9880d681SAndroid Build Coastguard Worker // Prints the given array of wide characters to the ostream.
283*9880d681SAndroid Build Coastguard Worker // The array starts at *begin, the length is len, it may include L'\0'
284*9880d681SAndroid Build Coastguard Worker // characters and may not be null-terminated.
PrintWideCharsAsStringTo(const wchar_t * begin,size_t len,ostream * os)285*9880d681SAndroid Build Coastguard Worker static void PrintWideCharsAsStringTo(const wchar_t* begin, size_t len,
286*9880d681SAndroid Build Coastguard Worker ostream* os) {
287*9880d681SAndroid Build Coastguard Worker *os << "L\"";
288*9880d681SAndroid Build Coastguard Worker bool is_previous_hex = false;
289*9880d681SAndroid Build Coastguard Worker for (size_t index = 0; index < len; ++index) {
290*9880d681SAndroid Build Coastguard Worker const wchar_t cur = begin[index];
291*9880d681SAndroid Build Coastguard Worker if (is_previous_hex && isascii(cur) && IsXDigit(static_cast<char>(cur))) {
292*9880d681SAndroid Build Coastguard Worker // Previous character is of '\x..' form and this character can be
293*9880d681SAndroid Build Coastguard Worker // interpreted as another hexadecimal digit in its number. Break string to
294*9880d681SAndroid Build Coastguard Worker // disambiguate.
295*9880d681SAndroid Build Coastguard Worker *os << "\" L\"";
296*9880d681SAndroid Build Coastguard Worker }
297*9880d681SAndroid Build Coastguard Worker is_previous_hex = PrintAsWideStringLiteralTo(cur, os) == kHexEscape;
298*9880d681SAndroid Build Coastguard Worker }
299*9880d681SAndroid Build Coastguard Worker *os << "\"";
300*9880d681SAndroid Build Coastguard Worker }
301*9880d681SAndroid Build Coastguard Worker
302*9880d681SAndroid Build Coastguard Worker // Prints the given C string to the ostream.
PrintTo(const char * s,ostream * os)303*9880d681SAndroid Build Coastguard Worker void PrintTo(const char* s, ostream* os) {
304*9880d681SAndroid Build Coastguard Worker if (s == NULL) {
305*9880d681SAndroid Build Coastguard Worker *os << "NULL";
306*9880d681SAndroid Build Coastguard Worker } else {
307*9880d681SAndroid Build Coastguard Worker *os << ImplicitCast_<const void*>(s) << " pointing to ";
308*9880d681SAndroid Build Coastguard Worker PrintCharsAsStringTo(s, strlen(s), os);
309*9880d681SAndroid Build Coastguard Worker }
310*9880d681SAndroid Build Coastguard Worker }
311*9880d681SAndroid Build Coastguard Worker
312*9880d681SAndroid Build Coastguard Worker // MSVC compiler can be configured to define whar_t as a typedef
313*9880d681SAndroid Build Coastguard Worker // of unsigned short. Defining an overload for const wchar_t* in that case
314*9880d681SAndroid Build Coastguard Worker // would cause pointers to unsigned shorts be printed as wide strings,
315*9880d681SAndroid Build Coastguard Worker // possibly accessing more memory than intended and causing invalid
316*9880d681SAndroid Build Coastguard Worker // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when
317*9880d681SAndroid Build Coastguard Worker // wchar_t is implemented as a native type.
318*9880d681SAndroid Build Coastguard Worker #if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED)
319*9880d681SAndroid Build Coastguard Worker // Prints the given wide C string to the ostream.
PrintTo(const wchar_t * s,ostream * os)320*9880d681SAndroid Build Coastguard Worker void PrintTo(const wchar_t* s, ostream* os) {
321*9880d681SAndroid Build Coastguard Worker if (s == NULL) {
322*9880d681SAndroid Build Coastguard Worker *os << "NULL";
323*9880d681SAndroid Build Coastguard Worker } else {
324*9880d681SAndroid Build Coastguard Worker *os << ImplicitCast_<const void*>(s) << " pointing to ";
325*9880d681SAndroid Build Coastguard Worker PrintWideCharsAsStringTo(s, wcslen(s), os);
326*9880d681SAndroid Build Coastguard Worker }
327*9880d681SAndroid Build Coastguard Worker }
328*9880d681SAndroid Build Coastguard Worker #endif // wchar_t is native
329*9880d681SAndroid Build Coastguard Worker
330*9880d681SAndroid Build Coastguard Worker // Prints a ::string object.
331*9880d681SAndroid Build Coastguard Worker #if GTEST_HAS_GLOBAL_STRING
PrintStringTo(const::string & s,ostream * os)332*9880d681SAndroid Build Coastguard Worker void PrintStringTo(const ::string& s, ostream* os) {
333*9880d681SAndroid Build Coastguard Worker PrintCharsAsStringTo(s.data(), s.size(), os);
334*9880d681SAndroid Build Coastguard Worker }
335*9880d681SAndroid Build Coastguard Worker #endif // GTEST_HAS_GLOBAL_STRING
336*9880d681SAndroid Build Coastguard Worker
PrintStringTo(const::std::string & s,ostream * os)337*9880d681SAndroid Build Coastguard Worker void PrintStringTo(const ::std::string& s, ostream* os) {
338*9880d681SAndroid Build Coastguard Worker PrintCharsAsStringTo(s.data(), s.size(), os);
339*9880d681SAndroid Build Coastguard Worker }
340*9880d681SAndroid Build Coastguard Worker
341*9880d681SAndroid Build Coastguard Worker // Prints a ::wstring object.
342*9880d681SAndroid Build Coastguard Worker #if GTEST_HAS_GLOBAL_WSTRING
PrintWideStringTo(const::wstring & s,ostream * os)343*9880d681SAndroid Build Coastguard Worker void PrintWideStringTo(const ::wstring& s, ostream* os) {
344*9880d681SAndroid Build Coastguard Worker PrintWideCharsAsStringTo(s.data(), s.size(), os);
345*9880d681SAndroid Build Coastguard Worker }
346*9880d681SAndroid Build Coastguard Worker #endif // GTEST_HAS_GLOBAL_WSTRING
347*9880d681SAndroid Build Coastguard Worker
348*9880d681SAndroid Build Coastguard Worker #if GTEST_HAS_STD_WSTRING
PrintWideStringTo(const::std::wstring & s,ostream * os)349*9880d681SAndroid Build Coastguard Worker void PrintWideStringTo(const ::std::wstring& s, ostream* os) {
350*9880d681SAndroid Build Coastguard Worker PrintWideCharsAsStringTo(s.data(), s.size(), os);
351*9880d681SAndroid Build Coastguard Worker }
352*9880d681SAndroid Build Coastguard Worker #endif // GTEST_HAS_STD_WSTRING
353*9880d681SAndroid Build Coastguard Worker
354*9880d681SAndroid Build Coastguard Worker } // namespace internal
355*9880d681SAndroid Build Coastguard Worker
356*9880d681SAndroid Build Coastguard Worker } // namespace testing
357