Inlined StringRef ctors/ dtor and size() and empty()

This commit is contained in:
Phil Nash 2017-11-17 18:33:34 +00:00
parent c443afcca0
commit b0c379f621
2 changed files with 49 additions and 72 deletions

View File

@ -14,63 +14,8 @@
#include "catch_stringref.h" #include "catch_stringref.h"
#include <ostream> #include <ostream>
#include <cstring>
#include <stdexcept>
namespace Catch { namespace Catch {
namespace {
auto getEmptyStringRef() -> StringRef {
static StringRef s_emptyStringRef("");
return s_emptyStringRef;
}
char const* enforceNonNull(char const* ptr) {
if (ptr == nullptr) {
std::abort();
}
return ptr;
}
}
StringRef::StringRef() noexcept
: StringRef( getEmptyStringRef() )
{}
StringRef::StringRef( StringRef const& other ) noexcept
: m_start( other.m_start ),
m_size( other.m_size )
{}
StringRef::StringRef( StringRef&& other ) noexcept
: m_start( other.m_start ),
m_size( other.m_size ),
m_data( other.m_data )
{
other.m_data = nullptr;
}
StringRef::StringRef(char const* rawChars) noexcept
: m_start( enforceNonNull(rawChars) ),
m_size( static_cast<size_type>(std::strlen(rawChars)))
{}
StringRef::StringRef( char const* rawChars, size_type size ) noexcept
: m_start( rawChars ),
m_size( size )
{}
StringRef::StringRef( std::string const& stdString ) noexcept
: m_start( stdString.c_str() ),
m_size( stdString.size() )
{}
StringRef::~StringRef() noexcept {
delete[] m_data;
}
auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& { auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& {
swap( other ); swap( other );
@ -129,13 +74,6 @@ namespace Catch {
return m_start[index]; return m_start[index];
} }
auto StringRef::empty() const noexcept -> bool {
return m_size == 0;
}
auto StringRef::size() const noexcept -> size_type {
return m_size;
}
auto StringRef::numberOfCharacters() const noexcept -> size_type { auto StringRef::numberOfCharacters() const noexcept -> size_type {
size_type noChars = m_size; size_type noChars = m_size;
// Make adjustments for uft encodings // Make adjustments for uft encodings

View File

@ -10,6 +10,8 @@
#include <cstddef> #include <cstddef>
#include <string> #include <string>
#include <iosfwd> #include <iosfwd>
#include <cassert>
#include <cstring>
namespace Catch { namespace Catch {
@ -33,16 +35,48 @@ namespace Catch {
char* m_data = nullptr; char* m_data = nullptr;
void takeOwnership(); void takeOwnership();
static constexpr char const* const s_empty = "";
public: // construction/ assignment public: // construction/ assignment
StringRef() noexcept; StringRef() noexcept
StringRef( StringRef const& other ) noexcept; : StringRef( s_empty, 0 )
StringRef( StringRef&& other ) noexcept; {}
StringRef( char const* rawChars ) noexcept;
StringRef( char const* rawChars, size_type size ) noexcept; StringRef( StringRef const& other ) noexcept
StringRef( std::string const& stdString ) noexcept; : m_start( other.m_start ),
~StringRef() noexcept; m_size( other.m_size )
{}
StringRef( StringRef&& other ) noexcept
: m_start( other.m_start ),
m_size( other.m_size ),
m_data( other.m_data )
{
other.m_data = nullptr;
}
StringRef( char const* rawChars ) noexcept
: m_start( rawChars ),
m_size( static_cast<size_type>(std::strlen(rawChars)))
{
assert( rawChars );
}
StringRef( char const* rawChars, size_type size ) noexcept
: m_start( rawChars ),
m_size( size )
{}
StringRef( std::string const& stdString ) noexcept
: m_start( stdString.c_str() ),
m_size( stdString.size() )
{}
~StringRef() noexcept {
delete[] m_data;
}
auto operator = ( StringRef other ) noexcept -> StringRef&; auto operator = ( StringRef other ) noexcept -> StringRef&;
operator std::string() const; operator std::string() const;
@ -55,8 +89,13 @@ namespace Catch {
auto operator[] ( size_type index ) const noexcept -> char; auto operator[] ( size_type index ) const noexcept -> char;
public: // named queries public: // named queries
auto empty() const noexcept -> bool; auto empty() const noexcept -> bool {
auto size() const noexcept -> size_type; return m_size == 0;
}
auto size() const noexcept -> size_type {
return m_size;
}
auto numberOfCharacters() const noexcept -> size_type; auto numberOfCharacters() const noexcept -> size_type;
auto c_str() const -> char const*; auto c_str() const -> char const*;