Nektar++
Loading...
Searching...
No Matches
StringUtils.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: StringUtils.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: String utilities. Semantics follow the Boost.StringAlgo
32// functions they replace; case handling is ASCII.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#ifndef NEKTAR_LIB_UTILITIES_BASIC_UTILS_STRINGUTILS_HPP
37#define NEKTAR_LIB_UTILITIES_BASIC_UTILS_STRINGUTILS_HPP
38
39#include <algorithm>
40#include <cctype>
41#include <cstddef>
42#include <string>
43#include <string_view>
44
46{
47
48namespace detail
49{
50inline char ToUpper(char c)
51{
52 return static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
53}
54
55inline char ToLower(char c)
56{
57 return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
58}
59
60inline bool IEqualChar(char a, char b)
61{
62 return ToUpper(a) == ToUpper(b);
63}
64
65inline bool IsSpace(char c)
66{
67 return std::isspace(static_cast<unsigned char>(c)) != 0;
68}
69} // namespace detail
70
71/// Predicate matching any of a set of characters.
73{
74public:
75 explicit IsAnyOf(std::string_view chars) : m_chars(chars)
76 {
77 }
78
79 bool operator()(char c) const
80 {
81 return m_chars.find(c) != std::string::npos;
82 }
83
84private:
85 std::string m_chars;
86};
87
88/// Whether Split() merges adjacent separators.
94
95/// Splits @p input at characters matching @p pred into @p result, which is
96/// cleared first. An empty input gives a single empty token; leading and
97/// trailing separators give empty tokens even when compressing.
98template <typename Container, typename Pred>
99Container &Split(Container &result, std::string_view input, Pred pred,
101{
102 result.clear();
103 const std::size_t n = input.size();
104 std::size_t start = 0;
105 for (std::size_t i = 0; i <= n; ++i)
106 {
107 if (i == n || pred(input[i]))
108 {
109 result.insert(result.end(),
110 std::string(input.substr(start, i - start)));
111 if (mode == eTokenCompressOn)
112 {
113 while (i + 1 < n && pred(input[i + 1]))
114 {
115 ++i;
116 }
117 }
118 start = i + 1;
119 }
120 }
121 return result;
122}
123
124/// Concatenates the strings in @p input separated by @p sep.
125template <typename Container>
126std::string Join(const Container &input, std::string_view sep)
127{
128 std::string result;
129 bool first = true;
130 for (const auto &item : input)
131 {
132 if (!first)
133 {
134 result += sep;
135 }
136 result += item;
137 first = false;
138 }
139 return result;
140}
141
142inline bool IEquals(std::string_view a, std::string_view b)
143{
144 return a.size() == b.size() &&
145 std::equal(a.begin(), a.end(), b.begin(), detail::IEqualChar);
146}
147
148inline bool StartsWith(std::string_view input, std::string_view test)
149{
150 return input.substr(0, test.size()) == test;
151}
152
153inline bool EndsWith(std::string_view input, std::string_view test)
154{
155 return input.size() >= test.size() &&
156 input.substr(input.size() - test.size()) == test;
157}
158
159inline bool IStartsWith(std::string_view input, std::string_view test)
160{
161 return input.size() >= test.size() &&
162 IEquals(input.substr(0, test.size()), test);
163}
164
165inline bool IContains(std::string_view input, std::string_view test)
166{
167 return std::search(input.begin(), input.end(), test.begin(), test.end(),
168 detail::IEqualChar) != input.end() ||
169 test.empty();
170}
171
172inline void ToUpper(std::string &str)
173{
174 std::transform(str.begin(), str.end(), str.begin(), detail::ToUpper);
175}
176
177inline void ToLower(std::string &str)
178{
179 std::transform(str.begin(), str.end(), str.begin(), detail::ToLower);
180}
181
182inline std::string ToUpperCopy(std::string str)
183{
184 ToUpper(str);
185 return str;
186}
187
188inline std::string ToLowerCopy(std::string str)
189{
190 ToLower(str);
191 return str;
192}
193
194/// Removes leading and trailing characters matching @p pred.
195template <typename Pred> void TrimIf(std::string &str, Pred pred)
196{
197 auto end = std::find_if_not(str.rbegin(), str.rend(), pred).base();
198 str.erase(end, str.end());
199 str.erase(str.begin(), std::find_if_not(str.begin(), str.end(), pred));
200}
201
202inline void Trim(std::string &str)
203{
205}
206
207inline std::string TrimCopy(std::string str)
208{
209 Trim(str);
210 return str;
211}
212
213inline void ReplaceAll(std::string &str, std::string_view search,
214 std::string_view format)
215{
216 if (search.empty())
217 {
218 return;
219 }
220 for (std::size_t pos = str.find(search); pos != std::string::npos;
221 pos = str.find(search, pos + format.size()))
222 {
223 str.replace(pos, search.size(), format);
224 }
225}
226
227inline void EraseFirst(std::string &str, std::string_view search)
228{
229 std::size_t pos = str.find(search);
230 if (!search.empty() && pos != std::string::npos)
231 {
232 str.erase(pos, search.size());
233 }
234}
235
236} // namespace Nektar::StringUtils
237
238#endif
Predicate matching any of a set of characters.
bool operator()(char c) const
IsAnyOf(std::string_view chars)
bool IEqualChar(char a, char b)
void TrimIf(std::string &str, Pred pred)
Removes leading and trailing characters matching pred.
bool StartsWith(std::string_view input, std::string_view test)
bool IEquals(std::string_view a, std::string_view b)
bool IStartsWith(std::string_view input, std::string_view test)
void ReplaceAll(std::string &str, std::string_view search, std::string_view format)
std::string ToUpperCopy(std::string str)
void EraseFirst(std::string &str, std::string_view search)
void Trim(std::string &str)
bool IContains(std::string_view input, std::string_view test)
std::string Join(const Container &input, std::string_view sep)
Concatenates the strings in input separated by sep.
std::string TrimCopy(std::string str)
bool EndsWith(std::string_view input, std::string_view test)
void ToUpper(std::string &str)
void ToLower(std::string &str)
std::string ToLowerCopy(std::string str)
TokenCompressMode
Whether Split() merges adjacent separators.
Container & Split(Container &result, std::string_view input, Pred pred, TokenCompressMode mode=eTokenCompressOff)
Splits input at characters matching pred into result, which is cleared first. An empty input gives a ...