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