Don't call strlen in StringRef when the length was already passed

This commit is contained in:
Martin Hořeňovský 2017-11-13 12:03:45 +01:00
parent 3537b7858f
commit e7c23b73da

View File

@ -10,7 +10,7 @@
# pragma clang diagnostic push # pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wexit-time-destructors" # pragma clang diagnostic ignored "-Wexit-time-destructors"
#endif #endif
#include "catch_stringref.h" #include "catch_stringref.h"
#include <ostream> #include <ostream>
@ -25,16 +25,16 @@ namespace Catch {
static StringRef s_emptyStringRef(""); static StringRef s_emptyStringRef("");
return s_emptyStringRef; return s_emptyStringRef;
} }
StringRef::StringRef() noexcept StringRef::StringRef() noexcept
: StringRef( getEmptyStringRef() ) : StringRef( getEmptyStringRef() )
{} {}
StringRef::StringRef( StringRef const& other ) noexcept StringRef::StringRef( StringRef const& other ) noexcept
: m_start( other.m_start ), : m_start( other.m_start ),
m_size( other.m_size ) m_size( other.m_size )
{} {}
StringRef::StringRef( StringRef&& other ) noexcept StringRef::StringRef( StringRef&& other ) noexcept
: m_start( other.m_start ), : m_start( other.m_start ),
m_size( other.m_size ), m_size( other.m_size ),
@ -42,23 +42,19 @@ namespace Catch {
{ {
other.m_data = nullptr; other.m_data = nullptr;
} }
StringRef::StringRef( char const* rawChars ) noexcept StringRef::StringRef( char const* rawChars ) noexcept
: m_start( rawChars ), : m_start( rawChars ),
m_size( static_cast<size_type>( std::strlen( rawChars ) ) ) m_size( static_cast<size_type>( std::strlen( rawChars ) ) )
{ {
assert( rawChars != nullptr ); assert( rawChars != nullptr );
} }
StringRef::StringRef( char const* rawChars, size_type size ) noexcept StringRef::StringRef( char const* rawChars, size_type size ) noexcept
: m_start( rawChars ), : m_start( rawChars ),
m_size( size ) m_size( size )
{ {}
size_type rawSize = rawChars == nullptr ? 0 : static_cast<size_type>( std::strlen( rawChars ) );
if( rawSize < size )
m_size = rawSize;
}
StringRef::StringRef( std::string const& stdString ) noexcept StringRef::StringRef( std::string const& stdString ) noexcept
: m_start( stdString.c_str() ), : m_start( stdString.c_str() ),
m_size( stdString.size() ) m_size( stdString.size() )
@ -67,7 +63,7 @@ namespace Catch {
StringRef::~StringRef() noexcept { StringRef::~StringRef() noexcept {
delete[] m_data; delete[] m_data;
} }
auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& { auto StringRef::operator = ( StringRef other ) noexcept -> StringRef& {
swap( other ); swap( other );
return *this; return *this;
@ -81,7 +77,7 @@ namespace Catch {
std::swap( m_size, other.m_size ); std::swap( m_size, other.m_size );
std::swap( m_data, other.m_data ); std::swap( m_data, other.m_data );
} }
auto StringRef::c_str() const -> char const* { auto StringRef::c_str() const -> char const* {
if( isSubstring() ) if( isSubstring() )
const_cast<StringRef*>( this )->takeOwnership(); const_cast<StringRef*>( this )->takeOwnership();
@ -97,7 +93,7 @@ namespace Catch {
auto StringRef::isSubstring() const noexcept -> bool { auto StringRef::isSubstring() const noexcept -> bool {
return m_start[m_size] != '\0'; return m_start[m_size] != '\0';
} }
void StringRef::takeOwnership() { void StringRef::takeOwnership() {
if( !isOwned() ) { if( !isOwned() ) {
m_data = new char[m_size+1]; m_data = new char[m_size+1];
@ -166,7 +162,7 @@ namespace Catch {
auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& { auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& {
return os << str.c_str(); return os << str.c_str();
} }
} // namespace Catch } // namespace Catch
#if defined(__clang__) #if defined(__clang__)