Update copyright year range in all GDB files.
[deliverable/binutils-gdb.git] / gdb / common / gdb_string_view.h
CommitLineData
7adcdf08
SM
1// Components for manipulating non-owning sequences of characters -*- C++ -*-
2
8345c4a2
SM
3// Note: This file has been stolen from the gcc repo
4// (libstdc++-v3/include/experimental/string_view) and has local modifications.
5
42a4f53d 6// Copyright (C) 2013-2019 Free Software Foundation, Inc.
7adcdf08
SM
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
7adcdf08
SM
28//
29// N3762 basic_string_view library
30//
31
8345c4a2
SM
32#ifndef GDB_STRING_VIEW_H
33#define GDB_STRING_VIEW_H 1
34
35#if __cplusplus >= 201703L
7adcdf08 36
8345c4a2 37#include <string_view>
7adcdf08 38
8345c4a2
SM
39namespace gdb {
40 using string_view = std::string_view;
41} /* namespace gdb */
42
43#else /* __cplusplus < 201703L */
7adcdf08
SM
44
45#include <string>
46#include <limits>
7adcdf08 47
8345c4a2 48namespace gdb {
7adcdf08
SM
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>
8345c4a2 99 basic_string_view(const std::basic_string<_CharT, _Traits,
7adcdf08
SM
100 _Allocator>& __str) noexcept
101 : _M_len{__str.length()}, _M_str{__str.data()}
102 { }
103
8345c4a2 104 /*constexpr*/ basic_string_view(const _CharT* __str)
7adcdf08
SM
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)
8345c4a2
SM
187 : (error (_("basic_string_view::at: __pos "
188 "(which is %zu) >= this->size() "
189 "(which is %zu)"),
190 __pos, this->size()),
7adcdf08
SM
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
8345c4a2 216 /*constexpr*/ void
7adcdf08
SM
217 remove_prefix(size_type __n)
218 {
8345c4a2 219 gdb_assert (this->_M_len >= __n);
7adcdf08
SM
220 this->_M_str += __n;
221 this->_M_len -= __n;
222 }
223
8345c4a2 224 /*constexpr*/ void
7adcdf08
SM
225 remove_suffix(size_type __n)
226 { this->_M_len -= __n; }
227
8345c4a2 228 /*constexpr*/ void
7adcdf08
SM
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>
8345c4a2 240 explicit operator std::basic_string<_CharT, _Traits, _Allocator>() const
7adcdf08
SM
241 {
242 return { this->_M_str, this->_M_len };
243 }
244
245 template<typename _Allocator = std::allocator<_CharT>>
8345c4a2 246 std::basic_string<_CharT, _Traits, _Allocator>
7adcdf08
SM
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 {
8345c4a2 255 gdb_assert (__str != nullptr || __n == 0);
7adcdf08 256 if (__pos > this->_M_len)
8345c4a2
SM
257 error (_("basic_string_view::copy: __pos "
258 "(which is %zu) > this->size() "
259 "(which is %zu)"),
260 __pos, this->size());
7adcdf08
SM
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
8345c4a2 271 /*constexpr*/ basic_string_view
7adcdf08
SM
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})}
8345c4a2
SM
277 : (error (_("basic_string_view::substr: __pos "
278 "(which is %zu) > this->size() "
279 "(which is %zu)"),
280 __pos, this->size()), basic_string_view{});
7adcdf08
SM
281 }
282
8345c4a2 283 /*constexpr*/ int
7adcdf08
SM
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
8345c4a2 293 /*constexpr*/ int
7adcdf08
SM
294 compare(size_type __pos1, size_type __n1, basic_string_view __str) const
295 { return this->substr(__pos1, __n1).compare(__str); }
296
8345c4a2 297 /*constexpr*/ int
7adcdf08
SM
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
8345c4a2 302 /*constexpr*/ int
7adcdf08
SM
303 compare(const _CharT* __str) const noexcept
304 { return this->compare(basic_string_view{__str}); }
305
8345c4a2 306 /*constexpr*/ int
7adcdf08
SM
307 compare(size_type __pos1, size_type __n1, const _CharT* __str) const
308 { return this->substr(__pos1, __n1).compare(basic_string_view{__str}); }
309
8345c4a2 310 /*constexpr*/ int
7adcdf08
SM
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
8345c4a2 318 /*constexpr*/ size_type
7adcdf08
SM
319 find(basic_string_view __str, size_type __pos = 0) const noexcept
320 { return this->find(__str._M_str, __pos, __str._M_len); }
321
8345c4a2 322 /*constexpr*/ size_type
7adcdf08
SM
323 find(_CharT __c, size_type __pos=0) const noexcept;
324
8345c4a2 325 /*constexpr*/ size_type
7adcdf08
SM
326 find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
327
8345c4a2 328 /*constexpr*/ size_type
7adcdf08
SM
329 find(const _CharT* __str, size_type __pos=0) const noexcept
330 { return this->find(__str, __pos, traits_type::length(__str)); }
331
8345c4a2 332 /*constexpr*/ size_type
7adcdf08
SM
333 rfind(basic_string_view __str, size_type __pos = npos) const noexcept
334 { return this->rfind(__str._M_str, __pos, __str._M_len); }
335
8345c4a2 336 /*constexpr*/ size_type
7adcdf08
SM
337 rfind(_CharT __c, size_type __pos = npos) const noexcept;
338
8345c4a2 339 /*constexpr*/ size_type
7adcdf08
SM
340 rfind(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
341
8345c4a2 342 /*constexpr*/ size_type
7adcdf08
SM
343 rfind(const _CharT* __str, size_type __pos = npos) const noexcept
344 { return this->rfind(__str, __pos, traits_type::length(__str)); }
345
8345c4a2 346 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 350 /*constexpr*/ size_type
7adcdf08
SM
351 find_first_of(_CharT __c, size_type __pos = 0) const noexcept
352 { return this->find(__c, __pos); }
353
8345c4a2 354 /*constexpr*/ size_type
7adcdf08
SM
355 find_first_of(const _CharT* __str, size_type __pos, size_type __n) const;
356
8345c4a2 357 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 361 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 366 size_type
7adcdf08
SM
367 find_last_of(_CharT __c, size_type __pos=npos) const noexcept
368 { return this->rfind(__c, __pos); }
369
8345c4a2 370 /*constexpr*/ size_type
7adcdf08
SM
371 find_last_of(const _CharT* __str, size_type __pos, size_type __n) const;
372
8345c4a2 373 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 377 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 382 /*constexpr*/ size_type
7adcdf08
SM
383 find_first_not_of(_CharT __c, size_type __pos = 0) const noexcept;
384
8345c4a2 385 /*constexpr*/ size_type
7adcdf08
SM
386 find_first_not_of(const _CharT* __str,
387 size_type __pos, size_type __n) const;
388
8345c4a2 389 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 396 /*constexpr*/ size_type
7adcdf08
SM
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
8345c4a2 401 /*constexpr*/ size_type
7adcdf08
SM
402 find_last_not_of(_CharT __c, size_type __pos = npos) const noexcept;
403
8345c4a2 404 /*constexpr*/ size_type
7adcdf08
SM
405 find_last_not_of(const _CharT* __str,
406 size_type __pos, size_type __n) const;
407
8345c4a2 408 /*constexpr*/ size_type
7adcdf08
SM
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>
8345c4a2 440 using __idt = typename std::common_type<_Tp>::type;
7adcdf08
SM
441 }
442
443 template<typename _CharT, typename _Traits>
8345c4a2 444 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 450 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 456 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 462 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 468 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 474 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 480 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 486 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 492 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 498 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 504 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 510 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 516 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 522 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 528 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 534 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 540 /*constexpr*/ bool
7adcdf08
SM
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>
8345c4a2 546 /*constexpr*/ bool
7adcdf08
SM
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
7adcdf08
SM
551 // basic_string_view typedef names
552
553 using string_view = basic_string_view<char>;
8345c4a2 554} /* namespace gdb */
7adcdf08 555
8345c4a2 556#include "gdb_string_view.tcc"
7adcdf08 557
8345c4a2 558#endif // __cplusplus < 201703L
7adcdf08 559
8345c4a2 560#endif /* GDB_STRING_VIEW_H */
This page took 0.097445 seconds and 4 git commands to generate.