SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
small_string.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 
41 template <size_t capacity_>
42 class small_string : public small_vector<char, capacity_ + 1>
43 {
44 private:
47 
48  // make data inherited members visible
49  using base_t::data_;
50  using base_t::sz;
51 public:
55  using typename base_t::value_type;
56  using typename base_t::reference;
57  using typename base_t::const_reference;
58  using typename base_t::iterator;
59  using typename base_t::const_iterator;
60  using typename base_t::difference_type;
61  using typename base_t::size_type;
63 
67  using base_t::base_t;
70  using base_t::assign;
71 
82  template <size_t N>
83  constexpr small_string(char const (&_lit)[N]) noexcept : small_string{}
84  {
85  static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
86  assign(_lit);
87  }
88 
96  explicit constexpr small_string(char const c) noexcept : small_string{}
97  {
98  assign(1, c);
99  }
100 
115  template <size_t N>
116  constexpr small_string & operator=(char const (&_lit)[N]) noexcept
117  {
118  static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
119  assign(_lit);
120  return *this;
121  }
122 
137  template <size_t N>
138  constexpr void assign(char const (&_lit)[N]) noexcept
139  {
140  static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
141  assert(_lit[N - 1] == '\0');
142  assign(&_lit[0], &_lit[N-1]);
143  }
144 
160  template <std::forward_iterator begin_it_type, typename end_it_type>
162  requires std::sentinel_for<end_it_type, begin_it_type> &&
165  constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
166  {
167  base_t::assign(begin_it, end_it);
168  data_[sz] = '\0';
169  }
171 
175  static constexpr size_type max_size() noexcept
177  {
178  return capacity_;
179  }
180 
182  static constexpr size_type capacity() noexcept
183  {
184  return capacity_;
185  }
187 
191  constexpr void clear() noexcept
193  {
194  sz = 0;
195  data_[0] = '\0';
196  }
197 
199  constexpr void push_back(char const value) noexcept
200  {
201  assert(sz < capacity_);
202  data_[sz] = value;
203  ++sz;
204  data_[sz] = '\0';
205  }
206 
208  constexpr void pop_back() noexcept
209  {
210  assert(sz > 0);
211  --sz;
212  data_[sz] = '\0';
213  }
214 
216  constexpr void resize(size_type const count) noexcept
217  {
218  resize(count, '\0');
219  }
220 
222  constexpr void resize(size_type const count, char const value) noexcept
223  {
224  assert(count <= capacity_);
225 
226  for (size_t i = sz; i < count; ++i) // sz < count; add `value` in [sz, count)
227  data_[i] = value;
228 
229  sz = count;
230  data_[sz] = '\0';
231  }
232 
251  constexpr small_string & erase(size_type index = 0, size_type count = max_size()) noexcept
252  {
253  assert(index <= this->size());
254 
255  iterator it = this->begin() + index;
256  base_t::erase(it, it + std::min<size_type>(count, this->size() - index));
257  return *this;
258  }
260 
277  template <size_t capacity2>
279  small_string<capacity2> const & rhs) noexcept
280  {
282  tmp.insert(tmp.end(), rhs.begin(), rhs.end());
283  return tmp;
284  }
286 
303  std::string str() const
304  {
305  return std::string{this->cbegin(), this->cend()};
306  }
307 
320  constexpr char const * c_str() const noexcept
321  {
322  return data_.data();
323  }
324 
335  operator std::string() const
336  {
337  return str();
338  }
340 
355  {
356  os << str.str();
357  return os;
358  }
359 
372  {
373  // Check if stream is ok and skip leading whitespaces.
374  std::istream::sentry s(is);
375  if (s)
376  {
377  str.erase(); // clear the string
378  std::streamsize num_char = (is.width() > 0)
379  ? std::min<std::streamsize>(is.width(), str.max_size())
380  : str.max_size();
381  assert(num_char > 0);
382  for (std::streamsize n = num_char; n > 0 && !std::isspace(static_cast<char>(is.peek()), is.getloc()); --n)
383  {
384  char c = is.get();
385  if (is.eof())
386  break;
387  str.push_back(c);
388  }
389 
390  if (str.size() == 0) // nothing extracted so we set the fail bit.
391  is.setstate(std::ios_base::failbit);
392 
393  is.width(0); // cancel the effects of std::setw, if any.
394  }
395 
396  return is;
397  }
399 };
400 
404 template <size_t N>
407 small_string(char const (&)[N]) -> small_string<N - 1>;
408 
411 template <size_t N>
412 small_string(std::array<char, N> const &) -> small_string<N>;
413 
416 small_string(char const) -> small_string<1>;
418 
419 } // namespace seqan3
seqan3::small_vector< char, capacity_+1 >::erase
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Removes specified elements from the container.
Definition: small_vector.hpp:705
std::istream::width
T width(T... args)
seqan3::small_vector< char, capacity_+1 >::size
constexpr size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition: small_vector.hpp:511
seqan3::small_vector
A constexpr vector implementation with dynamic size at compile time.
Definition: small_vector.hpp:44
std::istream::setstate
T setstate(T... args)
seqan3::small_string::pop_back
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition: small_string.hpp:208
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:597
std::string
seqan3::small_string::clear
constexpr void clear() noexcept
Removes all elements from the container.
Definition: small_string.hpp:192
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:222
seqan3::small_string::operator=
constexpr small_string & operator=(char const (&_lit)[N]) noexcept
Assign from literal.
Definition: small_string.hpp:116
constructible_from
The std::constructible_from concept specifies that a variable of type T can be initialized with the g...
seqan3::reference_t
typename reference< t >::type reference_t
Shortcut for seqan3::reference (transformation_trait shortcut).
Definition: pre.hpp:77
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:354
std::string::size
T size(T... args)
seqan3::small_vector< char, capacity_+1 >::cend
constexpr const_iterator cend() const noexcept
Returns iterator past the end of the vector.
Definition: small_vector.hpp:345
seqan3::small_string
Implements a small string that can be used for compile time computations.
Definition: small_string.hpp:42
std::istream::peek
T peek(T... args)
std::istream::get
T get(T... args)
seqan3::small_string::assign
constexpr void assign(char const (&_lit)[N]) noexcept
Assign from literal.
Definition: small_string.hpp:138
seqan3::small_vector< char, capacity_+1 >::iterator
value_type * iterator
The iterator type.
Definition: small_vector.hpp:57
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:371
std::string::push_back
T push_back(T... args)
std::streamsize
seqan3::small_vector< char, capacity_+1 >::begin
constexpr iterator begin() noexcept
Returns the begin to the string.
Definition: small_vector.hpp:315
std::ostream
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:278
seqan3::small_string::capacity
static constexpr size_type capacity() noexcept
Returns the maximal capacity.
Definition: small_string.hpp:182
std::array
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:36
std::string::erase
T erase(T... args)
seqan3::small_vector< char, capacity_+1 >::difference_type
ptrdiff_t difference_type
The difference_type type.
Definition: small_vector.hpp:59
seqan3::small_vector< char, capacity_+1 >::cbegin
constexpr const_iterator cbegin() const noexcept
Returns the begin to the string.
Definition: small_vector.hpp:327
std::string::max_size
T max_size(T... args)
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:251
seqan3::size_type
Exposes the size_type of another type.
Definition: pre.hpp:188
seqan3::small_vector< char, capacity_+1 >::const_reference
const value_type & const_reference
The const_reference type.
Definition: small_vector.hpp:56
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:165
seqan3::small_vector< char, capacity_+1 >::size_type
detail::min_viable_uint_t< capacity_ > size_type
The size_type type.
Definition: small_vector.hpp:60
seqan3::small_string::c_str
constexpr const char * c_str() const noexcept
Returns the content represented as 0-terminated c-style string.
Definition: small_string.hpp:320
seqan3::small_vector< char, capacity_+1 >::assign
constexpr void assign(std::initializer_list< value_type > ilist) noexcept(is_noexcept)
Assign from std::initializer_list.
Definition: small_vector.hpp:238
seqan3::small_vector< char, capacity_+1 >::reference
value_type & reference
The reference type.
Definition: small_vector.hpp:55
seqan3::small_vector< char, capacity_+1 >::value_type
char value_type
The value_type type.
Definition: small_vector.hpp:54
seqan3::small_string::str
std::string str() const
Returns the content represented as std::string.
Definition: small_string.hpp:303
seqan3::small_string::small_string
constexpr small_string(char const (&_lit)[N]) noexcept
Construction from literal.
Definition: small_string.hpp:83
std::conditional_t
std::istream::getloc
T getloc(T... args)
seqan3::small_string::small_string
constexpr small_string(char const c) noexcept
Construction from char.
Definition: small_string.hpp:96
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:199
std::istream
small_vector.hpp
A constexpr vector implementation with dynamic size at compile time.
seqan3::small_string::resize
constexpr void resize(size_type const count) noexcept
Resizes the container to contain count elements.
Definition: small_string.hpp:216
std::istream::sentry
seqan3::pack_traits::count
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition: traits.hpp:134
std::istream::eof
T eof(T... args)
seqan3::small_string::max_size
static constexpr size_type max_size() noexcept
Returns the maximal size which equals the capacity.
Definition: small_string.hpp:176
seqan3::small_vector< char, capacity_+1 >::const_iterator
value_type const * const_iterator
The const_iterator type.
Definition: small_vector.hpp:58