Added StringRef constructor that captures string literal size at compile time

This commit is contained in:
Phil Nash 2017-11-17 18:54:12 +00:00
parent f36817ef83
commit 4353614df7
3 changed files with 13 additions and 9 deletions

View File

@ -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 ) );
}
}

View File

@ -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 );

View File

@ -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;