gem5  v22.1.0.0
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 <cstring>
36 #include <limits>
37 #include <locale>
38 #include <stdexcept>
39 #include <string>
40 #include <type_traits>
41 #include <vector>
42 
43 #include "base/logging.hh"
44 
45 namespace gem5
46 {
47 
48 inline void
49 eat_lead_white(std::string &s)
50 {
51  std::string::size_type off = s.find_first_not_of(' ');
52  if (off != std::string::npos) {
53  std::string::iterator begin = s.begin();
54  s.erase(begin, begin + off);
55  }
56 }
57 
58 inline void
59 eat_end_white(std::string &s)
60 {
61  std::string::size_type off = s.find_last_not_of(' ');
62  if (off != std::string::npos)
63  s.erase(s.begin() + off + 1, s.end());
64 }
65 
66 inline void
67 eat_white(std::string &s)
68 {
71 }
72 
73 inline std::string
74 to_lower(const std::string &s)
75 {
76  std::string lower;
77  int len = s.size();
78 
79  lower.reserve(len);
80 
81  for (const auto &c : s)
82  lower.push_back(std::tolower(c));
83 
84  return lower;
85 }
86 
87 // Split the string s into lhs and rhs on the first occurence of the
88 // character c. Character c is not included in either lhs or rhs. If
89 // character c is not contained within string s, lsh equals s.
90 bool
91 split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
92 
93 // Split the string s into lhs and rhs on the last occurence of the
94 // character c. Character c is not included in either lhs or rhs. If
95 // character c is not contained within string s, lhs equals s.
96 bool
97 split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
98 
99 // Tokenize the string <s> splitting on the character <token>, and
100 // place the result in the string vector <vector>. If <ign> is true,
101 // then empty result strings (due to trailing tokens, or consecutive
102 // tokens) are skipped.
103 void
104 tokenize(std::vector<std::string> &vector, const std::string &s,
105  char token, bool ign = true);
106 
114 template <class T>
115 typename std::enable_if_t<std::is_integral_v<T>, T>
116 __to_number(const std::string &value)
117 {
118  // Cannot parse scientific numbers
119  if (value.find('e') != std::string::npos) {
120  throw std::invalid_argument("Cannot convert scientific to integral");
121  }
122  // start big and narrow it down if needed, determine the base dynamically
123  if constexpr (std::is_signed_v<T>) {
124  long long r = std::stoll(value, nullptr, 0);
125  if (r < std::numeric_limits<T>::lowest()
126  || r > std::numeric_limits<T>::max()) {
127  throw std::out_of_range("Out of range");
128  }
129  return static_cast<T>(r);
130  } else {
131  unsigned long long r = std::stoull(value, nullptr, 0);
132  if (r > std::numeric_limits<T>::max())
133  throw std::out_of_range("Out of range");
134  return static_cast<T>(r);
135  }
136 }
137 
138 template <class T>
139 typename std::enable_if_t<std::is_enum_v<T>, T>
140 __to_number(const std::string &value)
141 {
142  auto r = __to_number<typename std::underlying_type_t<T>>(value);
143  return static_cast<T>(r);
144 }
145 
146 template <class T>
147 typename std::enable_if_t<std::is_floating_point_v<T>, T>
148 __to_number(const std::string &value)
149 {
150  // start big and narrow it down if needed
151  long double r = std::stold(value);
152  if (r < std::numeric_limits<T>::lowest()
153  || r > std::numeric_limits<T>::max()) {
154  throw std::out_of_range("Out of range");
155  }
156  return static_cast<T>(r);
157 }
168 template <class T>
169 inline std::enable_if_t<(std::is_integral_v<T> ||
170  std::is_floating_point_v<T> ||
171  std::is_enum_v<T>) &&
172  !std::is_same_v<bool, T>, bool>
173 to_number(const std::string &value, T &retval)
174 {
175  try {
176  retval = __to_number<T>(value);
177  return true;
178  } catch (const std::out_of_range&) {
179  return false;
180  } catch (const std::invalid_argument&) {
181  return false;
182  } catch (...) {
183  panic("Unrecognized exception.\n");
184  }
185 }
186 
190 inline bool
191 to_bool(const std::string &value, bool &retval)
192 {
193  std::string s = to_lower(value);
194 
195  if (s == "true") {
196  retval = true;
197  return true;
198  } else if (s == "false") {
199  retval = false;
200  return true;
201  }
202 
203  return false;
204 }
205 
206 // Put quotes around string arg if it contains spaces.
207 inline std::string
208 quote(const std::string &s)
209 {
210  std::string ret;
211  bool quote = s.find(' ') != std::string::npos;
212 
213  if (quote)
214  ret = '"';
215 
216  ret += s;
217 
218  if (quote)
219  ret += '"';
220 
221  return ret;
222 }
223 
224 
228 inline bool
229 startswith(const char *s, const char *prefix)
230 {
231  return (strncmp(s, prefix, strlen(prefix)) == 0);
232 }
233 
234 
238 inline bool
239 startswith(const std::string &s, const char *prefix)
240 {
241  return (s.compare(0, strlen(prefix), prefix) == 0);
242 }
243 
244 
248 inline bool
249 startswith(const std::string &s, const std::string &prefix)
250 {
251  return (s.compare(0, prefix.size(), prefix) == 0);
252 }
253 
254 } // namespace gem5
255 
256 #endif //__BASE_STR_HH__
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
uint16_t len
Definition: helpers.cc:62
Bitfield< 5 > r
Definition: pagetable.hh:60
Bitfield< 1 > s
Definition: pagetable.hh:64
Bitfield< 2 > c
Definition: pagetable.hh:63
Bitfield< 15, 8 > vector
Definition: intmessage.hh:48
unsigned int size_type
Definition: types.hh:60
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
std::string to_lower(const std::string &s)
Definition: str.hh:74
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:229
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:191
std::string quote(const std::string &s)
Definition: str.hh:208
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:59
void eat_white(std::string &s)
Definition: str.hh:67
std::enable_if_t< std::is_integral_v< T >, T > __to_number(const std::string &value)
Definition: str.hh:116
void eat_lead_white(std::string &s)
Definition: str.hh:49
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition: str.cc:38

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1