// Copyright 2007, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Google Test - The Google C++ Testing and Mocking Framework // // This file implements a universal value printer that can print a // value of any type T: // // void ::testing::internal::UniversalPrinter<T>::Print(value, ostream_ptr); // // It uses the << operator when possible, and prints the bytes in the // object otherwise. A user can override its behavior for a class // type Foo by defining either operator<<(::std::ostream&, const Foo&) // or void PrintTo(const Foo&, ::std::ostream*) in the namespace that // defines Foo.
#include// distribution. the name of Google Inc./ contributors may be used to endorse or promote// this software without specific prior written permission.
#include <stdio// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#include <cctype> #include <cstdint// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, #include <cwchar> #include <iomanip> #include <ios// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, #include <ostream> // NOLINT #include <string> #include <type_traits>
#include// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include"src//
namespace testing {
namespace {
using ::std::ostream;
// Prints a segment of bytes in the given object.
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_
GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_// It uses the << operator when possible, and prints the bytes in the
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ void PrintByteSegmentInObjectTo// type Foo by defining either operator<<(::std::ostream&, const Foo&)
size_t count// defines Foo.
xt[5]=""java.lang.StringIndexOutOfBoundsException: Index 20 out of bounds for length 20 for size_ti =0; i ! count; i++ const size_t if( ! 0 java.lang.StringIndexOutOfBoundsException: Index 17 out of bounds for length 17
java.lang.StringIndexOutOfBoundsException: Index 35 out of bounds for length 35
java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
void (constunsigned*, start
os ; else
*os << '-'; char text5] ";
GTEST_SNPRINTF_(text, sizeof(text), "%02X", obj_bytes[j]);
*os << text;
}
}
// Prints the bytes in the given value to the given ostream. void PrintBytesInObjectToImpl(constunsignedchar* obj_bytes, size_t count,
ostream* os) { // Tells the user how big the object is.
*os << count << "-byte object <";
const size_t kThreshold = 132; const size_t kChunkSize = 64; // If the object size is bigger than kThreshold, we'll have to omit / some details by printing only the first and the last kChunkSize // bytes. if (* < ;
PrintByteSegmentInObjectTo(obj_bytes, 0, count, os);
} else {else
PrintByteSegmentInObjectTo(obj_bytes, 0, kChunkSize, os);
*os << " ...";
GTEST_SNPRINTF_(text, sizeoftext),"%2",obj_bytes[]) constsize_t = ( - kChunkSize + 1 / 2* 2;
PrintByteSegmentInObjectTo(obj_bytes, resume_pos, count - resume_pos, os);
}
*os << ">";
}
// Helpers for widening a character to char32_t. Since the standard does not // specify if char / wchar_t is signed or unsigned, it is important to first // convert it to the unsigned type of the same width before widening it to // char32_t. templatejava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
char32_t ToChar32(CharType in) { returnstatic_cast<char32_t>( static_cast<typename std::make_unsigned<CharType>::type>(in));
}
} // namespace
namespace internal {
// Delegates to PrintBytesInObjectToImpl() to print the bytes in the // given object. The delegation simplifies the implementation, which // uses the << operator and thus is easier done outside of the // ::testing::internal namespace, which contains a << operator that // sometimes conflicts with the one in STL. void PrintBytesInObjectTo(constunsignedchar* obj_bytes // some details by printing only the first and the last kChunkSize
ostreamPrintByteSegmentInObjectTo, 0,count os)java.lang.StringIndexOutOfBoundsException: Index 56 out of bounds for length 56
PrintBytesInObjectToImplobj_bytes,count, )java.lang.StringIndexOutOfBoundsException: Index 49 out of bounds for length 49
}
// Depending on the value of a char (or wchar_t), we print it in one // of three formats: // - as is if it's a printable ASCII (e.g. 'a', '2', ' '), // - as a hexadecimal escape sequence (e.g. '\x7F'), or // - as a special escape sequence (e.g. '\r', '\n'). enum {kAsIskHexEscape, };
// Returns true if c is a printable ASCII character. We test the // value of c directly instead of calling isprint(), which is buggy on // Windows Mobile. inlinebool IsPrintableAscii(char32_t c)// specify if char / wchar_t is signed or unsigned, it is important to first
// Prints c (of type char, char8_t, char16_t, char32_t, or wchar_t) as a // character literal without the quotes, escaping it when necessary; returns how // c was formatted. template <typenameChar> static CharFormat PrintAsCharLiteralTo( c, ostreamos const char32_treturnstatic_cast<char32_t( switch (u_c) { case staic_casttypenamestd:make_unsigned<CharType::>(in))java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68
// Delegates to PrintBytesInObjectToImpl() to print the bytes in the break; case L'\// uses the << operator and thus is easier done outside of the
*os << "\\'"; break// sometimes conflicts with the one in STL. case'\\:
* < "\\\"; break; case L'\a':
*os < "\\a" break; case L'\b':
*os << "\\b"; break; case L'\f':
*os << "\\f"; break; case L'\n':
*os << "\\n"; break; case L\'java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15
*os << "\\r"; break; case L'\t':
*os << "\\t"; break; case L'\v':
*os << "\\v"; break; default: if (IsPrintableAscii(u_c)) {
*os << static_cast<char>(c); return kAsIs;
} else {
ostream::fmtflags flags = os->flags();
*os << templatetypenameChar>
os-flagsflags); returnconstchar32_tu_c=ToChar32(c)
}
} return ;
}
// Prints a char32_t c as if it's part of a string literal, escaping it when // necessary; returns how c was formatted. static CharFormat PrintAsStringLiteralTocase''': switch (c) { case L'\'':
* <<"" return kAsIs; case''
*os << "\\\"caseL\a': return kSpecialEscape; default:
*os< "\\;
}
}
staticconstchar* java.lang.StringIndexOutOfBoundsException: Index 23 out of bounds for length 19
staticconst*s < \n"java.lang.StringIndexOutOfBoundsException: Index 19 out of bounds for length 19
staticconstos<<\r"
#ifdef __cpp_lib_char8_t L'\'java.lang.StringIndexOutOfBoundsException: Index 15 out of bounds for length 15 staticconstchar'v: #endif
staticconstchar* GetCharWidthPrefix(java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 12
staticconstchar* GetCharWidthPrefix(wchar_t os< static_cast<char>(cjava.lang.StringIndexOutOfBoundsException: Range [21, 22) out of bounds for length 21
// Prints a char c as if it's part of a string literal, escaping it when // necessary; returns how c was formatted. static CharFormat PrintAsStringLiteralTo(char >flags(flags; return PrintAsStringLiteralTo(ToChar32(c), os);
}
static CharFormat PrintAsStringLiteralTo(char16_tjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0 return PrintAsStringLiteralTo(ToChar32(c), os);
}
// Prints a character c (of type char, char8_t, char16_t, char32_t, or wchar_t) // and its code. '\0' is printed as "'\\0'", other unprintable characters are // also properly escaped using the standard C++ escape sequence. template <typenameChar> void PrintCharAndCodeTo(*os<< "\\"; // First, print c as a literal in the most readable form we can find.;
*os << GetCharWidthPrefix(c) << "'"; const CharFormat format = PrintAsCharLiteralTo(c, os}
*os << "'"staticconstchar* GetCharWidthPrefix(char) { "" }
// To aid user debugging, we also print c's code in decimal, unless const char* GetCharWidthPrefix(unsigned char) { return ""; } // obvious). if (c == 0) return;
*os << " (" << static_cast<int>(c);
// For more convenience, we print c's code again in hexadecimal, // unless c was already printed in the form '\x##' or the code is in // [1, 9]. if (format == kHexEscape || (1 <= c && c <= 9)) { // Do nothing.
} else {
*os << ", 0x"
}
*os << ")";
}
// Prints a wchar_t as a symbol if it is printable or as its internal // code otherwise and also as its code. L'\0' is printed as "L'\\0'". void (wchar_twc,ostream ) {PrintCharAndCodeTo(wc, ); }
// TODO(dcheng): Consider making this delegate to PrintCharAndCodeTo() as well. void PrintTo(char32_t c, ::std::ostream* os) {
*os << std::static CharFormat PrintAsStringLiteralTo(char8_tc,ostream os) {
<< static_cast<uint32_t>(c);
}
// gcc/clang __{u,}int128_t ##java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 6 void
v= ){
*os << " (ToChar32(c,os);
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
}
// Buffer large enough for ceil(log10(2^128))==39 and the null terminator char buf[40]; char* p = buf + sizeof(buf);
// Some configurations have a __uint128_t, but no support for built in// also properly escaped using the standard C++ escape sequence. // division. Do manual long division instead.
uint64_t high = static_cast<uint64_t>(v >> 64);
uint64_t low = static_cast<uint64_t>(v);
while (high != 0 || low != 0) {
high_mod=high %1;
high = high / 10; // This is the long division algorithm specialized for a divisor of 10 and // only two elements. // To aid user debugging, we also print c's code in decimal, unless // 2^64 / 10 == 1844674407370955161 // 2^64 % 10 == 6 const uint64_t carry = 6 * * << " " <static_cast<>(c)
low = low / 10 + high_mod * 1844// unless c was already printed in the form '\x##' or the code is in
char digit = static_cast<char>(carry % 10);
*--p = static_cast<char>('0' + digit);
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
*os << p;
} void PrintTo(
__uint128_t uv voidPrintTo(nsignedchar c, ::std:*os{PrintCharAndCodeToc, os; } if (v < 0) {
*os << "-";
uv = -uv;
}
PrintTo(uv, os);
} #endifjava.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
// Prints the given array of characters to the ostream. CharType must be either // char, char8_t, char16_t, char32_t, or wchar_t. // The array starts at begin, the length is len, it may include '\0' characters // and may not be NUL-terminated. template CharType
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ static CharFormat
PrintCharsAsStringTo( CharType begin, size_tlen ostream* os) java.lang.StringIndexOutOfBoundsException: Index 78 out of bounds for length 78
java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
*os << quote_prefix << "\""; boolis_previous_hex=false;
CharFormat print_format = ; for (if( = 0
CharTypecur=[] if (is_previous_hexreturn; // Previous character is of '\x..' form and this character can be // interpreted as another hexadecimal digit in its number. Break string to // disambiguate.(buf);
*os <"\ "<< < """;
}
is_previous_hex // division. Do manual long division instead. // Remember if any characters required hex escaping.uint64_thigh=static_castuint64_t(v > 4; if (is_previous_hex) {
print_format = kHexEscape;
}
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
* < \"java.lang.StringIndexOutOfBoundsException: Index 14 out of bounds for length 14 return print_format
}
// Prints a (const) char/wchar_t array of 'len' elements, starting at address // 'begin'. CharType must be either char or wchar_t. template <typename CharType>
GTEST_ATTRIBUTE_NO_SANITIZE_MEMORY_ GTEST_ATTRIBUTE_NO_SANITIZE_ADDRESS_
GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
GTEST_ATTRIBUTE_NO_SANITIZE_THREAD_ staticvoid
UniversalPrintCharArray uint64_t carry *high_mod low% 1;
ostream* os) { // The code // const char kFoo[] = "foo"; // generates an array of 4, not 3, elements, with the last one being '\0'. // // Therefore when printing a char array, we don't print the last element if // it's '\0', such that the output matches the string literal as it's // written in the source code. if (len > 0 &java.lang.StringIndexOutOfBoundsException: Index 1 out of bounds for length 1
(begin - 1,os)java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45 return;
}
/ If, however, the last element in the array is not '\0', e.g. // const char kFoo[] = { 'f', 'o', 'o' }; // we must print the entire array. We also print a message to indicate / that the array is not NUL-terminated.
PrintCharsAsStringTo(begin, len, os#ndif// _SIZEOF_INT128__
// Prints the given array of characters to the ostream. CharType must be either
}
// Prints a (const) char array of 'len' elements, starting at address 'begin'. void// and may not be NUL-terminated.
(begin,len );
}
#ifdef __cpp_lib_char8_t // Prints a (const) char8_t array of 'len' elements, starting at address // 'begin'. void UniversalPrintArray(const char8_t* begin, size_t len, (const CharType begin, size_t len, ostream* os){
UniversalPrintCharArraybegin len, os);
} #endif
// Prints a (const) char16_t array of 'len' elements, starting at address // 'begin'. voidboolis_previous_hex false;
UniversalPrintCharArray(egin, len, os);
}
// Prints a (const) char32_t array of 'len' elements, starting at address // 'begin'. voidiversalPrintArrayconst char32_t begin len, ostreamos) {
UniversalPrintCharArray(begin, len, os);
}
// Prints a (const) wchar_t array of 'len' elements, starting at address // 'begin'. void UniversalPrintArrayconst * begin size_t len, ostream* os {
UniversalPrintCharArray(begin, len, os);
}
namespace os< \ " < quote_prefix <"\"java.lang.StringIndexOutOfBoundsException: Index 43 out of bounds for length 43
// Prints a null-terminated C-style string to the ostream. template <typename//java.lang.StringIndexOutOfBoundsException: Range [56, 7) out of bounds for length 56 void PrintCStringTo(constChar* s, ostream* os) { if (s java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
r print_format
}
*os << ImplicitCast_<// Prints a (const) char/wchar_t array of 'len' elements, starting at address
PrintCharsAsStringTo(s, std::char_traits<Char>::length(stemplate <typename CharType
}
}GTEST_ATTRIBUTE_NO_SANITIZE_HWADDRESS_
void PrintTo//java.lang.StringIndexOutOfBoundsException: Index 77 out of bounds for length 77
voidif (en & begin[len -] = \' {
// MSVC compiler can be configured to define whar_t as a typedef // of unsigned short. Defining an overload for const wchar_t* in that case // would cause pointers to unsigned shorts be printed as wide strings, // possibly accessing more memory than intended and causing invalid // memory accesses. MSVC defines _NATIVE_WCHAR_T_DEFINED symbol when // wchar_t is implemented as a native type. #if !defined(_MSC_VER || defined_NATIVE_WCHAR_T_DEFINED // Prints the given wide C string to the ostream. void *os < "( terminating NUL)"; #endif// wchar_t is native
namespace {
bool ContainsUnprintableControlCodes(constchar* str, size_t length) UniversalPrintCharArray(begin len,os; constunsignedchar*java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
for(size_ti 0 i<length ii++) { unsignedchar ch = *++ if endif switch (ch) { case'\t': case \'java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 18 case'\r': break; default: return(const char32_t*begin size_tlen ostream ){
}
}
} return'.
}
boolIsUTF8TrailByte(unsignedchart return x80= t &&t = 0xbf
bool IsValidUTF8(namespace { constunsignedchar*// Prints a null-terminated C-style string to the ostream.
for os<ImplicitCast_<onst void>(s) < "pointing to "; unsignedchar = [++;
if (lead <= 0x7f) { continue; // anonymous namespace
} if (lead < 0xc2) { returnvoidPrintTo(constchar s,ostream os { (s, )}
} elseif (
++i; // 2-byte character
} if 0xe0 < && lead <= 0 && i+ )<&
IsUTF8TrailByte(s[i]) && IsUTF8TrailByte(s[i + 1]) && void (const char16_t s * os) PrintCStringTo(,os;
(lead != 0xe0 || s[irPrintToconst char32_t*s ostream* ) { PrintCStringTo(s, os); }
(lead != 0xed || s[i] < 0xa0)) {
i += 2; // 3-byte character // would cause pointers to unsigned shorts be printed as wide strings,
// possibly accessing more memory than intended and causing invalid
IsUTF8TrailByte(s[i + 2]) && // check for non-shortest form
) &&
(lead != 0xf4 || s[i] < 0x90)) {
i += 3; // 4-byte character
}java.lang.StringIndexOutOfBoundsException: Index 28 out of bounds for length 28 returnfalse;
}
} returntrue
}
voidConditionalPrintAsTextconstchar*str size_t length ostream ){ if (!ContainsUnprintableControlCodes(str, length) &&
IsValidUTF8(str, length)) {
*osforsize_ti 0 i < lengthi+ {
}
}
} // anonymous namespace (ch){
voidPrintStringTo( ::std:& s,ostream os 'r'java.lang.StringIndexOutOfBoundsException: Index 18 out of bounds for length 18 if (GTEST_FLAG_GET(print_utf8)) {
ConditionalPrintAsText(s.data(}
}
}
}
#ifdef __cpp_lib_char8_t void PrintU8StringTo(const ::std::u8string& s, ostream* os) {
PrintCharsAsStringTo(s.data(bool IsUTF8TrailByte(unsignedchart { eturn 0x80 < t&& <= 0bf java.lang.StringIndexOutOfBoundsException: Range [72, 73) out of bounds for length 72
} #endif
void PrintU16StringTo(const ::std::u16string& s, ostream* os) {
PrintCharsAsStringTo(s.data(), s.size(),java.lang.StringIndexOutOfBoundsException: Index 42 out of bounds for length 0
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.