gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
46 namespace gem5
47 {
48 
49 inline void
50 eat_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 
59 inline void
60 eat_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 
67 inline void
68 eat_white(std::string &s)
69 {
72 }
73 
74 inline std::string
75 to_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.
91 bool
92 split_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.
97 bool
98 split_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.
104 void
105 tokenize(std::vector<std::string> &vector, const std::string &s,
106  char token, bool ign = true);
107 
115 template <class T>
116 typename 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 
139 template <class T>
140 typename std::enable_if_t<std::is_enum_v<T>, T>
141 __to_number(const std::string &value)
142 {
143  auto r = __to_number<typename std::underlying_type_t<T>>(value);
144  return static_cast<T>(r);
145 }
146 
147 template <class T>
148 typename 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 }
169 template <class T>
170 inline 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>
174 to_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 
191 inline bool
192 to_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.
208 inline std::string
209 quote(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 
229 inline bool
230 startswith(const char *s, const char *prefix)
231 {
232  return (strncmp(s, prefix, strlen(prefix)) == 0);
233 }
234 
235 
239 inline bool
240 startswith(const std::string &s, const char *prefix)
241 {
242  return (s.compare(0, strlen(prefix), prefix) == 0);
243 }
244 
245 
249 inline bool
250 startswith(const std::string &s, const std::string &prefix)
251 {
252  return (s.compare(0, prefix.size(), prefix) == 0);
253 }
254 
255 inline std::string
256 replace(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__
gem5::VegaISA::s
Bitfield< 1 > s
Definition: pagetable.hh:64
gem5::replace
std::string replace(const std::string &s, char from, char to)
Definition: str.hh:256
gem5::eat_lead_white
void eat_lead_white(std::string &s)
Definition: str.hh:50
gem5::X86ISA::vector
Bitfield< 15, 8 > vector
Definition: intmessage.hh:48
gem5::startswith
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
Definition: str.hh:230
gem5::eat_white
void eat_white(std::string &s)
Definition: str.hh:68
gem5::to_number
bool to_number(const std::string &value, Pixel &retval)
Definition: pixel.hh:217
std::vector< std::string >
gem5::VegaISA::r
Bitfield< 5 > r
Definition: pagetable.hh:60
gem5::scmi::token
token
Definition: scmi_platform.hh:80
gem5::split_last
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition: str.cc:53
gem5::PowerISA::to
Bitfield< 25, 21 > to
Definition: types.hh:96
gem5::VegaISA::c
Bitfield< 2 > c
Definition: pagetable.hh:63
gem5::to_bool
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:192
len
uint16_t len
Definition: helpers.cc:62
gem5::quote
std::string quote(const std::string &s)
Definition: str.hh:209
gem5::tokenize
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
Definition: str.cc:68
gem5::statistics::size_type
unsigned int size_type
Definition: types.hh:59
gem5::to_lower
std::string to_lower(const std::string &s)
Definition: str.hh:75
gem5::split_first
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition: str.cc:38
logging.hh
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::__to_number
std::enable_if_t< std::is_integral_v< T >, T > __to_number(const std::string &value)
Definition: str.hh:117
gem5::eat_end_white
void eat_end_white(std::string &s)
Definition: str.hh:60
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:188

Generated on Sun Jul 30 2023 01:56:51 for gem5 by doxygen 1.8.17