gem5  v20.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 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<std::is_integral<T>::value &&
112  std::is_signed<T>::value, T>::type
113 __to_number(const std::string &value)
114 {
115  // start big and narrow it down if needed, determine the base dynamically
116  long long r = std::stoll(value, nullptr, 0);
117  if (r < std::numeric_limits<T>::lowest()
118  || r > std::numeric_limits<T>::max()) {
119  throw std::out_of_range("Out of range");
120  }
121  return static_cast<T>(r);
122 }
123 
124 template <class T>
125 typename std::enable_if<std::is_integral<T>::value &&
126  !std::is_signed<T>::value, T>::type
127 __to_number(const std::string &value)
128 {
129  // start big and narrow it down if needed, determine the base dynamically
130  unsigned long long r = std::stoull(value, nullptr, 0);
131  if (r > std::numeric_limits<T>::max())
132  throw std::out_of_range("Out of range");
133  return static_cast<T>(r);
134 }
135 
136 template <class T>
137 typename std::enable_if<std::is_enum<T>::value, T>::type
138 __to_number(const std::string &value)
139 {
141  return static_cast<T>(r);
142 }
143 
144 template <class T>
145 typename std::enable_if<std::is_floating_point<T>::value, T>::type
146 __to_number(const std::string &value)
147 {
148  // start big and narrow it down if needed
149  long double r = std::stold(value);
150  if (r < std::numeric_limits<T>::lowest()
151  || r > std::numeric_limits<T>::max()) {
152  throw std::out_of_range("Out of range");
153  }
154  return static_cast<T>(r);
155 }
166 template <class T>
167 inline bool
168 to_number(const std::string &value, T &retval)
169 {
170  try {
171  retval = __to_number<T>(value);
172  return true;
173  } catch (const std::out_of_range&) {
174  return false;
175  } catch (const std::invalid_argument&) {
176  return false;
177  } catch (...) {
178  panic("Unrecognized exception.\n");
179  }
180 }
181 
185 inline bool
186 to_bool(const std::string &value, bool &retval)
187 {
188  std::string s = to_lower(value);
189 
190  if (s == "true") {
191  retval = true;
192  return true;
193  } else if (s == "false") {
194  retval = false;
195  return true;
196  }
197 
198  return false;
199 }
200 
201 // Put quotes around string arg if it contains spaces.
202 inline std::string
203 quote(const std::string &s)
204 {
205  std::string ret;
206  bool quote = s.find(' ') != std::string::npos;
207 
208  if (quote)
209  ret = '"';
210 
211  ret += s;
212 
213  if (quote)
214  ret += '"';
215 
216  return ret;
217 }
218 
219 
223 inline bool
224 startswith(const char *s, const char *prefix)
225 {
226  return (strncmp(s, prefix, strlen(prefix)) == 0);
227 }
228 
229 
233 inline bool
234 startswith(const std::string &s, const char *prefix)
235 {
236  return (s.compare(0, strlen(prefix), prefix) == 0);
237 }
238 
239 
243 inline bool
244 startswith(const std::string &s, const std::string &prefix)
245 {
246  return (s.compare(0, prefix.size(), prefix) == 0);
247 }
248 
249 
250 #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:186
tokenize
void tokenize(std::vector< std::string > &vector, const std::string &s, char token, bool ign=true)
eat_end_white
void eat_end_white(std::string &s)
Definition: str.hh:56
__to_number
std::enable_if< std::is_integral< T >::value &&std::is_signed< T >::value, T >::type __to_number(const std::string &value)
Definition: str.hh:113
type
uint8_t type
Definition: inet.hh:421
std::vector< std::string >
MipsISA::r
r
Definition: pra_constants.hh:95
split_last
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
eat_white
void eat_white(std::string &s)
Definition: str.hh:64
quote
std::string quote(const std::string &s)
Definition: str.hh:203
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:224
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)
logging.hh
eat_lead_white
void eat_lead_white(std::string &s)
Definition: str.hh:46
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
to_number
bool to_number(const std::string &value, T &retval)
Turn a string representation of a number, either integral or floating point, into an actual number.
Definition: str.hh:168
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17