KSS Utility
C++ general utilities
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
kss::util::strings::SubString< Char, Traits, Alloc > Class Template Reference

Write-through substring. More...

#include <substring.hpp>

Inheritance diagram for kss::util::strings::SubString< Char, Traits, Alloc >:
kss::util::AddRelOps< SubString< Char, Traits, Alloc > >

Public Types

using size_type = typename std::basic_string< Char >::size_type
 
using allocator_type = Alloc
 
using pointer = typename Alloc::pointer
 
using const_pointer = typename Alloc::const_pointer
 
using value_type = Char
 

Public Member Functions

 SubString (std::basic_string< Char, Traits, Alloc > &str, size_type i, size_type n=std::basic_string< Char, Traits, Alloc >::npos)
 
 SubString (std::basic_string< Char, Traits, Alloc > &str, const std::basic_string< Char, Traits, Alloc > &str2, size_type i=0)
 
 ~SubString () noexcept
 
SubStringoperator= (const std::basic_string< Char, Traits, Alloc > &str)
 
SubStringoperator= (const SubString &str)
 
SubStringoperator= (const_pointer cptr)
 
SubStringoperator= (Char ch)
 
 operator std::basic_string< Char, Traits, Alloc > () const
 
const_pointer c_str () const
 
const_pointer data () const noexcept
 
size_type size () const noexcept
 
bool empty () const noexcept
 
bool operator== (const SubString &rhs) noexcept
 
bool operator< (const SubString &rhs) noexcept
 
- Public Member Functions inherited from kss::util::AddRelOps< SubString< Char, Traits, Alloc > >
bool operator!= (const SubString< Char, Traits, Alloc > &t) const noexcept
 
bool operator<= (const SubString< Char, Traits, Alloc > &t) const noexcept
 
bool operator> (const SubString< Char, Traits, Alloc > &t) const noexcept
 
bool operator>= (const SubString< Char, Traits, Alloc > &t) const noexcept
 

Detailed Description

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
class kss::util::strings::SubString< Char, Traits, Alloc >

Write-through substring.

The SubString class provides writable access to a substring of another string. Note that since this is backed by the original string, the substring will become invalid when the original string goes out of scope.

Note that if a SubString constructor results in an empty substring (for example the search fails), then any attempt to modify the substring will result in an out_of_range exception.

While the code in this class is original, it is motivated by the Basic_substring class described in THE C++ PROGRAMMING LANGUAGE, SPECIAL EDITION by Stroustrup, ISBN 0-201-88954-4.

Definition at line 45 of file substring.hpp.

Member Typedef Documentation

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
using kss::util::strings::SubString< Char, Traits, Alloc >::allocator_type = Alloc

Definition at line 49 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
using kss::util::strings::SubString< Char, Traits, Alloc >::const_pointer = typename Alloc::const_pointer

Definition at line 51 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
using kss::util::strings::SubString< Char, Traits, Alloc >::pointer = typename Alloc::pointer

Definition at line 50 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
using kss::util::strings::SubString< Char, Traits, Alloc >::size_type = typename std::basic_string<Char>::size_type

Definition at line 48 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
using kss::util::strings::SubString< Char, Traits, Alloc >::value_type = Char

Definition at line 52 of file substring.hpp.

Constructor & Destructor Documentation

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
kss::util::strings::SubString< Char, Traits, Alloc >::SubString ( std::basic_string< Char, Traits, Alloc > &  str,
size_type  i,
size_type  n = std::basic_string<Char, Traits, Alloc>::npos 
)
inline

Create an n-character substring from str, starting with position i. Note that if n goes beyond the end of str, it will be trimmed to the final position in str.

Definition at line 59 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
kss::util::strings::SubString< Char, Traits, Alloc >::SubString ( std::basic_string< Char, Traits, Alloc > &  str,
const std::basic_string< Char, Traits, Alloc > &  str2,
size_type  i = 0 
)
inline

Search for the first occurrence of str2 in str, starting at position i, and create the substring from it. (Substring is created based on str, not on str2.)

Definition at line 83 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
kss::util::strings::SubString< Char, Traits, Alloc >::~SubString ( )
inlinenoexcept

Destroy the substring.

Definition at line 105 of file substring.hpp.

Member Function Documentation

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
const_pointer kss::util::strings::SubString< Char, Traits, Alloc >::c_str ( ) const
inline

Return a NULL-terminated "C" style string built from the substring. Note that this results in the characters being copied. If the substring is currently empty, an empty but valid "C" string will be returned. Also note that the memory allocated by this method will become invalid once the substring goes out of scope. If we cannot allocate enough memory a bad_alloc exception is thrown. The memory will be built using the allocator from the original string.

Exceptions
std::bad_allocif memory could not be allocated

Definition at line 169 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
const_pointer kss::util::strings::SubString< Char, Traits, Alloc >::data ( ) const
inlinenoexcept

Return a pointer to the start of the substring characters. No guarantee is made regarding a NULL terminator hence you must use the size() method to determine the proper end of the characters. Unlike c_str() this requires no memory allocation.

Definition at line 188 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
bool kss::util::strings::SubString< Char, Traits, Alloc >::empty ( ) const
inlinenoexcept

Returns true if the substring is empty and false otherwise.

Definition at line 200 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
kss::util::strings::SubString< Char, Traits, Alloc >::operator std::basic_string< Char, Traits, Alloc > ( ) const
inline

Allow a substring to be cast to a string. This results in a copy of the substring characters. If the substring is currently empty, an empty but valid string will be returned.

Definition at line 149 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
bool kss::util::strings::SubString< Char, Traits, Alloc >::operator< ( const SubString< Char, Traits, Alloc > &  rhs)
inlinenoexcept

Definition at line 215 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
SubString& kss::util::strings::SubString< Char, Traits, Alloc >::operator= ( const std::basic_string< Char, Traits, Alloc > &  str)
inline

Replace the substring (in the original string) with str.

Exceptions
std::out_of_rangeif the substring is not valid

Definition at line 113 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
SubString& kss::util::strings::SubString< Char, Traits, Alloc >::operator= ( const SubString< Char, Traits, Alloc > &  str)
inline

Replace the substring (in the original string) with str.

Exceptions
std::out_of_rangeif the substring is not valid

Definition at line 121 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
SubString& kss::util::strings::SubString< Char, Traits, Alloc >::operator= ( const_pointer  cptr)
inline

Replace the substring (in the original string) with the NULL- terminated characters starting with cptr.

Exceptions
std::invalid_argumentif cptr is NULL
std::out_of_rangeif the substring is not valid

Definition at line 131 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
SubString& kss::util::strings::SubString< Char, Traits, Alloc >::operator= ( Char  ch)
inline

Replace the substring (in the original string) with the single character ch.

Exceptions
std::out_of_rangeif the substring is currently empty

Definition at line 140 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
bool kss::util::strings::SubString< Char, Traits, Alloc >::operator== ( const SubString< Char, Traits, Alloc > &  rhs)
inlinenoexcept

Comparators. The "missing" operators are added by the AddRelOps subclassing.

Definition at line 205 of file substring.hpp.

template<class Char, class Traits = std::char_traits<Char>, class Alloc = std::allocator<Char>>
size_type kss::util::strings::SubString< Char, Traits, Alloc >::size ( ) const
inlinenoexcept

Returns the size of the substring.

Definition at line 195 of file substring.hpp.


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