BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
small_string.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
17
18namespace bio::ranges
19{
20
45template <size_t capacity_>
46class small_string : public small_vector<char, capacity_ + 1>
47{
48private:
51
52 // make data inherited members visible
53 using base_t::data_;
54 using base_t::sz;
55
56public:
60 using typename base_t::const_iterator;
61 using typename base_t::const_reference;
62 using typename base_t::difference_type;
63 using typename base_t::iterator;
64 using typename base_t::reference;
65 using typename base_t::size_type;
66 using typename base_t::value_type;
68
73 using base_t::base_t;
75 using base_t::assign;
76
87 template <size_t N>
88 constexpr small_string(char const (&_lit)[N]) noexcept : small_string{}
89 {
90 static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
91 assign(_lit);
92 }
93
108 template <size_t N>
109 constexpr small_string & operator=(char const (&_lit)[N]) noexcept
110 {
111 static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
112 assign(_lit);
113 return *this;
114 }
115
130 template <size_t N>
131 constexpr void assign(char const (&_lit)[N]) noexcept
132 {
133 static_assert(N <= capacity_ + 1, "Length of string literal exceeds capacity of small_string.");
134 assert(_lit[N - 1] == '\0');
135 assign(&_lit[0], &_lit[N - 1]);
136 }
137
153 template <std::forward_iterator begin_it_type, typename end_it_type>
154 requires(std::sentinel_for<end_it_type, begin_it_type> &&
155 std::constructible_from<value_type, std::iter_reference_t<begin_it_type>>)
156 constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
157 {
158 base_t::assign(begin_it, end_it);
159 data_[sz] = '\0';
160 }
161
162#ifdef __clang__
163 constexpr void assign(char const * const begin_it, char const * const end_it) noexcept
164 {
165 base_t::assign(begin_it, end_it);
166 data_[sz] = '\0';
167 }
168#endif
170
175 static constexpr size_type max_size() noexcept { return capacity_; }
176
178 static constexpr size_type capacity() noexcept { return capacity_; }
180
185 constexpr void clear() noexcept
186 {
187 sz = 0;
188 data_[0] = '\0';
189 }
190
192 constexpr void push_back(char const value) noexcept
193 {
194 assert(sz < capacity_);
195 data_[sz] = value;
196 ++sz;
197 data_[sz] = '\0';
198 }
199
201 constexpr void pop_back() noexcept
202 {
203 assert(sz > 0);
204 --sz;
205 data_[sz] = '\0';
206 }
207
209 constexpr void resize(size_type const count) noexcept { resize(count, '\0'); }
210
213 constexpr void resize(size_type const count, char const value) noexcept
214 {
215 assert(count <= capacity_);
216
217 for (size_t i = sz; i < count; ++i) // sz < count; add `value` in [sz, count)
218 data_[i] = value;
219
220 sz = count;
221 data_[sz] = '\0';
222 }
223
242 constexpr small_string & erase(size_type index = 0, size_type count = max_size()) noexcept
243 {
244 assert(index <= this->size());
245
246 iterator it = this->begin() + index;
247 base_t::erase(it, it + std::min<size_type>(count, this->size() - index));
248 return *this;
249 }
251
268 template <size_t capacity2>
270 small_string<capacity2> const & rhs) noexcept
271 {
273 tmp.insert(tmp.end(), rhs.begin(), rhs.end());
274 return tmp;
275 }
277
294 std::string str() const { return std::string{this->cbegin(), this->cend()}; }
295
308 constexpr std::string_view view() const { return std::string_view{data_.data(), this->size()}; }
309
322 constexpr char const * c_str() const noexcept { return data_.data(); }
323
334 constexpr operator std::string_view() const { return view(); }
336
340 template <size_t cap2>
341 friend constexpr bool operator==(small_string const & lhs, small_string<cap2> const & rhs) noexcept
342 {
343 return operator==(lhs.view(), rhs.view());
344 }
345
347 template <size_t cap2>
348 friend constexpr auto operator<=>(small_string const & lhs, small_string<cap2> const & rhs) noexcept
349 {
350 return operator<=>(lhs.view(), rhs.view());
351 }
352
354 friend constexpr bool operator==(small_string const & lhs, std::string_view const & rhs) noexcept
355 {
356 return operator==(lhs.view(), rhs);
357 }
358
360 friend constexpr auto operator<=>(small_string const & lhs, std::string_view const & rhs) noexcept
361 {
362 return operator<=>(lhs.view(), rhs);
363 }
365
380 {
381 os << str.str();
382 return os;
383 }
385};
386
392template <size_t N>
393small_string(char const (&)[N]) -> small_string<N - 1>;
394
397template <size_t N>
399
402template <std::same_as<char>... t>
403 requires(sizeof...(t) > 0)
404small_string(t const... chars) -> small_string<sizeof...(chars)>;
406
407} // namespace bio::ranges
408
409#if __has_include(<fmt/format.h>)
410
411# include <fmt/ranges.h>
412
413template <size_t N>
414struct fmt::formatter<bio::ranges::small_string<N>> : fmt::formatter<std::string_view>
415{
416 constexpr auto format(bio::ranges::small_string<N> const & a, auto & ctx) const
417 {
418 return fmt::formatter<std::string_view>::format(std::string_view{a.data(), a.size()}, ctx);
419 }
420};
421
422#endif
423
425template <size_t capacity>
426 requires(capacity <= 30) // -> sizeof(small_string) <= 32
427inline constexpr bool std::ranges::enable_view<bio::ranges::small_string<capacity>> = true;
Implements a small string that can be used for compile time computations.
Definition: small_string.hpp:47
friend constexpr auto operator<=>(small_string const &lhs, small_string< cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_string.hpp:348
constexpr void clear() noexcept
Removes all elements from the container.
Definition: small_string.hpp:185
constexpr void pop_back() noexcept
Removes the last element of the container.
Definition: small_string.hpp:201
constexpr small_string & operator=(char const (&_lit)[N]) noexcept
Assign from literal.
Definition: small_string.hpp:109
constexpr small_string & erase(size_type index=0, size_type count=max_size()) noexcept
Removes specified elements from the container.
Definition: small_string.hpp:242
constexpr void resize(size_type const count) noexcept
Resizes the container to contain count elements.
Definition: small_string.hpp:209
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:269
friend constexpr auto operator<=>(small_string const &lhs, std::string_view const &rhs) noexcept
Performs element-wise comparison.
Definition: small_string.hpp:360
constexpr std::string_view view() const
Returns the content represented as std::string_view.
Definition: small_string.hpp:308
static constexpr size_type max_size() noexcept
Returns the maximal size which equals the capacity.
Definition: small_string.hpp:175
constexpr void resize(size_type const count, char const value) noexcept
Resizes the container to contain count elements.
Definition: small_string.hpp:213
friend constexpr bool operator==(small_string const &lhs, small_string< cap2 > const &rhs) noexcept
Performs element-wise comparison.
Definition: small_string.hpp:341
friend constexpr bool operator==(small_string const &lhs, std::string_view const &rhs) noexcept
Performs element-wise comparison.
Definition: small_string.hpp:354
constexpr void push_back(char const value) noexcept
Appends the given element value to the end of the container.
Definition: small_string.hpp:192
std::string str() const
Returns the content represented as std::string.
Definition: small_string.hpp:294
small_string(char const(&)[N]) -> small_string< N - 1 >
Deduces small_string from string literals.
constexpr small_string(char const (&_lit)[N]) noexcept
Construction from literal.
Definition: small_string.hpp:88
constexpr void assign(begin_it_type begin_it, end_it_type end_it) noexcept
Assign from pair of iterators.
Definition: small_string.hpp:156
constexpr char const * c_str() const noexcept
Returns the content represented as 0-terminated c-style string.
Definition: small_string.hpp:322
small_string(std::array< char, N > const &) -> small_string< N >
Deduces small_string from std::array of type char.
constexpr void assign(char const (&_lit)[N]) noexcept
Assign from literal.
Definition: small_string.hpp:131
static constexpr size_type capacity() noexcept
Returns the maximal capacity.
Definition: small_string.hpp:178
friend std::ostream & operator<<(std::ostream &os, small_string const &str)
Formatted output for the bio::ranges::small_string.
Definition: small_string.hpp:379
A constexpr vector implementation with dynamic size at compile time.
Definition: small_vector.hpp:45
constexpr const_iterator cend() const noexcept
Returns iterator past the end of the vector.
Definition: small_vector.hpp:313
value_type & reference
The reference type.
Definition: small_vector.hpp:55
char value_type
The value_type type.
Definition: small_vector.hpp:54
constexpr void assign(std::initializer_list< value_type > ilist) noexcept(is_noexcept)
Assign from std::initializer_list.
Definition: small_vector.hpp:225
constexpr value_type * data() noexcept
Direct access to the underlying array.
Definition: small_vector.hpp:434
constexpr iterator insert(const_iterator pos, value_type const value) noexcept(is_noexcept)
Inserts value before position in the container.
Definition: small_vector.hpp:541
meta::detail::min_viable_uint_t< capacity_ > size_type
The size_type type.
Definition: small_vector.hpp:60
ptrdiff_t difference_type
The difference_type type.
Definition: small_vector.hpp:59
constexpr const_iterator cbegin() const noexcept
Returns the begin iterator of the vector.
Definition: small_vector.hpp:304
value_type const * const_iterator
The const_iterator type.
Definition: small_vector.hpp:58
value_type const & const_reference
The const_reference type.
Definition: small_vector.hpp:56
constexpr size_type size() const noexcept
Returns the number of elements in the container, i.e. std::distance(begin(), end()).
Definition: small_vector.hpp:467
constexpr iterator erase(const_iterator begin_it, const_iterator end_it) noexcept
Removes specified elements from the container.
Definition: small_vector.hpp:647
constexpr iterator begin() noexcept
Returns the begin iterator of the vector.
Definition: small_vector.hpp:298
value_type * iterator
The iterator type.
Definition: small_vector.hpp:57
T data(T... args)
The ranges module's namespace.
The main BioC++ namespace.
Definition: aa10li.hpp:23
A constexpr vector implementation with dynamic size at compile time.