// Copyright Catch2 Authors // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) // SPDX-License-Identifier: BSL-1.0 #include #include #include #include #include #include namespace Catch { namespace Detail { namespace { const int hexThreshold = 255; struct Endianness { enum Arch : uint8_t { Big, Little }; static Arch which() { int one = 1; // If the lowest byte we read is non-zero, we can assume // that little endian format is used. auto value = *reinterpret_cast(&one); return value ? Little : Big; } }; template std::string fpToString(T value, int precision) { if (Catch::isnan(value)) { return "nan"; } ReusableStringStream rss; rss << std::setprecision(precision) << std::fixed << value; std::string d = rss.str(); std::size_t i = d.find_last_not_of('0'); if (i != std::string::npos && i != d.size() - 1) { if (d[i] == '.') i++; d = d.substr(0, i + 1); } return d; } } // end unnamed namespace std::size_t catch_strnlen( const char* str, std::size_t n ) { auto ret = std::char_traits::find( str, n, '\0' ); if ( ret != nullptr ) { return static_cast( ret - str ); } return n; } std::string formatTimeT(std::time_t time) { #ifdef _MSC_VER std::tm timeInfo = {}; const auto err = gmtime_s( &timeInfo, &time ); if ( err ) { return "gmtime from provided timepoint has failed. This " "happens e.g. with pre-1970 dates using Microsoft libc"; } #else std::tm* timeInfo = std::gmtime( &time ); #endif auto const timeStampSize = sizeof( "2017-01-16T17:06:45Z" ); char timeStamp[timeStampSize]; const char* const fmt = "%Y-%m-%dT%H:%M:%SZ"; #ifdef _MSC_VER std::strftime( timeStamp, timeStampSize, fmt, &timeInfo ); #else std::strftime( timeStamp, timeStampSize, fmt, timeInfo ); #endif return std::string( timeStamp, timeStampSize - 1 ); } std::string convertIntoString(StringRef string, bool escapeInvisibles) { std::string ret; // This is enough for the "don't escape invisibles" case, and a good // lower bound on the "escape invisibles" case. ret.reserve( string.size() + 2 ); if ( !escapeInvisibles ) { ret += '"'; ret += string; ret += '"'; return ret; } size_t last_start = 0; auto write_to = [&]( size_t idx ) { if ( last_start < idx ) { ret += string.substr( last_start, idx - last_start ); } last_start = idx + 1; }; ret += '"'; for ( size_t i = 0; i < string.size(); ++i ) { const char c = string[i]; if ( c == '\r' || c == '\n' || c == '\t' || c == '\f' ) { write_to( i ); if ( c == '\r' ) { ret.append( "\\r" ); } if ( c == '\n' ) { ret.append( "\\n" ); } if ( c == '\t' ) { ret.append( "\\t" ); } if ( c == '\f' ) { ret.append( "\\f" ); } } } write_to( string.size() ); ret += '"'; return ret; } std::string convertIntoString(StringRef string) { return convertIntoString(string, getCurrentContext().getConfig()->showInvisibles()); } std::string rawMemoryToString( const void *object, std::size_t size ) { // Reverse order for little endian architectures int i = 0, end = static_cast( size ), inc = 1; if( Endianness::which() == Endianness::Little ) { i = end-1; end = inc = -1; } unsigned char const *bytes = static_cast(object); ReusableStringStream rss; rss << "0x" << std::setfill('0') << std::hex; for( ; i != end; i += inc ) rss << std::setw(2) << static_cast(bytes[i]); return rss.str(); } std::string makeExceptionHappenedString() { return "{ stringification failed with an exception: \"" + translateActiveException() + "\" }"; } } // end Detail namespace //// ======================================================= //// // // Out-of-line defs for full specialization of StringMaker // //// ======================================================= //// std::string StringMaker::convert(const std::string& str) { return Detail::convertIntoString( str ); } #ifdef CATCH_CONFIG_CPP17_STRING_VIEW std::string StringMaker::convert(std::string_view str) { return Detail::convertIntoString( StringRef( str.data(), str.size() ) ); } #endif std::string StringMaker::convert(char const* str) { if (str) { return Detail::convertIntoString( str ); } else { return{ "{null string}" }; } } std::string StringMaker::convert(char* str) { // NOLINT(readability-non-const-parameter) if (str) { return Detail::convertIntoString( str ); } else { return{ "{null string}" }; } } #ifdef CATCH_CONFIG_WCHAR std::string StringMaker::convert(const std::wstring& wstr) { std::string s; s.reserve(wstr.size()); for (auto c : wstr) { s += (c <= 0xff) ? static_cast(c) : '?'; } return ::Catch::Detail::stringify(s); } # ifdef CATCH_CONFIG_CPP17_STRING_VIEW std::string StringMaker::convert(std::wstring_view str) { return StringMaker::convert(std::wstring(str)); } # endif std::string StringMaker::convert(wchar_t const * str) { if (str) { return ::Catch::Detail::stringify(std::wstring{ str }); } else { return{ "{null string}" }; } } std::string StringMaker::convert(wchar_t * str) { if (str) { return ::Catch::Detail::stringify(std::wstring{ str }); } else { return{ "{null string}" }; } } #endif #if defined(CATCH_CONFIG_CPP17_BYTE) #include std::string StringMaker::convert(std::byte value) { return ::Catch::Detail::stringify(std::to_integer(value)); } #endif // defined(CATCH_CONFIG_CPP17_BYTE) std::string StringMaker::convert(int value) { return ::Catch::Detail::stringify(static_cast(value)); } std::string StringMaker::convert(long value) { return ::Catch::Detail::stringify(static_cast(value)); } std::string StringMaker::convert(long long value) { ReusableStringStream rss; rss << value; if (value > Detail::hexThreshold) { rss << " (0x" << std::hex << value << ')'; } return rss.str(); } std::string StringMaker::convert(unsigned int value) { return ::Catch::Detail::stringify(static_cast(value)); } std::string StringMaker::convert(unsigned long value) { return ::Catch::Detail::stringify(static_cast(value)); } std::string StringMaker::convert(unsigned long long value) { ReusableStringStream rss; rss << value; if (value > Detail::hexThreshold) { rss << " (0x" << std::hex << value << ')'; } return rss.str(); } std::string StringMaker::convert(signed char value) { if (value == '\r') { return "'\\r'"; } else if (value == '\f') { return "'\\f'"; } else if (value == '\n') { return "'\\n'"; } else if (value == '\t') { return "'\\t'"; } else if ('\0' <= value && value < ' ') { return ::Catch::Detail::stringify(static_cast(value)); } else { char chstr[] = "' '"; chstr[1] = value; return chstr; } } std::string StringMaker::convert(char c) { return ::Catch::Detail::stringify(static_cast(c)); } std::string StringMaker::convert(unsigned char value) { return ::Catch::Detail::stringify(static_cast(value)); } int StringMaker::precision = std::numeric_limits::max_digits10; std::string StringMaker::convert(float value) { return Detail::fpToString(value, precision) + 'f'; } int StringMaker::precision = std::numeric_limits::max_digits10; std::string StringMaker::convert(double value) { return Detail::fpToString(value, precision); } } // end namespace Catch