rotate gdb/ChangeLog
[deliverable/binutils-gdb.git] / gdb / common / gdb_string_view.h
1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
2
3 // Note: This file has been stolen from the gcc repo
4 // (libstdc++-v3/include/experimental/string_view) and has local modifications.
5
6 // Copyright (C) 2013-2018 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // Under Section 7 of GPL version 3, you are granted additional
20 // permissions described in the GCC Runtime Library Exception, version
21 // 3.1, as published by the Free Software Foundation.
22
23 // You should have received a copy of the GNU General Public License and
24 // a copy of the GCC Runtime Library Exception along with this program;
25 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 // <http://www.gnu.org/licenses/>.
27
28 //
29 // N3762 basic_string_view library
30 //
31
32 #ifndef GDB_STRING_VIEW_H
33 #define GDB_STRING_VIEW_H 1
34
35 #if __cplusplus >= 201703L
36
37 #include <string_view>
38
39 namespace gdb {
40 using string_view = std::string_view;
41 } /* namespace gdb */
42
43 #else /* __cplusplus < 201703L */
44
45 #include <string>
46 #include <limits>
47
48 namespace gdb {
49
50 /**
51 * @class basic_string_view <experimental/string_view>
52 * @brief A non-owning reference to a string.
53 *
54 * @ingroup strings
55 * @ingroup sequences
56 * @ingroup experimental
57 *
58 * @tparam _CharT Type of character
59 * @tparam _Traits Traits for character type, defaults to
60 * char_traits<_CharT>.
61 *
62 * A basic_string_view looks like this:
63 *
64 * @code
65 * _CharT* _M_str
66 * size_t _M_len
67 * @endcode
68 */
69 template<typename _CharT, typename _Traits = std::char_traits<_CharT>>
70 class basic_string_view
71 {
72 public:
73
74 // types
75 using traits_type = _Traits;
76 using value_type = _CharT;
77 using pointer = const _CharT*;
78 using const_pointer = const _CharT*;
79 using reference = const _CharT&;
80 using const_reference = const _CharT&;
81 using const_iterator = const _CharT*;
82 using iterator = const_iterator;
83 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
84 using reverse_iterator = const_reverse_iterator;
85 using size_type = size_t;
86 using difference_type = ptrdiff_t;
87 static constexpr size_type npos = size_type(-1);
88
89 // [string.view.cons], construct/copy
90
91 constexpr
92 basic_string_view() noexcept
93 : _M_len{0}, _M_str{nullptr}
94 { }
95
96 constexpr basic_string_view(const basic_string_view&) noexcept = default;
97
98 template<typename _Allocator>
99 basic_string_view(const std::basic_string<_CharT, _Traits,
100 _Allocator>& __str) noexcept
101 : _M_len{__str.length()}, _M_str{__str.data()}
102 { }
103
104 /*constexpr*/ basic_string_view(const _CharT* __str)
105 : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
106 _M_str{__str}
107 { }
108
109 constexpr basic_string_view(const _CharT* __str, size_type __len)
110 : _M_len{__len},
111 _M_str{__str}
112 { }
113
114 basic_string_view&
115 operator=(const basic_string_view&) noexcept = default;
116
117 // [string.view.iterators], iterators
118
119 constexpr const_iterator
120 begin() const noexcept
121 { return this->_M_str; }
122
123 constexpr const_iterator
124 end() const noexcept
125 { return this->_M_str + this->_M_len; }
126
127 constexpr const_iterator
128 cbegin() const noexcept
129 { return this->_M_str; }
130
131 constexpr const_iterator
132 cend() const noexcept
133 { return this->_M_str + this->_M_len; }
134
135 const_reverse_iterator
136 rbegin() const noexcept
137 { return const_reverse_iterator(this->end()); }
138
139 const_reverse_iterator
140 rend() const noexcept
141 { return const_reverse_iterator(this->begin()); }
142
143 const_reverse_iterator
144 crbegin() const noexcept
145 { return const_reverse_iterator(this->end()); }
146
147 const_reverse_iterator
148 crend() const noexcept
149 { return const_reverse_iterator(this->begin()); }
150
151 // [string.view.capacity], capacity
152
153 constexpr size_type
154 size() const noexcept
155 { return this->_M_len; }
156
157 constexpr size_type
158 length() const noexcept
159 { return _M_len; }
160
161 constexpr size_type
162 max_size() const noexcept
163 {
164 return (npos - sizeof(size_type) - sizeof(void*))
165 / sizeof(value_type) / 4;
166 }
167
168 constexpr bool
169 empty() const noexcept
170 { return this->_M_len == 0; }
171
172 // [string.view.access], element access
173
174 constexpr const _CharT&
175 operator[](size_type __pos) const
176 {
177 // TODO: Assert to restore in a way compatible with the constexpr.
178 // __glibcxx_assert(__pos < this->_M_len);
179 return *(this->_M_str + __pos);
180 }
181
182 constexpr const _CharT&
183 at(size_type __pos) const
184 {
185 return __pos < this->_M_len
186 ? *(this->_M_str + __pos)
187 : (error (_("basic_string_view::at: __pos "
188 "(which is %zu) >= this->size() "
189 "(which is %zu)"),
190 __pos, this->size()),
191 *this->_M_str);
192 }
193
194 constexpr const _CharT&
195 front() const
196 {
197 // TODO: Assert to restore in a way compatible with the constexpr.
198 // __glibcxx_assert(this->_M_len > 0);
199 return *this->_M_str;
200 }
201
202 constexpr const _CharT&
203 back() const
204 {
205 // TODO: Assert to restore in a way compatible with the constexpr.
206 // __glibcxx_assert(this->_M_len > 0);
207 return *(this->_M_str + this->_M_len - 1);
208 }
209
210 constexpr const _CharT*
211 data() const noexcept
212 { return this->_M_str; }
213
214 // [string.view.modifiers], modifiers:
215
216 /*constexpr*/ void
217 remove_prefix(size_type __n)
218 {
219 gdb_assert (this->_M_len >= __n);
220 this->_M_str += __n;
221 this->_M_len -= __n;
222 }
223
224 /*constexpr*/ void
225 remove_suffix(size_type __n)
226 { this->_M_len -= __n; }
227
228 /*constexpr*/ void
229 swap(basic_string_view& __sv) noexcept
230 {
231 auto __tmp = *this;
232 *this = __sv;
233 __sv = __tmp;
234 }
235
236
237 // [string.view.ops], string operations:
238
239 template<typename _Allocator>
240 explicit operator std::basic_string<_CharT, _Traits, _Allocator>() const
241 {
242 return { this->_M_str, this->_M_len };
243 }
244
245 template<typename _Allocator = std::allocator<_CharT>>
246 std::basic_string<_CharT, _Traits, _Allocator>
247 to_string(const _Allocator& __alloc = _Allocator()) const
248 {
249 return { this->_M_str, this->_M_len, __alloc };
250 }
251
252 size_type
253 copy(_CharT* __str, size_type __n, size_type __pos = 0) const
254 {
255 gdb_assert (__str != nullptr || __n == 0);
256 if (__pos > this->_M_len)
257 error (_("basic_string_view::copy: __pos "
258 "(which is %zu) > this->size() "
259 "(which is %zu)"),
260 __pos, this->size());
261 size_type __rlen{std::min(__n, size_type{this->_M_len - __pos})};
262 for (auto __begin = this->_M_str + __pos,
263 __end = __begin + __rlen; __begin != __end;)
264 *__str++ = *__begin++;
265 return __rlen;
266 }
267
268
269 // [string.view.ops], string operations:
270
271 /*constexpr*/ basic_string_view
272 substr(size_type __pos, size_type __n=npos) const
273 {
274 return __pos <= this->_M_len
275 ? basic_string_view{this->_M_str + __pos,
276 std::min(__n, size_type{this->_M_len - __pos})}
277 : (error (_("basic_string_view::substr: __pos "
278 "(which is %zu) > this->size() "
279 "(which is %zu)"),
280 __pos, this->size()), basic_string_view{});
281 }
282
283 /*constexpr*/ int
284 compare(basic_string_view __str) const noexcept
285 {
286 int __ret = traits_type::compare(this->_M_str, __str._M_str,
287 std::min(this->_M_len, __str._M_len));
288 if (__ret == 0)
289 __ret = _S_compare(this->_M_len, __str._M_len);
290 return __ret;
291 }
292
293 /*constexpr*/ int
294 compare(size_type __pos1, size_type __n1, basic_string_view __str) const
295 { return this->substr(__pos1, __n1).compare(__str); }
296
297 /*constexpr*/ int
298 compare(size_type __pos1, size_type __n1,
299 basic_string_view __str, size_type __pos2, size_type __n2) const
300 { return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); }
301
302 /*constexpr*/ int
303 compare(const _CharT* __str) const noexcept
304 { return this->compare(basic_string_view{__str}); }
305
306 /*constexpr*/ int
307 compare(size_type __pos1, size_type __n1, const _CharT* __str) const
308 { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
309
310 /*constexpr*/ int
311 compare(size_type __pos1, size_type __n1,
312 const _CharT* __str, size_type __n2) const
313 {
314 return this->substr(__pos1, __n1)
315 .compare(basic_string_view(__str, __n2));
316 }
317
318 /*constexpr*/ size_type
319 find(basic_string_view __str, size_type __pos = 0) const noexcept
320 { return this->find(__str._M_str, __pos, __str._M_len); }
321
322 /*constexpr*/ size_type
323 find(_CharT __c, size_type __pos=0) const noexcept;
324
325 /*constexpr*/ size_type
326 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
327
328 /*constexpr*/ size_type
329 find(const _CharT* __str, size_type __pos=0) const noexcept
330 { return this->find(__str, __pos, traits_type::length(__str)); }
331
332 /*constexpr*/ size_type
333 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
334 { return this->rfind(__str._M_str, __pos, __str._M_len); }
335
336 /*constexpr*/ size_type
337 rfind(_CharT __c, size_type __pos = npos) const noexcept;
338
339 /*constexpr*/ size_type
340 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
341
342 /*constexpr*/ size_type
343 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
344 { return this->rfind(__str, __pos, traits_type::length(__str)); }
345
346 /*constexpr*/ size_type
347 find_first_of(basic_string_view __str, size_type __pos = 0) const noexcept
348 { return this->find_first_of(__str._M_str, __pos, __str._M_len); }
349
350 /*constexpr*/ size_type
351 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
352 { return this->find(__c, __pos); }
353
354 /*constexpr*/ size_type
355 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
356
357 /*constexpr*/ size_type
358 find_first_of(const _CharT* __str, size_type __pos = 0) const noexcept
359 { return this->find_first_of(__str, __pos, traits_type::length(__str)); }
360
361 /*constexpr*/ size_type
362 find_last_of(basic_string_view __str,
363 size_type __pos = npos) const noexcept
364 { return this->find_last_of(__str._M_str, __pos, __str._M_len); }
365
366 size_type
367 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
368 { return this->rfind(__c, __pos); }
369
370 /*constexpr*/ size_type
371 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
372
373 /*constexpr*/ size_type
374 find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
375 { return this->find_last_of(__str, __pos, traits_type::length(__str)); }
376
377 /*constexpr*/ size_type
378 find_first_not_of(basic_string_view __str,
379 size_type __pos = 0) const noexcept
380 { return this->find_first_not_of(__str._M_str, __pos, __str._M_len); }
381
382 /*constexpr*/ size_type
383 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
384
385 /*constexpr*/ size_type
386 find_first_not_of(const _CharT* __str,
387 size_type __pos, size_type __n) const;
388
389 /*constexpr*/ size_type
390 find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
391 {
392 return this->find_first_not_of(__str, __pos,
393 traits_type::length(__str));
394 }
395
396 /*constexpr*/ size_type
397 find_last_not_of(basic_string_view __str,
398 size_type __pos = npos) const noexcept
399 { return this->find_last_not_of(__str._M_str, __pos, __str._M_len); }
400
401 /*constexpr*/ size_type
402 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
403
404 /*constexpr*/ size_type
405 find_last_not_of(const _CharT* __str,
406 size_type __pos, size_type __n) const;
407
408 /*constexpr*/ size_type
409 find_last_not_of(const _CharT* __str,
410 size_type __pos = npos) const noexcept
411 {
412 return this->find_last_not_of(__str, __pos,
413 traits_type::length(__str));
414 }
415
416 private:
417
418 static constexpr int
419 _S_compare(size_type __n1, size_type __n2) noexcept
420 {
421 return difference_type(__n1 - __n2) > std::numeric_limits<int>::max()
422 ? std::numeric_limits<int>::max()
423 : difference_type(__n1 - __n2) < std::numeric_limits<int>::min()
424 ? std::numeric_limits<int>::min()
425 : static_cast<int>(difference_type(__n1 - __n2));
426 }
427
428 size_t _M_len;
429 const _CharT* _M_str;
430 };
431
432 // [string.view.comparison], non-member basic_string_view comparison functions
433
434 namespace __detail
435 {
436 // Identity transform to create a non-deduced context, so that only one
437 // argument participates in template argument deduction and the other
438 // argument gets implicitly converted to the deduced type. See n3766.html.
439 template<typename _Tp>
440 using __idt = typename std::common_type<_Tp>::type;
441 }
442
443 template<typename _CharT, typename _Traits>
444 /*constexpr*/ bool
445 operator==(basic_string_view<_CharT, _Traits> __x,
446 basic_string_view<_CharT, _Traits> __y) noexcept
447 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
448
449 template<typename _CharT, typename _Traits>
450 /*constexpr*/ bool
451 operator==(basic_string_view<_CharT, _Traits> __x,
452 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
453 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
454
455 template<typename _CharT, typename _Traits>
456 /*constexpr*/ bool
457 operator==(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
458 basic_string_view<_CharT, _Traits> __y) noexcept
459 { return __x.size() == __y.size() && __x.compare(__y) == 0; }
460
461 template<typename _CharT, typename _Traits>
462 /*constexpr*/ bool
463 operator!=(basic_string_view<_CharT, _Traits> __x,
464 basic_string_view<_CharT, _Traits> __y) noexcept
465 { return !(__x == __y); }
466
467 template<typename _CharT, typename _Traits>
468 /*constexpr*/ bool
469 operator!=(basic_string_view<_CharT, _Traits> __x,
470 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
471 { return !(__x == __y); }
472
473 template<typename _CharT, typename _Traits>
474 /*constexpr*/ bool
475 operator!=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
476 basic_string_view<_CharT, _Traits> __y) noexcept
477 { return !(__x == __y); }
478
479 template<typename _CharT, typename _Traits>
480 /*constexpr*/ bool
481 operator< (basic_string_view<_CharT, _Traits> __x,
482 basic_string_view<_CharT, _Traits> __y) noexcept
483 { return __x.compare(__y) < 0; }
484
485 template<typename _CharT, typename _Traits>
486 /*constexpr*/ bool
487 operator< (basic_string_view<_CharT, _Traits> __x,
488 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
489 { return __x.compare(__y) < 0; }
490
491 template<typename _CharT, typename _Traits>
492 /*constexpr*/ bool
493 operator< (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
494 basic_string_view<_CharT, _Traits> __y) noexcept
495 { return __x.compare(__y) < 0; }
496
497 template<typename _CharT, typename _Traits>
498 /*constexpr*/ bool
499 operator> (basic_string_view<_CharT, _Traits> __x,
500 basic_string_view<_CharT, _Traits> __y) noexcept
501 { return __x.compare(__y) > 0; }
502
503 template<typename _CharT, typename _Traits>
504 /*constexpr*/ bool
505 operator> (basic_string_view<_CharT, _Traits> __x,
506 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
507 { return __x.compare(__y) > 0; }
508
509 template<typename _CharT, typename _Traits>
510 /*constexpr*/ bool
511 operator> (__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
512 basic_string_view<_CharT, _Traits> __y) noexcept
513 { return __x.compare(__y) > 0; }
514
515 template<typename _CharT, typename _Traits>
516 /*constexpr*/ bool
517 operator<=(basic_string_view<_CharT, _Traits> __x,
518 basic_string_view<_CharT, _Traits> __y) noexcept
519 { return __x.compare(__y) <= 0; }
520
521 template<typename _CharT, typename _Traits>
522 /*constexpr*/ bool
523 operator<=(basic_string_view<_CharT, _Traits> __x,
524 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
525 { return __x.compare(__y) <= 0; }
526
527 template<typename _CharT, typename _Traits>
528 /*constexpr*/ bool
529 operator<=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
530 basic_string_view<_CharT, _Traits> __y) noexcept
531 { return __x.compare(__y) <= 0; }
532
533 template<typename _CharT, typename _Traits>
534 /*constexpr*/ bool
535 operator>=(basic_string_view<_CharT, _Traits> __x,
536 basic_string_view<_CharT, _Traits> __y) noexcept
537 { return __x.compare(__y) >= 0; }
538
539 template<typename _CharT, typename _Traits>
540 /*constexpr*/ bool
541 operator>=(basic_string_view<_CharT, _Traits> __x,
542 __detail::__idt<basic_string_view<_CharT, _Traits>> __y) noexcept
543 { return __x.compare(__y) >= 0; }
544
545 template<typename _CharT, typename _Traits>
546 /*constexpr*/ bool
547 operator>=(__detail::__idt<basic_string_view<_CharT, _Traits>> __x,
548 basic_string_view<_CharT, _Traits> __y) noexcept
549 { return __x.compare(__y) >= 0; }
550
551 // basic_string_view typedef names
552
553 using string_view = basic_string_view<char>;
554 } /* namespace gdb */
555
556 #include "gdb_string_view.tcc"
557
558 #endif // __cplusplus < 201703L
559
560 #endif /* GDB_STRING_VIEW_H */
This page took 0.056866 seconds and 4 git commands to generate.