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"
14#include "arene/base/strings/inline_string.hpp"
15#include "arene/base/strings/string_view.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>
46constexpr auto ltrim(inline_string<MaxLength> const& str) -> inline_string<MaxLength> {
47 return inline_string<MaxLength>{ltrim(string_view{str})};
48}
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 std::size_t const end{str.find_last_not_of(whitespace_chars())};
57 if (end == string_view::npos) {
58 return string_view{};
59 }
60 return str.substr(0U, end + 1U);
61}
62
63/// @brief Create a copy of the string view with trailing whitespace removed
64/// @tparam MaxLength The size of the inline string
65/// @param str The string_view reference to be trimmed
66/// @return A copy of the input string with trailing whitespace removed. Whitespace is defined by
67/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
68template <std::size_t MaxLength>
69constexpr auto rtrim(inline_string<MaxLength> const& str) -> inline_string<MaxLength> {
70 return inline_string<MaxLength>{rtrim(string_view{str})};
71}
72
73/// @brief Create a copy of the string view with leading and trailing whitespace removed
74/// @param str The string_view reference to be trimmed
75/// @return A copy of the input string with leading and trailing whitespace removed. Whitespace is defined by
76/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
77constexpr auto trim(string_view const str) -> string_view { return ltrim(rtrim(str)); }
78
79/// @brief Create a copy of the string view with leading and trailing whitespace removed
80/// @tparam MaxLength The size of the inline string
81/// @param str The string_view reference to be trimmed
82/// @return A copy of the input string with leading and trailing whitespace removed. Whitespace is defined by
83/// @c arene::base::whitespace_chars . An empty or all whitespace string will result in an empty return string.
84template <std::size_t MaxLength>
85constexpr auto trim(inline_string<MaxLength> const& str) -> inline_string<MaxLength> {
86 return inline_string<MaxLength>{trim(string_view{str})};
87}
88
89} // namespace base
90} // namespace arene
91
92#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_STRINGS_ALGORITHM_TRIM_HPP_
Definition array_exceptions_disabled.cpp:11
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:77
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
constexpr auto trim(inline_string< MaxLength > const &str) -> inline_string< MaxLength >
Create a copy of the string view with leading and trailing whitespace removed.
Definition trim.hpp:85
constexpr auto ltrim(inline_string< MaxLength > const &str) -> inline_string< MaxLength >
Create a copy of the string view with leading whitespace removed.
Definition trim.hpp:46
constexpr auto rtrim(inline_string< MaxLength > const &str) -> inline_string< MaxLength >
Create a copy of the string view with trailing whitespace removed.
Definition trim.hpp:69
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10