Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorPhil Nash2014-05-23 18:41:02 +0100
committerPhil Nash2014-05-23 18:41:02 +0100
commit7cbf74061b5f5ec27b1959c55dfd59b15bafec7f (patch)
treea55314334066c3e0ab89f4345da540f0a2556024 /include
parent48fac9cf01fedb074bd349dac922015c35aae82b (diff)
downloadcatch3-7cbf74061b5f5ec27b1959c55dfd59b15bafec7f.tar.gz
Tweaked rawMemoryToString() along lines of suggestion in #281
Diffstat (limited to 'include')
-rw-r--r--include/internal/catch_tostring.h36
-rw-r--r--include/internal/catch_tostring.hpp36
2 files changed, 39 insertions, 33 deletions
diff --git a/include/internal/catch_tostring.h b/include/internal/catch_tostring.h
index 25faa2a3..7268d156 100644
--- a/include/internal/catch_tostring.h
+++ b/include/internal/catch_tostring.h
@@ -80,41 +80,11 @@ namespace Detail {
}
};
- struct Endianness {
- enum Arch { Big, Little };
+ std::string rawMemoryToString( const void *object, std::size_t size );
- static Arch which() {
- union _{
- int asInt;
- char asChar[sizeof (int)];
- } u;
-
- u.asInt = 1;
- return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
- }
- };
-
- // Writes the raw memory into a string, considering endianness
template<typename T>
- std::string rawMemoryToString( T value ) {
- union _ {
- T typedValue;
- unsigned char bytes[sizeof(T)];
- } u;
-
- u.typedValue = value;
-
- std::ostringstream oss;
- oss << "0x";
-
- int i = 0, end = sizeof(T), inc = 1;
- if( Endianness::which() == Endianness::Little ) {
- i = end-1;
- end = inc = -1;
- }
- for( ; i != end; i += inc )
- oss << std::hex << std::setw(2) << std::setfill('0') << (unsigned int)u.bytes[i];
- return oss.str();
+ inline std::string rawMemoryToString( const T& object ) {
+ return rawMemoryToString( &object, sizeof(object) );
}
} // end namespace Detail
diff --git a/include/internal/catch_tostring.hpp b/include/internal/catch_tostring.hpp
index 2eb94e63..0b35f9d6 100644
--- a/include/internal/catch_tostring.hpp
+++ b/include/internal/catch_tostring.hpp
@@ -13,6 +13,42 @@
namespace Catch {
+namespace Detail {
+
+ namespace {
+ struct Endianness {
+ enum Arch { Big, Little };
+
+ static Arch which() {
+ union _{
+ int asInt;
+ char asChar[sizeof (int)];
+ } u;
+
+ u.asInt = 1;
+ return ( u.asChar[sizeof(int)-1] == 1 ) ? Big : Little;
+ }
+ };
+ }
+
+ std::string rawMemoryToString( const void *object, std::size_t size )
+ {
+ // Reverse order for little endian architectures
+ int i = 0, end = static_cast<int>( size ), inc = 1;
+ if( Endianness::which() == Endianness::Little ) {
+ i = end-1;
+ end = inc = -1;
+ }
+
+ unsigned char const *bytes = static_cast<unsigned char const *>(object);
+ std::ostringstream os;
+ os << "0x" << std::setfill('0') << std::hex;
+ for( ; i != end; i += inc )
+ os << std::setw(2) << static_cast<unsigned>(bytes[i]);
+ return os.str();
+ }
+}
+
std::string toString( std::string const& value ) {
std::string s = value;
if( getCurrentContext().getConfig()->showInvisibles() ) {