Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::RcString Class Reference

A reference counted string, that is copy-on-write and implements the small-string optimization. More...

#include "donner/base/RcString.h"

Public Types

using iterator = std::string_view::iterator
 String iterator.
 
using const_iterator = std::string_view::const_iterator
 Const string iterator.
 

Public Member Functions

constexpr RcString ()=default
 Create an empty string.
 
 ~RcString ()=default
 Destructor.
 
 RcString (std::string_view data)
 Constructs a new RcString object by copying an existing string.
 
 RcString (const char *data, size_t len=npos)
 Constructs a new RcString object from a C-style string and optional length.
 
constexpr RcString (const RcString &other)
 Copy constructor.
 
constexpr RcString (RcString &&other) noexcept
 Move constructor.
 
RcStringoperator= (const RcString &other)
 Copy assignment operator.
 
RcStringoperator= (RcString &&other) noexcept
 Move assignment operator.
 
RcStringoperator= (const char *data)
 Assignment operator from a C-style string.
 
RcStringoperator= (std::string_view data)
 Assignment operator from a std::string_view.
 
 operator std::string_view () const
 Cast operator to std::string_view.
 
const char * data () const
 
bool empty () const
 
size_t size () const
 
std::string str () const
 
constexpr const_iterator begin () const noexcept
 Begin iterator.
 
constexpr const_iterator end () const noexcept
 End iterator.
 
constexpr const_iterator cbegin () const noexcept
 Begin iterator.
 
constexpr const_iterator cend () const noexcept
 End iterator.
 
bool equalsLowercase (std::string_view lowercaseOther) const
 Returns true if the string equals another all-lowercase string, with a case insensitive comparison.
 
bool equalsIgnoreCase (std::string_view other) const
 Returns true if the string equals another string with a case-insensitive comparison.
 
RcString substr (size_t pos, size_t len=npos) const
 Returns a substring of the string, returning a reference to the original string's data.
 
void dedup ()
 Deduplicates the string, updating its underlying storage to ensure that it has a unique reference to the underlying contents.
 

Static Public Member Functions

static RcString fromVector (std::vector< char > &&data)
 Constructs an RcString by consuming an existing vector.
 

Static Public Attributes

static constexpr size_t npos = std::string_view::npos
 Sentinel value for the maximum value of size_t, used to indicate when the size is not known.
 

Friends

constexpr friend auto operator<=> (const RcString &lhs, const RcString &rhs)
 Spaceship equality operator to another RcString.
 
constexpr friend auto operator<=> (const RcString &lhs, const char *rhs)
 Spaceship equality operator to a C-style string.
 
constexpr friend auto operator<=> (const RcString &lhs, std::string_view rhs)
 Spaceship equality operator to a std::string_view.
 
constexpr friend auto operator<=> (const char *lhs, const RcString &rhs)
 Spaceship equality operator to another RcString.
 
constexpr friend auto operator<=> (std::string_view lhs, const RcString &rhs)
 Spaceship equality operator to a C-style string.
 
constexpr friend bool operator== (const RcString &lhs, const RcString &rhs)
 Equality operator to another RcString.
 
constexpr friend bool operator== (const RcString &lhs, const char *rhs)
 Equality operator to a C-style string.
 
constexpr friend bool operator== (const RcString &lhs, std::string_view rhs)
 Equality operator to a std::string_view.
 
constexpr friend bool operator== (const char *lhs, const RcString &rhs)
 Reversed equality operator to another RcString.
 
constexpr friend bool operator== (std::string_view lhs, const RcString &rhs)
 Reversed equality operator to a C-style string.
 
std::ostream & operator<< (std::ostream &os, const RcString &self)
 Ostream output operator.
 
std::string operator+ (const RcString &lhs, const RcString &rhs)
 Concatenation operator with another RcString.
 
std::string operator+ (const RcString &lhs, const char *rhs)
 Concatenation operator with a C-style string.
 
std::string operator+ (const RcString &lhs, std::string_view rhs)
 Concatenation operator with a std::string_view.
 
std::string operator+ (std::string_view lhs, const RcString &rhs)
 Reversed concatenation operator with a C-style string.
 
std::string operator+ (const char *lhs, const RcString &rhs)
 Reversed concatenation operator with a std::string_view.
 

Detailed Description

A reference counted string, that is copy-on-write and implements the small-string optimization.

Implements a short-string optimization similar to the libc++ std::string class, see https://joellaity.com/2020/01/31/string.html for details.

USAGE NOTES:

  • This class is not thread-safe, and should not be shared between threads without synchronization.
  • data() is not guaranteed to remain stable across references, and should not be stored longer than the original RcString which created it.

Constructor & Destructor Documentation

◆ RcString() [1/2]

donner::RcString::RcString ( std::string_view data)
inlineexplicit

Constructs a new RcString object by copying an existing string.

Parameters
dataInput string to copy.

◆ RcString() [2/2]

donner::RcString::RcString ( const char * data,
size_t len = npos )
inline

Constructs a new RcString object from a C-style string and optional length.

Parameters
dataC-style string.
lenLength of the string, or npos to automatically measure, which requires that data is null-terminated.

Member Function Documentation

◆ data()

const char * donner::RcString::data ( ) const
inline
Returns
a pointer to the string data.

◆ dedup()

void donner::RcString::dedup ( )
inline

Deduplicates the string, updating its underlying storage to ensure that it has a unique reference to the underlying contents.

This can be used to reduce memory usage whe taking a substring.

Has no effect if the string is short due to the short-string optimization.

◆ empty()

bool donner::RcString::empty ( ) const
inline
Returns
if the string is empty.

◆ equalsIgnoreCase()

bool donner::RcString::equalsIgnoreCase ( std::string_view other) const
inline

Returns true if the string equals another string with a case-insensitive comparison.

Parameters
otherstring to compare to.
Returns
true If the strings are equal (case insensitive).

◆ equalsLowercase()

bool donner::RcString::equalsLowercase ( std::string_view lowercaseOther) const
inline

Returns true if the string equals another all-lowercase string, with a case insensitive comparison.

Example:

RcString("EXAMPLe").equalsLowercase("example"); // true
constexpr RcString()=default
Create an empty string.
Parameters
lowercaseOtherstring to compare to, must be lowercase.
Returns
true If the strings are equal (case insensitive).

◆ fromVector()

static RcString donner::RcString::fromVector ( std::vector< char > && data)
inlinestatic

Constructs an RcString by consuming an existing vector.

Parameters
dataInput data to consume, moved into the RcString storage.
Returns
RcString using the input data.

◆ size()

size_t donner::RcString::size ( ) const
inline
Returns
the length of the string.

◆ str()

std::string donner::RcString::str ( ) const
inline
Returns
the string as a std::string.

◆ substr()

RcString donner::RcString::substr ( size_t pos,
size_t len = npos ) const
inline

Returns a substring of the string, returning a reference to the original string's data.

Note that due to the short-string optimization, this may not always reference the original data and may contain a copy if the string is below the short-string threshold.

Parameters
posThe position to start the substring.
lenThe length of the substring, or RcString::npos to return the whole string.

The documentation for this class was generated from the following file: