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 "memory.h"
36#include "iterator.h"
37#include "error_handler.h"
38#include "exception.h"
39#include "char_traits.h"
40#include "integral_limits.h"
41#include "hash.h"
42#include "basic_string.h"
43#include "algorithm.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 template <typename T, typename TTraits = etl::char_traits<T> >
104 {
105 public:
106
107 typedef T value_type;
108 typedef TTraits traits_type;
109 typedef size_t size_type;
110 typedef T& reference;
111 typedef const T& const_reference;
112 typedef T* pointer;
113 typedef const T* const_pointer;
114 typedef const T* iterator;
115 typedef const T* const_iterator;
116 typedef ETL_OR_STD::reverse_iterator<const_iterator> const_reverse_iterator;
117
118 enum
119 {
121 };
122
123 //*************************************************************************
125 //*************************************************************************
126 ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
127 : mbegin(ETL_NULLPTR)
128 , mend(ETL_NULLPTR)
129 {
130 }
131
132 //*************************************************************************
134 //*************************************************************************
135 ETL_CONSTEXPR basic_string_view(const etl::ibasic_string<T>& str) ETL_NOEXCEPT
136 : mbegin(str.begin())
137 , mend(str.end())
138 {
139 }
140
141 //*************************************************************************
143 //*************************************************************************
144 ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T* begin_) ETL_NOEXCEPT
145 : mbegin(begin_)
146 , mend(begin_ + TTraits::length(begin_))
147 {
148 }
149
150 //*************************************************************************
152 //*************************************************************************
153 ETL_CONSTEXPR basic_string_view(const T* begin_, const T* end_) ETL_NOEXCEPT
154 : mbegin(begin_)
155 , mend(end_)
156 {
157 }
158
159 //*************************************************************************
161 //*************************************************************************
162 ETL_CONSTEXPR basic_string_view(const T* begin_, size_t size_) ETL_NOEXCEPT
163 : mbegin(begin_)
164 , mend(begin_ + size_)
165 {
166 }
167
168 //*************************************************************************
170 //*************************************************************************
171 ETL_CONSTEXPR basic_string_view(const basic_string_view& other) ETL_NOEXCEPT
172 : mbegin(other.mbegin)
173 , mend(other.mend)
174 {
175 }
176
177 //*************************************************************************
179 //*************************************************************************
180 ETL_CONSTEXPR const_reference front() const
181 {
182 return *mbegin;
183 }
184
185 //*************************************************************************
187 //*************************************************************************
188 ETL_CONSTEXPR const_reference back() const
189 {
190 return *(mend - 1);
191 }
192
193 //*************************************************************************
195 //*************************************************************************
196 ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
197 {
198 return mbegin;
199 }
200
201 //*************************************************************************
203 //*************************************************************************
204 ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
205 {
206 return mbegin;
207 }
208
209 //*************************************************************************
211 //*************************************************************************
212 ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
213 {
214 return mbegin;
215 }
216
217 //*************************************************************************
219 //*************************************************************************
220 ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
221 {
222 return mend;
223 }
224
225 //*************************************************************************
226 // Returns a const iterator to the end of the array.
227 //*************************************************************************
228 ETL_CONSTEXPR const_iterator cend() const ETL_NOEXCEPT
229 {
230 return mend;
231 }
232
233 //*************************************************************************
235 //*************************************************************************
236 ETL_CONSTEXPR const_reverse_iterator rbegin() const ETL_NOEXCEPT
237 {
238 return const_reverse_iterator(mend);
239 }
240
241 //*************************************************************************
243 //*************************************************************************
244 ETL_CONSTEXPR const_reverse_iterator crbegin() const ETL_NOEXCEPT
245 {
246 return const_reverse_iterator(mend);
247 }
248
249 //*************************************************************************
251 //*************************************************************************
252 ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
253 {
254 return const_reverse_iterator(mbegin);
255 }
256
257 //*************************************************************************
259 //*************************************************************************
260 ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
261 {
262 return const_reverse_iterator(mbegin);
263 }
264
265 //*************************************************************************
266 // Capacity
267 //*************************************************************************
268
269 //*************************************************************************
271 //*************************************************************************
272 ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
273 {
274 return (mbegin == mend);
275 }
276
277 //*************************************************************************
279 //*************************************************************************
280 ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
281 {
282 return static_cast<size_t>(mend - mbegin);
283 }
284
285 //*************************************************************************
287 //*************************************************************************
288 ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
289 {
290 return size();
291 }
292
293 //*************************************************************************
295 //*************************************************************************
296 ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
297 {
298 return size();
299 }
300
301 //*************************************************************************
303 //*************************************************************************
305 {
306 mbegin = other.mbegin;
307 mend = other.mend;
308 return *this;
309 }
310
311 //*************************************************************************
313 //*************************************************************************
314 ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
315 {
316 mbegin = begin_;
317 mend = end_;
318 }
319
320 //*************************************************************************
322 //*************************************************************************
323 ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
324 {
325 mbegin = begin_;
326 mend = begin_ + size_;
327 }
328
329 //*************************************************************************
331 //*************************************************************************
332 ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT
333 {
334 return mbegin[i];
335 }
336
337 //*************************************************************************
339 //*************************************************************************
340 const_reference at(size_t i) const
341 {
342 ETL_ASSERT((mbegin != ETL_NULLPTR && mend != ETL_NULLPTR), ETL_ERROR(string_view_uninitialised));
343 ETL_ASSERT(i < size(), ETL_ERROR(string_view_bounds));
344 return mbegin[i];
345 }
346
347 //*************************************************************************
349 //*************************************************************************
350 ETL_CONSTEXPR14 void swap(basic_string_view& other) ETL_NOEXCEPT
351 {
352 using ETL_OR_STD::swap; // Allow ADL
353
354 swap(mbegin, other.mbegin);
355 swap(mend, other.mend);
356 }
357
358 //*************************************************************************
360 //*************************************************************************
361 ETL_CONSTEXPR14 size_type copy(T* destination, size_type count, size_type position = 0) const ETL_NOEXCEPT
362 {
363 size_t n = 0UL;
364
365 if (position < size())
366 {
367 n = etl::min(count, size() - position);
368
369 etl::mem_copy(mbegin + position, n, destination);
370 }
371
372 return n;
373 }
374
375 //*************************************************************************
377 //*************************************************************************
378 ETL_CONSTEXPR14 basic_string_view substr(size_type position = 0, size_type count = npos) const ETL_NOEXCEPT
379 {
381
382 if (position < size())
383 {
384 size_t n = etl::min(count, size() - position);
385
386 view = basic_string_view(mbegin + position, mbegin + position + n);
387 }
388
389 return view;
390 }
391
392 //*************************************************************************
394 //*************************************************************************
395 ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
396 {
397 mbegin += n;
398 }
399
400 //*************************************************************************
402 //*************************************************************************
403 ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
404 {
405 mend -= n;
406 }
407
408 //*************************************************************************
410 //*************************************************************************
411 ETL_CONSTEXPR14 int compare(basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
412 {
413 return (*this == view) ? 0 : ((*this > view) ? 1 : -1);
414 }
415
416 ETL_CONSTEXPR14 int compare(size_type position, size_type count, basic_string_view view) const ETL_NOEXCEPT
417 {
418 return substr(position, count).compare(view);
419 }
420
421 ETL_CONSTEXPR14 int compare(size_type position1, size_type count1,
423 size_type position2, size_type count2) const ETL_NOEXCEPT
424 {
425 return substr(position1, count1).compare(view.substr(position2, count2));
426 }
427
428 ETL_CONSTEXPR14 int compare(const T* text) const ETL_NOEXCEPT
429 {
430 const T* view_itr = mbegin;
431 const T* text_itr = text;
432
433 while (view_itr != mend && *text_itr != T(0))
434 {
435 if (*view_itr < *text_itr)
436 {
437 return -1;
438 }
439 else if (*view_itr > *text_itr)
440 {
441 return 1;
442 }
443 ++view_itr;
444 ++text_itr;
445 }
446
447 if ((view_itr == mend) && (*text_itr == T(0)))
448 {
449 return 0;
450 }
451 else if (view_itr == mend)
452 {
453 return -1;
454 }
455 else
456 {
457 return 1;
458 }
459 }
460
461 ETL_CONSTEXPR14 int compare(size_type position, size_type count, const T* text) const ETL_NOEXCEPT
462 {
463 return substr(position, count).compare(text);
464 }
465
466 ETL_CONSTEXPR14 int compare(size_type position, size_type count1, const T* text, size_type count2) const ETL_NOEXCEPT
467 {
468 return substr(position, count1).compare(etl::basic_string_view<T, TTraits>(text, count2));
469 }
470
471 //*************************************************************************
473 //*************************************************************************
474 ETL_CONSTEXPR14 bool starts_with(etl::basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
475 {
476 return (size() >= view.size()) &&
477 (compare(0, view.size(), view) == 0);
478 }
479
480 ETL_CONSTEXPR14 bool starts_with(T c) const ETL_NOEXCEPT
481 {
482 return !empty() && (front() == c);
483 }
484
485 ETL_CONSTEXPR14 bool starts_with(const T* text) const ETL_NOEXCEPT
486 {
487 size_t lengthtext = TTraits::length(text);
488
489 return (size() >= lengthtext) &&
490 (compare(0, lengthtext, text) == 0);
491 }
492
493 //*************************************************************************
495 //*************************************************************************
496 ETL_CONSTEXPR14 bool ends_with(etl::basic_string_view<T, TTraits> view) const ETL_NOEXCEPT
497 {
498 return (size() >= view.size()) &&
499 (compare(size() - view.size(), npos, view) == 0);
500 }
501
502 ETL_CONSTEXPR14 bool ends_with(T c) const
503 {
504 return !empty() && (back() == c);
505 }
506
507 ETL_CONSTEXPR14 bool ends_with(const T* text) const
508 {
509 size_t lengthtext = TTraits::length(text);
510 size_t lengthview = size();
511
512 return (lengthview >= lengthtext) &&
513 (compare(lengthview - lengthtext, lengthtext, text) == 0);
514 }
515
516 //*************************************************************************
518 //*************************************************************************
519 ETL_CONSTEXPR14 size_type find(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
520 {
521 if ((size() < view.size()))
522 {
523 return npos;
524 }
525
526 const_iterator iposition = etl::search(begin() + position, end(), view.begin(), view.end());
527
528 if (iposition == end())
529 {
530 return npos;
531 }
532 else
533 {
534 return etl::distance(begin(), iposition);
535 }
536 }
537
538 ETL_CONSTEXPR14 size_type find(T c, size_type position = 0) const ETL_NOEXCEPT
539 {
540 return find(etl::basic_string_view<T, TTraits>(&c, 1), position);
541 }
542
543 ETL_CONSTEXPR14 size_type find(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
544 {
545 return find(etl::basic_string_view<T, TTraits>(text, count), position);
546 }
547
548 ETL_CONSTEXPR14 size_type find(const T* text, size_type position = 0) const ETL_NOEXCEPT
549 {
550 return find(etl::basic_string_view<T, TTraits>(text), position);
551 }
552
553 //*************************************************************************
555 //*************************************************************************
556 ETL_CONSTEXPR14 size_type rfind(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
557 {
558 if ((size() < view.size()))
559 {
560 return npos;
561 }
562
563 position = etl::min(position, size());
564
565 const_iterator iposition = etl::find_end(begin(),
566 begin() + position,
567 view.begin(),
568 view.end());
569
570 if (iposition == end())
571 {
572 return npos;
573 }
574 else
575 {
576 return etl::distance(begin(), iposition);
577 }
578 }
579
580 ETL_CONSTEXPR14 size_type rfind(T c, size_type position = npos) const ETL_NOEXCEPT
581 {
582 return rfind(etl::basic_string_view<T, TTraits>(&c, 1), position);
583 }
584
585 ETL_CONSTEXPR14 size_type rfind(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
586 {
587 return rfind(etl::basic_string_view<T, TTraits>(text, count), position);
588 }
589
590 ETL_CONSTEXPR14 size_type rfind(const T* text, size_type position = npos) const ETL_NOEXCEPT
591 {
592 return rfind(etl::basic_string_view<T, TTraits>(text), position);
593 }
594
595 //*************************************************************************
597 //*************************************************************************
598 ETL_CONSTEXPR14 size_type find_first_of(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
599 {
600 const size_t lengthtext = size();
601
602 if (position < lengthtext)
603 {
604 for (size_t i = position; i < lengthtext; ++i)
605 {
606 const size_t lengthview = view.size();
607
608 for (size_t j = 0UL; j < lengthview; ++j)
609 {
610 if (mbegin[i] == view[j])
611 {
612 return i;
613 }
614 }
615 }
616 }
617
618 return npos;
619 }
620
621 ETL_CONSTEXPR14 size_type find_first_of(T c, size_type position = 0) const ETL_NOEXCEPT
622 {
623 return find_first_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
624 }
625
626 ETL_CONSTEXPR14 size_type find_first_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
627 {
628 return find_first_of(etl::basic_string_view<T, TTraits>(text, count), position);
629 }
630
631 ETL_CONSTEXPR14 size_type find_first_of(const T* text, size_type position = 0) const ETL_NOEXCEPT
632 {
633 return find_first_of(etl::basic_string_view<T, TTraits>(text), position);
634 }
635
636 //*************************************************************************
638 //*************************************************************************
639 ETL_CONSTEXPR14 size_type find_last_of(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
640 {
641 if (empty())
642 {
643 return npos;
644 }
645
646 position = etl::min(position, size() - 1);
647
648 const_reverse_iterator it = rbegin() + size() - position - 1;
649
650 while (it != rend())
651 {
652 const size_t viewlength = view.size();
653
654 for (size_t j = 0UL; j < viewlength; ++j)
655 {
656 if (mbegin[position] == view[j])
657 {
658 return position;
659 }
660 }
661
662 ++it;
663 --position;
664 }
665
666 return npos;
667 }
668
669 ETL_CONSTEXPR14 size_type find_last_of(T c, size_type position = npos) const ETL_NOEXCEPT
670 {
671 return find_last_of(etl::basic_string_view<T, TTraits>(&c, 1), position);
672 }
673
674 ETL_CONSTEXPR14 size_type find_last_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
675 {
676 return find_last_of(etl::basic_string_view<T, TTraits>(text, count), position);
677 }
678
679 ETL_CONSTEXPR14 size_type find_last_of(const T* text, size_type position = npos) const ETL_NOEXCEPT
680 {
681 return find_last_of(etl::basic_string_view<T, TTraits>(text), position);
682 }
683
684 //*************************************************************************
686 //*************************************************************************
687 ETL_CONSTEXPR14 size_type find_first_not_of(etl::basic_string_view<T, TTraits> view, size_type position = 0) const ETL_NOEXCEPT
688 {
689 const size_t lengthtext = size();
690
691 if (position < lengthtext)
692 {
693 for (size_t i = position; i < lengthtext; ++i)
694 {
695 bool found = false;
696
697 const size_t viewlength = view.size();
698
699 for (size_t j = 0UL; j < viewlength; ++j)
700 {
701 if (mbegin[i] == view[j])
702 {
703 found = true;
704 break;
705 }
706 }
707
708 if (!found)
709 {
710 return i;
711 }
712 }
713 }
714
715 return npos;
716 }
717
718 ETL_CONSTEXPR14 size_type find_first_not_of(T c, size_type position = 0) const ETL_NOEXCEPT
719 {
721 }
722
723 ETL_CONSTEXPR14 size_type find_first_not_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
724 {
725 return find_first_not_of(etl::basic_string_view<T, TTraits>(text, count), position);
726 }
727
728 ETL_CONSTEXPR14 size_type find_first_not_of(const T* text, size_type position = 0) const ETL_NOEXCEPT
729 {
730 return find_first_not_of(etl::basic_string_view<T, TTraits>(text), position);
731 }
732
733 //*************************************************************************
735 //*************************************************************************
736 ETL_CONSTEXPR14 size_type find_last_not_of(etl::basic_string_view<T, TTraits> view, size_type position = npos) const ETL_NOEXCEPT
737 {
738 if (empty())
739 {
740 return npos;
741 }
742
743 position = etl::min(position, size() - 1);
744
745 const_reverse_iterator it = rbegin() + size() - position - 1;
746
747 while (it != rend())
748 {
749 bool found = false;
750
751 const size_t viewlength = view.size();
752
753 for (size_t j = 0UL; j < viewlength; ++j)
754 {
755 if (mbegin[position] == view[j])
756 {
757 found = true;
758 break;
759 }
760 }
761
762 if (!found)
763 {
764 return position;
765 }
766
767 ++it;
768 --position;
769 }
770
771 return npos;
772 }
773
774 ETL_CONSTEXPR14 size_type find_last_not_of(T c, size_type position = npos) const ETL_NOEXCEPT
775 {
777 }
778
779 ETL_CONSTEXPR14 size_type find_last_not_of(const T* text, size_type position, size_type count) const ETL_NOEXCEPT
780 {
781 return find_last_not_of(etl::basic_string_view<T, TTraits>(text, count), position);
782 }
783
784 ETL_CONSTEXPR14 size_type find_last_not_of(const T* text, size_type position = npos) const ETL_NOEXCEPT
785 {
786 return find_last_not_of(etl::basic_string_view<T, TTraits>(text), position);
787 }
788
789 //*********************************************************************
791 //*********************************************************************
792 bool contains(const etl::basic_string_view<T, TTraits>& view) const ETL_NOEXCEPT
793 {
794 return find(view) != npos;
795 }
796
797 //*********************************************************************
799 //*********************************************************************
800 bool contains(const_pointer s) const ETL_NOEXCEPT
801 {
802 return find(s) != npos;
803 }
804
805 //*********************************************************************
807 //*********************************************************************
808 bool contains(value_type c) const ETL_NOEXCEPT
809 {
810 return find(c) != npos;
811 }
812
813 //*************************************************************************
815 //*************************************************************************
817 {
818 return (lhs.size() == rhs.size()) &&
819 etl::equal(lhs.begin(), lhs.end(), rhs.begin());
820 }
821
822 //*************************************************************************
824 //*************************************************************************
826 {
827 return !(lhs == rhs);
828 }
829
830 //*************************************************************************
832 //*************************************************************************
834 {
835 return etl::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
836 }
837
838 //*************************************************************************
840 //*************************************************************************
842 {
843 return rhs < lhs;
844 }
845
846 //*************************************************************************
848 //*************************************************************************
850 {
851 return !(lhs > rhs);
852 }
853
854 //*************************************************************************
856 //*************************************************************************
858 {
859 return !(lhs < rhs);
860 }
861
862 private:
863
864 const_pointer mbegin;
865 const_pointer mend;
866 };
867
868 typedef etl::basic_string_view<char> string_view;
869 typedef etl::basic_string_view<wchar_t> wstring_view;
870 typedef etl::basic_string_view<char8_t> u8string_view;
871 typedef etl::basic_string_view<char16_t> u16string_view;
872 typedef etl::basic_string_view<char32_t> u32string_view;
873
874 //*************************************************************************
876 //*************************************************************************
877 template<size_t Array_Size>
878 ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size]) ETL_NOEXCEPT
879 {
880 size_t length = etl::char_traits<char>::length(text, Array_Size - 1U);
881
882 return string_view(text, length);
883 }
884
885 //***********************************
886 template<size_t Array_Size>
887 ETL_CONSTEXPR14 wstring_view make_string_view(const wchar_t(&text)[Array_Size]) ETL_NOEXCEPT
888 {
889 size_t length = etl::char_traits<wchar_t>::length(text, Array_Size - 1U);
890
891 return wstring_view(text, length);
892 }
893
894 //***********************************
895 template<size_t Array_Size>
896 ETL_CONSTEXPR14 u8string_view make_string_view(const char8_t(&text)[Array_Size]) ETL_NOEXCEPT
897 {
898 size_t length = etl::char_traits<char8_t>::length(text, Array_Size - 1U);
899
900 return u8string_view(text, length);
901 }
902
903 //***********************************
904 template<size_t Array_Size>
905 ETL_CONSTEXPR14 u16string_view make_string_view(const char16_t(&text)[Array_Size]) ETL_NOEXCEPT
906 {
907 size_t length = etl::char_traits<char16_t>::length(text, Array_Size - 1U);
908
909 return u16string_view(text, length);
910 }
911
912 //***********************************
913 template<size_t Array_Size>
914 ETL_CONSTEXPR14 u32string_view make_string_view(const char32_t(&text)[Array_Size]) ETL_NOEXCEPT
915 {
916 size_t length = etl::char_traits<char32_t>::length(text, Array_Size - 1U);
917
918 return u32string_view(text, length);
919 }
920
921 //*************************************************************************
923 //*************************************************************************
924#if ETL_USING_8BIT_TYPES
925 template <>
926 struct hash<etl::string_view>
927 {
928 size_t operator()(const etl::string_view& text) const
929 {
930 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
931 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
932 }
933 };
934
935 template <>
936 struct hash<etl::wstring_view>
937 {
938 size_t operator()(const etl::wstring_view& text) const
939 {
940 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
941 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
942 }
943 };
944
945 template <>
946 struct hash<etl::u16string_view>
947 {
948 size_t operator()(const etl::u16string_view& text) const
949 {
950 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
951 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
952 }
953 };
954
955 template <>
956 struct hash<etl::u32string_view>
957 {
958 size_t operator()(const etl::u32string_view& text) const
959 {
960 return etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(text.data()),
961 reinterpret_cast<const uint8_t*>(text.data() + text.size()));
962 }
963 };
964#endif
965}
966
967//*************************************************************************
969//*************************************************************************
970template <typename T, typename TTraits >
972{
973 lhs.swap(rhs);
974}
975
976template <typename T>
978{
979 lhs.swap(rhs);
980}
981
982//*************************************************************************
984//*************************************************************************
985#if ETL_USING_STD_OSTREAM
986template <typename T>
987std::basic_ostream<T, std::char_traits<T> > &operator<<(std::basic_ostream<T, std::char_traits<T> > &os,
989{
990 os.write(text.data(), text.size());
991 return os;
992}
993#endif
994
995#include "private/minmax_pop.h"
996
997#endif
998
String view.
Definition string_view.h:104
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:519
ETL_CONSTEXPR const_reference operator[](size_t i) const ETL_NOEXCEPT
Returns a const reference to the indexed value.
Definition string_view.h:332
ETL_CONSTEXPR basic_string_view() ETL_NOEXCEPT
Default constructor.
Definition string_view.h:126
ETL_CONSTEXPR const_iterator cbegin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:212
ETL_CONSTEXPR size_t size() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:280
ETL_CONSTEXPR const_reverse_iterator rend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:252
ETL_CONSTEXPR const_reverse_iterator crend() const ETL_NOEXCEPT
Returns a const reverse iterator to the end of the array.
Definition string_view.h:260
ETL_CONSTEXPR basic_string_view(const basic_string_view &other) ETL_NOEXCEPT
Copy constructor.
Definition string_view.h:171
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:849
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:304
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:736
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:474
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:857
ETL_CONSTEXPR14 void remove_prefix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its start forward.
Definition string_view.h:395
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:598
ETL_CONSTEXPR const_pointer data() const ETL_NOEXCEPT
Returns a const pointer to the first element of the internal storage.
Definition string_view.h:196
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:792
ETL_CONSTEXPR size_t max_size() const ETL_NOEXCEPT
Returns the maximum possible size of the array.
Definition string_view.h:296
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:825
ETL_CONSTEXPR size_t length() const ETL_NOEXCEPT
Returns the size of the array.
Definition string_view.h:288
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:496
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:639
ETL_CONSTEXPR14 void swap(basic_string_view &other) ETL_NOEXCEPT
Swaps with another basic_string_view.
Definition string_view.h:350
bool contains(const_pointer s) const ETL_NOEXCEPT
Checks that text is within this string.
Definition string_view.h:800
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:833
ETL_CONSTEXPR const_reference back() const
Returns a const reference to the last element.
Definition string_view.h:188
ETL_CONSTEXPR14 int compare(basic_string_view< T, TTraits > view) const ETL_NOEXCEPT
Compares two views.
Definition string_view.h:411
ETL_CONSTEXPR const_iterator end() const ETL_NOEXCEPT
Returns a const iterator to the end of the array.
Definition string_view.h:220
const_reference at(size_t i) const
Returns a const reference to the indexed value.
Definition string_view.h:340
ETL_CONSTEXPR14 void remove_suffix(size_type n) ETL_NOEXCEPT
Shrinks the view by moving its end backward.
Definition string_view.h:403
ETL_CONSTEXPR14 void assign(const_pointer begin_, const_pointer end_) ETL_NOEXCEPT
Assign from iterators.
Definition string_view.h:314
bool contains(value_type c) const ETL_NOEXCEPT
Checks that character is within this string.
Definition string_view.h:808
ETL_CONSTEXPR basic_string_view(const T *begin_, size_t size_) ETL_NOEXCEPT
Construct from pointer/size.
Definition string_view.h:162
ETL_CONSTEXPR basic_string_view(const etl::ibasic_string< T > &str) ETL_NOEXCEPT
Construct from string.
Definition string_view.h:135
ETL_CONSTEXPR14 void assign(const_pointer begin_, size_t size_) ETL_NOEXCEPT
Assign from iterator and size.
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)
Greater-than for string_view.
Definition string_view.h:841
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:687
ETL_CONSTEXPR14 ETL_EXPLICIT_STRING_FROM_CHAR basic_string_view(const T *begin_) ETL_NOEXCEPT
Construct from T*.
Definition string_view.h:144
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:816
ETL_CONSTEXPR const_iterator begin() const ETL_NOEXCEPT
Returns a const iterator to the beginning of the array.
Definition string_view.h:204
ETL_CONSTEXPR const_reference front() const
Returns a const reference to the first element.
Definition string_view.h:180
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:244
ETL_CONSTEXPR14 basic_string_view substr(size_type position=0, size_type count=npos) const ETL_NOEXCEPT
Returns a substring.
Definition string_view.h:378
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:556
ETL_CONSTEXPR14 size_type copy(T *destination, size_type count, size_type position=0) const ETL_NOEXCEPT
Copies characters.
Definition string_view.h:361
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:236
ETL_CONSTEXPR bool empty() const ETL_NOEXCEPT
Returns true if the array size is zero.
Definition string_view.h:272
ETL_CONSTEXPR basic_string_view(const T *begin_, const T *end_) ETL_NOEXCEPT
Construct from pointer range.
Definition string_view.h:153
Definition basic_string.h:351
Definition string_view.h:76
Definition string_view.h:90
#define ETL_ASSERT(b, e)
Definition error_handler.h:356
ETL_CONSTEXPR exception(string_type reason_, string_type, numeric_type line_)
Constructor.
Definition exception.h:69
Definition integral_limits.h:516
bitset_ext
Definition absolute.h:39
ETL_CONSTEXPR14 string_view make_string_view(const char(&text)[Array_Size]) ETL_NOEXCEPT
make_string_view.
Definition string_view.h:878
T * mem_copy(const T *sb, const T *se, T *db) ETL_NOEXCEPT
Definition memory.h:2289
ETL_CONSTEXPR TContainer::const_iterator cend(const TContainer &container)
Definition iterator.h:1012
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:971
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:987
Character traits for any character type.
Definition char_traits.h:120