SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
small_string.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
13
14namespace seqan3
15{
16
39template <size_t capacity_>
40class small_string : public small_vector<char, capacity_ + 1>
41{
42private:
45
46 // make data inherited members visible
47 using base_t::data_;
48 using base_t::sz;
49
50public:
62
67 using base_t::base_t;
69 using base_t::assign;
70
83 template <size_t N>
84 constexpr small_string(char const (&_lit)[N]) noexcept : small_string{}
85 {
86 static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
87 assign(_lit);
88 }
89
99 explicit constexpr small_string(char const c) noexcept : small_string{}
100 {
101 assign(1, c);
102 }
103
120 template <size_t N>
121 constexpr small_string & operator=(char const (&_lit)[N]) noexcept
122 {
123 static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
124 assign(_lit);
125 return *this;
126 }
127
144 template <size_t N>
145 constexpr void assign(char const (&_lit)[N]) noexcept
146 {
147 static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
148 assert(_lit[N - 1] == '\0');
149 base_t::assign(&_lit[0], &_lit[N - 1]);
150 data_[sz] = '\0';
151 }
152
170 template <std::forward_iterator begin_it_type, typename end_it_type>
171 requires std::sentinel_for<end_it_type, begin_it_type>
172 && std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
173 constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
174 {
175 base_t::assign(begin_it, end_it);
176 data_[sz] = '\0';
177 }
179
187 static constexpr size_type max_size() noexcept
188 {
189 return capacity_;
190 }
191
196 static constexpr size_type capacity() noexcept
197 {
198 return capacity_;
199 }
201
206 constexpr void clear() noexcept
207 {
208 sz = 0;
209 data_[0] = '\0';
210 }
211
213 constexpr void push_back(char const value) noexcept
214 {
215 assert(sz < capacity_);
216 data_[sz] = value;
217 ++sz;
218 data_[sz] = '\0';
219 }
220
222 constexpr void pop_back() noexcept
223 {
224 assert(sz > 0);
225 --sz;
226 data_[sz] = '\0';
227 }
228
230 constexpr void resize(size_type const count) noexcept
231 {
232 resize(count, '\0');
233 }
234
236 constexpr void resize(size_type const count, char const value) noexcept
237 {
238 assert(count <= capacity_);
239
240 for (size_t i = sz; i < count; ++i) // sz < count; add `value` in [sz, count)
241 data_[i] = value;
242
243 sz = count;
244 data_[sz] = '\0';
245 }
246
267 constexpr small_string & erase(size_type index = 0, size_type count = max_size()) noexcept
268 {
270
271 iterator it = this->begin() + index;
272 base_t::erase(it, it + std::min<size_type>(count, this->size() - index));
273 return *this;
274 }
276
295 template <size_t capacity2>
297 small_string<capacity2> const & rhs) noexcept
298 {
300 tmp.insert(tmp.end(), rhs.begin(), rhs.end());
301 return tmp;
302 }
304
324 {
325 return std::string{this->cbegin(), this->cend()};
326 }
327
342 constexpr char const * c_str() const noexcept
343 {
344 return data_.data();
345 }
346
359 operator std::string() const
360 {
361 return str();
362 }
363
379 constexpr operator std::string_view() const noexcept
380 {
381 return std::string_view{data_.data(), this->size()};
382 }
384
401 {
402 os << str.str();
403 return os;
404 }
405
420 {
421 // Check if stream is ok and skip leading whitespaces.
423 if (s)
424 {
425 str.erase(); // clear the string
427 (is.width() > 0) ? std::min<std::streamsize>(is.width(), str.max_size()) : str.max_size();
428 assert(num_char > 0);
429 for (std::streamsize n = num_char; n > 0 && !std::isspace(static_cast<char>(is.peek()), is.getloc()); --n)
430 {
431 char c = is.get();
432 if (is.eof())
433 break;
434 str.push_back(c);
435 }
436
437 if (str.size() == 0) // nothing extracted so we set the fail bit.
438 is.setstate(std::ios_base::failbit);
439
440 is.width(0); // cancel the effects of std::setw, if any.
441 }
442
443 return is;
444 }
446};
447
456template <size_t N>
457small_string(char const (&)[N]) -> small_string<N - 1>;
458
464template <size_t N>
466
474
475} // namespace seqan3
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
Implements a small string that can be used for compile time computations.
Definition small_string.hpp:41
constexpr small_string(char const c) noexcept
Construction from char.
Definition small_string.hpp:99
constexpr void resize(size_type const count, char const value) noexcept
Resizes the container to contain count elements.
Definition small_string.hpp:236
constexpr void clear() noexcept
Removes all elements from the container.
Definition small_string.hpp:206
std::string str() const
Returns the content represented as std::string.
Definition small_string.hpp:323
constexpr char const * c_str() const noexcept
Returns the content represented as 0-terminated c-style string.
Definition small_string.hpp:342
static constexpr size_type capacity() noexcept
Returns the maximal capacity.
Definition small_string.hpp:196
static constexpr size_type max_size() noexcept
Returns the maximal size which equals the capacity.
Definition small_string.hpp:187
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:296
small_string(char const) -> small_string< 1 >
Deduces small_string from char.
constexpr void assign(char const (&_lit)[N]) noexcept
Assign from literal.
Definition small_string.hpp:145
small_string(char const(&)[N]) -> small_string< N - 1 >
Deduces small_string from string literals.
constexpr small_string & erase(size_type index=0, size_type count=max_size()) noexcept
Removes specified elements from the container.
Definition small_string.hpp:267
friend std::istream & operator>>(std::istream &is, small_string &str)
Formatted input for the seqan3::small_string.
Definition small_string.hpp:419
constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
Assign from pair of iterators.
Definition small_string.hpp:173
small_string(std::array< char, N > const &) -> small_string< N >
Deduces small_string from std::array of type char.
constexpr void push_back(char const value) noexcept
Appends the given element value to the end of the container.
Definition small_string.hpp:213
constexpr void resize(size_type const count) noexcept
Resizes the container to contain count elements.
Definition small_string.hpp:230
friend std::ostream & operator<<(std::ostream &os, small_string const &str)
Formatted output for the seqan3::small_string.
Definition small_string.hpp:400
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition small_string.hpp:222
constexpr small_string & operator=(char const (&_lit)[N]) noexcept
Assign from literal.
Definition small_string.hpp:121
constexpr small_string(char const (&_lit)[N]) noexcept
Construction from literal.
Definition small_string.hpp:84
A constexpr vector implementation with dynamic size at compile time.
Definition small_vector.hpp:44
constexpr const_iterator cbegin() const noexcept
Returns the begin to the string.
Definition small_vector.hpp:369
constexpr void assign(std::initializer_list< value_type > ilist) noexcept(is_noexcept)
Assign from std::initializer_list.
Definition small_vector.hpp:275
value_type * iterator
The iterator type.
Definition small_vector.hpp:75
value_type const * const_iterator
The const_iterator type.
Definition small_vector.hpp:81
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Removes specified elements from the container.
Definition small_vector.hpp:792
char value_type
The value_type type.
Definition small_vector.hpp:57
detail::min_viable_uint_t< capacity_ > size_type
The size_type type.
Definition small_vector.hpp:93
value_type const & const_reference
The const_reference type.
Definition small_vector.hpp:69
constexpr size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition small_vector.hpp:571
constexpr const_iterator cend() const noexcept
Returns iterator past the end of the vector.
Definition small_vector.hpp:390
constexpr iterator begin() noexcept
Returns the begin to the string.
Definition small_vector.hpp:357
ptrdiff_t difference_type
The difference_type type.
Definition small_vector.hpp:87
value_type & reference
The reference type.
Definition small_vector.hpp:63
T data(T... args)
T eof(T... args)
T erase(T... args)
T get(T... args)
T getloc(T... args)
T max_size(T... args)
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
T peek(T... args)
T push_back(T... args)
T setstate(T... args)
T size(T... args)
A constexpr vector implementation with dynamic size at compile time.
T width(T... args)
Hide me