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