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 - 2025 (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#pragma once
12
13#include "comms/Assert.h"
15
16#include <algorithm>
17#include <iterator>
18#include <limits>
19#include <string>
20
21namespace comms
22{
23
24namespace util
25{
26
33class StringView : public ArrayView<char>
34{
35 using Base = ArrayView<char>;
36public:
38 using value_type = typename Base::value_type;
39
42
44 using pointer = typename Base::pointer;
45
48
51
54
56 using reference = typename Base::reference;
57
60
63
66
68 using size_type = typename Base::size_type;
69
72
76
79
82
85
88
91
94
97
100 static const auto npos = static_cast<SizeType>(-1);
101
105 StringView() noexcept = default;
106
110 StringView(const StringView&) noexcept = default;
111
115 StringView(const char* str, size_type len) noexcept : Base(str, len) {}
116
120 StringView(const char* str) noexcept
121 {
122 static const auto MaxLen = std::numeric_limits<size_type>::max();
123 size_type len = 0;
124 while (len < MaxLen) {
125 if (str[len] == '\0') {
126 break;
127 }
128 ++len;
129 }
130 Base::operator=(Base(str, len));
131 }
132
134 StringView(const std::string& str) noexcept
135 : Base(str.c_str(), str.size())
136 {
137 }
138
141 template <std::size_t TN>
142 StringView(const char (&str)[TN]) noexcept : Base(str, TN)
143 {
144 if ((0U <= TN) && (back() == '\0')) {
145 remove_suffix(1);
146 }
147 }
148
151 template <std::size_t TN>
152 StringView(char (&str)[TN]) noexcept : Base(str, TN)
153 {
154 if ((0U <= TN) && (back() == '\0')) {
155 remove_suffix(1);
156 }
157 }
158
160 ~StringView() noexcept = default;
161
163 StringView& operator=(const StringView&) = default;
164
167 template <std::size_t TN>
168 StringView& operator=(const char (&str)[TN])
169 {
170 Base::operator=(str);
171 if ((0U <= TN) && (back() == '\0')) {
172 remove_suffix(1);
173 }
174 return *this;
175 }
176
179 template <std::size_t TN>
180 StringView& operator=(char (&str)[TN])
181 {
182 Base::operator=(str);
183 if ((0U <= TN) && (back() == '\0')) {
184 remove_suffix(1);
185 }
186 return *this;
187 }
188
190 constexpr iterator begin() const noexcept
191 {
192 return Base::begin();
193 }
194
196 constexpr const_iterator cbegin() const noexcept
197 {
198 return Base::cbegin();
199 }
200
202 constexpr iterator end() const noexcept
203 {
204 return Base::end();
205 }
206
208 constexpr const_iterator cend() const noexcept
209 {
210 return Base::cend();
211 }
212
215 {
216 return Base::rbegin();
217 }
218
221 {
222 return Base::crbegin();
223 }
224
226 reverse_iterator rend() const noexcept
227 {
228 return Base::rend();
229 }
230
233 {
234 return Base::crend();
235 }
236
239 {
240 return Base::operator[](pos);
241 }
242
246 {
247 return Base::at(pos);
248 }
249
251 constexpr const_reference front() const
252 {
253 return Base::front();
254 }
255
257 constexpr const_reference back() const
258 {
259 return Base::back();
260 }
261
263 constexpr const_pointer data() const noexcept
264 {
265 return &(*begin());
266 }
267
269 constexpr size_type size() const noexcept
270 {
271 return Base::size();
272 }
273
275 constexpr size_type length() const noexcept
276 {
277 return Base::length();
278 }
279
281 constexpr bool empty() const noexcept
282 {
283 return Base::empty();
284 }
285
287 std::string substr(size_type pos = 0, size_type count = npos) const
288 {
289 COMMS_ASSERT(pos <= size());
290 return std::string(begin() + pos, begin() + pos + std::min(size() - pos, count));
291 }
292
295 {
297 }
298
301 {
303 }
304
306 void swap(StringView& other) noexcept
307 {
308 Base::swap(other);
309 }
310
312 size_type copy(char* dest, size_type count, size_type pos = 0) const
313 {
314 if (size() <= pos) {
315 return 0U;
316 }
317
318 auto toCopy = std::min(count, size() - pos);
319 std::copy_n(cbegin() + pos, toCopy, dest);
320 return toCopy;
321 }
322
324 int compare(const StringView& other) const
325 {
326 return compare(0, size(), other);
327 }
328
330 int compare(size_type pos, size_type count, const StringView& other) const
331 {
332 return compare(pos, count, other, 0, other.size());
333 }
334
337 size_type pos1,
338 size_type count1,
339 const StringView& other,
340 size_type pos2,
341 size_type count2) const
342 {
343 COMMS_ASSERT(pos1 <= size());
344 COMMS_ASSERT(pos2 <= other.size());
345 count1 = std::min(count1, size() - pos1);
346 count2 = std::min(count2, other.size() - pos2);
347 auto minCount = std::min(count1, count2);
348 for (auto idx = 0U; idx < minCount; ++idx) {
349 auto thisCh = (*this)[pos1 + idx];
350 auto otherCh = other[pos2 + idx];
351 auto diff = static_cast<int>(thisCh) - static_cast<int>(otherCh);
352 if (diff != 0) {
353 return diff;
354 }
355 }
356
357 return static_cast<int>(count1) - static_cast<int>(count2);
358 }
359
361 int compare(const char* s) const
362 {
363 return compare(0U, size(), s);
364 }
365
367 int compare(size_type pos, size_type count, const char* s) const
368 {
369 return compare(pos, count, StringView(s));
370 }
371
373 int compare(size_type pos1, size_type count1, const char* s, size_type count2) const
374 {
375 return compare(pos1, count1, StringView(s, count2));
376 }
377
379 size_type find(const StringView& str, size_type pos = 0) const
380 {
381 if (size() <= pos) {
382 return npos;
383 }
384
385 COMMS_ASSERT(pos <= size());
386 auto remCount = size() - pos;
387 if (remCount < str.size()) {
388 return npos;
389 }
390
391 auto maxPos = size() - str.size();
392 for (auto idx = pos; idx <= maxPos; ++idx) {
393 auto thisStrBeg = cbegin() + idx;
394 auto thisStrEnd = thisStrBeg + str.size();
395 if (std::equal(thisStrBeg, thisStrEnd, str.begin())) {
396 return idx;
397 }
398 }
399 return npos;
400 }
401
403 size_type find(char c, size_type pos = 0) const
404 {
405 return find(StringView(&c, 1), pos);
406 }
407
409 size_type find(const char* str, size_type pos, size_type count) const
410 {
411 return find(StringView(str, count), pos);
412 }
413
415 size_type find(const char* str, size_type pos = 0) const
416 {
417 return find(StringView(str), pos);
418 }
421 {
422 if (empty() || (size() <= pos)) {
423 return npos;
424 }
425
426 pos = std::min(pos, size() - 1);
427 for (auto iter = cbegin() + pos; iter != cend(); ++iter) {
428 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
429 if (foundIter != other.cend()) {
430 return static_cast<SizeType>(std::distance(cbegin(), iter));
431 }
432 }
433
434 return npos;
435 }
436
439 {
440 return find_first_of(StringView(&c, 1), pos);
441 }
442
444 size_type find_first_of(const char* str, size_type pos, size_type count)
445 {
446 return find_first_of(StringView(str, count), pos);
447 }
448
450 size_type find_first_of(const char* str, size_type pos = 0)
451 {
452 return find_first_of(StringView(str), pos);
453 }
454
457 {
458 if (empty()) {
459 return npos;
460 }
461
462 pos = std::min(pos, size() - 1);
463 auto begIter = std::reverse_iterator<const_iterator>(cbegin() + pos + 1);
464 auto endIter = std::reverse_iterator<const_iterator>(cbegin());
465 for (auto iter = begIter; iter != endIter; ++iter) {
466 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
467 if (foundIter != other.cend()) {
468 return static_cast<std::size_t>(std::distance(iter, endIter)) - 1U;
469 }
470 }
471
472 return npos;
473 }
474
477 {
478 return find_last_of(StringView(&c, 1), pos);
479 }
480
482 size_type find_last_of(const char* str, size_type pos, size_type count)
483 {
484 return find_last_of(StringView(str, count), pos);
485 }
486
488 size_type find_last_of(const char* str, size_type pos = npos)
489 {
490 return find_last_of(StringView(str), pos);
491 }
492
495 {
496 if (empty() || (size() <= pos)) {
497 return npos;
498 }
499
500 pos = std::min(pos, size() - 1);
501 for (auto iter = cbegin() + pos; iter != cend(); ++iter) {
502 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
503 if (foundIter == other.cend()) {
504 return static_cast<SizeType>(std::distance(cbegin(), iter));
505 }
506 }
507
508 return npos;
509 }
510
513 {
514 return find_first_not_of(StringView(&c, 1), pos);
515 }
516
518 size_type find_first_not_of(const char* str, size_type pos, size_type count)
519 {
520 return find_first_not_of(StringView(str, count), pos);
521 }
522
524 size_type find_first_not_of(const char* str, size_type pos = 0)
525 {
526 return find_first_not_of(StringView(str), pos);
527 }
528
531 {
532 if (empty()) {
533 return npos;
534 }
535
536 pos = std::min(pos, size() - 1);
537 auto begIter = std::reverse_iterator<const_iterator>(cbegin() + pos + 1);
538 auto endIter = std::reverse_iterator<const_iterator>(cbegin());
539 for (auto iter = begIter; iter != endIter; ++iter) {
540 auto foundIter = std::find(other.cbegin(), other.cend(), *iter);
541 if (foundIter == other.cend()) {
542 return static_cast<std::size_t>(std::distance(iter, endIter)) - 1U;
543 }
544 }
545
546 return npos;
547 }
548
551 {
552 return find_last_not_of(StringView(&c, 1), pos);
553 }
554
556 size_type find_last_not_of(const char* str, size_type pos, size_type count)
557 {
558 return find_last_not_of(StringView(str, count), pos);
559 }
560
562 size_type find_last_not_of(const char* str, size_type pos = npos)
563 {
564 return find_last_not_of(StringView(str), pos);
565 }
566
567};
568
572inline
573bool operator<(const StringView& str1, const StringView& str2)
574{
575 return std::lexicographical_compare(str1.begin(), str1.end(), str2.begin(), str2.end());
576}
577
581inline
582bool operator<=(const StringView& str1, const StringView& str2)
583{
584 return !(str2 < str1);
585}
586
590inline
591bool operator>(const StringView& str1, const StringView& str2)
592{
593 return (str2 < str1);
594}
595
599inline
600bool operator>=(const StringView& str1, const StringView& str2)
601{
602 return !(str1 < str2);
603}
604
608inline
609bool operator==(const StringView& str1, const StringView& str2)
610{
611 return
612 (str1.size() == str2.size()) &&
613 std::equal(str1.begin(), str1.end(), str2.begin());
614}
615
619inline
620bool operator!=(const StringView& str1, const StringView& str2)
621{
622 return !(str1 == str2);
623}
624
625} // namespace util
626
627} // namespace comms
628
629namespace std
630{
631
635inline
637{
638 str1.swap(str2);
639}
640
641} // 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:168
Describes an object that can refer to a constant contiguous sequence of other objects.
Definition ArrayView.h:30
ArrayView & operator=(const ArrayView &)=default
Copy assign.
const_reference at(size_type pos) const
Element access with range check.
Definition ArrayView.h:219
constexpr const_iterator begin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:164
void remove_prefix(size_type n)
Narrow the view by skipping number of elements at the beginning.
Definition ArrayView.h:260
const_reverse_iterator crend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:206
constexpr const_iterator end() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:176
constexpr size_type size() const noexcept
Get number of element in the view.
Definition ArrayView.h:240
std::reverse_iterator< const_iterator > const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition ArrayView.h:82
std::size_t size_type
Equal to std::size_t.
Definition ArrayView.h:63
char * pointer
Pointer to the single element (T*)
Definition ArrayView.h:39
reverse_iterator rend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:200
void remove_suffix(size_type n)
Narrow the view by dropping number of elements at the end.
Definition ArrayView.h:268
constexpr const_reference operator[](size_type pos) const
Element access operator.
Definition ArrayView.h:212
constexpr size_type length() const noexcept
Same as ref size()
Definition ArrayView.h:246
constexpr bool empty() const noexcept
Check the view is empty.
Definition ArrayView.h:253
constexpr const_iterator cend() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:182
void swap(ArrayView &other) noexcept
Swap contents of two views.
Definition ArrayView.h:274
const_pointer const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is T.
Definition ArrayView.h:70
char & reference
Reference to an element (T&)
Definition ArrayView.h:51
char value_type
Type of the single element.
Definition ArrayView.h:33
constexpr const_iterator cbegin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:170
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:194
const char & const_reference
Reference to a const element (const T&)
Definition ArrayView.h:57
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:188
const char * const_pointer
Pointer to the constant element (const T*)
Definition ArrayView.h:45
constexpr const_reference front() const
Access the first element.
Definition ArrayView.h:227
constexpr const_reference back() const
Access the last element.
Definition ArrayView.h:234
Describes an object that can refer to a constant contiguous sequence of char-like objects with the fi...
Definition StringView.h:34
constexpr const_reference front() const
Same as std::string_view::front()
Definition StringView.h:251
reference Reference
Same as reference.
Definition StringView.h:59
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:306
const_reverse_iterator ConstReverseIterator
Same as const_reverse_iterator.
Definition StringView.h:90
bool operator<(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:573
const_reverse_iterator reverse_iterator
Same as const_reverse_iterator.
Definition StringView.h:93
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:450
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:476
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:444
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:562
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:336
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:512
const_reverse_iterator crend() const noexcept
Same as std::string_view::crend().
Definition StringView.h:232
StringView(const char *str) noexcept
Constructor.
Definition StringView.h:120
int compare(const char *s) const
Same as std::string_view::compare().
Definition StringView.h:361
const_reference ConstReference
Same as const_reference.
Definition StringView.h:65
typename Base::pointer pointer
Pointer to the character (char*)
Definition StringView.h:44
int compare(const StringView &other) const
Same as std::string_view::compare().
Definition StringView.h:324
const_reverse_iterator rbegin() const noexcept
Same as std::string_view::rbegin().
Definition StringView.h:214
value_type ValueType
Same as value_type.
Definition StringView.h:41
int compare(size_type pos, size_type count, const StringView &other) const
Same as std::string_view::compare().
Definition StringView.h:330
size_type find(char c, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:403
void remove_suffix(size_type n)
Same as std::string_view::remove_suffix()
Definition StringView.h:300
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:420
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:524
constexpr const_reference back() const
Same as std::string_view::back()
Definition StringView.h:257
StringView(const char(&str)[TN]) noexcept
Construct out of array of characters with known size.
Definition StringView.h:142
typename Base::const_reference const_reference
Reference to a const character (const char&)
Definition StringView.h:62
typename Base::const_reverse_iterator const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition StringView.h:87
constexpr const_iterator cbegin() const noexcept
Same as std::string_view::cbegin().
Definition StringView.h:196
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:488
void swap(comms::util::StringView &str1, comms::util::StringView &str2)
Specializes the std::swap algorithm.
Definition StringView.h:636
constexpr const_pointer data() const noexcept
Same as std::string_view::data()
Definition StringView.h:263
static const auto npos
Special value, the meaning is the same as std::string_view::npos.
Definition StringView.h:100
const_iterator iterator
Same as const_iterator.
Definition StringView.h:81
constexpr bool empty() const noexcept
Same as std::string_view::empty()
Definition StringView.h:281
size_type find(const StringView &str, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:379
typename Base::const_iterator const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is char.
Definition StringView.h:75
size_type copy(char *dest, size_type count, size_type pos=0) const
Same as std::string_view::copy().
Definition StringView.h:312
typename Base::size_type size_type
Equal to std::size_t.
Definition StringView.h:68
const_pointer ConstPointer
Same as const_pointer.
Definition StringView.h:53
void remove_prefix(size_type n)
Same as std::string_view::remove_prefix()
Definition StringView.h:294
bool operator>=(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:600
reverse_iterator ReverseIterator
Same as reverse_iterator.
Definition StringView.h:96
~StringView() noexcept=default
Destructor.
constexpr iterator begin() const noexcept
Same as std::string_view::begin().
Definition StringView.h:190
StringView(const std::string &str) noexcept
Constructor.
Definition StringView.h:134
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:456
int compare(size_type pos, size_type count, const char *s) const
Same as std::string_view::compare().
Definition StringView.h:367
size_type SizeType
Same as size_type;.
Definition StringView.h:71
reverse_iterator rend() const noexcept
Same as std::string_view::rend().
Definition StringView.h:226
iterator Iterator
Same as iterator.
Definition StringView.h:84
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:530
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:373
bool operator==(const StringView &str1, const StringView &str2)
Equality compare between the string views.
Definition StringView.h:609
pointer Pointer
Same as pointer.
Definition StringView.h:47
constexpr size_type length() const noexcept
Same as std::string_view::length()
Definition StringView.h:275
StringView & operator=(char(&str)[TN])
Assign array of characters with known size.
Definition StringView.h:180
typename Base::value_type value_type
Type of the character (char)
Definition StringView.h:38
constexpr const_reference operator[](size_type pos) const
Same as std::string_view::oprator[]()
Definition StringView.h:238
size_type find(const char *str, size_type pos, size_type count) const
Same as std::string_view::find().
Definition StringView.h:409
bool operator!=(const StringView &str1, const StringView &str2)
Inequality compare between the string views.
Definition StringView.h:620
constexpr iterator end() const noexcept
Same as std::string_view::end().
Definition StringView.h:202
bool operator>(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:591
std::string substr(size_type pos=0, size_type count=npos) const
Same as std::string_view::substr()
Definition StringView.h:287
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:518
typename Base::reference reference
Reference to a character (char&)
Definition StringView.h:56
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:482
const_reverse_iterator crbegin() const noexcept
Same as std::string_view::crbegin().
Definition StringView.h:220
typename Base::const_pointer const_pointer
Pointer to the constant character (const char*)
Definition StringView.h:50
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:550
constexpr size_type size() const noexcept
Same as std::string_view::size()
Definition StringView.h:269
const_reference at(size_type pos) const
Similar to std::string_view::at()
Definition StringView.h:245
StringView(char(&str)[TN]) noexcept
Construct out of array of characters with known size.
Definition StringView.h:152
const_iterator ConstIterator
Same as const_iterator.
Definition StringView.h:78
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:494
bool operator<=(const StringView &str1, const StringView &str2)
Lexicographical compare between the string views.
Definition StringView.h:582
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:556
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:438
constexpr const_iterator cend() const noexcept
Same as std::string_view::end().
Definition StringView.h:208
size_type find(const char *str, size_type pos=0) const
Same as std::string_view::find().
Definition StringView.h:415
Main namespace for all classes / functions of COMMS library.
STL namespace.