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