userver: utils::SmallString< N > Class Template Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
utils::SmallString< N > Class Template Reference

#include <userver/utils/small_string.hpp>

Detailed Description

template<std::size_t N>
class utils::SmallString< N >

An alternative to std::string with a custom SSO (small string optimization) container size. Unlike std::string, SmallString is not null-terminated thus it has no c_str(), data() returns a not null-terminated buffer.

Definition at line 26 of file small_string.hpp.

Public Types

using value_type = char
 
using iterator = typename Container::iterator
 
using const_iterator = typename Container::const_iterator
 

Public Member Functions

 SmallString ()=default
 Create empty string.
 
 SmallString (const SmallString< N > &)=default
 Create a string from another one.
 
 SmallString (SmallString< N > &&) noexcept=default
 Create a string from another one.
 
 SmallString (std::string_view sv)
 Create a string from std::string_view.
 
SmallStringoperator= (std::string_view sv)
 Assign the value of other string_view to this string.
 
SmallStringoperator= (const SmallString &)=default
 Assign the value of other string to this string.
 
SmallStringoperator= (SmallString &&) noexcept=default
 Assign the value of other string to this string.
 
SmallStringoperator+= (std::string_view sv)
 
SmallStringoperator+= (char c)
 
 operator std::string_view () const
 Convert string to a std::string_view.
 
const char & operator[] (std::size_t pos) const
 Read-only subscript access to the data contained in the string.
 
char & operator[] (std::size_t pos)
 Subscript access to the data contained in the string.
 
const char & at (std::size_t pos) const
 Provides access to the data contained in the string.
 
char & at (std::size_t pos)
 Provides access to the data contained in the string.
 
iterator begin () noexcept
 
const_iterator begin () const noexcept
 
iterator end () noexcept
 
const_iterator end () const noexcept
 
std::size_t size () const noexcept
 Get string size.
 
const char * data () const noexcept
 Get pointer to data.
 
char * data () noexcept
 Get pointer to data.
 
void resize (std::size_t n, char c)
 Resize the string. If its length is increased, fill new chars with c.
 
void resize (std::size_t n)
 Resize the string. If its length is increased, fill new chars with \0.
 
template<class Operation>
void resize_and_overwrite (std::size_t size, Operation op)
 Resize the string. Use op to write into the string and replace a sequence of characters.
 
void shrink_to_fit ()
 Shrink the string's size to fit all contents.
 
std::size_t capacity () const noexcept
 Get current capacity.
 
void reserve (std::size_t n)
 Reserve to n bytes.
 
void clear () noexcept
 Clear the string.
 
bool empty () const noexcept
 Is the string empty?
 
char & front ()
 Get a reference to the first character.
 
const char & front () const
 Get a reference to the first character.
 
char & back ()
 Get a reference to the last character.
 
const char & back () const
 Get a reference to the last character.
 
void push_back (char c)
 Append a character to the string.
 
void append (std::string_view str)
 Append contents of a string_view to the string.
 
void append (const char *begin, const char *end)
 Append contents of a begin...end to the string.
 
template<class InputIt>
void insert (const_iterator pos, InputIt begin, InputIt end)
 Inserts elements from range [begin, end) before pos.
 
void pop_back ()
 Remove the last character from the string.
 

Member Typedef Documentation

◆ const_iterator

template<std::size_t N>
using utils::SmallString< N >::const_iterator = typename Container::const_iterator

Definition at line 80 of file small_string.hpp.

◆ iterator

template<std::size_t N>
using utils::SmallString< N >::iterator = typename Container::iterator

Definition at line 78 of file small_string.hpp.

◆ value_type

template<std::size_t N>
using utils::SmallString< N >::value_type = char

Definition at line 30 of file small_string.hpp.

Constructor & Destructor Documentation

◆ SmallString()

template<std::size_t N>
utils::SmallString< N >::SmallString ( std::string_view sv)
explicit

Create a string from std::string_view.

Definition at line 160 of file small_string.hpp.

Member Function Documentation

◆ append() [1/2]

template<std::size_t N>
void utils::SmallString< N >::append ( const char * begin,
const char * end )

Append contents of a begin...end to the string.

Definition at line 287 of file small_string.hpp.

◆ append() [2/2]

template<std::size_t N>
void utils::SmallString< N >::append ( std::string_view str)

Append contents of a string_view to the string.

Definition at line 281 of file small_string.hpp.

◆ at() [1/2]

template<std::size_t N>
char & utils::SmallString< N >::at ( std::size_t pos)

Provides access to the data contained in the string.

Definition at line 210 of file small_string.hpp.

◆ at() [2/2]

template<std::size_t N>
const char & utils::SmallString< N >::at ( std::size_t pos) const

Provides access to the data contained in the string.

Definition at line 204 of file small_string.hpp.

◆ back() [1/2]

template<std::size_t N>
char & utils::SmallString< N >::back ( )

Get a reference to the last character.

Definition at line 266 of file small_string.hpp.

◆ back() [2/2]

template<std::size_t N>
const char & utils::SmallString< N >::back ( ) const

Get a reference to the last character.

Definition at line 271 of file small_string.hpp.

◆ begin() [1/2]

template<std::size_t N>
SmallString< N >::const_iterator utils::SmallString< N >::begin ( ) const
noexcept

Definition at line 221 of file small_string.hpp.

◆ begin() [2/2]

template<std::size_t N>
SmallString< N >::iterator utils::SmallString< N >::begin ( )
noexcept

Definition at line 216 of file small_string.hpp.

◆ capacity()

template<std::size_t N>
std::size_t utils::SmallString< N >::capacity ( ) const
noexcept

Get current capacity.

Definition at line 326 of file small_string.hpp.

◆ clear()

template<std::size_t N>
void utils::SmallString< N >::clear ( )
noexcept

Clear the string.

Definition at line 336 of file small_string.hpp.

◆ data() [1/2]

template<std::size_t N>
const char * utils::SmallString< N >::data ( ) const
noexcept

Get pointer to data.

Warning
The buffer is not null-terminated.

Definition at line 241 of file small_string.hpp.

◆ data() [2/2]

template<std::size_t N>
char * utils::SmallString< N >::data ( )
noexcept

Get pointer to data.

Warning
The buffer is not null-terminated.

Definition at line 246 of file small_string.hpp.

◆ empty()

template<std::size_t N>
bool utils::SmallString< N >::empty ( ) const
noexcept

Is the string empty?

Definition at line 251 of file small_string.hpp.

◆ end() [1/2]

template<std::size_t N>
SmallString< N >::const_iterator utils::SmallString< N >::end ( ) const
noexcept

Definition at line 231 of file small_string.hpp.

◆ end() [2/2]

template<std::size_t N>
SmallString< N >::iterator utils::SmallString< N >::end ( )
noexcept

Definition at line 226 of file small_string.hpp.

◆ front() [1/2]

template<std::size_t N>
char & utils::SmallString< N >::front ( )

Get a reference to the first character.

Definition at line 256 of file small_string.hpp.

◆ front() [2/2]

template<std::size_t N>
const char & utils::SmallString< N >::front ( ) const

Get a reference to the first character.

Definition at line 261 of file small_string.hpp.

◆ insert()

template<std::size_t N>
template<class InputIt>
void utils::SmallString< N >::insert ( SmallString< N >::const_iterator pos,
InputIt begin,
InputIt end )

Inserts elements from range [begin, end) before pos.

Definition at line 293 of file small_string.hpp.

◆ operator std::string_view()

template<std::size_t N>
utils::SmallString< N >::operator std::string_view ( ) const

Convert string to a std::string_view.

Definition at line 163 of file small_string.hpp.

◆ operator+=() [1/2]

template<std::size_t N>
SmallString & utils::SmallString< N >::operator+= ( char c)
inline

Definition at line 58 of file small_string.hpp.

◆ operator+=() [2/2]

template<std::size_t N>
SmallString & utils::SmallString< N >::operator+= ( std::string_view sv)
inline

Definition at line 53 of file small_string.hpp.

◆ operator=()

template<std::size_t N>
SmallString< N > & utils::SmallString< N >::operator= ( std::string_view sv)

Assign the value of other string_view to this string.

Definition at line 168 of file small_string.hpp.

◆ operator[]() [1/2]

template<std::size_t N>
char & utils::SmallString< N >::operator[] ( std::size_t pos)

Subscript access to the data contained in the string.

Definition at line 199 of file small_string.hpp.

◆ operator[]() [2/2]

template<std::size_t N>
const char & utils::SmallString< N >::operator[] ( std::size_t pos) const

Read-only subscript access to the data contained in the string.

Definition at line 194 of file small_string.hpp.

◆ pop_back()

template<std::size_t N>
void utils::SmallString< N >::pop_back ( )

Remove the last character from the string.

Definition at line 298 of file small_string.hpp.

◆ push_back()

template<std::size_t N>
void utils::SmallString< N >::push_back ( char c)

Append a character to the string.

Definition at line 276 of file small_string.hpp.

◆ reserve()

template<std::size_t N>
void utils::SmallString< N >::reserve ( std::size_t n)

Reserve to n bytes.

Definition at line 331 of file small_string.hpp.

◆ resize() [1/2]

template<std::size_t N>
void utils::SmallString< N >::resize ( std::size_t n)

Resize the string. If its length is increased, fill new chars with \0.

Definition at line 308 of file small_string.hpp.

◆ resize() [2/2]

template<std::size_t N>
void utils::SmallString< N >::resize ( std::size_t n,
char c )

Resize the string. If its length is increased, fill new chars with c.

Definition at line 303 of file small_string.hpp.

◆ resize_and_overwrite()

template<std::size_t N>
template<class Operation>
void utils::SmallString< N >::resize_and_overwrite ( std::size_t size,
Operation op )

Resize the string. Use op to write into the string and replace a sequence of characters.

Definition at line 314 of file small_string.hpp.

◆ shrink_to_fit()

template<std::size_t N>
void utils::SmallString< N >::shrink_to_fit ( )

Shrink the string's size to fit all contents.

Definition at line 321 of file small_string.hpp.

◆ size()

template<std::size_t N>
std::size_t utils::SmallString< N >::size ( ) const
noexcept

Get string size.

Definition at line 236 of file small_string.hpp.


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