Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Added StringRef constructor that captures string literal size at compile time

Commit
4353614df70350ca4eb67caa1c920e221891b212
Author
Phil Nash ·
Parents
f36817ef830a

Files · Download .tar.gz

Changes

 include/internal/catch_fatal_condition.cpp |  2 +-
 include/internal/catch_stringref.cpp       |  5 +++++
 include/internal/catch_stringref.h         | 15 +++++++--------
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/include/internal/catch_fatal_condition.cpp b/include/internal/catch_fatal_condition.cpp
index 39b63455..ac2863d2 100644
--- a/include/internal/catch_fatal_condition.cpp
+++ b/include/internal/catch_fatal_condition.cpp
@@ -15,7 +15,7 @@
 namespace {
     // Report the error condition
     void reportFatal( char const * const message ) {
-        Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message );
+        Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( Catch::StringRef::fromRaw( message ) );
     }
 }
 
diff --git a/include/internal/catch_stringref.cpp b/include/internal/catch_stringref.cpp
index 49d9629f..41c6ac5c 100644
--- a/include/internal/catch_stringref.cpp
+++ b/include/internal/catch_stringref.cpp
@@ -16,6 +16,11 @@
 #include <ostream>
 
 namespace Catch {
+    auto StringRef::fromRaw( char const* rawChars ) -> StringRef {
+        return rawChars
+            ? StringRef( rawChars,static_cast<StringRef::size_type>(std::strlen(rawChars) ) )
+            : StringRef();
+    }
 
     StringRef::operator std::string() const {
         return std::string( m_start, m_size );
diff --git a/include/internal/catch_stringref.h b/include/internal/catch_stringref.h
index 2a9ad4c2..29dcde26 100644
--- a/include/internal/catch_stringref.h
+++ b/include/internal/catch_stringref.h
@@ -10,8 +10,6 @@
 #include <cstddef>
 #include <string>
 #include <iosfwd>
-#include <cassert>
-#include <cstring>
 
 namespace Catch {
     
@@ -56,12 +54,11 @@ namespace Catch {
             other.m_data = nullptr;
         }
 
-        StringRef( char const* rawChars ) noexcept
-            :   m_start( rawChars ),
-                m_size( static_cast<size_type>(std::strlen(rawChars)))
-        {
-            assert( rawChars );
-        }
+        template<size_t Size>
+        StringRef( char const(& rawChars)[Size] ) noexcept
+        :   m_start( rawChars ),
+            m_size( static_cast<size_type>( Size-1 ) )
+        {}
 
         StringRef( char const* rawChars, size_type size ) noexcept
         :   m_start( rawChars ),
@@ -85,6 +82,8 @@ namespace Catch {
             return *this;
         }
 
+        static auto fromRaw( char const *rawChars ) -> StringRef;
+
         operator std::string() const;
 
         void swap( StringRef& other ) noexcept;