Loading...
Searching...
No Matches
small_string.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <istream>
#include <seqan3/utility/container/small_vector.hpp>
namespace seqan3
{
template <size_t capacity_>
class small_string : public small_vector<char, capacity_ + 1>
{
private:
using base_t = small_vector<char, capacity_ + 1>;
// make data inherited members visible
using base_t::data_;
using base_t::sz;
public:
using typename base_t::const_iterator;
using typename base_t::const_reference;
using typename base_t::difference_type;
using typename base_t::iterator;
using typename base_t::reference;
using typename base_t::size_type;
using typename base_t::value_type;
using base_t::base_t;
using base_t::assign;
template <size_t N>
constexpr small_string(char const (&_lit)[N]) noexcept : small_string{}
{
static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
assign(_lit);
}
explicit constexpr small_string(char const c) noexcept : small_string{}
{
assign(1, c);
}
template <size_t N>
constexpr small_string & operator=(char const (&_lit)[N]) noexcept
{
static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
assign(_lit);
return *this;
}
template <size_t N>
constexpr void assign(char const (&_lit)[N]) noexcept
{
static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
assert(_lit[N - 1] == '\0');
base_t::assign(&_lit[0], &_lit[N - 1]);
data_[sz] = '\0';
}
template <std::forward_iterator begin_it_type, typename end_it_type>
requires std::sentinel_for<end_it_type, begin_it_type>
&& std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
{
base_t::assign(begin_it, end_it);
data_[sz] = '\0';
}
static constexpr size_type max_size() noexcept
{
return capacity_;
}
static constexpr size_type capacity() noexcept
{
return capacity_;
}
constexpr void clear() noexcept
{
sz = 0;
data_[0] = '\0';
}
constexpr void push_back(char const value) noexcept
{
assert(sz < capacity_);
data_[sz] = value;
++sz;
data_[sz] = '\0';
}
constexpr void pop_back() noexcept
{
assert(sz > 0);
--sz;
data_[sz] = '\0';
}
constexpr void resize(size_type const count) noexcept
{
resize(count, '\0');
}
constexpr void resize(size_type const count, char const value) noexcept
{
assert(count <= capacity_);
for (size_t i = sz; i < count; ++i) // sz < count; add `value` in [sz, count)
data_[i] = value;
sz = count;
data_[sz] = '\0';
}
constexpr small_string & erase(size_type index = 0, size_type count = max_size()) noexcept
{
assert(index <= this->size());
iterator it = this->begin() + index;
base_t::erase(it, it + std::min<size_type>(count, this->size() - index));
return *this;
}
template <size_t capacity2>
constexpr friend small_string<capacity_ + capacity2> operator+(small_string const & lhs,
small_string<capacity2> const & rhs) noexcept
{
small_string<capacity_ + capacity2> tmp{lhs};
tmp.insert(tmp.end(), rhs.begin(), rhs.end());
return tmp;
}
std::string str() const
{
return std::string{this->cbegin(), this->cend()};
}
constexpr char const * c_str() const noexcept
{
return data_.data();
}
operator std::string() const
{
return str();
}
constexpr operator std::string_view() const noexcept
{
return std::string_view{data_.data(), this->size()};
}
friend std::ostream & operator<<(std::ostream & os, small_string const & str)
{
os << str.str();
return os;
}
friend std::istream & operator>>(std::istream & is, small_string & str)
{
// Check if stream is ok and skip leading whitespaces.
std::istream::sentry s(is);
if (s)
{
str.erase(); // clear the string
std::streamsize num_char =
(is.width() > 0) ? std::min<std::streamsize>(is.width(), str.max_size()) : str.max_size();
assert(num_char > 0);
for (std::streamsize n = num_char; n > 0 && !std::isspace(static_cast<char>(is.peek()), is.getloc()); --n)
{
char c = is.get();
if (is.eof())
break;
str.push_back(c);
}
if (str.size() == 0) // nothing extracted so we set the fail bit.
is.setstate(std::ios_base::failbit);
is.width(0); // cancel the effects of std::setw, if any.
}
return is;
}
};
template <size_t N>
small_string(char const (&)[N]) -> small_string<N - 1>;
template <size_t N>
small_string(std::array<char, N> const &) -> small_string<N>;
small_string(char const) -> small_string<1>;
} // namespace seqan3
std::array
std::istream
std::ostream
std::string
std::string_view
seqan3::small_string::small_string
constexpr small_string(char const c) noexcept
Construction from char.
Definition small_string.hpp:101
seqan3::small_string::iterator
value_type * iterator
The iterator type.
Definition small_vector.hpp:75
seqan3::small_string::resize
constexpr void resize(size_type const count, char const value) noexcept
Resizes the container to contain count elements.
Definition small_string.hpp:238
seqan3::small_string::clear
constexpr void clear() noexcept
Removes all elements from the container.
Definition small_string.hpp:208
seqan3::small_string::str
std::string str() const
Returns the content represented as std::string.
Definition small_string.hpp:325
seqan3::small_string::c_str
constexpr char const * c_str() const noexcept
Returns the content represented as 0-terminated c-style string.
Definition small_string.hpp:344
seqan3::small_string::capacity
static constexpr size_type capacity() noexcept
Returns the maximal capacity.
Definition small_string.hpp:198
seqan3::small_string::max_size
static constexpr size_type max_size() noexcept
Returns the maximal size which equals the capacity.
Definition small_string.hpp:189
seqan3::small_string::operator+
constexpr friend small_string< capacity_+capacity2 > operator+(small_string const &lhs, small_string< capacity2 > const &rhs) noexcept
Concatenates two small_strings by returning a new small_string.
Definition small_string.hpp:298
seqan3::small_string::small_string
small_string(char const) -> small_string< 1 >
Deduces small_string from char.
seqan3::small_string::assign
constexpr void assign(char const (&_lit)[N]) noexcept
Assign from literal.
Definition small_string.hpp:147
seqan3::small_string::size_type
detail::min_viable_uint_t< capacity_ > size_type
The size_type type.
Definition small_vector.hpp:93
seqan3::small_string::small_string
small_string(char const(&)[N]) -> small_string< N - 1 >
Deduces small_string from string literals.
seqan3::small_string::erase
constexpr small_string & erase(size_type index=0, size_type count=max_size()) noexcept
Removes specified elements from the container.
Definition small_string.hpp:269
seqan3::small_string::operator>>
friend std::istream & operator>>(std::istream &is, small_string &str)
Formatted input for the seqan3::small_string.
Definition small_string.hpp:421
seqan3::small_string::assign
constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
Assign from pair of iterators.
Definition small_string.hpp:175
seqan3::small_string::small_string
small_string(std::array< char, N > const &) -> small_string< N >
Deduces small_string from std::array of type char.
seqan3::small_string::push_back
constexpr void push_back(char const value) noexcept
Appends the given element value to the end of the container.
Definition small_string.hpp:215
seqan3::small_string::resize
constexpr void resize(size_type const count) noexcept
Resizes the container to contain count elements.
Definition small_string.hpp:232
seqan3::small_string::operator<<
friend std::ostream & operator<<(std::ostream &os, small_string const &str)
Formatted output for the seqan3::small_string.
Definition small_string.hpp:402
seqan3::small_string::pop_back
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition small_string.hpp:224
seqan3::small_string::operator=
constexpr small_string & operator=(char const (&_lit)[N]) noexcept
Assign from literal.
Definition small_string.hpp:123
seqan3::small_string::small_string
constexpr small_string(char const (&_lit)[N]) noexcept
Construction from literal.
Definition small_string.hpp:86
seqan3::small_vector< char, capacity_+1 >::cbegin
constexpr const_iterator cbegin() const noexcept
Definition small_vector.hpp:369
seqan3::small_vector< char, capacity_+1 >::small_vector
constexpr small_vector() noexcept=default
seqan3::small_vector< char, capacity_+1 >::assign
constexpr void assign(std::initializer_list< value_type > ilist) noexcept(is_noexcept)
Definition small_vector.hpp:275
seqan3::small_vector< char, capacity_+1 >::iterator
value_type * iterator
Definition small_vector.hpp:75
seqan3::small_vector< char, capacity_+1 >::const_iterator
value_type const * const_iterator
Definition small_vector.hpp:81
seqan3::small_vector< char, capacity_+1 >::erase
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Definition small_vector.hpp:787
seqan3::small_vector::insert
constexpr iterator insert(const_iterator pos, value_type const value) noexcept(is_noexcept)
Inserts value before position in the container.
Definition small_vector.hpp:671
seqan3::small_vector< char, capacity_+1 >::value_type
char value_type
Definition small_vector.hpp:57
seqan3::small_vector< char, capacity_+1 >::size_type
detail::min_viable_uint_t< capacity_ > size_type
Definition small_vector.hpp:93
seqan3::small_vector< char, capacity_+1 >::const_reference
value_type const & const_reference
Definition small_vector.hpp:69
seqan3::small_vector< char, capacity_+1 >::size
constexpr size_type size() const noexcept
Definition small_vector.hpp:571
seqan3::small_vector< char, capacity_+1 >::cend
constexpr const_iterator cend() const noexcept
Definition small_vector.hpp:390
seqan3::small_vector< char, capacity_+1 >::begin
constexpr iterator begin() noexcept
Definition small_vector.hpp:357
seqan3::small_vector< char, capacity_+1 >::difference_type
ptrdiff_t difference_type
Definition small_vector.hpp:87
seqan3::small_vector::end
constexpr iterator end() noexcept
Returns iterator past the end of the vector.
Definition small_vector.hpp:378
seqan3::small_vector< char, capacity_+1 >::reference
value_type & reference
Definition small_vector.hpp:63
std::istream::eof
T eof(T... args)
std::istream::get
T get(T... args)
std::istream::getloc
T getloc(T... args)
istream
std::min
T min(T... args)
seqan3
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
std::istream::peek
T peek(T... args)
std::istream::sentry
std::istream::setstate
T setstate(T... args)
small_vector.hpp
A constexpr vector implementation with dynamic size at compile time.
std::streamsize
std::istream::width
T width(T... args)
Hide me