Removed String and StringBuilder

This commit is contained in:
Phil Nash
2017-08-14 09:04:14 +01:00
parent ece64c3b3a
commit 3772f69f0f
14 changed files with 24 additions and 1092 deletions

View File

@@ -1,86 +0,0 @@
/*
* Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "catch_string.h"
#include "catch_stringref.h"
#include "catch_stringbuilder.h"
#include "catch_stringdata.h"
#include <ostream>
namespace Catch {
String::String()
: m_data( StringData::getEmpty() )
{}
String::String( StringRef const& stringRef )
: m_data( StringData::create( stringRef ) )
{}
String::String( char const* rawString )
: String( StringRef( rawString ) )
{}
String::String( String const& other )
: m_data( other.m_data )
{
m_data->addRef();
}
String::String( String&& other ) noexcept
: m_data( other.m_data )
{
other.m_data = StringData::getEmpty();
}
String::String( StringBuilder&& stringBuf )
: m_data( stringBuf.m_data )
{
// const_cast is ok here because we are taking ownership
const_cast<StringData*>( m_data )->size = stringBuf.size();
stringBuf.m_data = StringData::getEmpty();
stringBuf.m_size = 0;
}
String::~String() noexcept {
m_data->release();
}
auto String::operator = ( String const& other ) -> String& {
m_data = other.m_data;
m_data->addRef();
return *this;
}
auto String::empty() const noexcept -> bool {
return m_data->size == 0;
}
auto String::size() const noexcept -> size_type {
return m_data->size;
}
auto String::numberOfCharacters() const noexcept -> size_type {
return StringRef( *this ).numberOfCharacters();
}
auto String::c_str() const noexcept -> char const* {
return m_data->chars;
}
auto String::operator == ( StringRef const& other ) const noexcept -> bool {
return other == StringRef( *this );
}
auto String::operator == ( char const* other ) const noexcept -> bool {
return StringRef( other ) == StringRef( *this );
}
std::ostream& operator << ( std::ostream& os, String const& str ) {
os << str.c_str();
return os;
}
} // namespace Catch

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef CATCH_STRING_H_INCLUDED
#define CATCH_STRING_H_INCLUDED
#include <cstddef>
namespace Catch {
class StringData;
class StringRef;
class StringBuilder;
/// An owning, ref-counted, immutable string type.
/// The ref count (not visible here as it is defined in StringData) is atomic
/// so instances should be safe to share across threads
class String {
friend class StringRef;
friend class StringBuilder;
StringData const* m_data = nullptr;
public:
using size_type = size_t;
String();
String( StringRef const& stringRef );
String( char const* rawString );
String( String const& other );
String( String&& other ) noexcept;
String( StringBuilder&& stringBuf );
~String() noexcept;
auto operator = ( String const& other ) -> String&;
auto operator == ( StringRef const& other ) const noexcept -> bool;
auto operator == ( char const* other ) const noexcept -> bool;
auto empty() const noexcept -> bool;
auto size() const noexcept -> size_type;
auto numberOfCharacters() const noexcept -> size_type;
auto c_str() const noexcept -> char const*;
};
} // namespace Catch
#endif // CATCH_STRING_H_INCLUDED

View File

@@ -1,111 +0,0 @@
/*
* Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "catch_stringbuilder.h"
#include "catch_stringref.h"
#include "catch_stringdata.h"
#include "catch_string.h"
#include <cstring>
#include <algorithm>
#include <cassert>
#include <stdexcept>
namespace Catch {
static const StringBuilder::size_type s_minimumCapacity = 32;
StringBuilder::StringBuilder()
: m_data( StringData::getEmpty() )
{}
StringBuilder::StringBuilder( size_type initialCapacity )
: m_size( 0 ),
m_data( StringData::create( StringRef(), initialCapacity ) )
{}
StringBuilder::StringBuilder( StringRef const& str, size_type initialCapacity )
: m_size( str.size() ),
m_data( StringData::create( str, initialCapacity ) )
{}
StringBuilder::StringBuilder( StringBuilder const& other, size_type initialCapacity )
: StringBuilder( StringRef( other.m_data->chars, other.m_size ), initialCapacity )
{}
StringBuilder::StringBuilder( StringBuilder&& other ) noexcept
: StringBuilder()
{
swap( other );
}
StringBuilder::StringBuilder( String&& str )
: m_size( str.size() ),
m_data( StringData::getEmpty() )
{
if( str.m_data->isUniquelyOwned() )
{
std::swap( m_data, const_cast<StringData*&>( str.m_data ) );
}
else
{
size_type initialCapacity = std::min( s_minimumCapacity, m_size );
m_data = StringData::create( str, initialCapacity );
}
}
StringBuilder::StringBuilder( String const& other )
: StringBuilder( StringRef( other ), std::min( s_minimumCapacity, other.size() ) )
{}
StringBuilder::~StringBuilder() noexcept {
m_data->release();
}
auto StringBuilder::size() const noexcept -> size_type {
return m_size;
}
void StringBuilder::swap( StringBuilder& other ) noexcept {
std::swap( m_size, other.m_size );
std::swap( m_data, other.m_data );
}
void StringBuilder::reserve( size_type minimumCapacity ) {
if( minimumCapacity > capacity() ) {
StringBuilder temp( *this, minimumCapacity );
swap( temp );
}
}
void StringBuilder::reserveExponential( size_type minimumCapacity ) {
if( minimumCapacity > capacity() ) {
size_type candidateCapacity = capacity() < s_minimumCapacity ? s_minimumCapacity : capacity()*2;
while( candidateCapacity < minimumCapacity )
candidateCapacity = candidateCapacity * 3/2; // grow factor of 1.5
StringBuilder temp( *this, candidateCapacity );
swap( temp );
}
}
auto StringBuilder::capacity() const noexcept -> size_type {
return m_data->size;
}
void StringBuilder::writeTo( size_type index, StringRef const& str ) {
assert( index + str.size() < capacity() );
if( str.size() > 0 )
std::memcpy( m_data->chars+index, str.data(), str.size() );
}
void StringBuilder::append( StringRef const& str ) {
reserveExponential( m_size + str.size() + 1 );
writeTo( m_size, str );
m_size += str.size();
m_data->chars[m_size] = '\0';
}
auto operator << ( StringBuilder& sb, StringRef sr ) -> StringBuilder& {
sb.append( sr );
return sb;
}
auto operator << ( StringBuilder&& sb, StringRef sr ) -> StringBuilder&& {
sb.append( sr );
return std::move( sb );
}
} // namespace Catch

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2016 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef CATCH_STRINGBUILDER_H_INCLUDED
#define CATCH_STRINGBUILDER_H_INCLUDED
#include "catch_stringref.h"
#include <cstddef>
namespace Catch {
class String;
class StringData;
/// A mutable container for string data
/// Use to build up strings before transferring to an immutable String.
/// Construct the String using the rvalue reference constructor (which
/// will usually involve std::move-ing the StringBuilder). This will transfer
/// The underlying buffer without any extra allocations or ref counts.
class StringBuilder {
friend class String;
public:
using size_type = size_t;
StringBuilder();
StringBuilder( size_type initialCapacity );
StringBuilder( StringRef const& str, size_type initialCapacity );
StringBuilder( StringBuilder const& other, size_type initialCapacity );
StringBuilder( StringBuilder&& other ) noexcept;
StringBuilder( String&& other );
StringBuilder( String const& other );
~StringBuilder() noexcept;
void swap( StringBuilder& other ) noexcept;
auto size() const noexcept -> size_type;
auto capacity() const noexcept -> size_type;
/// Grows the buffer to exactly the capacity requested, or
/// does nothing if it is already at least as big
void reserve(size_type capacity);
/// Grows the buffer exponentially (from a baseline of 32 bytes)
/// until it is at least as large as the requested capacity -
/// or does nothing if already large enough
void reserveExponential(size_type capacity);
/// Writes the string at the current insertion point then moves
/// the insertion point forward by the string length.
/// If the buffer needs to grow to accomodate the string it does so
/// using the exponential strategy
void append( StringRef const& str );
friend auto operator << ( StringBuilder& sb, StringRef sr ) -> StringBuilder&;
friend auto operator << ( StringBuilder&& sb, StringRef sr ) -> StringBuilder&&;
/// Writes the contents of the string ref into the buffer at
/// the indexed location.
/// The bounds are not checked! Use append() to just add to the
/// end of the buffer, extending it if the capacity is not enough.
void writeTo( size_type index, StringRef const& str );
private:
size_type m_size = 0;
StringData* m_data;
};
} // namespace Catch
#endif // CATCH_STRINGBUILDER_H_INCLUDED

View File

@@ -6,8 +6,6 @@
*/
#include "catch_stringref.h"
#include "catch_stringbuilder.h"
#include "catch_string.h"
#include "catch_stringdata.h"
#include <cstring>
@@ -58,18 +56,6 @@ namespace Catch {
m_size = rawSize;
}
StringRef::StringRef( String const& other ) noexcept
: m_start( other.c_str() ),
m_size( other.size() )
{}
StringRef::StringRef( String&& str ) noexcept
: m_start( str.c_str() ),
m_size( str.size() ),
m_data( str.m_data )
{
str.m_data = StringData::getEmpty();
}
StringRef::StringRef( std::string const& stdString ) noexcept
: m_start( stdString.c_str() ),
m_size( stdString.size() )
@@ -112,9 +98,9 @@ namespace Catch {
void StringRef::takeOwnership() {
if( !isOwned() ) {
StringRef temp = String( *this );
swap( temp );
}
m_data = StringData::create( *this );
m_start = m_data->chars;
}
}
auto StringRef::substr( size_type start, size_type size ) const noexcept -> StringRef {
if( start < m_size )
@@ -159,18 +145,18 @@ namespace Catch {
return noChars;
}
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> String {
StringBuilder buf;
buf.reserve( lhs.size() + rhs.size() );
buf.append( lhs );
buf.append( rhs );
return String( std::move( buf ) );
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string {
std::string str;
str.reserve( lhs.size() + rhs.size() );
str += lhs;
str += rhs;
return str;
}
auto operator + ( StringRef const& lhs, const char* rhs ) -> String {
return lhs + StringRef( rhs );
auto operator + ( StringRef const& lhs, const char* rhs ) -> std::string {
return std::string( lhs ) + std::string( rhs );
}
auto operator + ( char const* lhs, StringRef const& rhs ) -> String {
return StringRef( lhs ) + rhs;
auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string {
return std::string( lhs ) + std::string( rhs );
}
auto operator << ( std::ostream& os, StringRef const& str ) -> std::ostream& {

View File

@@ -13,7 +13,6 @@
namespace Catch {
class String;
class StringData;
/// A non-owning string class (similar to the forthcoming std::string_view)
@@ -26,7 +25,6 @@ namespace Catch {
class StringRef {
friend struct StringRefTestAccess;
friend class StringData;
friend class StringBuilder;
using size_type = size_t;
@@ -43,8 +41,6 @@ namespace Catch {
StringRef( StringRef&& other ) noexcept;
StringRef( char const* rawChars ) noexcept;
StringRef( char const* rawChars, size_type size ) noexcept;
StringRef( String const& other ) noexcept;
StringRef( String&& other ) noexcept;
StringRef( std::string const& stdString ) noexcept;
~StringRef() noexcept;
@@ -74,9 +70,9 @@ namespace Catch {
auto data() const noexcept -> char const*;
};
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> String;
auto operator + ( StringRef const& lhs, char const* rhs ) -> String;
auto operator + ( char const* lhs, StringRef const& rhs ) -> String;
auto operator + ( StringRef const& lhs, StringRef const& rhs ) -> std::string;
auto operator + ( StringRef const& lhs, char const* rhs ) -> std::string;
auto operator + ( char const* lhs, StringRef const& rhs ) -> std::string;
auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&;