1 // Components for manipulating non-owning sequences of characters -*- C++ -*-
3 // Note: This file has been stolen from the gcc repo
4 // (libstdc++-v3/include/experimental/string_view) and has local modifications.
6 // Copyright (C) 2013-2018 Free Software Foundation, Inc.
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)
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.
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.
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/>.
29 // N3762 basic_string_view library
32 #ifndef GDB_STRING_VIEW_H
33 #define GDB_STRING_VIEW_H 1
35 #if __cplusplus >= 201703L
37 #include <string_view>
40 using string_view
= std::string_view
;
43 #else /* __cplusplus < 201703L */
51 * @class basic_string_view <experimental/string_view>
52 * @brief A non-owning reference to a string.
56 * @ingroup experimental
58 * @tparam _CharT Type of character
59 * @tparam _Traits Traits for character type, defaults to
60 * char_traits<_CharT>.
62 * A basic_string_view looks like this:
69 template<typename _CharT
, typename _Traits
= std::char_traits
<_CharT
>>
70 class basic_string_view
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);
89 // [string.view.cons], construct/copy
92 basic_string_view() noexcept
93 : _M_len
{0}, _M_str
{nullptr}
96 constexpr basic_string_view(const basic_string_view
&) noexcept
= default;
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()}
104 /*constexpr*/ basic_string_view(const _CharT
* __str
)
105 : _M_len
{__str
== nullptr ? 0 : traits_type::length(__str
)},
109 constexpr basic_string_view(const _CharT
* __str
, size_type __len
)
115 operator=(const basic_string_view
&) noexcept
= default;
117 // [string.view.iterators], iterators
119 constexpr const_iterator
120 begin() const noexcept
121 { return this->_M_str
; }
123 constexpr const_iterator
125 { return this->_M_str
+ this->_M_len
; }
127 constexpr const_iterator
128 cbegin() const noexcept
129 { return this->_M_str
; }
131 constexpr const_iterator
132 cend() const noexcept
133 { return this->_M_str
+ this->_M_len
; }
135 const_reverse_iterator
136 rbegin() const noexcept
137 { return const_reverse_iterator(this->end()); }
139 const_reverse_iterator
140 rend() const noexcept
141 { return const_reverse_iterator(this->begin()); }
143 const_reverse_iterator
144 crbegin() const noexcept
145 { return const_reverse_iterator(this->end()); }
147 const_reverse_iterator
148 crend() const noexcept
149 { return const_reverse_iterator(this->begin()); }
151 // [string.view.capacity], capacity
154 size() const noexcept
155 { return this->_M_len
; }
158 length() const noexcept
162 max_size() const noexcept
164 return (npos
- sizeof(size_type
) - sizeof(void*))
165 / sizeof(value_type
) / 4;
169 empty() const noexcept
170 { return this->_M_len
== 0; }
172 // [string.view.access], element access
174 constexpr const _CharT
&
175 operator[](size_type __pos
) const
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
);
182 constexpr const _CharT
&
183 at(size_type __pos
) const
185 return __pos
< this->_M_len
186 ? *(this->_M_str
+ __pos
)
187 : (error (_("basic_string_view::at: __pos "
188 "(which is %zu) >= this->size() "
190 __pos
, this->size()),
194 constexpr const _CharT
&
197 // TODO: Assert to restore in a way compatible with the constexpr.
198 // __glibcxx_assert(this->_M_len > 0);
199 return *this->_M_str
;
202 constexpr const _CharT
&
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);
210 constexpr const _CharT
*
211 data() const noexcept
212 { return this->_M_str
; }
214 // [string.view.modifiers], modifiers:
217 remove_prefix(size_type __n
)
219 gdb_assert (this->_M_len
>= __n
);
225 remove_suffix(size_type __n
)
226 { this->_M_len
-= __n
; }
229 swap(basic_string_view
& __sv
) noexcept
237 // [string.view.ops], string operations:
239 template<typename _Allocator
>
240 explicit operator std::basic_string
<_CharT
, _Traits
, _Allocator
>() const
242 return { this->_M_str
, this->_M_len
};
245 template<typename _Allocator
= std::allocator
<_CharT
>>
246 std::basic_string
<_CharT
, _Traits
, _Allocator
>
247 to_string(const _Allocator
& __alloc
= _Allocator()) const
249 return { this->_M_str
, this->_M_len
, __alloc
};
253 copy(_CharT
* __str
, size_type __n
, size_type __pos
= 0) const
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() "
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
++;
269 // [string.view.ops], string operations:
271 /*constexpr*/ basic_string_view
272 substr(size_type __pos
, size_type __n
=npos
) const
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() "
280 __pos
, this->size()), basic_string_view
{});
284 compare(basic_string_view __str
) const noexcept
286 int __ret
= traits_type::compare(this->_M_str
, __str
._M_str
,
287 std::min(this->_M_len
, __str
._M_len
));
289 __ret
= _S_compare(this->_M_len
, __str
._M_len
);
294 compare(size_type __pos1
, size_type __n1
, basic_string_view __str
) const
295 { return this->substr(__pos1
, __n1
).compare(__str
); }
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
)); }
303 compare(const _CharT
* __str
) const noexcept
304 { return this->compare(basic_string_view
{__str
}); }
307 compare(size_type __pos1
, size_type __n1
, const _CharT
* __str
) const
308 { return this->substr(__pos1
, __n1
).compare(basic_string_view
{__str
}); }
311 compare(size_type __pos1
, size_type __n1
,
312 const _CharT
* __str
, size_type __n2
) const
314 return this->substr(__pos1
, __n1
)
315 .compare(basic_string_view(__str
, __n2
));
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
); }
322 /*constexpr*/ size_type
323 find(_CharT __c
, size_type __pos
=0) const noexcept
;
325 /*constexpr*/ size_type
326 find(const _CharT
* __str
, size_type __pos
, size_type __n
) const noexcept
;
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
)); }
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
); }
336 /*constexpr*/ size_type
337 rfind(_CharT __c
, size_type __pos
= npos
) const noexcept
;
339 /*constexpr*/ size_type
340 rfind(const _CharT
* __str
, size_type __pos
, size_type __n
) const noexcept
;
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
)); }
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
); }
350 /*constexpr*/ size_type
351 find_first_of(_CharT __c
, size_type __pos
= 0) const noexcept
352 { return this->find(__c
, __pos
); }
354 /*constexpr*/ size_type
355 find_first_of(const _CharT
* __str
, size_type __pos
, size_type __n
) const;
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
)); }
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
); }
367 find_last_of(_CharT __c
, size_type __pos
=npos
) const noexcept
368 { return this->rfind(__c
, __pos
); }
370 /*constexpr*/ size_type
371 find_last_of(const _CharT
* __str
, size_type __pos
, size_type __n
) const;
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
)); }
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
); }
382 /*constexpr*/ size_type
383 find_first_not_of(_CharT __c
, size_type __pos
= 0) const noexcept
;
385 /*constexpr*/ size_type
386 find_first_not_of(const _CharT
* __str
,
387 size_type __pos
, size_type __n
) const;
389 /*constexpr*/ size_type
390 find_first_not_of(const _CharT
* __str
, size_type __pos
= 0) const noexcept
392 return this->find_first_not_of(__str
, __pos
,
393 traits_type::length(__str
));
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
); }
401 /*constexpr*/ size_type
402 find_last_not_of(_CharT __c
, size_type __pos
= npos
) const noexcept
;
404 /*constexpr*/ size_type
405 find_last_not_of(const _CharT
* __str
,
406 size_type __pos
, size_type __n
) const;
408 /*constexpr*/ size_type
409 find_last_not_of(const _CharT
* __str
,
410 size_type __pos
= npos
) const noexcept
412 return this->find_last_not_of(__str
, __pos
,
413 traits_type::length(__str
));
419 _S_compare(size_type __n1
, size_type __n2
) noexcept
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
));
429 const _CharT
* _M_str
;
432 // [string.view.comparison], non-member basic_string_view comparison functions
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
;
443 template<typename _CharT
, typename _Traits
>
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; }
449 template<typename _CharT
, typename _Traits
>
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; }
455 template<typename _CharT
, typename _Traits
>
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; }
461 template<typename _CharT
, typename _Traits
>
463 operator!=(basic_string_view
<_CharT
, _Traits
> __x
,
464 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
465 { return !(__x
== __y
); }
467 template<typename _CharT
, typename _Traits
>
469 operator!=(basic_string_view
<_CharT
, _Traits
> __x
,
470 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
471 { return !(__x
== __y
); }
473 template<typename _CharT
, typename _Traits
>
475 operator!=(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
476 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
477 { return !(__x
== __y
); }
479 template<typename _CharT
, typename _Traits
>
481 operator< (basic_string_view
<_CharT
, _Traits
> __x
,
482 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
483 { return __x
.compare(__y
) < 0; }
485 template<typename _CharT
, typename _Traits
>
487 operator< (basic_string_view
<_CharT
, _Traits
> __x
,
488 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
489 { return __x
.compare(__y
) < 0; }
491 template<typename _CharT
, typename _Traits
>
493 operator< (__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
494 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
495 { return __x
.compare(__y
) < 0; }
497 template<typename _CharT
, typename _Traits
>
499 operator> (basic_string_view
<_CharT
, _Traits
> __x
,
500 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
501 { return __x
.compare(__y
) > 0; }
503 template<typename _CharT
, typename _Traits
>
505 operator> (basic_string_view
<_CharT
, _Traits
> __x
,
506 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
507 { return __x
.compare(__y
) > 0; }
509 template<typename _CharT
, typename _Traits
>
511 operator> (__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
512 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
513 { return __x
.compare(__y
) > 0; }
515 template<typename _CharT
, typename _Traits
>
517 operator<=(basic_string_view
<_CharT
, _Traits
> __x
,
518 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
519 { return __x
.compare(__y
) <= 0; }
521 template<typename _CharT
, typename _Traits
>
523 operator<=(basic_string_view
<_CharT
, _Traits
> __x
,
524 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
525 { return __x
.compare(__y
) <= 0; }
527 template<typename _CharT
, typename _Traits
>
529 operator<=(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
530 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
531 { return __x
.compare(__y
) <= 0; }
533 template<typename _CharT
, typename _Traits
>
535 operator>=(basic_string_view
<_CharT
, _Traits
> __x
,
536 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
537 { return __x
.compare(__y
) >= 0; }
539 template<typename _CharT
, typename _Traits
>
541 operator>=(basic_string_view
<_CharT
, _Traits
> __x
,
542 __detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __y
) noexcept
543 { return __x
.compare(__y
) >= 0; }
545 template<typename _CharT
, typename _Traits
>
547 operator>=(__detail::__idt
<basic_string_view
<_CharT
, _Traits
>> __x
,
548 basic_string_view
<_CharT
, _Traits
> __y
) noexcept
549 { return __x
.compare(__y
) >= 0; }
551 // basic_string_view typedef names
553 using string_view
= basic_string_view
<char>;
554 } /* namespace gdb */
556 #include "gdb_string_view.tcc"
558 #endif // __cplusplus < 201703L
560 #endif /* GDB_STRING_VIEW_H */
This page took 0.064514 seconds and 4 git commands to generate.