SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
small_string.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
16 
17 namespace seqan3
18 {
19 
42 template <size_t capacity_>
43 class small_string : public small_vector<char, capacity_ + 1>
44 {
45 private:
48 
49  // make data inherited members visible
50  using base_t::data_;
51  using base_t::sz;
52 public:
56  using typename base_t::value_type;
57  using typename base_t::reference;
58  using typename base_t::const_reference;
59  using typename base_t::iterator;
60  using typename base_t::const_iterator;
61  using typename base_t::difference_type;
62  using typename base_t::size_type;
64 
69  using base_t::base_t;
71  using base_t::assign;
72 
85  template <size_t N>
86  constexpr small_string(char const (&_lit)[N]) noexcept : small_string{}
87  {
88  static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
89  assign(_lit);
90  }
91 
101  explicit constexpr small_string(char const c) noexcept : small_string{}
102  {
103  assign(1, c);
104  }
105 
122  template <size_t N>
123  constexpr small_string & operator=(char const (&_lit)[N]) noexcept
124  {
125  static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
126  assign(_lit);
127  return *this;
128  }
129 
146  template <size_t N>
147  constexpr void assign(char const (&_lit)[N]) noexcept
148  {
149  static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
150  assert(_lit[N - 1] == '\0');
151  assign(&_lit[0], &_lit[N-1]);
152  }
153 
171  template <std::forward_iterator begin_it_type, typename end_it_type>
173  requires std::sentinel_for<end_it_type, begin_it_type> &&
174  std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>
176  constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
177  {
178  base_t::assign(begin_it, end_it);
179  data_[sz] = '\0';
180  }
182 
190  static constexpr size_type max_size() noexcept
191  {
192  return capacity_;
193  }
194 
199  static constexpr size_type capacity() noexcept
200  {
201  return capacity_;
202  }
204 
209  constexpr void clear() noexcept
210  {
211  sz = 0;
212  data_[0] = '\0';
213  }
214 
216  constexpr void push_back(char const value) noexcept
217  {
218  assert(sz < capacity_);
219  data_[sz] = value;
220  ++sz;
221  data_[sz] = '\0';
222  }
223 
225  constexpr void pop_back() noexcept
226  {
227  assert(sz > 0);
228  --sz;
229  data_[sz] = '\0';
230  }
231 
233  constexpr void resize(size_type const count) noexcept
234  {
235  resize(count, '\0');
236  }
237 
239  constexpr void resize(size_type const count, char const value) noexcept
240  {
241  assert(count <= capacity_);
242 
243  for (size_t i = sz; i < count; ++i) // sz < count; add `value` in [sz, count)
244  data_[i] = value;
245 
246  sz = count;
247  data_[sz] = '\0';
248  }
249 
270  constexpr small_string & erase(size_type index = 0, size_type count = max_size()) noexcept
271  {
272  assert(index <= this->size());
273 
274  iterator it = this->begin() + index;
275  base_t::erase(it, it + std::min<size_type>(count, this->size() - index));
276  return *this;
277  }
279 
298  template <size_t capacity2>
300  small_string<capacity2> const & rhs) noexcept
301  {
303  tmp.insert(tmp.end(), rhs.begin(), rhs.end());
304  return tmp;
305  }
307 
326  std::string str() const
327  {
328  return std::string{this->cbegin(), this->cend()};
329  }
330 
345  constexpr char const * c_str() const noexcept
346  {
347  return data_.data();
348  }
349 
362  operator std::string() const
363  {
364  return str();
365  }
367 
384  {
385  os << str.str();
386  return os;
387  }
388 
403  {
404  // Check if stream is ok and skip leading whitespaces.
405  std::istream::sentry s(is);
406  if (s)
407  {
408  str.erase(); // clear the string
409  std::streamsize num_char = (is.width() > 0)
410  ? std::min<std::streamsize>(is.width(), str.max_size())
411  : str.max_size();
412  assert(num_char > 0);
413  for (std::streamsize n = num_char; n > 0 && !std::isspace(static_cast<char>(is.peek()), is.getloc()); --n)
414  {
415  char c = is.get();
416  if (is.eof())
417  break;
418  str.push_back(c);
419  }
420 
421  if (str.size() == 0) // nothing extracted so we set the fail bit.
422  is.setstate(std::ios_base::failbit);
423 
424  is.width(0); // cancel the effects of std::setw, if any.
425  }
426 
427  return is;
428  }
430 };
431 
440 template <size_t N>
441 small_string(char const (&)[N]) -> small_string<N - 1>;
442 
448 template <size_t N>
450 
458 
459 } // namespace seqan3
Implements a small string that can be used for compile time computations.
Definition: small_string.hpp:44
constexpr small_string(char const c) noexcept
Construction from char.
Definition: small_string.hpp:101
friend std::istream & operator>>(std::istream &is, small_string &str)
Formatted input for the seqan3::small_string.
Definition: small_string.hpp:402
constexpr void resize(size_type const count, char const value) noexcept
Resizes the container to contain count elements.
Definition: small_string.hpp:239
constexpr void clear() noexcept
Removes all elements from the container.
Definition: small_string.hpp:209
std::string str() const
Returns the content represented as std::string.
Definition: small_string.hpp:326
static constexpr size_type capacity() noexcept
Returns the maximal capacity.
Definition: small_string.hpp:199
static constexpr size_type max_size() noexcept
Returns the maximal size which equals the capacity.
Definition: small_string.hpp:190
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:147
friend std::ostream & operator<<(std::ostream &os, small_string const &str)
Formatted output for the seqan3::small_string.
Definition: small_string.hpp:383
constexpr small_string & erase(size_type index=0, size_type count=max_size()) noexcept
Removes specified elements from the container.
Definition: small_string.hpp:270
small_string(char const(&)[N]) -> small_string< N - 1 >
Deduces small_string from string literals.
constexpr char const * c_str() const noexcept
Returns the content represented as 0-terminated c-style string.
Definition: small_string.hpp:345
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:216
constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
Assign from pair of iterators.
Definition: small_string.hpp:176
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:299
constexpr void resize(size_type const count) noexcept
Resizes the container to contain count elements.
Definition: small_string.hpp:233
constexpr small_string & operator=(char const (&_lit)[N]) noexcept
Assign from literal.
Definition: small_string.hpp:123
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition: small_string.hpp:225
constexpr small_string(char const (&_lit)[N]) noexcept
Construction from literal.
Definition: small_string.hpp:86
A constexpr vector implementation with dynamic size at compile time.
Definition: small_vector.hpp:47
constexpr const_iterator cbegin() const noexcept
Returns the begin to the string.
Definition: small_vector.hpp:388
const value_type & const_reference
The const_reference type.
Definition: small_vector.hpp:72
constexpr void assign(std::initializer_list< value_type > ilist) noexcept(is_noexcept)
Assign from std::initializer_list.
Definition: small_vector.hpp:290
value_type * iterator
The iterator type.
Definition: small_vector.hpp:78
value_type const * const_iterator
The const_iterator type.
Definition: small_vector.hpp:84
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Removes specified elements from the container.
Definition: small_vector.hpp:806
constexpr iterator insert(const_iterator pos, value_type const value) noexcept(is_noexcept)
Inserts value before position in the container.
Definition: small_vector.hpp:690
char value_type
The value_type type.
Definition: small_vector.hpp:60
detail::min_viable_uint_t< capacity_ > size_type
The size_type type.
Definition: small_vector.hpp:96
constexpr size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition: small_vector.hpp:590
constexpr const_iterator cend() const noexcept
Returns iterator past the end of the vector.
Definition: small_vector.hpp:409
constexpr iterator begin() noexcept
Returns the begin to the string.
Definition: small_vector.hpp:376
ptrdiff_t difference_type
The difference_type type.
Definition: small_vector.hpp:90
value_type & reference
The reference type.
Definition: small_vector.hpp:66
T eof(T... args)
T erase(T... args)
T get(T... args)
T getloc(T... args)
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition: traits.hpp:169
T max_size(T... args)
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
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)