gem5  v21.0.0.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 <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 inline void
46 eat_lead_white(std::string &s)
47 {
48  std::string::size_type off = s.find_first_not_of(' ');
49  if (off != std::string::npos) {
50  std::string::iterator begin = s.begin();
51  s.erase(begin, begin + off);
52  }
53 }
54 
55 inline void
56 eat_end_white(std::string &s)
57 {
58  std::string::size_type off = s.find_last_not_of(' ');
59  if (off != std::string::npos)
60  s.erase(s.begin() + off + 1, s.end());
61 }
62 
63 inline void
64 eat_white(std::string &s)
65 {
68 }
69 
70 inline std::string
71 to_lower(const std::string &s)
72 {
73  std::string lower;
74  int len = s.size();
75 
76  lower.reserve(len);
77 
78  for (const auto &c : s)
79  lower.push_back(std::tolower(c));
80 
81  return lower;
82 }
83 
84 // Split the string s into lhs and rhs on the first occurence of the
85 // character c. Character c is not included in either lhs or rhs. If
86 // character c is not contained within string s, lsh equals s.
87 bool
88 split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
89 
90 // Split the string s into lhs and rhs on the last occurence of the
91 // character c. Character c is not included in either lhs or rhs. If
92 // character c is not contained within string s, lhs equals s.
93 bool
94 split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
95 
96 // Tokenize the string <s> splitting on the character <token>, and
97 // place the result in the string vector <vector>. If <ign> is true,
98 // then empty result strings (due to trailing tokens, or consecutive
99 // tokens) are skipped.
100 void
101 tokenize(std::vector<std::string> &vector, const std::string &s,
102  char token, bool ign = true);
103 
110 template <class T>
111 typename std::enable_if_t<std::is_integral<T>::value &&
112  std::is_signed<T>::value, T>
113 __to_number(const std::string &value)
114 {
115  // Cannot parse scientific numbers
116  if (value.find('e') != std::string::npos) {
117  throw std::invalid_argument("Cannot convert scientific to integral");
118  }
119  // start big and narrow it down if needed, determine the base dynamically
120  long long r = std::stoll(value, nullptr, 0);
121  if (r < std::numeric_limits<T>::lowest()
122  || r > std::numeric_limits<T>::max()) {
123  throw std::out_of_range("Out of range");
124  }
125  return static_cast<T>(r);
126 }
127 
128 template <class T>
129 typename std::enable_if_t<std::is_integral<T>::value &&
130  !std::is_signed<T>::value, T>
131 __to_number(const std::string &value)
132 {
133  // Cannot parse scientific numbers
134  if (value.find('e') != std::string::npos) {
135  throw std::invalid_argument("Cannot convert scientific to integral");
136  }
137  // start big and narrow it down if needed, determine the base dynamically
138  unsigned long long r = std::stoull(value, nullptr, 0);
139  if (r > std::numeric_limits<T>::max())
140  throw std::out_of_range("Out of range");
141  return static_cast<T>(r);
142 }
143 
144 template <class T>
145 typename std::enable_if_t<std::is_enum<T>::value, T>
146 __to_number(const std::string &value)
147 {
148  // Cannot parse scientific numbers
149  if (value.find('e') != std::string::npos) {
150  throw std::invalid_argument("Cannot convert scientific to integral");
151  }
153  return static_cast<T>(r);
154 }
155 
156 template <class T>
157 typename std::enable_if_t<std::is_floating_point<T>::value, T>
158 __to_number(const std::string &value)
159 {
160  // start big and narrow it down if needed
161  long double r = std::stold(value);
162  if (r < std::numeric_limits<T>::lowest()
163  || r > std::numeric_limits<T>::max()) {
164  throw std::out_of_range("Out of range");
165  }
166  return static_cast<T>(r);
167 }
178 template <class T>
179 inline std::enable_if_t<(std::is_integral<T>::value ||
180  std::is_floating_point<T>::value ||
181  std::is_enum<T>::value) &&
182  !std::is_same<bool, T>::value, bool>
183 to_number(const std::string &value, T &retval)
184 {
185  try {
186  retval = __to_number<T>(value);
187  return true;
188  } catch (const std::out_of_range&) {
189  return false;
190  } catch (const std::invalid_argument&) {
191  return false;
192  } catch (...) {
193  panic("Unrecognized exception.\n");
194  }
195 }
196 
200 inline bool
201 to_bool(const std::string &value, bool &retval)
202 {
203  std::string s = to_lower(value);
204 
205  if (s == "true") {
206  retval = true;
207  return true;
208  } else if (s == "false") {
209  retval = false;
210  return true;
211  }
212 
213  return false;
214 }
215 
216 // Put quotes around string arg if it contains spaces.
217 inline std::string
218 quote(const std::string &s)
219 {
220  std::string ret;
221  bool quote = s.find(' ') != std::string::npos;
222 
223  if (quote)
224  ret = '"';
225 
226  ret += s;
227 
228  if (quote)
229  ret += '"';
230 
231  return ret;
232 }
233 
234 
238 inline bool
239 startswith(const char *s, const char *prefix)
240 {
241  return (strncmp(s, prefix, strlen(prefix)) == 0);
242 }
243 
244 
248 inline bool
249 startswith(const std::string &s, const char *prefix)
250 {
251  return (s.compare(0, strlen(prefix), prefix) == 0);
252 }
253 
254 
258 inline bool
259 startswith(const std::string &s, const std::string &prefix)
260 {
261  return (s.compare(0, prefix.size(), prefix) == 0);
262 }
263 
264 
265 #endif //__BASE_STR_HH__
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:201
tokenize
void tokenize(std::vector< std::string > &vector, const std::string &s, char token, bool ign=true)
Definition: str.cc:65
eat_end_white
void eat_end_white(std::string &s)
Definition: str.hh:56
std::vector< std::string >
to_number
std::enable_if_t<(std::is_integral< T >::value||std::is_floating_point< T >::value||std::is_enum< T >::value) &&!std::is_same< bool, T >::value, bool > to_number(const std::string &value, T &retval)
Turn a string representation of a number, either integral, floating point, or enum into an actual num...
Definition: str.hh:183
MipsISA::r
r
Definition: pra_constants.hh:95
__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:113
SCMI::token
token
Definition: scmi_platform.hh:77
split_last
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition: str.cc:50
eat_white
void eat_white(std::string &s)
Definition: str.hh:64
quote
std::string quote(const std::string &s)
Definition: str.hh:218
to_lower
std::string to_lower(const std::string &s)
Definition: str.hh:71
X86ISA::vector
Bitfield< 15, 8 > vector
Definition: intmessage.hh:44
startswith
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
Definition: str.hh:239
ArmISA::len
Bitfield< 18, 16 > len
Definition: miscregs_types.hh:439
split_first
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)
Definition: str.cc:35
logging.hh
eat_lead_white
void eat_lead_white(std::string &s)
Definition: str.hh:46
X86ISA::type
type
Definition: misc.hh:727
ArmISA::c
Bitfield< 29 > c
Definition: miscregs_types.hh:50
Stats::size_type
unsigned int size_type
Definition: types.hh:54
ArmISA::s
Bitfield< 4 > s
Definition: miscregs_types.hh:556
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Tue Mar 23 2021 19:41:24 for gem5 by doxygen 1.8.17