-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_literal
317 lines (246 loc) · 11.3 KB
/
string_literal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// D3933 - A compile-time string library template with UDL operator templates
// N4236 - A compile-time string library template with UDL operator templates
namespace std
{
template<typename _CharT, _CharT... _Chars>
class basic_string_literal;
inline namespace literals {
template<typename _CharT, _CharT... _Chars>
constexpr basic_string_literal<_CharT, _Chars...> operator""S();
}
template<typename _CharT, _CharT... _Chars>
class basic_string_literal
{
friend /*constexpr*/ basic_string_literal operator""S<_CharT, _Chars...>();
private:
static constexpr const _CharT _M_data[]{_Chars..., _CharT{}};
public:
constexpr basic_string_literal() = default;
using value_type = _CharT;
using size_type = std::size_t;
using difference_type = std::size_t;
using tail_type = basic_string_literal<_CharT>;
// Size-related functions
constexpr size_type size() const
{ return sizeof...(_Chars); }
constexpr size_type length() const
{ return sizeof...(_Chars); }
constexpr bool empty() const
{ return sizeof...(_Chars) == 0; }
// Element accessor functions
constexpr _CharT
operator[](size_type pos) const
{ return _M_data[pos]; }
template<size_type _Idx>
constexpr _CharT
at() const
{ return _M_data[_Idx]; }
constexpr _CharT
at(size_type pos) const
{ return _M_data[pos]; }
constexpr _CharT
back() const
{ return _M_data[this->size() - 1]; }
constexpr _CharT
front() const
{ return _M_data[0]; }
// Search functions
template<_CharT... _OtherChars>
constexpr size_type find(const basic_string_literal<_CharT, _OtherChars...>& str, size_type pos = 0) const;
constexpr size_type find(const _CharT* s, size_type pos, size_type count) const;
constexpr size_type find(const _CharT* s, size_type pos = 0) const;
constexpr size_type find(_CharT ch, size_type pos = 0) const;
template<_CharT... _OtherChars>
constexpr size_type rfind(const basic_string_literal<_CharT, _OtherChars...>& str, std::size_t pos = sizeof...(_Chars)) const;
constexpr size_type rfind(const _CharT* s, size_type pos, size_type count) const;
constexpr size_type rfind(const _CharT* s, size_type pos = sizeof...(_Chars)) const;
constexpr size_type rfind(_CharT ch, size_t pos = sizeof...(_Chars)) const;
template<_CharT... _OtherChars>
constexpr size_type find_first_of(const basic_string_literal<_CharT, _OtherChars...>& str, size_type pos = 0) const;
constexpr size_type find_first_of(const _CharT* s, size_type pos, size_type count) const;
constexpr size_type find_first_of(const _CharT* s, size_type pos = 0) const;
constexpr size_type find_first_of(_CharT ch, size_type pos = 0) const;
template<_CharT... _OtherChars>
constexpr size_type find_first_not_of(const basic_string_literal<_CharT, _OtherChars...>& str, size_type pos = 0) const;
constexpr size_type find_first_not_of(const _CharT* s, size_type pos, size_type count) const;
constexpr size_type find_first_not_of(const _CharT* s, size_type pos = 0) const;
constexpr size_type find_first_not_of(_CharT ch, size_type pos = 0) const;
template<_CharT... _OtherChars>
constexpr size_type find_last_of(const basic_string_literal<_CharT, _OtherChars...>& str, size_type pos = sizeof...(_Chars)) const;
constexpr size_type find_last_of(const _CharT* s, size_type pos, size_type count) const;
constexpr size_type find_last_of(const _CharT* s, size_type pos = sizeof...(_Chars)) const;
constexpr size_type find_last_of(_CharT ch, size_t pos = sizeof...(_Chars)) const;
template<_CharT... _OtherChars>
constexpr size_type find_last_not_of(const basic_string_literal<_CharT, _OtherChars...>& str, size_type pos = sizeof...(_Chars)) const;
constexpr size_type find_last_not_of(const _CharT* s, size_type pos, size_type count) const;
constexpr size_type find_last_not_of(const _CharT* s, size_type pos = sizeof...(_Chars)) const;
constexpr size_type find_last_not_of(_CharT ch, size_t pos = sizeof...(_Chars)) const;
// Comparison functions
template<_CharT... _OtherChars>
constexpr int compare(const basic_string_literal<_CharT, _OtherChars...> & other) const;
template<typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT>>
constexpr int compare(const basic_string<_CharT, _Traits, _Alloc>& str) const;
template<typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT>>
constexpr int compare(size_type pos1, size_type count1,
const basic_string<_CharT, _Traits, _Alloc>& str) const;
template<typename _Traits = char_traits<_CharT>, typename _Alloc = allocator<_CharT>>
constexpr int compare(size_type pos1, size_type count1,
const basic_string<_CharT, _Traits, _Alloc>& str,
size_type pos2, size_type count2) const;
constexpr int compare(const _CharT* s) const;
constexpr int compare(size_type pos1, size_type count1, const _CharT* s) const;
constexpr int compare(size_type pos1, size_type count1,
const _CharT* s, size_type count2) const;
// Conversion functions
constexpr const char *
c_str() const
{ return _M_data; }
constexpr const char *
data() const
{ return _M_data; }
std::string
to_string() const;
constexpr operator const char*() const;
constexpr long long
to_number() const;
};
template<typename _CharT>
class basic_string_literal<_CharT>
{
friend /*constexpr*/ basic_string_literal operator""S<_CharT>();
private:
static constexpr const _CharT _M_data[]{_CharT{}};
public:
constexpr basic_string_literal() = default;
using value_type = _CharT;
using size_type = std::size_t;
using difference_type = std::size_t;
using tail_type = basic_string_literal<_CharT>;
constexpr size_type
size() const
{ return 1; }
constexpr size_type
length() const
{ return 1; }
constexpr bool
empty() const
{ return false; }
template<_CharT... _OtherChars>
constexpr int compare(const basic_string_literal<_CharT, _OtherChars...>& other) const;
constexpr int compare(const basic_string<_CharT>& str) const;
constexpr int compare(std::size_t pos1, std::size_t count1, const basic_string<_CharT>& str) const;
constexpr int compare(std::size_t pos1, std::size_t count1, const basic_string<_CharT>& str, std::size_t pos2, std::size_t count2) const;
constexpr int compare(const _CharT* s) const;
constexpr int compare(std::size_t pos1, std::size_t count1, const _CharT* s) const;
constexpr int compare(std::size_t pos1, std::size_t count1, const _CharT* s, std::size_t count2) const;
constexpr const char * c_str() const;
constexpr const char * data() const;
std::string to_string() const;
constexpr size_type to_number() const;
constexpr operator const char*() const;
operator std::string() const;
};
inline namespace literals
{
template<typename _CharT, _CharT... _Chars>
constexpr basic_string_literal<_CharT, _Chars...>
operator""S()
{ return basic_string_literal<_CharT, _Chars...>(); }
}
// Concatenation
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
inline constexpr auto
operator+(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs)
-> basic_string_literal<_CharT, _LeftChars..., _RightChars...>;
// Relational operators
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
constexpr bool
operator==(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs);
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
constexpr bool
operator!=(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs);
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
constexpr bool
operator<(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs);
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
constexpr bool
operator<=(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs);
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
constexpr bool
operator>(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs);
template<typename _CharT, _CharT... _LeftChars, _CharT... _RightChars>
constexpr bool
operator>=(const basic_string_literal<_CharT, _LeftChars...>& lhs,
const basic_string_literal<_CharT, _RightChars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator==(const _CharT* lhs,
const basic_string_literal<_CharT, _Chars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator==(const basic_string_literal<_CharT, _Chars...>& lhs,
const _CharT* rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator!=(const _CharT* lhs,
const basic_string_literal<_CharT, _Chars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator!=(const basic_string_literal<_CharT, _Chars...>& lhs,
const _CharT* rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator<(const _CharT* lhs,
const basic_string_literal<_CharT, _Chars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator<(const basic_string_literal<_CharT, _Chars...>& lhs,
const _CharT* rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator<=(const _CharT* lhs,
const basic_string_literal<_CharT, _Chars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator<=(const basic_string_literal<_CharT, _Chars...>& lhs,
const _CharT* rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator>(const _CharT* lhs,
const basic_string_literal<_CharT, _Chars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator>(const basic_string_literal<_CharT, _Chars...>& lhs,
const _CharT* rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator>=(const _CharT* lhs,
const basic_string_literal<_CharT, _Chars...>& rhs);
template<typename _CharT, _CharT... _Chars>
constexpr bool
operator>=(const basic_string_literal<_CharT, _Chars...>& lhs,
const _CharT* rhs);
// Stream inseration operator
template<typename _CharT, _CharT... _Chars>
inline std::ostream&
operator<<(std::ostream& os,
const basic_string_literal<_CharT, _Chars...>& str);
// Literal operator
template <typename _CharT, _CharT... _Chars>
inline constexpr basic_string_literal<_CharT, _Chars...>
operator""S();
template<char... _Chars>
using string_literal = basic_string_literal<char, _Chars...>;
template<char16_t... _Chars>
using u16string_literal = basic_string_literal<char16_t, _Chars...>;
template<char32_t... _Chars>
using u32string_literal = basic_string_literal<char32_t, _Chars...>;
template<wchar_t... _Chars>
using wstring_literal = basic_string_literal<wchar_t, _Chars...>;
} // namespace std