1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
4 #ifndef COMMON_GDB_STRING_VIEW_H
5 #define COMMON_GDB_STRING_VIEW_H
7 // Note: This file has been stolen from the gcc repo
8 // (libstdc++-v3/include/experimental/string_view) and has local modifications.
10 // Copyright (C) 2013-2019 Free Software Foundation, Inc.
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)
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.
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.
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/>.
33 // N3762 basic_string_view library
37 #if __cplusplus >= 201703L
39 #include <string_view>
42 using string_view
= std::string_view
;
45 #else /* __cplusplus < 201703L */
53 * @class basic_string_view <experimental/string_view>
54 * @brief A non-owning reference to a string.
58 * @ingroup experimental
60 * @tparam _CharT Type of character
61 * @tparam _Traits Traits for character type, defaults to
62 * char_traits<_CharT>.
64 * A basic_string_view looks like this:
71 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>>
72 class basic_string_view
77 using traits_type
= _Traits
;
78 using value_type
= _CharT
;
79 using pointer
= const _CharT
*;
80 using const_pointer
= const _CharT
*;
81 using reference
= const _CharT
&;
82 using const_reference
= const _CharT
&;
83 using const_iterator
= const _CharT
*;
84 using iterator
= const_iterator
;
85 using const_reverse_iterator
= std::reverse_iterator
<const_iterator
>;
86 using reverse_iterator
= const_reverse_iterator
;
87 using size_type
= size_t;
88 using difference_type
= ptrdiff_t;
89 static constexpr size_type npos
= size_type(-1);
91 // [string.view.cons], construct/copy
94 basic_string_view() noexcept
95 : _M_len
{0}, _M_str
{nullptr}
98 constexpr basic_string_view(const basic_string_view
&) noexcept
= default;
100 template<typename _Allocator
>
101 basic_string_view(const std::basic_string
<_CharT
, _Traits
,
102 _Allocator
>& __str
) noexcept
103 : _M_len
{__str
.length()}, _M_str
{__str
.data()}
106 /*constexpr*/ basic_string_view(const _CharT
* __str
)
107 : _M_len
{__str
== nullptr ? 0 : traits_type::length(__str
)},
111 constexpr basic_string_view(const _CharT
* __str
, size_type __len
)
117 operator=(const basic_string_view
&) noexcept
= default;
119 // [string.view.iterators], iterators
121 constexpr const_iterator
122 begin() const noexcept
123 { return this->_M_str
; }
125 constexpr const_iterator
127 { return this->_M_str
+ this->_M_len
; }
129 constexpr const_iterator
130 cbegin() const noexcept
131 { return this->_M_str
; }
133 constexpr const_iterator
134 cend() const noexcept
135 { return this->_M_str
+ this->_M_len
; }
137 const_reverse_iterator
138 rbegin() const noexcept
139 { return const_reverse_iterator(this->end()); }
141 const_reverse_iterator
142 rend() const noexcept
143 { return const_reverse_iterator(this->begin()); }
145 const_reverse_iterator
146 crbegin() const noexcept
147 { return const_reverse_iterator(this->end()); }
149 const_reverse_iterator
150 crend() const noexcept
151 { return const_reverse_iterator(this->begin()); }
153 // [string.view.capacity], capacity
156 size() const noexcept
157 { return this->_M_len
; }
160 length() const noexcept
164 max_size() const noexcept
166 return (npos
- sizeof(size_type
) - sizeof(void*))
167 / sizeof(value_type
) / 4;
171 empty() const noexcept
172 { return this->_M_len
== 0; }
174 // [string.view.access], element access
176 constexpr const _CharT
&
177 operator[](size_type __pos
) const
179 // TODO: Assert to restore in a way compatible with the constexpr.
180 // __glibcxx_assert(__pos < this->_M_len);
181 return *(this->_M_str
+ __pos
);
184 constexpr const _CharT
&
185 at(size_type __pos
) const
187 return __pos
< this->_M_len
188 ? *(this->_M_str
+ __pos
)
189 : (error (_("basic_string_view::at: __pos "
190 "(which is %zu) >= this->size() "
192 __pos
, this->size()),
196 constexpr const _CharT
&
199 // TODO: Assert to restore in a way compatible with the constexpr.
200 // __glibcxx_assert(this->_M_len > 0);
201 return *this->_M_str
;
204 constexpr const _CharT
&
207 // TODO: Assert to restore in a way compatible with the constexpr.
208 // __glibcxx_assert(this->_M_len > 0);
209 return *(this->_M_str
+ this->_M_len
- 1);
212 constexpr const _CharT
*
213 data() const noexcept
214 { return this->_M_str
; }
216 // [string.view.modifiers], modifiers:
219 remove_prefix(size_type __n
)
221 gdb_assert (this->_M_len
>= __n
);
227 remove_suffix(size_type __n
)
228 { this->_M_len
-= __n
; }
231 swap(basic_string_view
& __sv
) noexcept
239 // [string.view.ops], string operations:
241 template<typename _Allocator
>
242 explicit operator std::basic_string
<_CharT
, _Traits
, _Allocator
>() const
244 return { this->_M_str
, this->_M_len
};
247 template<typename _Allocator
= std::allocator
<_CharT
>>
248 std::basic_string
<_CharT
, _Traits
, _Allocator
>
249 to_string(const _Allocator
& __alloc
= _Allocator()) const
251 return { this->_M_str
, this->_M_len
, __alloc
};
255 copy(_CharT
* __str
, size_type __n
, size_type __pos
= 0) const
257 gdb_assert (__str
!= nullptr || __n
== 0);
258 if (__pos
> this->_M_len
)
259 error (_("basic_string_view::copy: __pos "
260 "(which is %zu) > this->size() "
262 __pos
, this->size());
263 size_type __rlen
{std::min(__n
, size_type
{this->_M_len
- __pos
})};
264 for (auto __begin
= this->_M_str
+ __pos
,
265 __end
= __begin
+ __rlen
; __begin
!= __end
;)
266 *__str
++ = *__begin
++;
271 // [string.view.ops], string operations:
273 /*constexpr*/ basic_string_view
274 substr(size_type __pos
, size_type __n
=npos
) const
276 return __pos
<= this->_M_len
277 ? basic_string_view
{this->_M_str
+ __pos
,
278 std::min(__n
, size_type
{this->_M_len
- __pos
})}
279 : (error (_("basic_string_view::substr: __pos "
280 "(which is %zu) > this->size() "
282 __pos
, this->size()), basic_string_view
{});
286 compare(basic_string_view __str
) const noexcept
288 int __ret
= traits_type::compare(this->_M_str
, __str
._M_str
,
289 std::min(this->_M_len
, __str
._M_len
));
291 __ret
= _S_compare(this->_M_len
, __str
._M_len
);
296 compare(size_type __pos1
, size_type __n1
, basic_string_view __str
) const
297 { return this->substr(__pos1
, __n1
).compare(__str
); }
300 compare(size_type __pos1
, size_type __n1
,
301 basic_string_view __str
, size_type __pos2
, size_type __n2
) const
302 { return this->substr(__pos1
, __n1
).compare(__str
.substr(__pos2
, __n2
)); }
305 compare(const _CharT
* __str
) const noexcept
306 { return this->compare(basic_string_view
{__str
}); }
309 compare(size_type __pos1
, size_type __n1
, const _CharT
* __str
) const
310 { return this->substr(__pos1
, __n1
).compare(basic_string_view
{__str
}); }
313 compare(size_type __pos1
, size_type __n1
,
314 const _CharT
* __str
, size_type __n2
) const
316 return this->substr(__pos1
, __n1
)
317 .compare(basic_string_view(__str
, __n2
));
320 /*constexpr*/ size_type
321 find(basic_string_view __str
, size_type __pos
= 0) const noexcept
322 { return this->find(__str
._M_str
, __pos
, __str
._M_len
); }
324 /*constexpr*/ size_type
325 find(_CharT __c
, size_type __pos
=0) const noexcept
;
327 /*constexpr*/ size_type
328 find(const _CharT
* __str
, size_type __pos
, size_type __n
) const noexcept
;
330 /*constexpr*/ size_type
331 find(const _CharT
* __str
, size_type __pos
=0) const noexcept
332 { return this->find(__str
, __pos
, traits_type::length(__str
)); }
334 /*constexpr*/ size_type
335 rfind(basic_string_view __str
, size_type __pos
= npos
) const noexcept
336 { return this->rfind(__str
._M_str
, __pos
, __str
._M_len
); }
338 /*constexpr*/ size_type
339 rfind(_CharT __c
, size_type __pos
= npos
) const noexcept
;
341 /*constexpr*/ size_type
342 rfind(const _CharT
* __str
, size_type __pos
, size_type __n
) const noexcept
;
344 /*constexpr*/ size_type
345 rfind(const _CharT
* __str
, size_type __pos
= npos
) const noexcept
346 { return this->rfind(__str
, __pos
, traits_type::length(__str
)); }
348 /*constexpr*/ size_type
349 find_first_of(basic_string_view __str
, size_type __pos
= 0) const noexcept
350 { return this->find_first_of(__str
._M_str
, __pos
, __str
._M_len
); }
352 /*constexpr*/ size_type
353 find_first_of(_CharT __c
, size_type __pos
= 0) const noexcept
354 { return this->find(__c
, __pos
); }
356 /*constexpr*/ size_type
357 find_first_of(const _CharT
* __str
, size_type __pos
, size_type __n
) const;
359 /*constexpr*/ size_type
360 find_first_of(const _CharT
* __str
, size_type __pos
= 0) const noexcept
361 { return this->find_first_of(__str
, __pos
, traits_type::length(__str
)); }
363 /*constexpr*/ size_type
364 find_last_of(basic_string_view __str
,
365 size_type __pos
= npos
) const noexcept
366 { return this->find_last_of(__str
._M_str
, __pos
, __str
._M_len
); }
369 find_last_of(_CharT __c
, size_type __pos
=npos
) const noexcept
370 { return this->rfind(__c
, __pos
); }
372 /*constexpr*/ size_type
373 find_last_of(const _CharT
* __str
, size_type __pos
, size_type __n
) const;
375 /*constexpr*/ size_type
376 find_last_of(const _CharT
* __str
, size_type __pos
= npos
) const noexcept
377 { return this->find_last_of(__str
, __pos
, traits_type::length(__str
)); }
379 /*constexpr*/ size_type
380 find_first_not_of(basic_string_view __str
,
381 size_type __pos
= 0) const noexcept
382 { return this->find_first_not_of(__str
._M_str
, __pos
, __str
._M_len
); }
384 /*constexpr*/ size_type
385 find_first_not_of(_CharT __c
, size_type __pos
= 0) const noexcept
;
387 /*constexpr*/ size_type
388 find_first_not_of(const _CharT
* __str
,
389 size_type __pos
, size_type __n
) const;
391 /*constexpr*/ size_type
392 find_first_not_of(const _CharT
* __str
, size_type __pos
= 0) const noexcept
394 return this->find_first_not_of(__str
, __pos
,
395 traits_type::length(__str
));
398 /*constexpr*/ size_type
399 find_last_not_of(basic_string_view __str
,
400 size_type __pos
= npos
) const noexcept
401 { return this->find_last_not_of(__str
._M_str
, __pos
, __str
._M_len
); }
403 /*constexpr*/ size_type
404 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const noexcept
;
406 /*constexpr*/ size_type
407 find_last_not_of(const _CharT
* __str
,
408 size_type __pos
, size_type __n
) const;
410 /*constexpr*/ size_type
411 find_last_not_of(const _CharT
* __str
,
412 size_type __pos
= npos
) const noexcept
414 return this->find_last_not_of(__str
, __pos
,
415 traits_type::length(__str
));
421 _S_compare(size_type __n1
, size_type __n2
) noexcept
423 return difference_type(__n1
- __n2
) > std::numeric_limits
<int>::max()
424 ? std::numeric_limits
<int>::max()
425 : difference_type(__n1
- __n2
) < std::numeric_limits
<int>::min()
426 ? std::numeric_limits
<int>::min()
427 : static_cast<int>(difference_type(__n1
- __n2
));
431 const _CharT
* _M_str
;
434 // [string.view.comparison], non-member basic_string_view comparison functions
438 // Identity transform to create a non-deduced context, so that only one
439 // argument participates in template argument deduction and the other
440 // argument gets implicitly converted to the deduced type. See n3766.html.
441 template<typename _Tp
>
442 using __idt
= typename
std::common_type
<_Tp
>::type
;
445 template<typename _CharT
, typename _Traits
>
447 operator==(basic_string_view
<_CharT
, _Traits
> __x
,
448 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
449 { return __x
.size() == __y
.size() && __x
.compare(__y
) == 0; }
451 template<typename _CharT
, typename _Traits
>
453 operator==(basic_string_view
<_CharT
, _Traits
> __x
,
454 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
455 { return __x
.size() == __y
.size() && __x
.compare(__y
) == 0; }
457 template<typename _CharT
, typename _Traits
>
459 operator==(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
460 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
461 { return __x
.size() == __y
.size() && __x
.compare(__y
) == 0; }
463 template<typename _CharT
, typename _Traits
>
465 operator!=(basic_string_view
<_CharT
, _Traits
> __x
,
466 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
467 { return !(__x
== __y
); }
469 template<typename _CharT
, typename _Traits
>
471 operator!=(basic_string_view
<_CharT
, _Traits
> __x
,
472 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
473 { return !(__x
== __y
); }
475 template<typename _CharT
, typename _Traits
>
477 operator!=(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
478 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
479 { return !(__x
== __y
); }
481 template<typename _CharT
, typename _Traits
>
483 operator< (basic_string_view
<_CharT
, _Traits
> __x
,
484 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
485 { return __x
.compare(__y
) < 0; }
487 template<typename _CharT
, typename _Traits
>
489 operator< (basic_string_view
<_CharT
, _Traits
> __x
,
490 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
491 { return __x
.compare(__y
) < 0; }
493 template<typename _CharT
, typename _Traits
>
495 operator< (__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
496 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
497 { return __x
.compare(__y
) < 0; }
499 template<typename _CharT
, typename _Traits
>
501 operator> (basic_string_view
<_CharT
, _Traits
> __x
,
502 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
503 { return __x
.compare(__y
) > 0; }
505 template<typename _CharT
, typename _Traits
>
507 operator> (basic_string_view
<_CharT
, _Traits
> __x
,
508 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
509 { return __x
.compare(__y
) > 0; }
511 template<typename _CharT
, typename _Traits
>
513 operator> (__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
514 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
515 { return __x
.compare(__y
) > 0; }
517 template<typename _CharT
, typename _Traits
>
519 operator<=(basic_string_view
<_CharT
, _Traits
> __x
,
520 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
521 { return __x
.compare(__y
) <= 0; }
523 template<typename _CharT
, typename _Traits
>
525 operator<=(basic_string_view
<_CharT
, _Traits
> __x
,
526 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
527 { return __x
.compare(__y
) <= 0; }
529 template<typename _CharT
, typename _Traits
>
531 operator<=(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
532 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
533 { return __x
.compare(__y
) <= 0; }
535 template<typename _CharT
, typename _Traits
>
537 operator>=(basic_string_view
<_CharT
, _Traits
> __x
,
538 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
539 { return __x
.compare(__y
) >= 0; }
541 template<typename _CharT
, typename _Traits
>
543 operator>=(basic_string_view
<_CharT
, _Traits
> __x
,
544 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
545 { return __x
.compare(__y
) >= 0; }
547 template<typename _CharT
, typename _Traits
>
549 operator>=(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
550 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
551 { return __x
.compare(__y
) >= 0; }
553 // basic_string_view typedef names
555 using string_view
= basic_string_view
<char>;
556 } /* namespace gdb */
558 #include "gdb_string_view.tcc"
560 #endif // __cplusplus < 201703L
562 #endif /* COMMON_GDB_STRING_VIEW_H */
This page took 0.041581 seconds and 4 git commands to generate.