Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
string_view.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_STRING_VIEW_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_STRING_VIEW_HPP_
7
8// IWYU pragma: private
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
20#include "arene/base/detail/raw_c_string.hpp"
25#include "arene/base/stdlib_choice/cstddef.hpp"
26#include "arene/base/stdlib_choice/enable_if.hpp"
27#include "arene/base/stdlib_choice/ignore.hpp"
28#include "arene/base/stdlib_choice/integral_constant.hpp"
29#include "arene/base/stdlib_choice/is_same.hpp"
30#include "arene/base/stdlib_choice/min_value_overload.hpp"
31#include "arene/base/stdlib_choice/numeric_limits.hpp"
32#include "arene/base/stdlib_choice/string.hpp"
33#include "arene/base/strings/detail/lexicographic_string_compare.hpp"
34#include "arene/base/strings/detail/string_length.hpp"
35#include "arene/base/strings/detail/string_view_ostream_mixin.hpp"
37// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
38
39// parasoft-begin-suppress AUTOSAR-A3_1_5-a "False positive: all functions are constexpr so are intended to be inlined"
40
41namespace arene {
42namespace base {
43
44// NOLINTBEGIN(google-explicit-constructor, hicpp-avoid-c-arrays)
45
46// parasoft-begin-suppress AUTOSAR-A13_5_1-a-2 "False positive: no non-const overload"
47/// @brief Backport of the C++17 std::string_view class.
48///
49/// http://en.cppreference.com/w/cpp/string/basic_string_view
50///
51/// This implementation is minimalist but API compatible with the std:: one.
53 : private generic_ordering_from_three_way_compare<string_view>
54 , private string_view_ostream_mixin<string_view> {
55 /// @brief The character type
56 using character = detail::character;
57 /// @brief The raw NUL-terminated C string type
58 using raw_c_string = detail::raw_c_string;
59 /// @brief Internal. Type alias to the underlying span
60 using const_char_span = span<character const>;
61 // parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "Returning an int for compatibility with std::string_view"
62 /// @brief The type of comparisons that yield an integer rather than a @c strong_ordering
63 using integral_comparison_result = int;
64 // parasoft-end-suppress AUTOSAR-A3_9_1-b-2
65
66 public:
67 // parasoft-begin-suppress AUTOSAR-M11_0_1-a-2 "False positive: this is not 'member data', it is a public property"
68 /// @brief Special marker value for end of string
70 // parasoft-end-suppress AUTOSAR-M11_0_1-a-2
71
72 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False Positive: identifiers at class/namespace scope are exempt"
73
74 /// @brief Alias to the type associated to a single contained character
76
77 /// @brief Same that element_type but without any cv-qualifier
79
80 /// @brief Alias for the type associated to the size of the string
82 /// @brief Alias associated to the type used for indexing
84 /// @brief Alias to the pointer type associated to the underlying data
86 /// @brief Const qualified version of @c pointer
88 /// @brief Alias to the reference type associated to the underlying data
90 /// @brief Const qualified version of @c reference
92 /// @brief Alias to the iterator associated with the string_view class
94 /// @brief Const qualified version of @c iterator
96 /// @brief Alias to the reverse iterator
98 /// @brief Alias to the const-qualified reverse iterator
100
101 // parasoft-end-suppress AUTOSAR-A2_10_1-e
102
103 /// @brief Default constructor, creates an empty string_view.
104 ///
105 /// @post <c>data() == nullptr</c>
106 /// @post <c>size() == 0</c>
107 constexpr string_view() noexcept = default;
108
109 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
110 /// @brief Constructor for string_view from const char* string and explicit size
111 ///
112 /// @param str Pointer to the first character of the string. May contain null characters.
113 /// @param str_length The number of @c char elements in the string.
114 /// @post <c>data() == str</c>
115 /// @post <c>size() == str_length</c>
116 /// @pre @c str must point to an array of at least @c str_length characters, else behavior is undefined.
117 // NOLINTNEXTLINE(hicpp-avoid-c-arrays,hicpp-explicit-conversions,google-explicit-constructor)
118 constexpr string_view(raw_c_string const str, std::size_t const str_length) noexcept
120 str_span_(str, str_length) {}
121 // parasoft-end-suppress AUTOSAR-A7_1_3-a
122
123 /// @brief Constructor from compile time string literal or c-array
124 ///
125 /// @param literal string literal used for the string_view construction
126 /// @tparam N The number of @c char elements in the string
127 template <std::size_t N>
128 // NOLINTNEXTLINE(hicpp-avoid-c-arrays,hicpp-explicit-conversions,google-explicit-constructor)
129 inline constexpr string_view(character const (&literal)[N]) noexcept
130 : string_view(literal, N - 1) {}
131
132 // parasoft-begin-suppress AUTOSAR-A12_1_1-a-2 "False positive: This constructor delegates to another, which does
133 // initialize the base class"
134 /// @brief Construct a string_view from a null terminated string, non-inclusive of the null terminator
135 /// @param null_terminated_string null terminated string
136 /// @pre Behavior is undefined if null_terminated_string is not a pointer to a null terminated sequence of characters.
137 /// @post <c>data() == null_terminated_string</c>
138 /// @post @c size() will be the count of characters from the first element in the string to the first instance of a
139 /// null terminator.
140 // NOLINTNEXTLINE(hicpp-avoid-c-arrays,hicpp-explicit-conversions,google-explicit-constructor)
141 constexpr string_view(raw_c_string const null_terminated_string) noexcept
142 : string_view(null_terminated_string, detail::string_length(null_terminated_string)) {}
143 // parasoft-end-suppress AUTOSAR-A12_1_1-a-2
144
145 // parasoft-begin-suppress AUTOSAR-A12_1_1-a-2 "False positive: This constructor delegates to another, which does
146 // initialize the base class"
147 /// @brief Construct a string_view from a @c std::string.
148 ///
149 /// @param str The @c std::string to create a view to.
150 /// @post <c>data() == str.data()</c>
151 /// @post <c>size() == str.size()</c>
152 ///
153 /// @note This constructor is a template rather than a non-template taking a <c>const std::string&</c> to avoid the
154 /// possibility of unknowingly constructing a temporary @c std::string implicitly by passing something that is
155 /// convertible to @c std::string such as a @c std::initializer_list<char>
156 template <typename String, constraints<std::enable_if_t<std::is_same<String, std::string>::value>> = nullptr>
157 // NOLINTNEXTLINE(hicpp-explicit-conversions,google-explicit-constructor)
158 string_view(String const& str) noexcept
159 : string_view(str.data(), str.size()) {}
160 // parasoft-end-suppress AUTOSAR-A12_1_1-a-2
161
162 /// @brief Default copy constructor
163 constexpr string_view(string_view const& other) noexcept = default;
164
165 /// @brief Default move constructor
166 constexpr string_view(string_view&& other) noexcept = default;
167
168 /// @brief Default destructor
169 ~string_view() = default;
170
171 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: identifier 'begin' does not hide anything"
172 /// @brief Begin const iterator.
173 ///
174 /// @return An iterator that references the first character of the string.
175 constexpr auto begin() const noexcept -> const_iterator { return str_span_.begin(); }
176 // parasoft-end-suppress AUTOSAR-A2_10_1-d
177
178 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: identifier 'end' does not hide anything"
179 /// @brief End const iterator.
180 ///
181 /// @return An iterator that is one-past the last character of the string_view.
182 constexpr auto end() const noexcept -> const_iterator { return str_span_.end(); }
183 // parasoft-end-suppress AUTOSAR-A2_10_1-d
184
185 /// @brief Begin const iterator.
186 ///
187 /// @return An iterator that references the first character of the string_view.
188 constexpr auto cbegin() const noexcept -> const_iterator { return begin(); }
189
190 /// @brief End const iterator.
191 ///
192 /// @return An iterator that is one-past the last character of the string_view.
193 constexpr auto cend() const noexcept -> const_iterator { return end(); }
194
195 /// @brief Obtain a reverse iterator referring to the end of the string
196 ///
197 /// @return An iterator that references the last character of the string_view.
198 constexpr auto rbegin() const noexcept -> reverse_iterator { return str_span_.rbegin(); }
199
200 /// @brief Obtain a reverse iterator referring to one-before the front of the string_view
201 ///
202 /// @return An iterator that references one-before the first character of the string_view
203 constexpr auto rend() const noexcept -> reverse_iterator { return str_span_.rend(); }
204
205 /// @brief Obtain a reverse iterator referring to the end of the string_view
206 ///
207 /// @return An iterator that references the last character of the string_view.
208 constexpr auto crbegin() const noexcept -> const_reverse_iterator { return str_span_.rbegin(); }
209
210 /// @brief Obtain a reverse iterator referring to one-before the front of the string_view
211 ///
212 /// @return An iterator that references one-before the first character of the string_view
213 constexpr auto crend() const noexcept -> const_reverse_iterator { return str_span_.rend(); }
214
215 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False positive: Does not hide any identifiers"
216 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Does not hide any identifiers"
217 /// @brief Get the length of the string view
218 /// @return The number of 'char' values in this string.
219 /// @invariant <c> size() == std::distance(begin(), end()) </c>
220 constexpr auto size() const noexcept -> size_type { return str_span_.size(); }
221
222 /// @brief Get the length of the string view
223 /// @return The number of 'char' values in this string.
224 /// @invariant <c> length() == std::distance(begin(), end()) </c>
225 constexpr auto length() const noexcept -> size_type { return str_span_.size(); }
226
227 /// @brief Query if this is the empty string.
228 ///
229 /// @return true IIF <c>size() == 0</c>.
230 /// @return false otherwise
231 constexpr auto empty() const noexcept -> bool { return size() == 0U; }
232
233 /// @brief Get the maximum possible length of a @c string_view
234 /// @return Return the maximum string size supported by a @c string_view
235 static constexpr auto max_size() noexcept -> size_type { return std::numeric_limits<string_view::size_type>::max(); }
236 // parasoft-end-suppress AUTOSAR-A2_10_1-d
237 // parasoft-end-suppress AUTOSAR-A2_10_1-e
238
239 /// @brief lexicographically compares two string_view, behavior similar to std::string
240 ///
241 /// @param other other string view to compare with
242 /// @return @c 0 if the length of @c this and @c other are equal and all characters compare equal.
243 /// @return @c -1 if the value of the first character that does not match is lexicographically less in @c this than
244 /// @c other .
245 /// @return @c -1 if the length of @c this is less than the length of @c other and all characters in the overlapping
246 /// subset of @c this match those in @c other .
247 /// @return @c 1 if the value of the first character that does not match is lexicographically greater in @c this than
248 /// @c other .
249 /// @return @c 1 if the length of @c this is greater than the length of @c other and all characters in the
250 /// overlapping subset of lhs match those in @c other .
251 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
252 constexpr auto compare(string_view const other) const noexcept -> integral_comparison_result {
253 return from_strong_ordering(three_way_compare(*this, other));
254 }
255 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
256
257 /// @brief Compare a substring against another string view
258 ///
259 /// @param pos1 The offset of the substring to compare
260 /// @param length1 The length of the substring to compare
261 /// @param other The other string view to compare with
262 /// @return Equivalent to calling <c>*this.substr(pos1, length1).compare(other)</c>.
263 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
264 constexpr auto compare(size_type const pos1, size_type const length1, string_view const other) const noexcept
265 -> integral_comparison_result {
266 return substr(pos1, length1).compare(other);
267 }
268 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
269
270 /// @brief Compare a substring against a substring of another string view
271 ///
272 /// @param pos1 The offset of the substring to compare
273 /// @param length1 The length of the substring to compare
274 /// @param other The other string view to compare with
275 /// @param pos2 The offset of the other substring to compare
276 /// @param length2 The length of the other substring to compare
277 /// @return Equivalent to calling <c>*this.substr(pos1, length1).compare(other.substr(pos2, length2))</c>.
278 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
279 constexpr auto compare(
280 size_type const pos1,
281 size_type const length1,
282 string_view const other,
283 size_type const pos2,
284 size_type const length2
285 ) const noexcept -> integral_comparison_result {
286 return substr(pos1, length1).compare(other.substr(pos2, length2));
287 }
288 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
289
290 /// @brief Compare a string_view against a null-terminated string
291 ///
292 /// @param other The other string view to compare with
293 /// @return Equivalent to calling <c>*this.compare(string_view{other})</c>.
294 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
295 constexpr auto compare(raw_c_string const other) const noexcept -> integral_comparison_result {
296 return from_strong_ordering(detail::lexicographic_string_compare(str_span_, other));
297 }
298 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
299
300 /// @brief Compare a substring against a null terminated string
301 ///
302 /// @param pos1 The offset of the substring to compare
303 /// @param length1 The length of the substring to compare
304 /// @param other The other string view to compare with
305 /// @return Equivalent to calling <c>*this.substr(pos1, length1).compare(string_view{other})</c>.
306 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
307 constexpr auto compare(size_type const pos1, size_type const length1, raw_c_string const other) const noexcept
308 -> integral_comparison_result {
309 return substr(pos1, length1).compare(other);
310 }
311 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
312
313 /// @brief Compare a substring against a string specified as a pointer and length
314 ///
315 /// @param pos1 The offset of the substring to compare
316 /// @param length1 The length of the substring to compare
317 /// @param other Pointer to the other string to compare with
318 /// @param length2 The length of the other string to compare
319 /// @return Equivalent to calling <c>*this.substr(pos1, length1).compare(string_view{other, length2})</c>.
320 /// @pre @c length2 is in the range of @c other otherwise behavior is undefined.
321 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
322 constexpr auto compare(
323 size_type const pos1,
324 size_type const length1,
325 raw_c_string const other,
326 size_type const length2
327 ) const noexcept -> integral_comparison_result {
328 return substr(pos1, length1).compare(string_view{other, length2});
329 }
330 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
331
332 /// @brief Create a new string view referencing the same string as @c *this
333 /// @return A copy of @c *this
334 constexpr auto substr() const noexcept -> string_view { return *this; }
335
336 /// @brief Create a new string view based on a substring of the current one.
337 ///
338 /// @param pos Position of the first character of the substring.
339 /// @param count The requested length of the substring.
340 /// @return string_view Will contain content beginning at @c pos and continuing for @c count characters. If
341 /// @c pos>=size() , or @c pos+count>=size() , the returned substring is clamped to the end of the
342 /// @c string_view .
343 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False positive: const is placed on the right hand side"
344 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: no 'count' becomes inaccessible due to this declaration"
345 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
346 constexpr auto substr(size_type const pos, size_type const count = npos) const noexcept -> string_view {
347 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
348 size_type const bounded_pos{std::min(pos, size())};
349 size_type const bounded_size{std::min((size() - bounded_pos), count)};
350 return {arene::base::next(data(), static_cast<std::ptrdiff_t>(bounded_pos)), bounded_size};
351 }
352 // parasoft-end-suppress AUTOSAR-A2_10_1-a
353
354 /// @brief Swap the content of two string_view objects.
355 ///
356 /// @param other The other string_view to swap with.
357 // parasoft-begin-suppress AUTOSAR-A15_4_5-a "False positive: There is no function that throws an exception of 'Any'
358 // type"
359 // parasoft-begin-suppress AUTOSAR-A2_10_1-d "False Positive: 'swap' does not hide anything"
360 constexpr void swap(string_view& other) noexcept { ::arene::base::swap(str_span_, other.str_span_); }
361 // parasoft-end-suppress AUTOSAR-A2_10_1-d
362 // parasoft-end-suppress AUTOSAR-A15_4_5-a
363
364 /// @brief Get a pointer to the first character in the string.
365 /// @return A pointer to the first character in the string.
366 constexpr auto data() const noexcept -> const_pointer { return str_span_.data(); }
367
368 /// @brief Assignment operator for the string view
369 ///
370 /// @param other The other string to assign from
371 /// @return reference to the current string_view
372 constexpr auto operator=(string_view const& other) noexcept -> string_view& = default;
373
374 /// @brief Move assignment operator for the string view
375 ///
376 /// @param other The other string to move from
377 /// @return reference to the current string_view
378 constexpr auto operator=(string_view&& other) noexcept -> string_view& = default;
379
380 /// @brief Get a character at position pos in the string
381 ///
382 /// @param pos Index of the character to access.
383 /// @return A reference to the character at pos position.
384 /// @pre <c>pos < size()</c> else @c ARENE_PRECONDITION violation
385 constexpr auto operator[](std::size_t pos) const noexcept -> const_reference {
386 ARENE_PRECONDITION(pos < size());
387 return str_span_[pos];
388 }
389
390 /// @brief Copy the first <c>min(n,size())</c> characters to the destination buffer.
391 ///
392 /// @param dest A pointer to the destination buffer.
393 /// @param n The number of characters to attempt to copy.
394 /// @return The number of characters actually copied.
395 constexpr auto copy(character* const dest, size_type const n) const noexcept -> size_type {
396 return copy(dest, n, 0U);
397 }
398
399 // parasoft-begin-suppress AUTOSAR-A15_4_5-a "False positive: none of the expressions using 'operator=' throw"
400 /// @brief Copy the first <c>min(n,size()-pos)</c> characters to the destination buffer.
401 ///
402 /// @param dest A pointer to the destination buffer to copy the characters to.
403 /// @param n The maximum number of characters to copy
404 /// @param pos The offset in the string of the first character to copy. If @c >=size() , no content is copied.
405 /// @return The number of characters actually copied.
406 constexpr auto copy(character* const dest, size_type const n, size_type const pos) const noexcept -> size_type {
407 auto const sub = substr(pos, n);
408 auto const target = span<character>{dest, sub.length()};
409 std::ignore = ::arene::base::copy(sub.begin(), sub.end(), target.begin());
410 return sub.size();
411 }
412 // parasoft-end-suppress AUTOSAR-A15_4_5-a
413
414 /// @brief Get the first character in the string
415 ///
416 /// @return A reference to the first character
417 /// @pre <c>empty() != false</c> else @c ARENE_PRECONDITION violation
418 constexpr auto front() const noexcept -> const_reference {
420 return str_span_.front();
421 }
422
423 /// @brief Get the last character in the string
424 ///
425 /// @return A reference to the last character
426 /// @pre The string must not be empty
427 /// @pre <c>empty() != false</c> else @c ARENE_PRECONDITION violation
428 constexpr auto back() const noexcept -> const_reference {
430 return str_span_.back();
431 }
432
433 /// @brief Remove the first @c n characters from the string
434 ///
435 /// @param count the number of characters to remove
436 /// @post The string_view will be equivalent to a substring produced as if by <c>substr(n)</c>
437 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False positive: const is placed on the right hand side"
438 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: no 'count' becomes inaccessible due to this declaration"
439 constexpr void remove_prefix(size_type const count) noexcept { *this = substr(count); }
440 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
441 // parasoft-end-suppress AUTOSAR-A2_10_1-a
442
443 /// @brief Remove the last @c n characters from the string
444 ///
445 /// @param count the number of characters to remove
446 /// @post The string_view will be equivalent to a substring produced as if by
447 /// <c>substr(0, size() - std::min(count, size()))</c>
448 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False positive: const is placed on the right hand side"
449 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: no 'count' becomes inaccessible due to this declaration"
450 constexpr void remove_suffix(size_type const count) noexcept { *this = substr(0U, size() - std::min(count, size())); }
451 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
452 // parasoft-end-suppress AUTOSAR-A2_10_1-a
453
454 /// @brief Check if the string starts with another string
455 /// @param other The string to compare against.
456 /// @return true if the first <c>other.size()</c> characters of @c this are equal to those in @c other .
457 /// @return false otherwise.
458 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
459 constexpr auto starts_with(string_view const other) const noexcept -> bool {
460 return substr(0U, other.size()) == other;
461 }
462 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
463
464 /// @brief Check if the string starts with another string
465 /// @param other The string to compare against.
466 /// @return true if the first <c>string_length(other)</c> characters of @c this are equal to those in @c other .
467 /// @return false otherwise.
468 /// @pre @c other must be a null terminated string or else behavior is undefined.
469 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
470 constexpr auto starts_with(raw_c_string const other) const noexcept -> bool {
471 return starts_with(string_view{other});
472 }
473 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
474
475 /// @brief Check if the string starts with a specific character
476 /// @param chr The character to check
477 /// @return true if the string is not empty and the first character is @c c
478 /// @return false otherwise.
479 constexpr auto starts_with(character const chr) const noexcept -> bool { return (!empty()) && (front() == chr); }
480
481 /// @brief Check if the string ends with another string
482 /// @param other The other string
483 /// @return true if the last <c>other.size()</c> characters of @c this are equal to those in @c other .
484 /// @return false otherwise.
485 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
486 constexpr auto ends_with(string_view const other) const noexcept -> bool {
487 return (other.size() <= size()) && substr(size() - other.size(), other.size()) == other;
488 }
489 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
490
491 /// @brief Check if the string ends with another string
492 /// @param other The other string
493 /// @return true if the last <c>string_length(other)</c> characters of @c this are equal to those in @c other .
494 /// @return false otherwise.
495 /// @pre @c other must be a null terminated string or else behavior is undefined.
496 // parasoft-begin-suppress AUTOSAR-A7_1_3-a-2 "False Positive: const is on rhs"
497 constexpr auto ends_with(raw_c_string const other) const noexcept -> bool { return ends_with(string_view{other}); }
498 // parasoft-end-suppress AUTOSAR-A7_1_3-a-2
499
500 /// @brief Check if the string ends with a specific character
501 /// @param chr The character to check
502 /// @return @c true if the string is not empty and the last character is @c c
503 constexpr auto ends_with(character const chr) const noexcept -> bool { return (!empty()) && (back() == chr); }
504
505 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
506 /// @brief Find the earliest position of the given substring within the string, starting at the specified position
507 /// @param str The substring to search for
508 /// @param pos The lowest position to search from. Defaults to @c 0 .
509 /// @return The offset at which @c str can be found within the string, or @c npos if not found.
510 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
511 constexpr auto find(string_view const str, size_type const pos = 0U) const noexcept -> size_type {
512 if (pos > size()) {
513 return npos;
514 }
515 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
516 for (size_type index{pos}; (size() - index) >= str.size(); ++index) {
517 if (substr(index, str.size()) == str) {
518 return index;
519 }
520 }
521 // parasoft-end-suppress AUTOSAR-A2_10_1-a
522 return npos;
523 }
524 // parasoft-end-suppress AUTOSAR-A7_1_3-a
525
526 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
527 /// @brief Find the earliest position of the given substring within the string, starting at the specified position
528 /// @param str The substring to search for
529 /// @param pos The lowest position to search from. Defaults to @c 0 .
530 /// @return The offset at which @c str can be found within the string, or @c npos if not found.
531 /// @pre @c str must be a null terminated string or else behavior is undefined.
532 constexpr auto find(raw_c_string const str, size_type const pos = 0U) const noexcept -> size_type {
533 return find(string_view{str}, pos);
534 }
535 // parasoft-end-suppress AUTOSAR-A7_1_3-a
536
537 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
538 /// @brief Find the earliest position of the @c n character string starting at @c str within the string, starting at
539 /// the specified position
540 /// @param str The start of the string to search for
541 /// @param pos The lowest position in @c this to search from
542 /// @param n The length of the string to search for
543 /// @return Equivalent to <c>find(string_view{str, n}, pos)</c>
544 /// @pre @c str must be a pointer to a character array of at least @c n characters else behavior is undefined.
545 constexpr auto find(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type {
546 return find(string_view{str, n}, pos);
547 }
548 // parasoft-end-suppress AUTOSAR-A7_1_3-a
549
550 /// @brief Find the earliest position of the specified character within the string, starting at the specified position
551 /// @param chr The character to search for.
552 /// @param pos The lowest position to search from. Defaults to @c 0.
553 /// @return The position of that character, or @c npos if not found
554 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
555 constexpr auto find(character const chr, size_type const pos = 0U) const noexcept -> size_type {
556 difference_type const offset{static_cast<difference_type>(std::min(pos, size()))};
557 auto const location = ::arene::base::find(begin() + offset, end(), chr);
558 return location == end() ? npos : static_cast<size_type>(location - begin());
559 }
560
561 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
562 /// @brief Find the latest position of the given substring within the string, starting at the specified position
563 /// @param str The substring to search for
564 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
565 /// string_view.
566 /// @return The offset at which @c str can be found within the string, or @c npos if not found.
567 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
568 constexpr auto rfind(string_view const str, size_type const pos = npos) const noexcept -> size_type {
569 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
570 for (size_type index{pos > size() ? size() : pos}; index <= size(); --index) {
571 if (substr(index, str.size()) == str) {
572 return index;
573 }
574 }
575 // parasoft-end-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
576 return npos;
577 }
578 // parasoft-end-suppress AUTOSAR-A7_1_3-a
579
580 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
581 /// @brief Find the latest position of the given substring within the string, starting at the specified position
582 /// @param str The substring to search for
583 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
584 /// string_view.
585 /// @return The offset at which @c str can be found within the string, or @c npos if not found.
586 /// @pre @c str must be a null terminated string or else behavior is undefined.
587 constexpr auto rfind(raw_c_string const str, size_type const pos = npos) const noexcept -> size_type {
588 return rfind(string_view{str}, pos);
589 }
590 // parasoft-end-suppress AUTOSAR-A7_1_3-a
591
592 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
593 /// @brief Find the latest position of the @c n character string starting at @c str
594 /// within the string, starting at the specified position
595 /// @param str The start of the string to search for
596 /// @param pos The highest position in @c this to search from. If <c>pos >= size()</c>, the search starts from the
597 /// end of the string_view.
598 /// @param n The length of the string to search for
599 /// @return The offset at which @c str can be found within the string, or @c npos if not found.
600 /// @pre @c str must be a pointer to a character array of at least @c n characters else behavior is undefined.
601 constexpr auto rfind(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type {
602 return rfind(string_view{str, n}, pos);
603 }
604 // parasoft-end-suppress AUTOSAR-A7_1_3-a
605
606 /// @brief Find the latest position of the specified character within the string, starting at the specified position
607 /// @param chr The character to search for
608 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
609 /// string_view.
610 /// @return The position of that character, or @c npos if not found
611 // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
612 constexpr auto rfind(character const chr, size_type const pos = npos) const noexcept -> size_type {
613 auto start = rbegin();
614 // parasoft-begin-suppress AUTOSAR-M5_0_7-b-2 "False positive: no floating point conversions"
615 // parasoft-begin-suppress AUTOSAR-M5_0_8-a-2 "False positive: the sizes of the types are the same"
616 // parasoft-begin-suppress AUTOSAR-M5_0_9-a-2 "False positive: Guaranteed in-range by precondition"
617 if (pos < size()) {
618 start += static_cast<difference_type>(size() - pos - 1U);
619 }
620 auto const location = ::arene::base::find(start, rend(), chr);
621 return location == rend() ? npos : static_cast<size_type>(rend() - location - 1);
622 // parasoft-end-suppress AUTOSAR-M5_0_7-b-2
623 // parasoft-end-suppress AUTOSAR-M5_0_8-a-2
624 // parasoft-end-suppress AUTOSAR-M5_0_9-a-2
625 }
626
627 // parasoft-begin-suppress AUTOSAR-A15_4_5-a "False positive: 'str_span_[]' does not throw"
628 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
629 /// @brief Finds the first character equal to any of the characters in the given character sequence.
630 /// @param str The list of characters to find any of
631 /// @param pos The start position to search from. Defaults to @c 0 .
632 /// @return The offset of the first character in @c this that matches any character in @c str if there is one,
633 /// otherwise @c npos
634 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
635 constexpr auto find_first_of(string_view const str, size_type const pos = 0U) const noexcept -> size_type {
636 if (str.empty()) {
637 return npos;
638 }
639 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
640 for (size_type index{pos}; index < size(); ++index) {
641 if (str.find(str_span_[index]) != npos) {
642 return index;
643 }
644 }
645 // parasoft-end-suppress AUTOSAR-A2_10_1-a
646 return npos;
647 }
648 // parasoft-end-suppress AUTOSAR-A7_1_3-a
649 // parasoft-end-suppress AUTOSAR-A15_4_5-a
650
651 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
652 /// @brief Finds the first character equal to any of the characters in the given character sequence.
653 /// @param str The list of characters to find any of
654 /// @param pos The start position to search from. Defaults to @c 0 .
655 /// @return The offset of the first character in @c this that matches any character in @c str if there is one,
656 /// otherwise @c npos
657 /// @pre @c str must be a null terminated string or else behavior is undefined.
658 constexpr auto find_first_of(raw_c_string const str, size_type const pos = 0U) const noexcept -> size_type {
659 return find_first_of(string_view{str}, pos);
660 }
661 // parasoft-end-suppress AUTOSAR-A7_1_3-a
662
663 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
664 /// @brief Finds the first character equal to any of the characters in the given character sequence.
665 /// @param str The list of characters to find any of.
666 /// @param pos The start position in @c this to search from.
667 /// @param n The number of characters in @c str to consider.
668 /// @return The offset of the first character in @c str after @c pos if there is one, otherwise @c npos
669 /// @pre @c str must be a pointer to a character array of at least @c n characters else behavior is undefined.
670 constexpr auto find_first_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept
671 -> size_type {
672 return find_first_of(string_view{str, n}, pos);
673 }
674 // parasoft-end-suppress AUTOSAR-A7_1_3-a
675
676 /// @brief Finds the first character equal to the specified character
677 /// @param chr The character to find
678 /// @param pos The start position to search from. Defaults to @c 0 .
679 /// @return The offset of the first character in @c this that matches @c chr if there is one, otherwise @c npos
680 constexpr auto find_first_of(character const chr, size_type const pos = 0U) const noexcept -> size_type {
681 return find(chr, pos);
682 }
683
684 // parasoft-begin-suppress AUTOSAR-A15_4_5-a "False positive: 'str_span_[]' does not throw"
685 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
686 /// @brief Finds the first character not equal to any of the characters in the given character sequence.
687 /// @param str The list of characters to compare against.
688 /// @param pos The start position to search from. Defaults to @c 0 .
689 /// @return The offset of the first character not in @c str after @c pos if there is one, otherwise @c npos
690 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
691 constexpr auto find_first_not_of(string_view const str, size_type const pos = 0U) const noexcept -> size_type {
692 if (str.empty()) {
693 return pos < size() ? pos : npos;
694 }
695 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
696 for (size_type index{pos}; index < size(); ++index) {
697 if (str.find(str_span_[index]) == npos) {
698 return index;
699 }
700 }
701 // parasoft-end-suppress AUTOSAR-A2_10_1-a
702 return npos;
703 }
704 // parasoft-end-suppress AUTOSAR-A7_1_3-a
705 // parasoft-end-suppress AUTOSAR-A15_4_5-a
706
707 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
708 /// @brief Finds the first character not equal to any of the characters in the given character sequence.
709 /// @param str The list of characters to compare against.
710 /// @param pos The start position to search from. Defaults to @c 0 .
711 /// @return The offset of the first character not in @c str after @c pos if there is one, otherwise @c npos
712 /// @pre @c str must be a null terminated string or else behavior is undefined.
713 constexpr auto find_first_not_of(raw_c_string const str, size_type const pos = 0U) const noexcept -> size_type {
714 return find_first_not_of(string_view{str}, pos);
715 }
716 // parasoft-end-suppress AUTOSAR-A7_1_3-a
717
718 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
719 /// @brief Finds the first character not equal to any of the characters in the given character sequence.
720 /// @param str The list of characters to compare against.
721 /// @param pos The start position to search from in @c this .
722 /// @param n The number of characters in @c str to consider.
723 /// @return The offset of the first character not in @c str after @c pos if there is one, otherwise @c npos
724 /// @pre @c str must be a pointer to a character array of at least @c n characters else behavior is undefined.
725 constexpr auto find_first_not_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept
726 -> size_type {
727 return find_first_not_of(string_view{str, n}, pos);
728 }
729 // parasoft-end-suppress AUTOSAR-A7_1_3-a
730
731 /// @brief Finds the first character not equal to the provided character.
732 /// @param chr The character to find
733 /// @param pos The start position to search from. Defaults to @c 0 .
734 /// @return The offset of the first character not matching @c chr if there is one, otherwise @c npos
735 constexpr auto find_first_not_of(character const chr, size_type const pos = 0U) const noexcept -> size_type {
736 return find_first_not_of(string_view{&chr, 1U}, pos);
737 }
738
739 // parasoft-begin-suppress AUTOSAR-A15_4_5-a "False positive: 'str_span_[]' does not throw"
740 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
741 /// @brief Finds the last character equal to one of characters in the given character sequence.
742 /// @param str The list of characters to compare against.
743 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
744 /// string_view. Defaults to @c npos .
745 /// @return The offset of the last character matching any of those in @c str if there is one, otherwise @c npos
746 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
747 constexpr auto find_last_of(string_view const str, size_type const pos = npos) const noexcept -> size_type {
748 if (str.empty()) {
749 return npos;
750 }
751 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
752 for (size_type index{pos >= size() ? size() - 1U : pos}; index < size(); --index) {
753 if (str.find(str_span_[index]) != npos) {
754 return index;
755 }
756 }
757 // parasoft-end-suppress AUTOSAR-A2_10_1-a
758 return npos;
759 }
760 // parasoft-end-suppress AUTOSAR-A7_1_3-a
761 // parasoft-end-suppress AUTOSAR-A15_4_5-a
762
763 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
764 /// @brief Finds the last character equal to one of characters in the given character sequence.
765 /// @param str The list of characters to compare against.
766 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
767 /// string_view. Defaults to @c npos .
768 /// @return The offset of the last character matching any of those in @c str if there is one, otherwise @c npos
769 /// @pre @c str must be a null terminated string or else behavior is undefined.
770 constexpr auto find_last_of(raw_c_string const str, size_type const pos = npos) const noexcept -> size_type {
771 return find_last_of(string_view{str}, pos);
772 }
773 // parasoft-end-suppress AUTOSAR-A7_1_3-a
774
775 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
776 /// @brief Finds the last character equal to one of characters in the given character sequence.
777 /// @param str The list of characters to compare against.
778 /// @param pos The highest position in @c this to search from. If <c>pos >= size()</c>, the search starts from the
779 /// end of the string_view.
780 /// @param n The number of characters in @c str to consider.
781 /// @return The offset of the last character matching any of the first @c n characters of @c str at or after @c pos
782 /// if there is one, otherwise @c npos
783 /// @pre @c str must be a pointer to a character array of at least @c n characters else behavior is undefined.
784 constexpr auto find_last_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept
785 -> size_type {
786 return find_last_of(string_view{str, n}, pos);
787 }
788 // parasoft-end-suppress AUTOSAR-A7_1_3-a
789
790 /// @brief Finds the last character equal to the provided character.
791 /// @param chr The character to find.
792 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
793 /// string_view.
794 /// @return The offset of the last character matching @c chr if there is one, otherwise @c npos
795 constexpr auto find_last_of(character const chr, size_type const pos = npos) const noexcept -> size_type {
796 return rfind(chr, pos);
797 }
798
799 // parasoft-begin-suppress AUTOSAR-A15_4_5-a "False positive: 'str_span_[]' does not throw"
800 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
801 /// @brief Finds the last character not equal to any of the characters in the given character sequence.
802 /// @param str The list of characters to consider.
803 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
804 /// string_view. Defaults to @c npos
805 /// @return The offset of the last character not in @c str if there is one, otherwise @c npos
806 // NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
807 constexpr auto find_last_not_of(string_view const str, size_type const pos = npos) const noexcept -> size_type {
808 if (str.empty()) {
809 return pos < size() ? pos : size() - 1U;
810 }
811 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False Positive: 'index' does not hide anything"
812 for (size_type index{pos >= size() ? size() - 1U : pos}; index < size(); --index) {
813 if (str.find(str_span_[index]) == npos) {
814 return index;
815 }
816 }
817 // parasoft-end-suppress AUTOSAR-A2_10_1-a
818 return npos;
819 }
820 // parasoft-end-suppress AUTOSAR-A7_1_3-a
821 // parasoft-end-suppress AUTOSAR-A15_4_5-a
822
823 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
824 /// @brief Finds the last character not equal to any of the characters in the given character sequence.
825 /// @param str The list of characters to consider.
826 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
827 /// string_view. Defaults to @c npos
828 /// @return The offset of the last character not in @c str if there is one, otherwise @c npos
829 /// @pre @c str must be a null terminated string or else behavior is undefined.
830 constexpr auto find_last_not_of(raw_c_string const str, size_type const pos = npos) const noexcept -> size_type {
831 return find_last_not_of(string_view{str}, pos);
832 }
833 // parasoft-end-suppress AUTOSAR-A7_1_3-a
834
835 // parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
836 /// @brief Finds the last character not equal to any of the characters in the given character sequence.
837 /// @param str The list of characters to consider.
838 /// @param pos The highest position in @c this to search from. If <c>pos >= size()</c>, the search starts from the
839 /// end of the string_view.
840 /// @param n The number of characters in @c str to consider
841 /// @return The offset of the last character not in @c str at or before @c pos if there is one, otherwise @c npos
842 /// @pre @c str must be a pointer to a character array of at least @c n characters else behavior is undefined.
843 constexpr auto find_last_not_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept
844 -> size_type {
845 return find_last_not_of(string_view{str, n}, pos);
846 }
847 // parasoft-end-suppress AUTOSAR-A7_1_3-a
848
849 /// @brief Finds the last character equal to the provided character.
850 /// @param chr The character to find.
851 /// @param pos The highest position to search from. If <c>pos >= size()</c>, the search starts from the end of the
852 /// string_view. Defaults to @c npos .
853 /// @return The offset of the last character not equal to @c chr if there is one, otherwise @c npos
854 constexpr auto find_last_not_of(character const chr, size_type const pos = npos) const noexcept -> size_type {
855 return find_last_not_of(string_view{&chr, 1U}, pos);
856 }
857
858 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Does not hide any identifiers"
859 /// @brief Compare two @c string_view objects lexicographically.
860 ///
861 /// @param lhs The first string to compare
862 /// @param rhs The second string to compare
863 ///
864 /// @return strong_ordering::equal if the length of lhs and rhs are equal and all characters compare equal.
865 /// @return strong_ordering::less if the value of the first character that does not match is lexicographically less
866 /// in
867 /// lhs than rhs.
868 /// @return strong_ordering::less if the length of lhs is less than the length of rhs and all characters in the
869 /// overlapping subset of rhs match those in lhs.
870 /// @return strong_ordering::greater if the value of the first character that does not match is lexicographically
871 /// greater in lhs than rhs.
872 /// @return strong_ordering::greater if the length of lhs is greater than the length of rhs and all characters in the
873 /// overlapping subset of lhs match those in rhs.
874 static constexpr auto three_way_compare(string_view const lhs, string_view const rhs) noexcept -> strong_ordering {
875 return detail::lexicographic_string_compare(lhs.str_span_, rhs.str_span_);
876 }
877 // parasoft-end-suppress AUTOSAR-A2_10_1-e
878
879 // parasoft-begin-suppress AUTOSAR-A13_5_2-a-2 "Implicit conversion to std::string is part of the API"
880 /// @brief Implicit conversion to std::string. Provided since we cannot patch C++17's string_view constructor onto
881 /// std::string.
882 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
883 operator std::string() const;
884 // parasoft-end-suppress AUTOSAR-A13_5_2-a-2
885
886 // parasoft-begin-suppress AUTOSAR-A2_10_1-e "False positive: Does not hide any identifiers"
887 /// @brief quick check that string views have the same size to quickly identify non-equal strings.
888 ///
889 /// @param first The first string_view to compare.
890 /// @param second The second string_view to compare.
891 /// @return inequality_heuristic @c inequality_heuristic::definitely_not_equal if the strings have different lengths,
892 /// @c inequality_heuristic::may_be_equal_or_not_equal otherwise.
893 static constexpr auto fast_inequality_check(string_view const first, string_view const second) noexcept
895 return (first.size() == second.size()) ? inequality_heuristic::may_be_equal_or_not_equal
897 }
898 // parasoft-end-suppress AUTOSAR-A2_10_1-e
899
900 private:
901 /// @brief A @c span over the character sequence
902 const_char_span str_span_;
903};
904// parasoft-end-suppress AUTOSAR-A13_5_1-a-2
905
906// parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
907/// @brief Construct a @c std::string from the @c string_view
908/// @param str The @c string_view to convert
909/// @return A new @c std::string holding a copy of the string from the view
910// NOLINTNEXTLINE(readability-avoid-const-params-in-decls)
911auto to_string(string_view const str) -> std::string;
912// parasoft-end-suppress AUTOSAR-A7_1_3-a
913
914namespace literals {
915
916// parasoft-begin-suppress AUTOSAR-A7_1_3-a "False positive: const is placed on the right hand side"
917/// @brief String literal operator to construct a string_view from an annotated string literal. This @c string_view
918/// operator is named "asv" (arene string view) to avoid any conflict with the sv operator in a third party code base
919/// @param str A pointer to the start of the string literal characters
920/// @param len The number of characters in the string literal
921/// @return A @c string_view for the string literal
922constexpr auto operator""_asv(detail::raw_c_string const str, std::size_t const len) noexcept -> string_view {
923 return {str, len};
924}
925// parasoft-end-suppress AUTOSAR-A7_1_3-a
926
927} // namespace literals
928
929// NOLINTEND(google-explicit-constructor, hicpp-avoid-c-arrays)
930
931namespace ranges {
932/// @brief Indicate that @c string_view is a borrowed range
933template <>
935} // namespace ranges
936
937} // namespace base
938} // namespace arene
939
940// parasoft-end-suppress AUTOSAR-A3_1_5-a
941
942#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_STRING_VIEW_HPP_
An opt-in trait to indicate that a range type is a borrowed range.
Definition enable_borrowed_range.hpp:31
A backport of std::span for C++14. Provides a view over a contiguous range of elements of type T.
Definition span.hpp:315
::arene::base::reverse_iterator< iterator > reverse_iterator
The type of an iterator into a reversed view of the data viewed by the span.
Definition span.hpp:392
detail::wrapped_iterator< const_pointer, span_detail::span_iter_passkey > const_iterator
The type of an iterator into a const view of the data viewed by the span.
Definition span.hpp:390
detail::wrapped_iterator< pointer, span_detail::span_iter_passkey > iterator
The type of an iterator into the data viewed by the span.
Definition span.hpp:388
T & reference
The type of references to elements of the span.
Definition span.hpp:384
T element_type
The type of the elements of the span.
Definition span.hpp:374
arene::base::remove_cvref_t< T > value_type
The type of the elements of the span without const or volatile qualification.
Definition span.hpp:376
::arene::base::reverse_iterator< const_iterator > const_reverse_iterator
The type of an iterator into a const and reversed view of the data viewed by the span.
Definition span.hpp:394
T * pointer
The type of pointers to elements of the span.
Definition span.hpp:380
T const & const_reference
The type of references to const elements of the span.
Definition span.hpp:386
T const * const_pointer
The type of pointers to const elements of the span.
Definition span.hpp:382
Backport of the C++17 std::string_view class.
Definition string_view.hpp:54
constexpr auto copy(character *const dest, size_type const n) const noexcept -> size_type
Copy the first min(n,size()) characters to the destination buffer.
Definition string_view.hpp:395
constexpr string_view() noexcept=default
Default constructor, creates an empty string_view.
constexpr auto operator=(string_view const &other) noexcept -> string_view &=default
Assignment operator for the string view.
constexpr auto crbegin() const noexcept -> const_reverse_iterator
Obtain a reverse iterator referring to the end of the string_view.
Definition string_view.hpp:208
constexpr auto rend() const noexcept -> reverse_iterator
Obtain a reverse iterator referring to one-before the front of the string_view.
Definition string_view.hpp:203
const_char_span::value_type value_type
Same that element_type but without any cv-qualifier.
Definition string_view.hpp:78
constexpr auto find_first_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence.
Definition string_view.hpp:670
constexpr void remove_suffix(size_type const count) noexcept
Remove the last n characters from the string.
Definition string_view.hpp:450
const_char_span::const_reverse_iterator const_reverse_iterator
Alias to the const-qualified reverse iterator.
Definition string_view.hpp:99
const_char_span::reference reference
Alias to the reference type associated to the underlying data.
Definition string_view.hpp:89
constexpr auto starts_with(character const chr) const noexcept -> bool
Check if the string starts with a specific character.
Definition string_view.hpp:479
constexpr auto find_last_not_of(raw_c_string const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:830
constexpr auto find_last_not_of(character const chr, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to the provided character.
Definition string_view.hpp:854
constexpr auto find(string_view const str, size_type const pos=0U) const noexcept -> size_type
Find the earliest position of the given substring within the string, starting at the specified positi...
Definition string_view.hpp:511
constexpr auto find_last_of(raw_c_string const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence.
Definition string_view.hpp:770
constexpr string_view(character const (&literal)[N]) noexcept
Constructor from compile time string literal or c-array.
Definition string_view.hpp:129
constexpr auto rfind(string_view const str, size_type const pos=npos) const noexcept -> size_type
Find the latest position of the given substring within the string, starting at the specified position...
Definition string_view.hpp:568
static constexpr auto three_way_compare(string_view const lhs, string_view const rhs) noexcept -> strong_ordering
Compare two string_view objects lexicographically.
Definition string_view.hpp:874
const_char_span::const_iterator const_iterator
Const qualified version of iterator.
Definition string_view.hpp:95
constexpr auto compare(size_type const pos1, size_type const length1, raw_c_string const other, size_type const length2) const noexcept -> integral_comparison_result
Compare a substring against a string specified as a pointer and length.
Definition string_view.hpp:322
constexpr auto find_last_not_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:843
constexpr auto find_first_not_of(character const chr, size_type const pos=0U) const noexcept -> size_type
Finds the first character not equal to the provided character.
Definition string_view.hpp:735
constexpr auto length() const noexcept -> size_type
Get the length of the string view.
Definition string_view.hpp:225
constexpr auto find_last_not_of(string_view const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:807
constexpr auto find(raw_c_string const str, size_type const pos=0U) const noexcept -> size_type
Find the earliest position of the given substring within the string, starting at the specified positi...
Definition string_view.hpp:532
constexpr void remove_prefix(size_type const count) noexcept
Remove the first n characters from the string.
Definition string_view.hpp:439
constexpr auto cbegin() const noexcept -> const_iterator
Begin const iterator.
Definition string_view.hpp:188
constexpr auto rfind(raw_c_string const str, size_type const pos=npos) const noexcept -> size_type
Find the latest position of the given substring within the string, starting at the specified position...
Definition string_view.hpp:587
static constexpr auto fast_inequality_check(string_view const first, string_view const second) noexcept -> inequality_heuristic
quick check that string views have the same size to quickly identify non-equal strings.
Definition string_view.hpp:893
const_char_span::const_pointer const_pointer
Const qualified version of pointer.
Definition string_view.hpp:87
constexpr auto compare(size_type const pos1, size_type const length1, string_view const other) const noexcept -> integral_comparison_result
Compare a substring against another string view.
Definition string_view.hpp:264
constexpr auto find_first_not_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:725
std::ptrdiff_t difference_type
Alias associated to the type used for indexing.
Definition string_view.hpp:83
constexpr auto find_last_of(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence.
Definition string_view.hpp:784
constexpr auto ends_with(character const chr) const noexcept -> bool
Check if the string ends with a specific character.
Definition string_view.hpp:503
static constexpr std::size_t npos
Special marker value for end of string.
Definition string_view.hpp:69
constexpr auto copy(character *const dest, size_type const n, size_type const pos) const noexcept -> size_type
Copy the first min(n,size()-pos) characters to the destination buffer.
Definition string_view.hpp:406
constexpr auto find_first_not_of(raw_c_string const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:713
const_char_span::element_type element_type
Alias to the type associated to a single contained character.
Definition string_view.hpp:75
constexpr string_view(string_view const &other) noexcept=default
Default copy constructor.
constexpr auto find_first_of(string_view const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence.
Definition string_view.hpp:635
constexpr auto rbegin() const noexcept -> reverse_iterator
Obtain a reverse iterator referring to the end of the string.
Definition string_view.hpp:198
constexpr auto ends_with(raw_c_string const other) const noexcept -> bool
Check if the string ends with another string.
Definition string_view.hpp:497
constexpr auto empty() const noexcept -> bool
Query if this is the empty string.
Definition string_view.hpp:231
const_char_span::const_reference const_reference
Const qualified version of reference.
Definition string_view.hpp:91
constexpr auto data() const noexcept -> const_pointer
Get a pointer to the first character in the string.
Definition string_view.hpp:366
constexpr auto find_first_of(character const chr, size_type const pos=0U) const noexcept -> size_type
Finds the first character equal to the specified character.
Definition string_view.hpp:680
constexpr auto find(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Find the earliest position of the n character string starting at str within the string,...
Definition string_view.hpp:545
constexpr string_view(string_view &&other) noexcept=default
Default move constructor.
constexpr auto find_first_not_of(string_view const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character not equal to any of the characters in the given character sequence.
Definition string_view.hpp:691
constexpr auto substr(size_type const pos, size_type const count=npos) const noexcept -> string_view
Create a new string view based on a substring of the current one.
Definition string_view.hpp:346
constexpr auto size() const noexcept -> size_type
Get the length of the string view.
Definition string_view.hpp:220
constexpr string_view(raw_c_string const null_terminated_string) noexcept
Construct a string_view from a null terminated string, non-inclusive of the null terminator.
Definition string_view.hpp:141
std::size_t size_type
Alias for the type associated to the size of the string.
Definition string_view.hpp:81
constexpr auto compare(size_type const pos1, size_type const length1, string_view const other, size_type const pos2, size_type const length2) const noexcept -> integral_comparison_result
Compare a substring against a substring of another string view.
Definition string_view.hpp:279
~string_view()=default
Default destructor.
constexpr auto front() const noexcept -> const_reference
Get the first character in the string.
Definition string_view.hpp:418
constexpr auto starts_with(raw_c_string const other) const noexcept -> bool
Check if the string starts with another string.
Definition string_view.hpp:470
constexpr void swap(string_view &other) noexcept
Swap the content of two string_view objects.
Definition string_view.hpp:360
constexpr auto rfind(character const chr, size_type const pos=npos) const noexcept -> size_type
Find the latest position of the specified character within the string, starting at the specified posi...
Definition string_view.hpp:612
constexpr auto starts_with(string_view const other) const noexcept -> bool
Check if the string starts with another string.
Definition string_view.hpp:459
constexpr auto find_last_of(string_view const str, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to one of characters in the given character sequence.
Definition string_view.hpp:747
constexpr auto cend() const noexcept -> const_iterator
End const iterator.
Definition string_view.hpp:193
constexpr auto compare(string_view const other) const noexcept -> integral_comparison_result
lexicographically compares two string_view, behavior similar to std::string
Definition string_view.hpp:252
constexpr auto operator[](std::size_t pos) const noexcept -> const_reference
Get a character at position pos in the string.
Definition string_view.hpp:385
constexpr auto operator=(string_view &&other) noexcept -> string_view &=default
Move assignment operator for the string view.
constexpr auto find(character const chr, size_type const pos=0U) const noexcept -> size_type
Find the earliest position of the specified character within the string, starting at the specified po...
Definition string_view.hpp:555
constexpr auto begin() const noexcept -> const_iterator
Begin const iterator.
Definition string_view.hpp:175
constexpr auto compare(raw_c_string const other) const noexcept -> integral_comparison_result
Compare a string_view against a null-terminated string.
Definition string_view.hpp:295
constexpr auto substr() const noexcept -> string_view
Create a new string view referencing the same string as *this.
Definition string_view.hpp:334
constexpr auto rfind(raw_c_string const str, size_type const pos, size_type const n) const noexcept -> size_type
Find the latest position of the n character string starting at str within the string,...
Definition string_view.hpp:601
const_char_span::pointer pointer
Alias to the pointer type associated to the underlying data.
Definition string_view.hpp:85
const_char_span::iterator iterator
Alias to the iterator associated with the string_view class.
Definition string_view.hpp:93
string_view(String const &str) noexcept
Construct a string_view from a std::string.
Definition string_view.hpp:158
const_char_span::reverse_iterator reverse_iterator
Alias to the reverse iterator.
Definition string_view.hpp:97
constexpr auto find_last_of(character const chr, size_type const pos=npos) const noexcept -> size_type
Finds the last character equal to the provided character.
Definition string_view.hpp:795
constexpr auto compare(size_type const pos1, size_type const length1, raw_c_string const other) const noexcept -> integral_comparison_result
Compare a substring against a null terminated string.
Definition string_view.hpp:307
constexpr auto find_first_of(raw_c_string const str, size_type const pos=0U) const noexcept -> size_type
Finds the first character equal to any of the characters in the given character sequence.
Definition string_view.hpp:658
static constexpr auto max_size() noexcept -> size_type
Get the maximum possible length of a string_view.
Definition string_view.hpp:235
constexpr auto end() const noexcept -> const_iterator
End const iterator.
Definition string_view.hpp:182
constexpr auto crend() const noexcept -> const_reverse_iterator
Obtain a reverse iterator referring to one-before the front of the string_view.
Definition string_view.hpp:213
constexpr auto back() const noexcept -> const_reference
Get the last character in the string.
Definition string_view.hpp:428
constexpr auto ends_with(string_view const other) const noexcept -> bool
Check if the string ends with another string.
Definition string_view.hpp:486
static constexpr auto max() noexcept -> T
The maximum finite value.
Definition numeric_limits.hpp:230
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition byte.hpp:178
static ARENE_MAYBE_UNUSED constexpr auto const & swap
A Customization Point Object implementation of the constexpr compatible std::swap from C++17.
Definition swap.hpp:130
Definition accumulate.hpp:37
constexpr std::integral_constant< std::size_t, N > array< T, N >::size
Definition array.hpp:534
ARENE_NODISCARD constexpr auto next(Iterator itr, typename std::iterator_traits< Iterator >::difference_type steps=1) noexcept(noexcept(::arene::base::advance(itr, steps))) -> Iterator
Produces the Nth successor (or predecessor if steps is negative) of an iterator.
Definition next.hpp:38
strong_ordering_detail::strong_ordering strong_ordering
The enumeration specifying the result of an ordering comparison.
Definition strong_ordering.hpp:147
constexpr auto copy(InputIterator first, InputIterator last, OutputIterator output) noexcept(noexcept(*(++output)) &&noexcept(*(++first)) &&noexcept(first !=last) &&std::is_nothrow_assignable<, typename std::iterator_traits< InputIterator >::reference >::value) -> OutputIterator
Copies the elements in the range, defined by [first, last), to another range beginning at output.
Definition copy.hpp:40
auto to_string(string_view const str) -> std::string
Construct a std::string from the string_view.
constexpr auto from_strong_ordering(strong_ordering const ordering) noexcept -> int
Convert a strong_ordering into an integer three-way-comparison result.
Definition strong_ordering.hpp:177
constexpr auto find(Iterator first, Iterator last, Value const &value_to_find) noexcept(denotes_nothrow_iterable_range_v< Iterator, Iterator > &&noexcept(*first==value_to_find) &&std::is_nothrow_copy_constructible< Iterator >::value) -> Iterator
Constexpr-compatible implementation of std::find.
Definition find.hpp:42
inequality_heuristic
An enumeration used for the return value of fast_inequality_check functions to indicate if two values...
Definition operators.hpp:696
@ may_be_equal_or_not_equal
The values checked may or may not be equal to each other; a more detailed check is required.
Definition operators.hpp:700
@ definitely_not_equal
The two values checked are definitely not equal to each other.
Definition operators.hpp:698
Definition accumulate.hpp:36
STL namespace.
::ptrdiff_t ptrdiff_t
A type suitable for holding the difference between two pointers.
Definition cstddef.hpp:31
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
bool_constant< true > true_type
An alias for bool_constant<true>
Definition integral_constant.hpp:76
basic_string< arene::base::detail::character > string
Alias for the common case of a string with char characters, standard traits and standard allocator.
Definition string.hpp:28
constexpr auto min(initializer_list< T > list, Compare comp) noexcept(noexcept(comp(std::declval< T & >(), std::declval< T & >())) &&std::is_nothrow_copy_constructible_v< T >) -> T
Returns the smallest value in the std::initializer_list.
Definition min_initializer_list_overload.hpp:50
static constexpr auto const & ignore
An object to which anything can be assigned, which ignores the assigned value.
Definition ignore.hpp:72
Provides a backport of the constexpr implementation of std::reverse_iterator .