hybstr
Hybrid constexpr string library for compile-time and runtime use
Loading...
Searching...
No Matches
hybstr::string_impl< BufferCapacity, DynamicExpandCapacity > Class Template Reference

Fixed-capacity string that will expand on demand. More...

#include <hybstr.hpp>

Public Types

using value_type = char
using reference = value_type&
using const_reference = const value_type&
using size_type = std::size_t
using view_type = std::string_view
using pointer = char*
using const_pointer = const char*
using iterator = typename std::array<char, BufferCapacity + 1>::iterator
using const_iterator = typename std::array<char, BufferCapacity + 1>::const_iterator
using reverse_iterator = typename std::array<char, BufferCapacity + 1>::reverse_iterator
using const_reverse_iterator = typename std::array<char, BufferCapacity + 1>::const_reverse_iterator

Public Member Functions

constexpr string_impl () noexcept
 Default constructor. Initializes an empty string.
template<std::size_t N>
constexpr string_impl (const char(&str)[N]) noexcept
 Constructs from a string literal.
constexpr string_impl (const std::size_t &n, char c) noexcept
 Fills the buffer with a repeated character.
constexpr string_impl (std::string_view sv) noexcept
 Constructs from a std::string_view. Copies up to BufferCapacity characters.
template<typename _Iter>
constexpr string_impl (_Iter start, _Iter end) noexcept
 Constructs from an iterator range. Copies until 'end' or until BufferCapacity is reached.
constexpr string_impl (const string_impl &) noexcept=default
constexpr string_impl (string_impl &&) noexcept=default
constexpr string_imploperator= (const string_impl &) noexcept=default
constexpr string_imploperator= (string_impl &&) noexcept=default
constexpr auto size () const noexcept -> size_type
constexpr auto capacity () const noexcept -> size_type
constexpr auto empty () const noexcept -> bool
constexpr auto data () noexcept -> pointer
constexpr auto data () const noexcept -> const_pointer
constexpr auto c_str () const noexcept -> const_pointer
constexpr auto view () const noexcept -> view_type
auto str () const -> std::string
constexpr auto operator[] (size_type i) noexcept -> reference
 Accesses a character by index (mutable).
constexpr auto operator[] (size_type i) const noexcept -> const_reference
 Accesses a character by index (const).
constexpr auto begin () noexcept -> iterator
constexpr auto begin () const noexcept -> const_iterator
constexpr auto cbegin () const noexcept -> const_iterator
constexpr auto end () noexcept -> iterator
constexpr auto end () const noexcept -> const_iterator
constexpr auto cend () const noexcept -> const_iterator
constexpr auto rbegin () noexcept -> reverse_iterator
constexpr auto rbegin () const noexcept -> const_reverse_iterator
constexpr auto crbegin () const noexcept -> const_reverse_iterator
constexpr auto rend () noexcept -> reverse_iterator
constexpr auto rend () const noexcept -> const_reverse_iterator
constexpr auto crend () const noexcept -> const_reverse_iterator
template<std::size_t N>
constexpr auto set (char c) const noexcept
 Replaces a character at compile time.
template<std::size_t N>
constexpr auto append (const char(&str)[N]) const noexcept
 Appends a C-style string literal.
template<std::size_t TargetSize = DynamicExpandCapacity, std::size_t N, std::size_t ExpandCapacity>
constexpr auto append (const string_impl< N, ExpandCapacity > &other) const noexcept
 Appends another hybrid string.
template<std::size_t TargetSize = DynamicExpandCapacity>
constexpr auto append (std::string_view sv) const noexcept
 Appends a std::string_view.
template<std::size_t N = 1>
constexpr auto append (char c) const noexcept
 Appends one or more repeated characters.
constexpr auto push_back (char c) const noexcept
 Appends a single character.
template<std::size_t N>
constexpr auto resize (char c=' ') const noexcept
 Resizes the string to a new compile-time capacity.
template<std::size_t N>
constexpr auto reserve () const noexcept
 Ensures compile-time capacity is at least N. If current capacity is smaller, a new string_impl is created.

Public Attributes

std::array< char, BufferCapacity+1 > _data {}
 Internal fixed buffer (+1 for null terminator).
size_type _size {}
 Number of characters currently stored.

Detailed Description

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
class hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >

Fixed-capacity string that will expand on demand.

Template Parameters
BufferCapacityNumber of characters stored in the compile-time buffer (excluding the null terminator).
DynamicExpandCapacitySize of the dynamic buffer allocated for runtime operations.

Combines a compile-time buffer for constexpr evaluation with a dynamic buffer for runtime use. 'BufferCapacity' defines how many characters are stored in the fixed buffer, and 'DynamicExpandCapacity' specifies the size of the dynamic region used when handling inputs such as 'std::string_view' or iterator ranges whose lengths cannot be determined at compile time. The dynamic buffer size can also be customized through template arguments.

This class is not intended to be used directly. Prefer using the factory functions:

auto s11 = hybstr::string(); // empty string
auto s12 = hybstr::string<10>(); // empty string with custom dynamic expand capacity
auto s21 = hybstr::string("abc"); // from string literal
auto s22 = hybstr::string<10>("abc"); // from literal with custom dynamic expand capacity
constexpr std::string_view sv = "abc"; // non-constexpr string views also work
auto s31 = hybstr::string<3>(sv); // from string_view (specify length, default to HYBSTR_DYNAMIC_EXPAND_CAPACITY)
auto s32 = hybstr::string<3, 10>(sv); // from string_view with custom expand capacity
std::string str = "Hello world";
auto s41 = hybstr::string<11>(str.begin(), str.end()); // from iterator range (specify length, default to HYBSTR_DYNAMIC_EXPAND_CAPACITY)
auto s42 = hybstr::string<11, 10>(str.begin(), str.end()); // from iterator range with custom expand capacity
auto str() const -> std::string
Definition hybstr.hpp:273
constexpr auto string()
Creates an empty hybrid string.
Definition hybstr.hpp:945

Example:

constexpr auto s1 = std::string_view("Greetings");
// Constexpr
constexpr auto s2 = hybstr::string("Name");
constexpr auto s3 = hybstr::string(s1) + ", " + s2;
static_assert(s3 == hybstr::string("Greetings, Name"));
// Run time
std::string s4;
std::cin >> s4;
auto s5 = hybstr::string(s1) + ", " + s4;
assert(s5.view() == std::string(s1.begin(), s1.end()) + ", " + s4);

Constructor & Destructor Documentation

◆ string_impl() [1/2]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t N>
hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::string_impl ( const char(&) str[N])
inlineconstexprnoexcept

Constructs from a string literal.

Template Parameters
NNumber of characters including null terminator.
Parameters
strNull-terminated C-style string.

◆ string_impl() [2/2]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::string_impl ( const std::size_t & n,
char c )
inlineconstexprnoexcept

Fills the buffer with a repeated character.

Parameters
nNumber of characters to fill.
cFill character.

Member Function Documentation

◆ append() [1/4]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t N = 1>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::append ( char c) const
inlinenodiscardconstexprnoexcept

Appends one or more repeated characters.

Template Parameters
NNumber of characters to add.

◆ append() [2/4]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t N>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::append ( const char(&) str[N]) const
inlinenodiscardconstexprnoexcept

Appends a C-style string literal.

Template Parameters
NNumber of characters including null terminator.
Returns
A new string_impl containing the concatenated result.

◆ append() [3/4]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t TargetSize = DynamicExpandCapacity, std::size_t N, std::size_t ExpandCapacity>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::append ( const string_impl< N, ExpandCapacity > & other) const
inlinenodiscardconstexprnoexcept

Appends another hybrid string.

Template Parameters
TargetSizeBuffer expansion size for the result.
NBufferCapacity of the other string.
ExpandCapacityDynamic capacity of the other string.
Returns
A new string_impl combining both contents.

◆ append() [4/4]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t TargetSize = DynamicExpandCapacity>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::append ( std::string_view sv) const
inlinenodiscardconstexprnoexcept

Appends a std::string_view.

Template Parameters
TargetSizeBuffer expansion size for the result.

◆ c_str()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::c_str ( ) const -> const_pointer
inlinenodiscardconstexprnoexcept
Returns
Null-terminated C-string.

◆ capacity()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::capacity ( ) const -> size_type
inlinenodiscardconstexprnoexcept
Returns
Maximum number of characters in the fixed buffer.

◆ data() [1/2]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::data ( ) const -> const_pointer
inlinenodiscardconstexprnoexcept
Returns
Const pointer to internal buffer.

◆ data() [2/2]

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::data ( ) -> pointer
inlinenodiscardconstexprnoexcept
Returns
Mutable pointer to internal buffer.

◆ empty()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::empty ( ) const -> bool
inlinenodiscardconstexprnoexcept
Returns
True if the string is empty.

◆ resize()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t N>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::resize ( char c = ' ') const
inlinenodiscardconstexprnoexcept

Resizes the string to a new compile-time capacity.

Template Parameters
NNew capacity.
Parameters
cFill character if expanded.
Returns
A new string_impl with resized storage.

◆ set()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
template<std::size_t N>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::set ( char c) const
inlinenodiscardconstexprnoexcept

Replaces a character at compile time.

Template Parameters
NIndex to replace (must be less than BufferCapacity).
Parameters
cNew character value.
Returns
A new string_impl with the modified character.

◆ size()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::size ( ) const -> size_type
inlinenodiscardconstexprnoexcept
Returns
Number of characters currently stored.

◆ str()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::str ( ) const -> std::string
inlinenodiscard
Returns
A new std::string copy of the contents (runtime).

◆ view()

template<std::size_t BufferCapacity = 0, std::size_t DynamicExpandCapacity = HYBSTR_DYNAMIC_EXPAND_CAPACITY>
auto hybstr::string_impl< BufferCapacity, DynamicExpandCapacity >::view ( ) const -> view_type
inlinenodiscardconstexprnoexcept
Returns
Read-only view of the string.

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