gem5  v21.1.0.2
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 
113 template <class T>
114 typename std::enable_if_t<std::is_integral<T>::value &&
115  std::is_signed<T>::value, 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  long long r = std::stoll(value, nullptr, 0);
124  if (r < std::numeric_limits<T>::lowest()
125  || r > std::numeric_limits<T>::max()) {
126  throw std::out_of_range("Out of range");
127  }
128  return static_cast<T>(r);
129 }
130 
131 template <class T>
132 typename std::enable_if_t<std::is_integral<T>::value &&
133  !std::is_signed<T>::value, T>
134 __to_number(const std::string &value)
135 {
136  // Cannot parse scientific numbers
137  if (value.find('e') != std::string::npos) {
138  throw std::invalid_argument("Cannot convert scientific to integral");
139  }
140  // start big and narrow it down if needed, determine the base dynamically
141  unsigned long long r = std::stoull(value, nullptr, 0);
142  if (r > std::numeric_limits<T>::max())
143  throw std::out_of_range("Out of range");
144  return static_cast<T>(r);
145 }
146 
147 template <class T>
148 typename std::enable_if_t<std::is_enum<T>::value, T>
149 __to_number(const std::string &value)
150 {
151  // Cannot parse scientific numbers
152  if (value.find('e') != std::string::npos) {
153  throw std::invalid_argument("Cannot convert scientific to integral");
154  }
156  return static_cast<T>(r);
157 }
158 
159 template <class T>
160 typename std::enable_if_t<std::is_floating_point<T>::value, T>
161 __to_number(const std::string &value)
162 {
163  // start big and narrow it down if needed
164  long double r = std::stold(value);
165  if (r < std::numeric_limits<T>::lowest()
166  || r > std::numeric_limits<T>::max()) {
167  throw std::out_of_range("Out of range");
168  }
169  return static_cast<T>(r);
170 }
181 template <class T>
182 inline std::enable_if_t<(std::is_integral<T>::value ||
183  std::is_floating_point<T>::value ||
184  std::is_enum<T>::value) &&
185  !std::is_same<bool, T>::value, bool>
186 to_number(const std::string &value, T &retval)
187 {
188  try {
189  retval = __to_number<T>(value);
190  return true;
191  } catch (const std::out_of_range&) {
192  return false;
193  } catch (const std::invalid_argument&) {
194  return false;
195  } catch (...) {
196  panic("Unrecognized exception.\n");
197  }
198 }
199 
203 inline bool
204 to_bool(const std::string &value, bool &retval)
205 {
206  std::string s = to_lower(value);
207 
208  if (s == "true") {
209  retval = true;
210  return true;
211  } else if (s == "false") {
212  retval = false;
213  return true;
214  }
215 
216  return false;
217 }
218 
219 // Put quotes around string arg if it contains spaces.
220 inline std::string
221 quote(const std::string &s)
222 {
223  std::string ret;
224  bool quote = s.find(' ') != std::string::npos;
225 
226  if (quote)
227  ret = '"';
228 
229  ret += s;
230 
231  if (quote)
232  ret += '"';
233 
234  return ret;
235 }
236 
237 
241 inline bool
242 startswith(const char *s, const char *prefix)
243 {
244  return (strncmp(s, prefix, strlen(prefix)) == 0);
245 }
246 
247 
251 inline bool
252 startswith(const std::string &s, const char *prefix)
253 {
254  return (s.compare(0, strlen(prefix), prefix) == 0);
255 }
256 
257 
261 inline bool
262 startswith(const std::string &s, const std::string &prefix)
263 {
264  return (s.compare(0, prefix.size(), prefix) == 0);
265 }
266 
267 } // namespace gem5
268 
269 #endif //__BASE_STR_HH__
gem5::eat_lead_white
void eat_lead_white(std::string &s)
Definition: str.hh:49
gem5::ArmISA::len
Bitfield< 18, 16 > len
Definition: misc_types.hh:444
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:242
gem5::eat_white
void eat_white(std::string &s)
Definition: str.hh:67
gem5::to_number
bool to_number(const std::string &value, Pixel &retval)
Definition: pixel.hh:217
std::vector< std::string >
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::X86ISA::type
type
Definition: misc.hh:733
gem5::ArmISA::s
Bitfield< 4 > s
Definition: misc_types.hh:561
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:204
gem5::quote
std::string quote(const std::string &s)
Definition: str.hh:221
gem5::ArmISA::c
Bitfield< 29 > c
Definition: misc_types.hh:53
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:60
gem5::to_lower
std::string to_lower(const std::string &s)
Definition: str.hh:74
gem5::__to_number
std::enable_if_t< std::is_integral< T >::value &&std::is_signed< T >::value, T > __to_number(const std::string &value)
Definition: str.hh:116
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::MipsISA::r
r
Definition: pra_constants.hh:98
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::eat_end_white
void eat_end_white(std::string &s)
Definition: str.hh:59
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:177

Generated on Tue Sep 21 2021 12:25:00 for gem5 by doxygen 1.8.17