mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
Added StringRef constructor that captures string literal size at compile time
This commit is contained in:
parent
f36817ef83
commit
4353614df7
@ -15,7 +15,7 @@
|
|||||||
namespace {
|
namespace {
|
||||||
// Report the error condition
|
// Report the error condition
|
||||||
void reportFatal( char const * const message ) {
|
void reportFatal( char const * const message ) {
|
||||||
Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( message );
|
Catch::getCurrentContext().getResultCapture()->handleFatalErrorCondition( Catch::StringRef::fromRaw( message ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,11 @@
|
|||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
||||||
namespace Catch {
|
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 {
|
StringRef::operator std::string() const {
|
||||||
return std::string( m_start, m_size );
|
return std::string( m_start, m_size );
|
||||||
|
@ -10,8 +10,6 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <cassert>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace Catch {
|
namespace Catch {
|
||||||
|
|
||||||
@ -56,12 +54,11 @@ namespace Catch {
|
|||||||
other.m_data = nullptr;
|
other.m_data = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringRef( char const* rawChars ) noexcept
|
template<size_t Size>
|
||||||
: m_start( rawChars ),
|
StringRef( char const(& rawChars)[Size] ) noexcept
|
||||||
m_size( static_cast<size_type>(std::strlen(rawChars)))
|
: m_start( rawChars ),
|
||||||
{
|
m_size( static_cast<size_type>( Size-1 ) )
|
||||||
assert( rawChars );
|
{}
|
||||||
}
|
|
||||||
|
|
||||||
StringRef( char const* rawChars, size_type size ) noexcept
|
StringRef( char const* rawChars, size_type size ) noexcept
|
||||||
: m_start( rawChars ),
|
: m_start( rawChars ),
|
||||||
@ -85,6 +82,8 @@ namespace Catch {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static auto fromRaw( char const *rawChars ) -> StringRef;
|
||||||
|
|
||||||
operator std::string() const;
|
operator std::string() const;
|
||||||
|
|
||||||
void swap( StringRef& other ) noexcept;
|
void swap( StringRef& other ) noexcept;
|
||||||
|
Loading…
Reference in New Issue
Block a user