Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
trim.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_ALGORITHM_TRIM_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_ALGORITHM_TRIM_HPP_
7
8// IWYU pragma: private, include "arene/base/string_algorithms.hpp"
9// IWYU pragma: friend "arene/base/strings/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12
13#include "arene/base/stdlib_choice/cstddef.hpp"
16
17// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
18
19// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
20
21namespace arene {
22namespace base {
23
24/// @brief Characters defined as whitespace to be trimmed
25/// @return sequence of whitespace characters
26inline constexpr auto whitespace_chars() -> string_view { return {" \f\n\r\t\v"}; }
27
28/// @brief Create a copy of the string view with leading whitespace removed
29/// @param str The string to be trimmed
30/// @return A copy of the input string with leading whitespace removed. Whitespace is defined by
31/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
32constexpr auto ltrim(string_view const str) -> string_view {
33 std::size_t const start{str.find_first_not_of(whitespace_chars())};
34 if (start == string_view::npos) {
35 return string_view{};
36 }
37 return str.substr(start);
38}
39
40/// @brief Create a copy of the string view with leading whitespace removed
41/// @tparam MaxLength The size of the inline string
42/// @param str The string to be trimmed
43/// @return A copy of the input string with leading whitespace removed. Whitespace is defined by
44/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
45template <std::size_t MaxLength>
49///
50
51/// @brief Create a copy of the string view with trailing whitespace removed
52/// @param str The string_view reference to be trimmed
53/// @return A copy of the input string with trailing whitespace removed. Whitespace is defined by
54/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
55constexpr auto rtrim(string_view const str) -> string_view {
56 // parasoft-begin-suppress AUTOSAR-A2_10_1-a "False positive: identifier 'end' does not hide anything"
57 std::size_t const end{str.find_last_not_of(whitespace_chars())};
58 if (end == string_view::npos) {
59 return string_view{};
60 }
61 return str.substr(0U, end + 1U);
62 // parasoft-end-suppress AUTOSAR-A2_10_1-a
63}
64
65/// @brief Create a copy of the string view with trailing whitespace removed
66/// @tparam MaxLength The size of the inline string
67/// @param str The string_view reference to be trimmed
68/// @return A copy of the input string with trailing whitespace removed. Whitespace is defined by
69/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
70template <std::size_t MaxLength>
74
75/// @brief Create a copy of the string view with leading and trailing whitespace removed
76/// @param str The string_view reference to be trimmed
77/// @return A copy of the input string with leading and trailing whitespace removed. Whitespace is defined by
78/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
79constexpr auto trim(string_view const str) -> string_view { return ltrim(rtrim(str)); }
80
81/// @brief Create a copy of the string view with leading and trailing whitespace removed
82/// @tparam MaxLength The size of the inline string
83/// @param str The string_view reference to be trimmed
84/// @return A copy of the input string with leading and trailing whitespace removed. Whitespace is defined by
85/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
86template <std::size_t MaxLength>
90
91} // namespace base
92} // namespace arene
93
94#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_ALGORITHM_TRIM_HPP_
A string type with a fixed maximum capacity, where the data is stored internally without allocations.
Definition inline_string.hpp:162
Backport of the C++17 std::string_view class.
Definition string_view.hpp:54
static constexpr std::size_t npos
Special marker value for end of string.
Definition string_view.hpp:69
Definition accumulate.hpp:37
constexpr auto trim(string_view const str) -> string_view
Create a copy of the string view with leading and trailing whitespace removed.
Definition trim.hpp:79
constexpr auto whitespace_chars() -> string_view
Characters defined as whitespace to be trimmed.
Definition trim.hpp:26
constexpr auto rtrim(string_view const str) -> string_view
Create a copy of the string view with trailing whitespace removed.
Definition trim.hpp:55
constexpr auto ltrim(string_view const str) -> string_view
Create a copy of the string view with leading whitespace removed.
Definition trim.hpp:32
Definition accumulate.hpp:36
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29