COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
StringView.h
Go to the documentation of this file.
1//
2// Copyright 2017 - 2024 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
10
11
12#pragma once
13
14#include <algorithm>
15#include <iterator>
16#include <limits>
17#include <string>
18
19#include "comms/Assert.h"
20#include "ArrayView.h"
21
22namespace comms
23{
24
25namespace util
26{
27
34class StringView : public ArrayView<char>
35{
36 using Base = ArrayView<char>;
37public:
39 using value_type = typename Base::value_type;
40
43
45 using pointer = typename Base::pointer;
46
49
52
55
57 using reference = typename Base::reference;
58
61
64
67
69 using size_type = typename Base::size_type;
70
73
77
80
83
86
89
92
95
98
101 static const auto npos = static_cast<SizeType>(-1);
102
106 StringView() noexcept = default;
107
111 StringView(const StringView&) noexcept = default;
112
116 StringView(const char* str, size_type len) noexcept : Base(str, len) {}
117
121 StringView(const char* str) noexcept
122 {
123 static const auto MaxLen = std::numeric_limits<size_type>::max();
124 size_type len = 0;
125 while (len < MaxLen) {
126 if (str[len] == '\0') {
127 break;
128 }
129 ++len;
130 }
131 Base::operator=(Base(str, len));
132 }
133
135 StringView(const std::string& str) noexcept
136 : Base(str.c_str(), str.size())
137 {
138 }
139
142 template <std::size_t TN>
143 StringView(const char (&str)[TN]) noexcept : Base(str, TN)
144 {
145 if ((0U <= TN) && (back() == '\0')) {
146 remove_suffix(1);
147 }
148 }
149
152 template <std::size_t TN>
153 StringView(char (&str)[TN]) noexcept : Base(str, TN)
154 {
155 if ((0U <= TN) && (back() == '\0')) {
156 remove_suffix(1);
157 }
158 }
159
161 ~StringView() noexcept = default;
162
164 StringView& operator=(const StringView&) = default;
165
168 template <std::size_t TN>
169 StringView& operator=(const char (&str)[TN])
170 {
171 Base::operator=(str);
172 if ((0U <= TN) && (back() == '\0')) {
173 remove_suffix(1);
174 }
175 return *this;
176 }
177
180 template <std::size_t TN>
181 StringView& operator=(char (&str)[TN])
182 {
183 Base::operator=(str);
184 if ((0U <= TN) && (back() == '\0')) {
185 remove_suffix(1);
186 }
187 return *this;
188 }
189
191 constexpr iterator begin() const noexcept
192 {
193 return Base::begin();
194 }
195
197 constexpr const_iterator cbegin() const noexcept
198 {
199 return Base::cbegin();
200 }
201
203 constexpr iterator end() const noexcept
204 {
205 return Base::end();
206 }
207
209 constexpr const_iterator cend() const noexcept
210 {
211 return Base::cend();
212 }
213
216 {
217 return Base::rbegin();
218 }
219
222 {
223 return Base::crbegin();
224 }
225
227 reverse_iterator rend() const noexcept
228 {
229 return Base::rend();
230 }
231
234 {
235 return Base::crend();
236 }
237
240 {
241 return Base::operator[](pos);
242 }
243
247 {
248 return Base::at(pos);
249 }
250
252 constexpr const_reference front() const
253 {
254 return Base::front();
255 }
256
258 constexpr const_reference back() const
259 {
260 return Base::back();
261 }
262
264 constexpr const_pointer data() const noexcept
265 {
266 return &(*begin());
267 }
268
270 constexpr size_type size() const noexcept
271 {
272 return Base::size();
273 }
274
276 constexpr size_type length() const noexcept
277 {
278 return Base::length();
279 }
280
282 constexpr bool empty() const noexcept
283 {
284 return Base::empty();
285 }
286
288 std::string substr(size_type pos = 0, size_type count = npos) const
289 {
290 COMMS_ASSERT(pos <= size());
291 return std::string(begin() + pos, begin() + pos + std::min(size() - pos, count));
292 }
293
296 {
298 }
299
302 {
304 }
305
307 void swap(StringView& other) noexcept
308 {
309 Base::swap(other);
310 }
311
313 size_type copy(char* dest, size_type count, size_type pos = 0) const
314 {
315 if (size() <= pos) {
316 return 0U;
317 }
318
319 auto toCopy = std::min(count, size() - pos);
320 std::copy_n(cbegin() + pos, toCopy, dest);
321 return toCopy;
322 }
323
325 int compare(const StringView& other) const
326 {
327 return compare(0, size(), other);
328 }
329
331 int compare(size_type pos, size_type count, const StringView& other) const
332 {
333 return compare(pos, count, other, 0, other.size());
334 }
335
338 size_type pos1,
339 size_type count1,
340 const StringView& other,
341 size_type pos2,
342 size_type count2) const
343 {
344 COMMS_ASSERT(pos1 <= size());
345 COMMS_ASSERT(pos2 <= other.size());
346 count1 = std::min(count1, size() - pos1);
347 count2 = std::min(count2, other.size() - pos2);
348 auto minCount = std::min(count1, count2);
349 for (auto idx = 0U; idx < minCount; ++idx) {
350 auto thisCh = (*this)[pos1 + idx];
351 auto otherCh = other[pos2 + idx];
352 auto diff = static_cast<int>(thisCh) - static_cast<int>(otherCh);
353 if (diff != 0) {
354 return diff;
355 }
356 }
357
358 return static_cast<int>(count1) - static_cast<int>(count2);
359 }
360
362 int compare(const char* s) const
363 {
364 return compare(0U, size(), s);
365 }
366
368 int compare(size_type pos, size_type count, const char* s) const
369 {
370 return compare(pos, count, StringView(s));
371 }
372
374 int compare(size_type pos1, size_type count1, const char* s, size_type count2) const
375 {
376 return compare(pos1, count1, StringView(s, count2));
377 }
378
380 size_type find(const StringView& str, size_type pos = 0) const
381 {
382 if (size() <= pos) {
383 return npos;
384 }
385
386 COMMS_ASSERT(pos <= size());
387 auto remCount = size() - pos;
388 if (remCount < str.size()) {
389 return npos;
390 }
391
392 auto maxPos = size() - str.size();
393 for (auto idx = pos; idx <= maxPos; ++idx) {
394 auto thisStrBeg = cbegin() + idx;
395 auto thisStrEnd = thisStrBeg + str.size();
396 if (std::equal(thisStrBeg, thisStrEnd, str.begin())) {
397 return idx;
398 }
399 }
400 return npos;
401 }
402
404 size_type find(char c, size_type pos = 0) const
405 {
406 return find(StringView(&c, 1), pos);
407 }
408
410 size_type find(const char* str, size_type pos, size_type count) const
411 {
412 return find(StringView(str, count), pos);
413 }
414
416 size_type find(const char* str, size_type pos = 0) const
417 {
418 return find(StringView(str), pos);
419 }
422 {
423 if (empty() || (size() <= pos)) {
424 return npos;
425 }
426
427 pos = std::min(pos, size() - 1);
428 for (auto iter = cbegin() + pos; iter != cend(); ++iter) {
429 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
430 if (foundIter != other.cend()) {
431 return static_cast<SizeType>(std::distance(cbegin(), iter));
432 }
433 }
434
435 return npos;
436 }
437
440 {
441 return find_first_of(StringView(&c, 1), pos);
442 }
443
445 size_type find_first_of(const char* str, size_type pos, size_type count)
446 {
447 return find_first_of(StringView(str, count), pos);
448 }
449
451 size_type find_first_of(const char* str, size_type pos = 0)
452 {
453 return find_first_of(StringView(str), pos);
454 }
455
458 {
459 if (empty()) {
460 return npos;
461 }
462
463 pos = std::min(pos, size() - 1);
464 auto begIter = std::reverse_iterator<const_iterator>(cbegin() + pos + 1);
465 auto endIter = std::reverse_iterator<const_iterator>(cbegin());
466 for (auto iter = begIter; iter != endIter; ++iter) {
467 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
468 if (foundIter != other.cend()) {
469 return static_cast<std::size_t>(std::distance(iter, endIter)) - 1U;
470 }
471 }
472
473 return npos;
474 }
475
478 {
479 return find_last_of(StringView(&c, 1), pos);
480 }
481
483 size_type find_last_of(const char* str, size_type pos, size_type count)
484 {
485 return find_last_of(StringView(str, count), pos);
486 }
487
489 size_type find_last_of(const char* str, size_type pos = npos)
490 {
491 return find_last_of(StringView(str), pos);
492 }
493
496 {
497 if (empty() || (size() <= pos)) {
498 return npos;
499 }
500
501 pos = std::min(pos, size() - 1);
502 for (auto iter = cbegin() + pos; iter != cend(); ++iter) {
503 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
504 if (foundIter == other.cend()) {
505 return static_cast<SizeType>(std::distance(cbegin(), iter));
506 }
507 }
508
509 return npos;
510 }
511
514 {
515 return find_first_not_of(StringView(&c, 1), pos);
516 }
517
519 size_type find_first_not_of(const char* str, size_type pos, size_type count)
520 {
521 return find_first_not_of(StringView(str, count), pos);
522 }
523
525 size_type find_first_not_of(const char* str, size_type pos = 0)
526 {
527 return find_first_not_of(StringView(str), pos);
528 }
529
532 {
533 if (empty()) {
534 return npos;
535 }
536
537 pos = std::min(pos, size() - 1);
538 auto begIter = std::reverse_iterator<const_iterator>(cbegin() + pos + 1);
539 auto endIter = std::reverse_iterator<const_iterator>(cbegin());
540 for (auto iter = begIter; iter != endIter; ++iter) {
541 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
542 if (foundIter == other.cend()) {
543 return static_cast<std::size_t>(std::distance(iter, endIter)) - 1U;
544 }
545 }
546
547 return npos;
548 }
549
552 {
553 return find_last_not_of(StringView(&c, 1), pos);
554 }
555
557 size_type find_last_not_of(const char* str, size_type pos, size_type count)
558 {
559 return find_last_not_of(StringView(str, count), pos);
560 }
561
563 size_type find_last_not_of(const char* str, size_type pos = npos)
564 {
565 return find_last_not_of(StringView(str), pos);
566 }
567
568};
569
573inline
574bool operator<(const StringView& str1, const StringView& str2)
575{
576 return std::lexicographical_compare(str1.begin(), str1.end(), str2.begin(), str2.end());
577}
578
579
583inline
584bool operator<=(const StringView& str1, const StringView& str2)
585{
586 return !(str2 < str1);
587}
588
592inline
593bool operator>(const StringView& str1, const StringView& str2)
594{
595 return (str2 < str1);
596}
597
601inline
602bool operator>=(const StringView& str1, const StringView& str2)
603{
604 return !(str1 < str2);
605}
606
610inline
611bool operator==(const StringView& str1, const StringView& str2)
612{
613 return
614 (str1.size() == str2.size()) &&
615 std::equal(str1.begin(), str1.end(), str2.begin());
616}
617
621inline
622bool operator!=(const StringView& str1, const StringView& str2)
623{
624 return !(str1 == str2);
625}
626
627
628} // namespace util
629
630} // namespace comms
631
632namespace std
633{
634
638inline
640{
641 str1.swap(str2);
642}
643
644} // namespace std
Contains comms::util::ArrayView class.
This file contains classes required for generic custom assertion functionality.
#define COMMS_ASSERT(expr)
Generic assert macro.
Definition Assert.h:170
Describes an object that can refer to a constant contiguous sequence of other objects.
Definition ArrayView.h:31
ArrayView & operator=(const ArrayView &)=default
Copy assign.
const_reference at(size_type pos) const
Element access with range check.
Definition ArrayView.h:211
constexpr const_iterator begin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:156
void remove_prefix(size_type n)
Narrow the view by skipping number of elements at the beginning.
Definition ArrayView.h:252
const_reverse_iterator crend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:198
constexpr const_iterator end() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:168
constexpr size_type size() const noexcept
Get number of element in the view.
Definition ArrayView.h:232
std::reverse_iterator< const_iterator > const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition ArrayView.h:83
std::size_t size_type
Equal to std::size_t.
Definition ArrayView.h:64
char * pointer
Pointer to the single element (T*)
Definition ArrayView.h:40
reverse_iterator rend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:192
void remove_suffix(size_type n)
Narrow the view by dropping number of elements at the end.
Definition ArrayView.h:260
constexpr const_reference operator[](size_type pos) const
Element access operator.
Definition ArrayView.h:204
constexpr size_type length() const noexcept
Same as ref size()
Definition ArrayView.h:238
constexpr bool empty() const noexcept
Check the view is empty.
Definition ArrayView.h:245
constexpr const_iterator cend() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:174
void swap(ArrayView &other) noexcept
Swap contents of two views.
Definition ArrayView.h:266
const_pointer const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is T.
Definition ArrayView.h:71
char & reference
Reference to an element (T&)
Definition ArrayView.h:52
char value_type
Type of the single element.
Definition ArrayView.h:34
constexpr const_iterator cbegin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:162
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:186
const char & const_reference
Reference to a const element (const T&)
Definition ArrayView.h:58
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:180
const char * const_pointer
Pointer to the constant element (const T*)
Definition ArrayView.h:46
constexpr const_reference front() const
Access the first element.
Definition ArrayView.h:219
constexpr const_reference back() const
Access the last element.
Definition ArrayView.h:226
Describes an object that can refer to a constant contiguous sequence of char-like objects with the fi...
Definition StringView.h:35
constexpr const_reference front() const
Same as std::string_view::front()
Definition StringView.h:252
reference Reference
Same as reference.
Definition StringView.h:60
void swap(StringView &other) noexcept
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/swap>std::string_view::swa...
Definition StringView.h:307
const_reverse_iterator ConstReverseIterator
Same as const_reverse_iterator.
Definition StringView.h:91
bool operator<(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:574
const_reverse_iterator reverse_iterator
Same as const_reverse_iterator.
Definition StringView.h:94
size_type find_first_of(const char *str, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_of>std::string_...
Definition StringView.h:451
size_type find_last_of(char c, size_type pos=npos)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_of>std::string_v...
Definition StringView.h:477
size_type find_first_of(const char *str, size_type pos, size_type count)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_of>std::string_...
Definition StringView.h:445
size_type find_last_not_of(const char *str, size_type pos=npos)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_not_of>std::stri...
Definition StringView.h:563
int compare(size_type pos1, size_type count1, const StringView &other, size_type pos2, size_type count2) const
Same as std::string_view::compare().
Definition StringView.h:337
size_type find_first_not_of(char c, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_not_of>std::str...
Definition StringView.h:513
const_reverse_iterator crend() const noexcept
Same as std::string_view::crend().
Definition StringView.h:233
StringView(const char *str) noexcept
Constructor.
Definition StringView.h:121
int compare(const char *s) const
Same as std::string_view::compare().
Definition StringView.h:362
const_reference ConstReference
Same as const_reference.
Definition StringView.h:66
typename Base::pointer pointer
Pointer to the character (char*)
Definition StringView.h:45
int compare(const StringView &other) const
Same as std::string_view::compare().
Definition StringView.h:325
const_reverse_iterator rbegin() const noexcept
Same as std::string_view::rbegin().
Definition StringView.h:215
value_type ValueType
Same as value_type.
Definition StringView.h:42
int compare(size_type pos, size_type count, const StringView &other) const
Same as std::string_view::compare().
Definition StringView.h:331
size_type find(char c, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:404
void remove_suffix(size_type n)
Same as std::string_view::remove_suffix()
Definition StringView.h:301
size_type find_first_of(const StringView &other, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_of>std::string_...
Definition StringView.h:421
size_type find_first_not_of(const char *str, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_not_of>std::str...
Definition StringView.h:525
constexpr const_reference back() const
Same as std::string_view::back()
Definition StringView.h:258
StringView(const char(&str)[TN]) noexcept
Construct out of array of characters with known size.
Definition StringView.h:143
typename Base::const_reference const_reference
Reference to a const character (const char&)
Definition StringView.h:63
typename Base::const_reverse_iterator const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition StringView.h:88
constexpr const_iterator cbegin() const noexcept
Same as std::string_view::cbegin().
Definition StringView.h:197
size_type find_last_of(const char *str, size_type pos=npos)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_of>std::string_v...
Definition StringView.h:489
void swap(comms::util::StringView &str1, comms::util::StringView &str2)
Specializes the std::swap algorithm.
Definition StringView.h:639
constexpr const_pointer data() const noexcept
Same as std::string_view::data()
Definition StringView.h:264
static const auto npos
Special value, the meaning is the same as std::string_view::npos.
Definition StringView.h:101
const_iterator iterator
Same as const_iterator.
Definition StringView.h:82
constexpr bool empty() const noexcept
Same as std::string_view::empty()
Definition StringView.h:282
size_type find(const StringView &str, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:380
typename Base::const_iterator const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is char.
Definition StringView.h:76
size_type copy(char *dest, size_type count, size_type pos=0) const
Same as std::string_view::copy().
Definition StringView.h:313
typename Base::size_type size_type
Equal to std::size_t.
Definition StringView.h:69
const_pointer ConstPointer
Same as const_pointer.
Definition StringView.h:54
void remove_prefix(size_type n)
Same as std::string_view::remove_prefix()
Definition StringView.h:295
bool operator>=(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:602
reverse_iterator ReverseIterator
Same as reverse_iterator.
Definition StringView.h:97
~StringView() noexcept=default
Destructor.
constexpr iterator begin() const noexcept
Same as std::string_view::begin().
Definition StringView.h:191
StringView(const std::string &str) noexcept
Constructor.
Definition StringView.h:135
size_type find_last_of(const StringView &other, size_type pos=npos)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_of>std::string_v...
Definition StringView.h:457
int compare(size_type pos, size_type count, const char *s) const
Same as std::string_view::compare().
Definition StringView.h:368
size_type SizeType
Same as size_type;.
Definition StringView.h:72
reverse_iterator rend() const noexcept
Same as std::string_view::rend().
Definition StringView.h:227
iterator Iterator
Same as iterator.
Definition StringView.h:85
size_type find_last_not_of(const StringView &other, size_type pos=npos)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_not_of>std::stri...
Definition StringView.h:531
StringView() noexcept=default
Default constructor.
int compare(size_type pos1, size_type count1, const char *s, size_type count2) const
Same as std::string_view::compare().
Definition StringView.h:374
bool operator==(const StringView &str1, const StringView &str2)
Equality compare between the string views.
Definition StringView.h:611
pointer Pointer
Same as pointer.
Definition StringView.h:48
constexpr size_type length() const noexcept
Same as std::string_view::length()
Definition StringView.h:276
StringView & operator=(char(&str)[TN])
Assign array of characters with known size.
Definition StringView.h:181
typename Base::value_type value_type
Type of the character (char)
Definition StringView.h:39
constexpr const_reference operator[](size_type pos) const
Same as std::string_view::oprator[]()
Definition StringView.h:239
size_type find(const char *str, size_type pos, size_type count) const
Same as std::string_view::find().
Definition StringView.h:410
bool operator!=(const StringView &str1, const StringView &str2)
Inequality compare between the string views.
Definition StringView.h:622
constexpr iterator end() const noexcept
Same as std::string_view::end().
Definition StringView.h:203
bool operator>(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:593
std::string substr(size_type pos=0, size_type count=npos) const
Same as std::string_view::substr()
Definition StringView.h:288
size_type find_first_not_of(const char *str, size_type pos, size_type count)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_not_of>std::str...
Definition StringView.h:519
typename Base::reference reference
Reference to a character (char&)
Definition StringView.h:57
size_type find_last_of(const char *str, size_type pos, size_type count)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_of>std::string_v...
Definition StringView.h:483
const_reverse_iterator crbegin() const noexcept
Same as std::string_view::crbegin().
Definition StringView.h:221
typename Base::const_pointer const_pointer
Pointer to the constant character (const char*)
Definition StringView.h:51
size_type find_last_not_of(char c, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_not_of>std::stri...
Definition StringView.h:551
constexpr size_type size() const noexcept
Same as std::string_view::size()
Definition StringView.h:270
const_reference at(size_type pos) const
Similar to std::string::at()
Definition StringView.h:246
StringView(char(&str)[TN]) noexcept
Construct out of array of characters with known size.
Definition StringView.h:153
const_iterator ConstIterator
Same as const_iterator.
Definition StringView.h:79
size_type find_first_not_of(const StringView &other, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_not_of>std::str...
Definition StringView.h:495
bool operator<=(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:584
size_type find_last_not_of(const char *str, size_type pos, size_type count)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_last_not_of>std::stri...
Definition StringView.h:557
size_type find_first_of(char c, size_type pos=0)
Same as <a href="http://en.cppreference.com/w/cpp/string/basic_string_view/find_first_of>std::string_...
Definition StringView.h:439
constexpr const_iterator cend() const noexcept
Same as std::string_view::end().
Definition StringView.h:209
size_type find(const char *str, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:416
Main namespace for all classes / functions of COMMS library.
STL namespace.