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 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include "comms/Assert.h"
17
18#include <algorithm>
19#include <cstddef>
20#include <iterator>
21#include <limits>
22#include <string>
23
24namespace comms
25{
26
27namespace util
28{
29
36class StringView : public ArrayView<char>
37{
38 using Base = ArrayView<char>;
39public:
41 using value_type = typename Base::value_type;
42
45
47 using pointer = typename Base::pointer;
48
51
54
57
59 using reference = typename Base::reference;
60
63
66
69
71 using size_type = typename Base::size_type;
72
75
79
82
85
88
91
94
97
100
103 static const auto npos = static_cast<SizeType>(-1);
104
108 StringView() noexcept = default;
109
113 StringView(const StringView&) noexcept = default;
114
118 StringView(const char* str, size_type len) noexcept : Base(str, len) {}
119
123 StringView(const char* str) noexcept
124 {
125 static const auto MaxLen = std::numeric_limits<size_type>::max();
126 size_type len = 0;
127 while (len < MaxLen) {
128 if (str[len] == '\0') {
129 break;
130 }
131 ++len;
132 }
133 Base::operator=(Base(str, len));
134 }
135
137 StringView(const std::string& str) noexcept
138 : Base(str.c_str(), str.size())
139 {
140 }
141
144 template <std::size_t TN>
145 StringView(const char (&str)[TN]) noexcept : Base(str, TN)
146 {
147 if ((0U <= TN) && (back() == '\0')) {
148 remove_suffix(1);
149 }
150 }
151
154 template <std::size_t TN>
155 StringView(char (&str)[TN]) noexcept : Base(str, TN)
156 {
157 if ((0U <= TN) && (back() == '\0')) {
158 remove_suffix(1);
159 }
160 }
161
163 ~StringView() noexcept = default;
164
166 StringView& operator=(const StringView&) = default;
167
170 template <std::size_t TN>
171 StringView& operator=(const char (&str)[TN])
172 {
173 Base::operator=(str);
174 if ((0U <= TN) && (back() == '\0')) {
175 remove_suffix(1);
176 }
177 return *this;
178 }
179
182 template <std::size_t TN>
183 StringView& operator=(char (&str)[TN])
184 {
185 Base::operator=(str);
186 if ((0U <= TN) && (back() == '\0')) {
187 remove_suffix(1);
188 }
189 return *this;
190 }
191
193 constexpr iterator begin() const noexcept
194 {
195 return Base::begin();
196 }
197
199 constexpr const_iterator cbegin() const noexcept
200 {
201 return Base::cbegin();
202 }
203
205 constexpr iterator end() const noexcept
206 {
207 return Base::end();
208 }
209
211 constexpr const_iterator cend() const noexcept
212 {
213 return Base::cend();
214 }
215
218 {
219 return Base::rbegin();
220 }
221
224 {
225 return Base::crbegin();
226 }
227
229 reverse_iterator rend() const noexcept
230 {
231 return Base::rend();
232 }
233
236 {
237 return Base::crend();
238 }
239
242 {
243 return Base::operator[](pos);
244 }
245
249 {
250 return Base::at(pos);
251 }
252
254 constexpr const_reference front() const
255 {
256 return Base::front();
257 }
258
260 constexpr const_reference back() const
261 {
262 return Base::back();
263 }
264
266 constexpr const_pointer data() const noexcept
267 {
268 return &(*begin());
269 }
270
272 constexpr size_type size() const noexcept
273 {
274 return Base::size();
275 }
276
278 constexpr size_type length() const noexcept
279 {
280 return Base::length();
281 }
282
284 constexpr bool empty() const noexcept
285 {
286 return Base::empty();
287 }
288
290 std::string substr(size_type pos = 0, size_type count = npos) const
291 {
292 COMMS_ASSERT(pos <= size());
293 return std::string(begin() + pos, begin() + pos + std::min(size() - pos, count));
294 }
295
298 {
300 }
301
304 {
306 }
307
309 void swap(StringView& other) noexcept
310 {
311 Base::swap(other);
312 }
313
315 size_type copy(char* dest, size_type count, size_type pos = 0) const
316 {
317 if (size() <= pos) {
318 return 0U;
319 }
320
321 auto toCopy = std::min(count, size() - pos);
322 std::copy_n(cbegin() + pos, toCopy, dest);
323 return toCopy;
324 }
325
327 int compare(const StringView& other) const
328 {
329 return compare(0, size(), other);
330 }
331
333 int compare(size_type pos, size_type count, const StringView& other) const
334 {
335 return compare(pos, count, other, 0, other.size());
336 }
337
340 size_type pos1,
341 size_type count1,
342 const StringView& other,
343 size_type pos2,
344 size_type count2) const
345 {
346 COMMS_ASSERT(pos1 <= size());
347 COMMS_ASSERT(pos2 <= other.size());
348 count1 = std::min(count1, size() - pos1);
349 count2 = std::min(count2, other.size() - pos2);
350 auto minCount = std::min(count1, count2);
351 for (auto idx = 0U; idx < minCount; ++idx) {
352 auto thisCh = (*this)[pos1 + idx];
353 auto otherCh = other[pos2 + idx];
354 auto diff = static_cast<int>(thisCh) - static_cast<int>(otherCh);
355 if (diff != 0) {
356 return diff;
357 }
358 }
359
360 return static_cast<int>(count1) - static_cast<int>(count2);
361 }
362
364 int compare(const char* s) const
365 {
366 return compare(0U, size(), s);
367 }
368
370 int compare(size_type pos, size_type count, const char* s) const
371 {
372 return compare(pos, count, StringView(s));
373 }
374
376 int compare(size_type pos1, size_type count1, const char* s, size_type count2) const
377 {
378 return compare(pos1, count1, StringView(s, count2));
379 }
380
382 size_type find(const StringView& str, size_type pos = 0) const
383 {
384 if (size() <= pos) {
385 return npos;
386 }
387
388 COMMS_ASSERT(pos <= size());
389 auto remCount = size() - pos;
390 if (remCount < str.size()) {
391 return npos;
392 }
393
394 auto maxPos = size() - str.size();
395 for (auto idx = pos; idx <= maxPos; ++idx) {
396 auto thisStrBeg = cbegin() + idx;
397 auto thisStrEnd = thisStrBeg + str.size();
398 if (std::equal(thisStrBeg, thisStrEnd, str.begin())) {
399 return idx;
400 }
401 }
402 return npos;
403 }
404
406 size_type find(char c, size_type pos = 0) const
407 {
408 return find(StringView(&c, 1), pos);
409 }
410
412 size_type find(const char* str, size_type pos, size_type count) const
413 {
414 return find(StringView(str, count), pos);
415 }
416
418 size_type find(const char* str, size_type pos = 0) const
419 {
420 return find(StringView(str), pos);
421 }
424 {
425 if (empty() || (size() <= pos)) {
426 return npos;
427 }
428
429 pos = std::min(pos, size() - 1);
430 for (auto iter = cbegin() + pos; iter != cend(); ++iter) {
431 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
432 if (foundIter != other.cend()) {
433 return static_cast<SizeType>(std::distance(cbegin(), iter));
434 }
435 }
436
437 return npos;
438 }
439
442 {
443 return find_first_of(StringView(&c, 1), pos);
444 }
445
447 size_type find_first_of(const char* str, size_type pos, size_type count)
448 {
449 return find_first_of(StringView(str, count), pos);
450 }
451
453 size_type find_first_of(const char* str, size_type pos = 0)
454 {
455 return find_first_of(StringView(str), pos);
456 }
457
460 {
461 if (empty()) {
462 return npos;
463 }
464
465 pos = std::min(pos, size() - 1);
466 auto begIter = std::reverse_iterator<const_iterator>(cbegin() + pos + 1);
467 auto endIter = std::reverse_iterator<const_iterator>(cbegin());
468 for (auto iter = begIter; iter != endIter; ++iter) {
469 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
470 if (foundIter != other.cend()) {
471 return static_cast<std::size_t>(std::distance(iter, endIter)) - 1U;
472 }
473 }
474
475 return npos;
476 }
477
480 {
481 return find_last_of(StringView(&c, 1), pos);
482 }
483
485 size_type find_last_of(const char* str, size_type pos, size_type count)
486 {
487 return find_last_of(StringView(str, count), pos);
488 }
489
491 size_type find_last_of(const char* str, size_type pos = npos)
492 {
493 return find_last_of(StringView(str), pos);
494 }
495
498 {
499 if (empty() || (size() <= pos)) {
500 return npos;
501 }
502
503 pos = std::min(pos, size() - 1);
504 for (auto iter = cbegin() + pos; iter != cend(); ++iter) {
505 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
506 if (foundIter == other.cend()) {
507 return static_cast<SizeType>(std::distance(cbegin(), iter));
508 }
509 }
510
511 return npos;
512 }
513
516 {
517 return find_first_not_of(StringView(&c, 1), pos);
518 }
519
521 size_type find_first_not_of(const char* str, size_type pos, size_type count)
522 {
523 return find_first_not_of(StringView(str, count), pos);
524 }
525
527 size_type find_first_not_of(const char* str, size_type pos = 0)
528 {
529 return find_first_not_of(StringView(str), pos);
530 }
531
534 {
535 if (empty()) {
536 return npos;
537 }
538
539 pos = std::min(pos, size() - 1);
540 auto begIter = std::reverse_iterator<const_iterator>(cbegin() + pos + 1);
541 auto endIter = std::reverse_iterator<const_iterator>(cbegin());
542 for (auto iter = begIter; iter != endIter; ++iter) {
543 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
544 if (foundIter == other.cend()) {
545 return static_cast<std::size_t>(std::distance(iter, endIter)) - 1U;
546 }
547 }
548
549 return npos;
550 }
551
554 {
555 return find_last_not_of(StringView(&c, 1), pos);
556 }
557
559 size_type find_last_not_of(const char* str, size_type pos, size_type count)
560 {
561 return find_last_not_of(StringView(str, count), pos);
562 }
563
565 size_type find_last_not_of(const char* str, size_type pos = npos)
566 {
567 return find_last_not_of(StringView(str), pos);
568 }
569
570};
571
575inline
576bool operator<(const StringView& str1, const StringView& str2)
577{
578 return std::lexicographical_compare(str1.begin(), str1.end(), str2.begin(), str2.end());
579}
580
584inline
585bool operator<=(const StringView& str1, const StringView& str2)
586{
587 return !(str2 < str1);
588}
589
593inline
594bool operator>(const StringView& str1, const StringView& str2)
595{
596 return (str2 < str1);
597}
598
602inline
603bool operator>=(const StringView& str1, const StringView& str2)
604{
605 return !(str1 < str2);
606}
607
611inline
612bool operator==(const StringView& str1, const StringView& str2)
613{
614 return
615 (str1.size() == str2.size()) &&
616 std::equal(str1.begin(), str1.end(), str2.begin());
617}
618
622inline
623bool operator!=(const StringView& str1, const StringView& str2)
624{
625 return !(str1 == str2);
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:32
ArrayView & operator=(const ArrayView &)=default
Copy assign.
const_reference at(size_type pos) const
Element access with range check.
Definition ArrayView.h:221
constexpr const_iterator begin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:166
void remove_prefix(size_type n)
Narrow the view by skipping number of elements at the beginning.
Definition ArrayView.h:262
const_reverse_iterator crend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:208
constexpr const_iterator end() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:178
constexpr size_type size() const noexcept
Get number of element in the view.
Definition ArrayView.h:242
std::reverse_iterator< const_iterator > const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition ArrayView.h:84
std::size_t size_type
Equal to std::size_t.
Definition ArrayView.h:65
char * pointer
Pointer to the single element (T*)
Definition ArrayView.h:41
reverse_iterator rend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:202
void remove_suffix(size_type n)
Narrow the view by dropping number of elements at the end.
Definition ArrayView.h:270
constexpr const_reference operator[](size_type pos) const
Element access operator.
Definition ArrayView.h:214
constexpr size_type length() const noexcept
Same as ref size()
Definition ArrayView.h:248
constexpr bool empty() const noexcept
Check the view is empty.
Definition ArrayView.h:255
constexpr const_iterator cend() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:184
void swap(ArrayView &other) noexcept
Swap contents of two views.
Definition ArrayView.h:276
const_pointer const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is T.
Definition ArrayView.h:72
char & reference
Reference to an element (T&)
Definition ArrayView.h:53
char value_type
Type of the single element.
Definition ArrayView.h:35
constexpr const_iterator cbegin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:172
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:196
const char & const_reference
Reference to a const element (const T&)
Definition ArrayView.h:59
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:190
const char * const_pointer
Pointer to the constant element (const T*)
Definition ArrayView.h:47
constexpr const_reference front() const
Access the first element.
Definition ArrayView.h:229
constexpr const_reference back() const
Access the last element.
Definition ArrayView.h:236
Describes an object that can refer to a constant contiguous sequence of char-like objects with the fi...
Definition StringView.h:37
constexpr const_reference front() const
Same as std::string_view::front()
Definition StringView.h:254
reference Reference
Same as reference.
Definition StringView.h:62
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:309
const_reverse_iterator ConstReverseIterator
Same as const_reverse_iterator.
Definition StringView.h:93
bool operator<(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:576
const_reverse_iterator reverse_iterator
Same as const_reverse_iterator.
Definition StringView.h:96
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:453
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:479
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:447
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:565
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:339
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:515
const_reverse_iterator crend() const noexcept
Same as std::string_view::crend().
Definition StringView.h:235
StringView(const char *str) noexcept
Constructor.
Definition StringView.h:123
int compare(const char *s) const
Same as std::string_view::compare().
Definition StringView.h:364
const_reference ConstReference
Same as const_reference.
Definition StringView.h:68
typename Base::pointer pointer
Pointer to the character (char*)
Definition StringView.h:47
int compare(const StringView &other) const
Same as std::string_view::compare().
Definition StringView.h:327
const_reverse_iterator rbegin() const noexcept
Same as std::string_view::rbegin().
Definition StringView.h:217
value_type ValueType
Same as value_type.
Definition StringView.h:44
int compare(size_type pos, size_type count, const StringView &other) const
Same as std::string_view::compare().
Definition StringView.h:333
size_type find(char c, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:406
void remove_suffix(size_type n)
Same as std::string_view::remove_suffix()
Definition StringView.h:303
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:423
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:527
constexpr const_reference back() const
Same as std::string_view::back()
Definition StringView.h:260
StringView(const char(&str)[TN]) noexcept
Construct out of array of characters with known size.
Definition StringView.h:145
typename Base::const_reference const_reference
Reference to a const character (const char&)
Definition StringView.h:65
typename Base::const_reverse_iterator const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition StringView.h:90
constexpr const_iterator cbegin() const noexcept
Same as std::string_view::cbegin().
Definition StringView.h:199
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:491
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:266
static const auto npos
Special value, the meaning is the same as std::string_view::npos.
Definition StringView.h:103
const_iterator iterator
Same as const_iterator.
Definition StringView.h:84
constexpr bool empty() const noexcept
Same as std::string_view::empty()
Definition StringView.h:284
size_type find(const StringView &str, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:382
typename Base::const_iterator const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is char.
Definition StringView.h:78
size_type copy(char *dest, size_type count, size_type pos=0) const
Same as std::string_view::copy().
Definition StringView.h:315
typename Base::size_type size_type
Equal to std::size_t.
Definition StringView.h:71
const_pointer ConstPointer
Same as const_pointer.
Definition StringView.h:56
void remove_prefix(size_type n)
Same as std::string_view::remove_prefix()
Definition StringView.h:297
bool operator>=(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:603
reverse_iterator ReverseIterator
Same as reverse_iterator.
Definition StringView.h:99
~StringView() noexcept=default
Destructor.
constexpr iterator begin() const noexcept
Same as std::string_view::begin().
Definition StringView.h:193
StringView(const std::string &str) noexcept
Constructor.
Definition StringView.h:137
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:459
int compare(size_type pos, size_type count, const char *s) const
Same as std::string_view::compare().
Definition StringView.h:370
size_type SizeType
Same as size_type;.
Definition StringView.h:74
reverse_iterator rend() const noexcept
Same as std::string_view::rend().
Definition StringView.h:229
iterator Iterator
Same as iterator.
Definition StringView.h:87
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:533
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:376
bool operator==(const StringView &str1, const StringView &str2)
Equality compare between the string views.
Definition StringView.h:612
pointer Pointer
Same as pointer.
Definition StringView.h:50
constexpr size_type length() const noexcept
Same as std::string_view::length()
Definition StringView.h:278
StringView & operator=(char(&str)[TN])
Assign array of characters with known size.
Definition StringView.h:183
typename Base::value_type value_type
Type of the character (char)
Definition StringView.h:41
constexpr const_reference operator[](size_type pos) const
Same as std::string_view::oprator[]()
Definition StringView.h:241
size_type find(const char *str, size_type pos, size_type count) const
Same as std::string_view::find().
Definition StringView.h:412
bool operator!=(const StringView &str1, const StringView &str2)
Inequality compare between the string views.
Definition StringView.h:623
constexpr iterator end() const noexcept
Same as std::string_view::end().
Definition StringView.h:205
bool operator>(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:594
std::string substr(size_type pos=0, size_type count=npos) const
Same as std::string_view::substr()
Definition StringView.h:290
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:521
typename Base::reference reference
Reference to a character (char&)
Definition StringView.h:59
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:485
const_reverse_iterator crbegin() const noexcept
Same as std::string_view::crbegin().
Definition StringView.h:223
typename Base::const_pointer const_pointer
Pointer to the constant character (const char*)
Definition StringView.h:53
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:553
constexpr size_type size() const noexcept
Same as std::string_view::size()
Definition StringView.h:272
const_reference at(size_type pos) const
Similar to std::string_view::at()
Definition StringView.h:248
StringView(char(&str)[TN]) noexcept
Construct out of array of characters with known size.
Definition StringView.h:155
const_iterator ConstIterator
Same as const_iterator.
Definition StringView.h:81
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:497
bool operator<=(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:585
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:559
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:441
constexpr const_iterator cend() const noexcept
Same as std::string_view::end().
Definition StringView.h:211
size_type find(const char *str, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:418
Main namespace for all classes / functions of COMMS library.
STL namespace.