jaulib v1.4.1-10-ga2c96e0
Jau Support Library (C++, Java, ..)
Loading...
Searching...
No Matches
string_literal.hpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Gothel Software e.K.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25#ifndef JAU_STRING_LITERAL_HPP_
26#define JAU_STRING_LITERAL_HPP_
27
28#include <algorithm>
29#include <climits>
30#include <string_view>
31#include <cstring>
32
33#include <jau/cpp_lang_util.hpp>
34
35namespace jau {
36
37 /** \addtogroup StringUtils
38 *
39 * @{
40 */
41
42 /**
43 * Static compile-time string literal storage.
44 *
45 * Properties
46 * - includes buffered EOS
47 * -
48 *
49 * Implementation has been aligned to [p3094r5](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3094r5.html),
50 * but maintains original properties (see above).
51 *
52 * @tparam N string length excluding EOS
53 */
54 template<typename CharT, std::size_t N>
56 {
57 public:
58 using value_type = CharT;
59 using size_type = std::size_t;
60 using difference_type = ptrdiff_t;
61 using pointer = CharT*;
62 using const_pointer = const CharT*;
63 using reference = CharT&;
64 using const_reference = const CharT&;
65 using const_iterator = const CharT*;
67 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
68
69 private:
70 CharT buf[N+1];
71
72 public:
73 template<std::convertible_to<CharT>... Chars>
74 requires(sizeof...(Chars) == N) && (... && !std::is_pointer_v<Chars>)
75 constexpr explicit BasicStringLiteral(Chars... chars) noexcept
76 : buf {chars..., CharT{}} // add EOS
77 { }
78
79 /// Implicit constructor from string literal `const CharT (&str)[N+1]`, i.e. including EOS
80 constexpr BasicStringLiteral(const CharT (&str)[N+1]) noexcept {
81 std::copy(str, str+N+1, buf); // include EOS
82 }
83
84 template<std::size_t S, std::size_t T>
85 constexpr explicit BasicStringLiteral(const BasicStringLiteral<CharT, S> &s1, const BasicStringLiteral<CharT, T> &s2) noexcept
86 requires(N == S + T)
87 {
88 std::copy(s1.cbegin(), s1.cend(), buf);
89 std::copy(s2.cbegin(), s2.cend(), buf+S);
90 buf[N] = CharT{}; // EOS
91 }
92
93 constexpr BasicStringLiteral(const BasicStringLiteral& o) noexcept = default;
94 constexpr BasicStringLiteral& operator=(const BasicStringLiteral& o) noexcept = default;
95
96 constexpr BasicStringLiteral(BasicStringLiteral&& o) noexcept = default;
97 constexpr BasicStringLiteral& operator=(BasicStringLiteral&& o) noexcept = default;
98
99 constexpr const_iterator begin() const noexcept { return data(); }
100 constexpr const_iterator end() const noexcept { return data() + N; }
101 constexpr const_iterator cbegin() const noexcept { return begin(); }
102 constexpr const_iterator cend() const noexcept { return end(); }
103 constexpr const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
104 constexpr const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
105 constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
106 constexpr const_reverse_iterator crend() const noexcept { return rend(); }
107
108 constexpr bool operator==(const BasicStringLiteral &o) const noexcept {
109 return std::equal(o.cbegin(), o.cend(), buf);
110 }
111 template<std::size_t N2>
112 constexpr bool operator==(const BasicStringLiteral<CharT, N2>) const noexcept {
113 return false;
114 }
115 constexpr CharT operator[](std::size_t n) const noexcept {
116 return n < N ? buf[n] : CharT{};
117 }
118
119 template<std::size_t N2>
123
124 //
125 // std::string_view functions
126 //
127
128 /// string literal size w/o EOS.
129 constexpr size_type size() const noexcept { return N; };
130 /// string literal size w/o EOS.
131 constexpr size_type length() const noexcept { return N; };
132 /// string literal size w/o EOS.
133 constexpr size_type max_size() const noexcept { return N; };
134 constexpr bool empty() const noexcept { return 0==N; };
135 /// Returns c-string w/ EOS.
136 constexpr const char* c_str() const noexcept { return buf; }
137 /// Returns c-string w/ EOS.
138 constexpr const_pointer data() const noexcept { return static_cast<const_pointer>(buf); }
139 constexpr std::basic_string_view<CharT> view() const noexcept {
140 return std::basic_string_view<CharT>(cbegin(), cend());
141 }
142 constexpr operator std::basic_string_view<CharT>() const noexcept { return view(); }
143 };
144
145 // deduction guide for char sequence
146 template<typename CharT, std::convertible_to<CharT>... Rest>
147 BasicStringLiteral(CharT, Rest...) -> BasicStringLiteral<CharT, 1 + sizeof...(Rest)>;
148
149 // deduction guide for c-string literal
150 template<typename CharT, size_t O>
151 BasicStringLiteral(const CharT (&str)[O]) -> BasicStringLiteral<CharT, O - 1>;
152
153 // deduction guide
154 // template<typename CharT, size_t N>
155 // BasicStringLiteral(from_range_t, std::array<CharT, N>) -> BasicStringLiteral<CharT, N>;
156
157 template<typename CharT, std::size_t N, std::size_t O>
158 constexpr BasicStringLiteral<CharT, N+O-1> operator+(const BasicStringLiteral<CharT, N> &lhs, const char (&rhs) [O]) {
159 return lhs + BasicStringLiteral<CharT, O-1>(rhs);
160 }
161
162 template<typename CharT, size_t O, std::size_t N>
163 constexpr BasicStringLiteral<CharT, N+O-1> operator+(const char (&lhs) [O], const BasicStringLiteral<CharT, N> rhs) {
164 return BasicStringLiteral<CharT, O-1>(lhs) + rhs;
165 }
166
167 template<size_t N>
169
170 template<size_t N>
172
173 /**@}*/
174
175} // namespace jau
176
177#endif /* JAU_STRING_LITERAL_HPP_ */
Static compile-time string literal storage.
constexpr BasicStringLiteral(BasicStringLiteral &&o) noexcept=default
constexpr size_type size() const noexcept
string literal size w/o EOS.
constexpr const_reverse_iterator rbegin() const noexcept
constexpr BasicStringLiteral(const CharT(&str)[N+1]) noexcept
Implicit constructor from string literal const CharT (&str)[N+1], i.e. including EOS.
constexpr const_iterator end() const noexcept
constexpr const_iterator begin() const noexcept
constexpr size_type length() const noexcept
string literal size w/o EOS.
constexpr BasicStringLiteral(const BasicStringLiteral< CharT, S > &s1, const BasicStringLiteral< CharT, T > &s2) noexcept
constexpr const char * c_str() const noexcept
Returns c-string w/ EOS.
constexpr const_iterator cend() const noexcept
constexpr BasicStringLiteral(Chars... chars) noexcept
constexpr BasicStringLiteral & operator=(BasicStringLiteral &&o) noexcept=default
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr const_reverse_iterator rend() const noexcept
constexpr bool empty() const noexcept
constexpr BasicStringLiteral & operator=(const BasicStringLiteral &o) noexcept=default
constexpr BasicStringLiteral< CharT, N+N2 > operator+(const BasicStringLiteral< CharT, N2 > &o) const
constexpr size_type max_size() const noexcept
string literal size w/o EOS.
constexpr const_pointer data() const noexcept
Returns c-string w/ EOS.
constexpr BasicStringLiteral(const BasicStringLiteral &o) noexcept=default
constexpr CharT operator[](std::size_t n) const noexcept
constexpr const_reverse_iterator crbegin() const noexcept
constexpr const_iterator cbegin() const noexcept
constexpr bool operator==(const BasicStringLiteral &o) const noexcept
constexpr bool operator==(const BasicStringLiteral< CharT, N2 >) const noexcept
constexpr const_reverse_iterator crend() const noexcept
constexpr std::basic_string_view< CharT > view() const noexcept
constexpr fraction< int_type > operator+(const fraction< int_type > &lhs, const fraction< int_type > &rhs) noexcept
Returns sum of two fraction.
BasicStringLiteral(CharT, Rest...) -> BasicStringLiteral< CharT, 1+sizeof...(Rest)>
BasicStringLiteral< char, N > StringLiteral
BasicStringLiteral< wchar_t, N > WStringLiteral
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
Definition backtrace.hpp:32