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 {
|
||||
// Report the error condition
|
||||
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>
|
||||
|
||||
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 );
|
||||
|
@ -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
|
||||
template<size_t Size>
|
||||
StringRef( char const(& rawChars)[Size] ) noexcept
|
||||
: m_start( rawChars ),
|
||||
m_size( static_cast<size_type>(std::strlen(rawChars)))
|
||||
{
|
||||
assert( 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;
|
||||
|
Loading…
Reference in New Issue
Block a user