gem5 v24.0.0.0
Loading...
Searching...
No Matches
str.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 ARM Limited
3 * All rights reserved
4 *
5 * Copyright (c) 2001-2005 The Regents of The University of Michigan
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met: redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer;
12 * redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution;
15 * neither the name of the copyright holders nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef __BASE_STR_HH__
33#define __BASE_STR_HH__
34
35#include <algorithm>
36#include <cstring>
37#include <limits>
38#include <locale>
39#include <stdexcept>
40#include <string>
41#include <type_traits>
42#include <vector>
43
44#include "base/logging.hh"
45
46namespace gem5
47{
48
49inline void
50eat_lead_white(std::string &s)
51{
52 std::string::size_type off = s.find_first_not_of(' ');
53 if (off != std::string::npos) {
54 std::string::iterator begin = s.begin();
55 s.erase(begin, begin + off);
56 }
57}
58
59inline void
60eat_end_white(std::string &s)
61{
62 std::string::size_type off = s.find_last_not_of(' ');
63 if (off != std::string::npos)
64 s.erase(s.begin() + off + 1, s.end());
65}
66
67inline void
68eat_white(std::string &s)
69{
72}
73
74inline std::string
75to_lower(const std::string &s)
76{
77 std::string lower;
78 int len = s.size();
79
80 lower.reserve(len);
81
82 for (const auto &c : s)
83 lower.push_back(std::tolower(c));
84
85 return lower;
86}
87
88// Split the string s into lhs and rhs on the first occurence of the
89// character c. Character c is not included in either lhs or rhs. If
90// character c is not contained within string s, lsh equals s.
91bool
92split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
93
94// Split the string s into lhs and rhs on the last occurence of the
95// character c. Character c is not included in either lhs or rhs. If
96// character c is not contained within string s, lhs equals s.
97bool
98split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
99
100// Tokenize the string <s> splitting on the character <token>, and
101// place the result in the string vector <vector>. If <ign> is true,
102// then empty result strings (due to trailing tokens, or consecutive
103// tokens) are skipped.
104void
105tokenize(std::vector<std::string> &vector, const std::string &s,
106 char token, bool ign = true);
107
115template <class T>
116typename std::enable_if_t<std::is_integral_v<T>, T>
117__to_number(const std::string &value)
118{
119 // Cannot parse scientific numbers
120 if (value.find('e') != std::string::npos) {
121 throw std::invalid_argument("Cannot convert scientific to integral");
122 }
123 // start big and narrow it down if needed, determine the base dynamically
124 if constexpr (std::is_signed_v<T>) {
125 long long r = std::stoll(value, nullptr, 0);
126 if (r < std::numeric_limits<T>::lowest()
127 || r > std::numeric_limits<T>::max()) {
128 throw std::out_of_range("Out of range");
129 }
130 return static_cast<T>(r);
131 } else {
132 unsigned long long r = std::stoull(value, nullptr, 0);
133 if (r > std::numeric_limits<T>::max())
134 throw std::out_of_range("Out of range");
135 return static_cast<T>(r);
136 }
137}
138
139template <class T>
140typename std::enable_if_t<std::is_enum_v<T>, T>
141__to_number(const std::string &value)
142{
144 return static_cast<T>(r);
145}
146
147template <class T>
148typename std::enable_if_t<std::is_floating_point_v<T>, T>
149__to_number(const std::string &value)
150{
151 // start big and narrow it down if needed
152 long double r = std::stold(value);
153 if (r < std::numeric_limits<T>::lowest()
154 || r > std::numeric_limits<T>::max()) {
155 throw std::out_of_range("Out of range");
156 }
157 return static_cast<T>(r);
158}
169template <class T>
170inline std::enable_if_t<(std::is_integral_v<T> ||
171 std::is_floating_point_v<T> ||
172 std::is_enum_v<T>) &&
173 !std::is_same_v<bool, T>, bool>
174to_number(const std::string &value, T &retval)
175{
176 try {
177 retval = __to_number<T>(value);
178 return true;
179 } catch (const std::out_of_range&) {
180 return false;
181 } catch (const std::invalid_argument&) {
182 return false;
183 } catch (...) {
184 panic("Unrecognized exception.\n");
185 }
186}
187
191inline bool
192to_bool(const std::string &value, bool &retval)
193{
194 std::string s = to_lower(value);
195
196 if (s == "true") {
197 retval = true;
198 return true;
199 } else if (s == "false") {
200 retval = false;
201 return true;
202 }
203
204 return false;
205}
206
207// Put quotes around string arg if it contains spaces.
208inline std::string
209quote(const std::string &s)
210{
211 std::string ret;
212 bool quote = s.find(' ') != std::string::npos;
213
214 if (quote)
215 ret = '"';
216
217 ret += s;
218
219 if (quote)
220 ret += '"';
221
222 return ret;
223}
224
225
229inline bool
230startswith(const char *s, const char *prefix)
231{
232 return (strncmp(s, prefix, strlen(prefix)) == 0);
233}
234
235
239inline bool
240startswith(const std::string &s, const char *prefix)
241{
242 return (s.compare(0, strlen(prefix), prefix) == 0);
243}
244
245
249inline bool
250startswith(const std::string &s, const std::string &prefix)
251{
252 return (s.compare(0, prefix.size(), prefix) == 0);
253}
254
255inline std::string
256replace(const std::string &s, char from, char to)
257{
258 std::string replaced = s;
259 std::replace(replaced.begin(), replaced.end(), from, to);
260 return replaced;
261}
262
263} // namespace gem5
264
265#endif //__BASE_STR_HH__
STL vector class.
Definition stl.hh:37
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
Bitfield< 18, 16 > len
Bitfield< 4 > s
Bitfield< 29 > c
Definition misc_types.hh:53
Bitfield< 25, 21 > to
Definition types.hh:96
Bitfield< 15, 8 > vector
Definition intmessage.hh:48
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::enable_if_t< std::is_integral_v< T >, T > __to_number(const std::string &value)
Definition str.hh:117
std::string to_lower(const std::string &s)
Definition str.hh:75
bool to_number(const std::string &value, Pixel &retval)
Definition pixel.hh:217
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
Definition str.hh:230
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition str.hh:192
std::string quote(const std::string &s)
Definition str.hh:209
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
Definition str.cc:68
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition str.cc:53
void eat_end_white(std::string &s)
Definition str.hh:60
std::string replace(const std::string &s, char from, char to)
Definition str.hh:256
void eat_white(std::string &s)
Definition str.hh:68
void eat_lead_white(std::string &s)
Definition str.hh:50
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition str.cc:38

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0