Embedded Template Library 1.0
Loading...
Searching...
No Matches
string_view.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2017 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_STRING_VIEW_INCLUDED
32#define ETL_STRING_VIEW_INCLUDED
33
34#include "platform.h"
35#include "algorithm.h"
36#include "basic_string.h"
37#include "char_traits.h"
38#include "error_handler.h"
39#include "exception.h"
40#include "hash.h"
41#include "integral_limits.h"
42#include "iterator.h"
43#include "memory.h"
44#include "private/minmax_push.h"
45
46#if ETL_USING_STL && ETL_USING_CPP17
47 #include <string_view>
48#endif
49
50#if ETL_USING_STD_OSTREAM
51 #include <ostream>
52#endif
53
54#include <stdint.h>
55
56namespace etl
57{
58 //***************************************************************************
60 //***************************************************************************
61 class string_view_exception : public exception
62 {
63 public:
64
65 string_view_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
66 : exception(reason_, file_name_, line_number_)
67 {
68 }
69 };
70
71 //***************************************************************************
74 //***************************************************************************
75 class string_view_bounds : public string_view_exception
76 {
77 public:
78
79 string_view_bounds(string_type file_name_, numeric_type line_number_)
80 : string_view_exception(ETL_ERROR_TEXT("basic_string_view:bounds", ETL_STRING_VIEW_FILE_ID"A"), file_name_, line_number_)
81 {
82 }
83 };
84
85 //***************************************************************************
88 //***************************************************************************
89 class string_view_uninitialised : public string_view_exception
90 {
91 public:
92
93 string_view_uninitialised(string_type file_name_, numeric_type line_number_)
94 : string_view_exception(ETL_ERROR_TEXT("basic_string_view:uninitialised", ETL_STRING_VIEW_FILE_ID"B"), file_name_, line_number_)
95 {
96 }
97 };
98
99 //***************************************************************************
101 //***************************************************************************
102 class string_view_empty : public string_view_exception
103 {
104 public:
105
106 string_view_empty(string_type file_name_, numeric_type line_number_)
107 : string_view_exception(ETL_ERROR_TEXT("basic_string_view:empty", ETL_STRING_VIEW_FILE_ID"C"), file_name_, line_number_)
108 {
109 }
110 };
111
112 //***************************************************************************
114 //***************************************************************************
115 template <typename T, typename TTraits = etl::char_traits<T> >
117 {
118 public:
119
120 typedef T value_type;
121 typedef TTraits traits_type;
122 typedef size_t size_type;
123 typedef T& reference;
124 typedef const T& const_reference;
125 typedef T* pointer;
126 typedef const T* const_pointer;
127 typedef const T* iterator;
128 typedef const T* const_iterator;
129 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
130
131 enum
132 {
134 };
135
136 //*************************************************************************
138 //*************************************************************************
139 ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
140 : mbegin(ETL_NULLPTR)
141 , mend(ETL_NULLPTR)
142 {
143 }
144
145 //*************************************************************************
147 //*************************************************************************
148 ETL_CONSTEXPR basic_string_view(const etl::ibasic_string<T>& str) ETL_NOEXCEPT
149 : mbegin(str.begin())
150 , mend(str.end())
151 {
152 }
153
154 //*************************************************************************
156 //*************************************************************************
157 ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T* begin_) ETL_NOEXCEPT
158 : mbegin(begin_)
159 , mend(begin_ + TTraits::length(begin_))
160 {
161 }
162
163 //*************************************************************************
165 //*************************************************************************
166 ETL_CONSTEXPR basic_string_view(const T* begin_, const T* end_) ETL_NOEXCEPT
167 : mbegin(begin_)
168 , mend(end_)
169 {
170 }
171
172 //*************************************************************************
174 //*************************************************************************
175 ETL_CONSTEXPR basic_string_view(const T* begin_, size_t size_) ETL_NOEXCEPT
176 : mbegin(begin_)
177 , mend(begin_ + size_)
178 {
179 }
180
181 //*************************************************************************
183 //*************************************************************************
184 ETL_CONSTEXPR basic_string_view(const basic_string_view& other) ETL_NOEXCEPT
185 : mbegin(other.mbegin)
186 , mend(other.mend)
187 {
188 }
189
190 //*************************************************************************
194 //*************************************************************************
195 ETL_CONSTEXPR const_reference front() const
196 {
197#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
198 return !empty() ? *mbegin : throw(ETL_ERROR(string_view_empty));
199#else
200 ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(string_view_empty));
201 return *mbegin;
202#endif
203 }
204
205 //*************************************************************************
209 //*************************************************************************
210 ETL_CONSTEXPR const_reference back() const
211 {
212#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_EXTRA
213 return !empty() ? *(mend - 1) : throw(ETL_ERROR(string_view_empty));
214#else
215 ETL_ASSERT_CHECK_EXTRA(!empty(), ETL_ERROR(string_view_empty));
216 return *(mend - 1);
217#endif
218 }
219
220 //*************************************************************************
222 //*************************************************************************
223 ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
224 {
225 return mbegin;
226 }
227
228 //*************************************************************************
230 //*************************************************************************
231 ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
232 {
233 return mbegin;
234 }
235
236 //*************************************************************************
238 //*************************************************************************
239 ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
240 {
241 return mbegin;
242 }
243
244 //*************************************************************************
246 //*************************************************************************
247 ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
248 {
249 return mend;
250 }
251
252 //*************************************************************************
253 // Returns a const iterator to the end of the array.
254 //*************************************************************************
255 ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
256 {
257 return mend;
258 }
259
260 //*************************************************************************
262 //*************************************************************************
263 ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
264 {
265 return const_reverse_iterator(mend);
266 }
267
268 //*************************************************************************
270 //*************************************************************************
271 ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
272 {
273 return const_reverse_iterator(mend);
274 }
275
276 //*************************************************************************
278 //*************************************************************************
279 ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
280 {
281 return const_reverse_iterator(mbegin);
282 }
283
284 //*************************************************************************
286 //*************************************************************************
287 ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
288 {
289 return const_reverse_iterator(mbegin);
290 }
291
292 //*************************************************************************
293 // Capacity
294 //*************************************************************************
295
296 //*************************************************************************
298 //*************************************************************************
299 ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
300 {
301 return (mbegin == mend);
302 }
303
304 //*************************************************************************
306 //*************************************************************************
307 ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
308 {
309 return static_cast<size_t>(mend - mbegin);
310 }
311
312 //*************************************************************************
314 //*************************************************************************
315 ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
316 {
317 return size();
318 }
319
320 //*************************************************************************
322 //*************************************************************************
323 ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
324 {
325 return size();
326 }
327
328 //*************************************************************************
330 //*************************************************************************
332 {
333 mbegin = other.mbegin;
334 mend = other.mend;
335 return *this;
336 }
337
338 //*************************************************************************
340 //*************************************************************************
341 ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
342 {
343 mbegin = begin_;
344 mend = end_;
345 }
346
347 //*************************************************************************
349 //*************************************************************************
350 ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
351 {
352 mbegin = begin_;
353 mend = begin_ + size_;
354 }
355
356 //*************************************************************************
360 //*************************************************************************
361 ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS || ETL_NOT_CHECKING_INDEX_OPERATOR)
362 {
363#if ETL_USING_CPP11 && ETL_NOT_USING_CPP14 && ETL_USING_EXCEPTIONS && ETL_CHECKING_INDEX_OPERATOR
364 return i < size() ? mbegin[i] : throw(ETL_ERROR(string_view_bounds));
365#else
366 ETL_ASSERT_CHECK_INDEX_OPERATOR(i < size(), ETL_ERROR(string_view_bounds));
367 return mbegin[i];
368#endif
369 }
370
371 //*************************************************************************
373 //*************************************************************************
374 const_reference at(size_t i) const
375 {
376 ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(string_view_uninitialised));
377 ETL_ASSERT(i < size(), ETL_ERROR(string_view_bounds));
378 return mbegin[i];
379 }
380
381 //*************************************************************************
383 //*************************************************************************
384 ETL_CONSTEXPR14 void swap(basic_string_view& other) ETL_NOEXCEPT
385 {
386 using ETL_OR_STD::swap; // Allow ADL
387
388 swap(mbegin, other.mbegin);
389 swap(mend, other.mend);
390 }
391
392 //*************************************************************************
394 //*************************************************************************
395 ETL_CONSTEXPR14 size_type copy(T* destination, size_type count, size_type position = 0) const ETL_NOEXCEPT
396 {
397 size_t n = 0UL;
398
399 if (position < size())
400 {
401 n = etl::min(count, size() - position);
402
403 etl::mem_copy(mbegin + position, n, destination);
404 }
405
406 return n;
407 }
408
409 //*************************************************************************
411 //*************************************************************************
412 ETL_CONSTEXPR14 basic_string_view substr(size_type position = 0, size_type count = npos) const ETL_NOEXCEPT
413 {
415
416 if (position < size())
417 {
418 size_t n = etl::min(count, size() - position);
419
420 view = basic_string_view(mbegin + position, mbegin + position + n);
421 }
422
423 return view;
424 }
425
426 //*************************************************************************
428 //*************************************************************************
429 ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
430 {
431 mbegin += n;
432 }
433
434 //*************************************************************************
436 //*************************************************************************
437 ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
438 {
439 mend -= n;
440 }
441
442 //*************************************************************************
444 //*************************************************************************
445 ETL_CONSTEXPR14 int compare(basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
446 {
447 return (*this == view) ? 0 : ((*this > view) ? 1 : -1);
448 }
449
450 ETL_CONSTEXPR14 int compare(size_type position, size_type count, basic_string_view view) const ETL_NOEXCEPT
451 {
452 return substr(position, count).compare(view);
453 }
454
455 ETL_CONSTEXPR14 int compare(size_type position1, size_type count1, basic_string_view view, size_type position2, size_type count2) const
456 ETL_NOEXCEPT
457 {
458 return substr(position1, count1).compare(view.substr(position2, count2));
459 }
460
461 ETL_CONSTEXPR14 int compare(const T* text) const ETL_NOEXCEPT
462 {
463 const T* view_itr = mbegin;
464 const T* text_itr = text;
465
466 while (view_itr != mend && *text_itr != T(0))
467 {
468 if (*view_itr < *text_itr)
469 {
470 return -1;
471 }
472 else if (*view_itr > *text_itr)
473 {
474 return 1;
475 }
476 ++view_itr;
477 ++text_itr;
478 }
479
480 if ((view_itr == mend) && (*text_itr == T(0)))
481 {
482 return 0;
483 }
484 else if (view_itr == mend)
485 {
486 return -1;
487 }
488 else
489 {
490 return 1;
491 }
492 }
493
494 ETL_CONSTEXPR14 int compare(size_type position, size_type count, const T* text) const ETL_NOEXCEPT
495 {
496 return substr(position, count).compare(text);
497 }
498
499 ETL_CONSTEXPR14 int compare(size_type position, size_type count1, const T* text, size_type count2) const ETL_NOEXCEPT
500 {
501 return substr(position, count1).compare(etl::basic_string_view<T, TTraits>(text, count2));
502 }
503
504 //*************************************************************************
506 //*************************************************************************
507 ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
508 {
509 return (size() >= view.size()) && (compare(0, view.size(), view) == 0);
510 }
511
512 ETL_CONSTEXPR14 bool starts_with(T c) const ETL_NOEXCEPT
513 {
514 return !empty() && (front() == c);
515 }
516
517 ETL_CONSTEXPR14 bool starts_with(const T* text) const ETL_NOEXCEPT
518 {
519 size_t lengthtext = TTraits::length(text);
520
521 return (size() >= lengthtext) && (compare(0, lengthtext, text) == 0);
522 }
523
524 //*************************************************************************
526 //*************************************************************************
527 ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
528 {
529 return (size() >= view.size()) && (compare(size() - view.size(), npos, view) == 0);
530 }
531
532 ETL_CONSTEXPR14 bool ends_with(T c) const
533 {
534 return !empty() && (back() == c);
535 }
536
537 ETL_CONSTEXPR14 bool ends_with(const T* text) const
538 {
539 size_t lengthtext = TTraits::length(text);
540 size_t lengthview = size();
541
542 return (lengthview >= lengthtext) && (compare(lengthview - lengthtext, lengthtext, text) == 0);
543 }
544
545 //*************************************************************************
547 //*************************************************************************
548 ETL_CONSTEXPR14 size_type find(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
549 {
550 if ((size() < view.size()))
551 {
552 return npos;
553 }
554
555 const_iterator iposition = etl::search(begin() + position, end(), view.begin(), view.end());
556
557 if (iposition == end())
558 {
559 return npos;
560 }
561 else
562 {
563 return static_cast<size_type>(etl::distance(begin(), iposition));
564 }
565 }
566
567 ETL_CONSTEXPR14 size_type find(T c, size_type position = 0) const ETL_NOEXCEPT
568 {
569 return find(etl::basic_string_view<T, TTraits>(&c, 1), position);
570 }
571
572 ETL_CONSTEXPR14 size_type find(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
573 {
574 return find(etl::basic_string_view<T, TTraits>(text, count), position);
575 }
576
577 ETL_CONSTEXPR14 size_type find(const T* text, size_type position = 0) const ETL_NOEXCEPT
578 {
579 return find(etl::basic_string_view<T, TTraits>(text), position);
580 }
581
582 //*************************************************************************
584 //*************************************************************************
585 ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
586 {
587 if ((size() < view.size()))
588 {
589 return npos;
590 }
591
592 position = etl::min(position, size());
593
594 const_iterator iposition = etl::find_end(begin(), begin() + position, view.begin(), view.end());
595
596 if (iposition == end())
597 {
598 return npos;
599 }
600 else
601 {
602 return static_cast<size_type>(etl::distance(begin(), iposition));
603 }
604 }
605
606 ETL_CONSTEXPR14 size_type rfind(T c, size_type position = npos) const ETL_NOEXCEPT
607 {
608 return rfind(etl::basic_string_view<T, TTraits>(&c, 1), position);
609 }
610
611 ETL_CONSTEXPR14 size_type rfind(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
612 {
613 return rfind(etl::basic_string_view<T, TTraits>(text, count), position);
614 }
615
616 ETL_CONSTEXPR14 size_type rfind(const T* text, size_type position = npos) const ETL_NOEXCEPT
617 {
618 return rfind(etl::basic_string_view<T, TTraits>(text), position);
619 }
620
621 //*************************************************************************
623 //*************************************************************************
624 ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
625 {
626 const size_t lengthtext = size();
627
628 if (position < lengthtext)
629 {
630 for (size_t i = position; i < lengthtext; ++i)
631 {
632 const size_t lengthview = view.size();
633
634 for (size_t j = 0UL; j < lengthview; ++j)
635 {
636 if (mbegin[i] == view[j])
637 {
638 return i;
639 }
640 }
641 }
642 }
643
644 return npos;
645 }
646
647 ETL_CONSTEXPR14 size_type find_first_of(T c, size_type position = 0) const ETL_NOEXCEPT
648 {
649 return find_first_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
650 }
651
652 ETL_CONSTEXPR14 size_type find_first_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
653 {
654 return find_first_of(etl::basic_string_view<T, TTraits>(text, count), position);
655 }
656
657 ETL_CONSTEXPR14 size_type find_first_of(const T* text, size_type position = 0) const ETL_NOEXCEPT
658 {
659 return find_first_of(etl::basic_string_view<T, TTraits>(text), position);
660 }
661
662 //*************************************************************************
664 //*************************************************************************
665 ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
666 {
667 if (empty())
668 {
669 return npos;
670 }
671
672 position = etl::min(position, size() - 1);
673
674 const_reverse_iterator it = rbegin() + static_cast<ptrdiff_t>(size() - position - 1);
675
676 while (it != rend())
677 {
678 const size_t viewlength = view.size();
679
680 for (size_t j = 0UL; j < viewlength; ++j)
681 {
682 if (mbegin[position] == view[j])
683 {
684 return position;
685 }
686 }
687
688 ++it;
689 --position;
690 }
691
692 return npos;
693 }
694
695 ETL_CONSTEXPR14 size_type find_last_of(T c, size_type position = npos) const ETL_NOEXCEPT
696 {
697 return find_last_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
698 }
699
700 ETL_CONSTEXPR14 size_type find_last_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
701 {
702 return find_last_of(etl::basic_string_view<T, TTraits>(text, count), position);
703 }
704
705 ETL_CONSTEXPR14 size_type find_last_of(const T* text, size_type position = npos) const ETL_NOEXCEPT
706 {
707 return find_last_of(etl::basic_string_view<T, TTraits>(text), position);
708 }
709
710 //*************************************************************************
712 //*************************************************************************
713 ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
714 {
715 const size_t lengthtext = size();
716
717 if (position < lengthtext)
718 {
719 for (size_t i = position; i < lengthtext; ++i)
720 {
721 bool found = false;
722
723 const size_t viewlength = view.size();
724
725 for (size_t j = 0UL; j < viewlength; ++j)
726 {
727 if (mbegin[i] == view[j])
728 {
729 found = true;
730 break;
731 }
732 }
733
734 if (!found)
735 {
736 return i;
737 }
738 }
739 }
740
741 return npos;
742 }
743
744 ETL_CONSTEXPR14 size_type find_first_not_of(T c, size_type position = 0) const ETL_NOEXCEPT
745 {
747 }
748
749 ETL_CONSTEXPR14 size_type find_first_not_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
750 {
751 return find_first_not_of(etl::basic_string_view<T, TTraits>(text, count), position);
752 }
753
754 ETL_CONSTEXPR14 size_type find_first_not_of(const T* text, size_type position = 0) const ETL_NOEXCEPT
755 {
756 return find_first_not_of(etl::basic_string_view<T, TTraits>(text), position);
757 }
758
759 //*************************************************************************
761 //*************************************************************************
762 ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
763 {
764 if (empty())
765 {
766 return npos;
767 }
768
769 position = etl::min(position, size() - 1);
770
771 const_reverse_iterator it = rbegin() + static_cast<ptrdiff_t>(size() - position - 1);
772
773 while (it != rend())
774 {
775 bool found = false;
776
777 const size_t viewlength = view.size();
778
779 for (size_t j = 0UL; j < viewlength; ++j)
780 {
781 if (mbegin[position] == view[j])
782 {
783 found = true;
784 break;
785 }
786 }
787
788 if (!found)
789 {
790 return position;
791 }
792
793 ++it;
794 --position;
795 }
796
797 return npos;
798 }
799
800 ETL_CONSTEXPR14 size_type find_last_not_of(T c, size_type position = npos) const ETL_NOEXCEPT
801 {
803 }
804
805 ETL_CONSTEXPR14 size_type find_last_not_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
806 {
807 return find_last_not_of(etl::basic_string_view<T, TTraits>(text, count), position);
808 }
809
810 ETL_CONSTEXPR14 size_type find_last_not_of(const T* text, size_type position = npos) const ETL_NOEXCEPT
811 {
812 return find_last_not_of(etl::basic_string_view<T, TTraits>(text), position);
813 }
814
815 //*********************************************************************
817 //*********************************************************************
818 bool contains(const etl::basic_string_view<T, TTraits>& view) const ETL_NOEXCEPT
819 {
820 return find(view) != npos;
821 }
822
823 //*********************************************************************
825 //*********************************************************************
826 bool contains(const_pointer s) const ETL_NOEXCEPT
827 {
828 return find(s) != npos;
829 }
830
831 //*********************************************************************
833 //*********************************************************************
834 bool contains(value_type c) const ETL_NOEXCEPT
835 {
836 return find(c) != npos;
837 }
838
839 //*************************************************************************
841 //*************************************************************************
842 friend ETL_CONSTEXPR14 bool operator==(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
843 {
844 return (lhs.size() == rhs.size()) && etl::equal(lhs.begin(), lhs.end(), rhs.begin());
845 }
846
847 //*************************************************************************
849 //*************************************************************************
850 friend ETL_CONSTEXPR14 bool operator!=(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
851 {
852 return !(lhs == rhs);
853 }
854
855 //*************************************************************************
857 //*************************************************************************
858 friend ETL_CONSTEXPR14 bool operator<(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
859 {
860 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
861 }
862
863 //*************************************************************************
865 //*************************************************************************
866 friend ETL_CONSTEXPR14 bool operator>(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
867 {
868 return rhs < lhs;
869 }
870
871 //*************************************************************************
873 //*************************************************************************
874 friend ETL_CONSTEXPR14 bool operator<=(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
875 {
876 return !(lhs > rhs);
877 }
878
879 //*************************************************************************
881 //*************************************************************************
882 friend ETL_CONSTEXPR14 bool operator>=(const etl::basic_string_view<T, TTraits>& lhs, const etl::basic_string_view<T, TTraits>& rhs)
883 {
884 return !(lhs < rhs);
885 }
886
887 private:
888
889 const_pointer mbegin;
890 const_pointer mend;
891 };
892
893 typedef etl::basic_string_view<char> string_view;
894 typedef etl::basic_string_view<wchar_t> wstring_view;
895 typedef etl::basic_string_view<char8_t> u8string_view;
896 typedef etl::basic_string_view<char16_t> u16string_view;
897 typedef etl::basic_string_view<char32_t> u32string_view;
898
899 //*************************************************************************
901 //*************************************************************************
902 template <size_t Array_Size>
903 ETL_CONSTEXPR14 string_view make_string_view(const char (&text)[Array_Size]) ETL_NOEXCEPT
904 {
905 size_t length = etl::char_traits<char>::length(text, Array_Size - 1U);
906
907 return string_view(text, length);
908 }
909
910 //***********************************
911 template <size_t Array_Size>
912 ETL_CONSTEXPR14 wstring_view make_string_view(const wchar_t (&text)[Array_Size]) ETL_NOEXCEPT
913 {
914 size_t length = etl::char_traits<wchar_t>::length(text, Array_Size - 1U);
915
916 return wstring_view(text, length);
917 }
918
919 //***********************************
920 template <size_t Array_Size>
921 ETL_CONSTEXPR14 u8string_view make_string_view(const char8_t (&text)[Array_Size]) ETL_NOEXCEPT
922 {
923 size_t length = etl::char_traits<char8_t>::length(text, Array_Size - 1U);
924
925 return u8string_view(text, length);
926 }
927
928 //***********************************
929 template <size_t Array_Size>
930 ETL_CONSTEXPR14 u16string_view make_string_view(const char16_t (&text)[Array_Size]) ETL_NOEXCEPT
931 {
932 size_t length = etl::char_traits<char16_t>::length(text, Array_Size - 1U);
933
934 return u16string_view(text, length);
935 }
936
937 //***********************************
938 template <size_t Array_Size>
939 ETL_CONSTEXPR14 u32string_view make_string_view(const char32_t (&text)[Array_Size]) ETL_NOEXCEPT
940 {
941 size_t length = etl::char_traits<char32_t>::length(text, Array_Size - 1U);
942
943 return u32string_view(text, length);
944 }
945
946 //*************************************************************************
948 //*************************************************************************
949#if ETL_USING_8BIT_TYPES
950 template <>
951 struct hash<etl::string_view>
952 {
953 size_t operator()(const etl::string_view& text) const
954 {
955 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
956 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
957 }
958 };
959
960 template <>
961 struct hash<etl::wstring_view>
962 {
963 size_t operator()(const etl::wstring_view& text) const
964 {
965 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
966 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
967 }
968 };
969
970 template <>
971 struct hash<etl::u16string_view>
972 {
973 size_t operator()(const etl::u16string_view& text) const
974 {
975 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
976 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
977 }
978 };
979
980 template <>
981 struct hash<etl::u32string_view>
982 {
983 size_t operator()(const etl::u32string_view& text) const
984 {
985 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
986 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
987 }
988 };
989#endif
990} // namespace etl
991
992//*************************************************************************
994//*************************************************************************
995template <typename T, typename TTraits >
997{
998 lhs.swap(rhs);
999}
1000
1001template <typename T>
1003{
1004 lhs.swap(rhs);
1005}
1006
1007//*************************************************************************
1009//*************************************************************************
1010#if ETL_USING_STD_OSTREAM
1011template <typename T>
1012std::basic_ostream<T, std::char_traits<T> >& operator<<(std::basic_ostream<T, std::char_traits<T> >& os,
1014{
1015 os.write(text.data(), static_cast<std::streamsize>(text.size()));
1016 return os;
1017}
1018#endif
1019
1020#include "private/minmax_pop.h"
1021
1022#endif
String view.
Definition string_view.h:117
ETL_CONSTEXPR14 size_type find(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find characters in the view.
Definition string_view.h:548
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition string_view.h:139
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:239
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:307
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:279
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:287
ETL_CONSTEXPR basic_string_view(const basic_string_view &other) ETL_NOEXCEPT
Copy constructor.
Definition string_view.h:184
friend ETL_CONSTEXPR14 bool operator<=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than-equal for string_view.
Definition string_view.h:874
ETL_CONSTEXPR14 etl::basic_string_view< T, TTraits > & operator=(const etl::basic_string_view< T, TTraits > &other) ETL_NOEXCEPT
Assign from a view.
Definition string_view.h:331
ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find last absence of characters.
Definition string_view.h:762
ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Checks if the string view starts with the given prefix.
Definition string_view.h:507
friend ETL_CONSTEXPR14 bool operator>=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than-equal for string_view.
Definition string_view.h:882
ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its start forward.
Definition string_view.h:429
ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find first occurrence of characters.
Definition string_view.h:624
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:223
bool contains(const etl::basic_string_view< T, TTraits > &view) const ETL_NOEXCEPT
Checks that the view is within this string.
Definition string_view.h:818
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
Definition string_view.h:323
friend ETL_CONSTEXPR14 bool operator!=(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Inequality for string_view.
Definition string_view.h:850
ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:315
ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Checks if the string view ends with the given suffix.
Definition string_view.h:527
ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find last occurrence of characters.
Definition string_view.h:665
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition string_view.h:384
bool contains(const_pointer s) const ETL_NOEXCEPT
Checks that text is within this string.
Definition string_view.h:826
friend ETL_CONSTEXPR14 bool operator<(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Less-than for string_view.
Definition string_view.h:858
ETL_CONSTEXPR const_reference back() const
Definition string_view.h:210
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Compares two views.
Definition string_view.h:445
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition string_view.h:247
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:374
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its end backward.
Definition string_view.h:437
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
Assign from iterators.
Definition string_view.h:341
bool contains(value_type c) const ETL_NOEXCEPT
Checks that character is within this string.
Definition string_view.h:834
ETL_CONSTEXPR basic_string_view(const T *begin_, size_t size_) ETL_NOEXCEPT
Construct from pointer/size.
Definition string_view.h:175
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string< T > &str) ETL_NOEXCEPT
Construct from string.
Definition string_view.h:148
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
Assign from iterator and size.
Definition string_view.h:350
friend ETL_CONSTEXPR14 bool operator>(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Greater-than for string_view.
Definition string_view.h:866
ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view< T, TTraits > view, size_type position=0) const ETL_NOEXCEPT
Find first absence of characters.
Definition string_view.h:713
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T *begin_) ETL_NOEXCEPT
Construct from T*.
Definition string_view.h:157
friend ETL_CONSTEXPR14 bool operator==(const etl::basic_string_view< T, TTraits > &lhs, const etl::basic_string_view< T, TTraits > &rhs)
Equality for string_view.
Definition string_view.h:842
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:231
ETL_CONSTEXPR const_reference front() const
Definition string_view.h:195
ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:271
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const ETL_NOEXCEPT
Returns a substring.
Definition string_view.h:412
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT_EXPR(ETL_NOT_USING_EXCEPTIONS||ETL_NOT_CHECKING_INDEX_OPERATOR)
Definition string_view.h:361
ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view< T, TTraits > view, size_type position=npos) const ETL_NOEXCEPT
Find the last occurrence of a substring.
Definition string_view.h:585
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const ETL_NOEXCEPT
Copies characters.
Definition string_view.h:395
ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
Returns a const reverse iterator to the reverse beginning of the array.
Definition string_view.h:263
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the array size is zero.
Definition string_view.h:299
ETL_CONSTEXPR basic_string_view(const T *begin_, const T *end_) ETL_NOEXCEPT
Construct from pointer range.
Definition string_view.h:166
Definition basic_string.h:350
Definition string_view.h:76
The exception thrown when the view is empty.
Definition string_view.h:103
Definition string_view.h:90
#define ETL_ASSERT(b, e)
Definition error_handler.h:511
ETL_EXCEPTION_CONSTEXPR exception(string_type reason_, string_type, numeric_type)
Constructor.
Definition exception.h:81
Definition integral_limits.h:518
bitset_ext
Definition absolute.h:40
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size]) ETL_NOEXCEPT
make_string_view.
Definition string_view.h:903
T * mem_copy(const T *sb, const T *se, T *db) ETL_NOEXCEPT
Definition memory.h:2835
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1017
void swap(etl::basic_string_view< T, TTraits > &lhs, etl::basic_string_view< T, TTraits > &rhs) ETL_NOEXCEPT
Swaps the values.
Definition string_view.h:996
std::basic_ostream< T, std::char_traits< T > > & operator<<(std::basic_ostream< T, std::char_traits< T > > &os, etl::basic_string_view< T, etl::char_traits< T > > text)
Operator overload to write to std basic_ostream.
Definition string_view.h:1012
Character traits for any character type.
Definition char_traits.h:128