gem5  v19.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  * Authors: Nathan Binkert
32  * Steve Reinhardt
33  */
34 
35 #ifndef __BASE_STR_HH__
36 #define __BASE_STR_HH__
37 
38 #include <cstring>
39 #include <limits>
40 #include <locale>
41 #include <stdexcept>
42 #include <string>
43 #include <type_traits>
44 #include <vector>
45 
46 #include "base/logging.hh"
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 {
69  eat_lead_white(s);
70  eat_end_white(s);
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<std::is_integral<T>::value &&
115  std::is_signed<T>::value, T>::type
116 __to_number(const std::string &value)
117 {
118  // start big and narrow it down if needed, determine the base dynamically
119  long long r = std::stoll(value, nullptr, 0);
120  if (r < std::numeric_limits<T>::lowest()
121  || r > std::numeric_limits<T>::max()) {
122  throw std::out_of_range("Out of range");
123  }
124  return static_cast<T>(r);
125 }
126 
127 template <class T>
128 typename std::enable_if<std::is_integral<T>::value &&
129  !std::is_signed<T>::value, T>::type
130 __to_number(const std::string &value)
131 {
132  // start big and narrow it down if needed, determine the base dynamically
133  unsigned long long r = std::stoull(value, nullptr, 0);
134  if (r > std::numeric_limits<T>::max())
135  throw std::out_of_range("Out of range");
136  return static_cast<T>(r);
137 }
138 
139 template <class T>
140 typename std::enable_if<std::is_enum<T>::value, T>::type
141 __to_number(const std::string &value)
142 {
144  return static_cast<T>(r);
145 }
146 
147 template <class T>
148 typename std::enable_if<std::is_floating_point<T>::value, T>::type
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 bool
171 to_number(const std::string &value, T &retval)
172 {
173  try {
174  retval = __to_number<T>(value);
175  return true;
176  } catch (const std::out_of_range&) {
177  return false;
178  } catch (const std::invalid_argument&) {
179  return false;
180  } catch (...) {
181  panic("Unrecognized exception.\n");
182  }
183 }
184 
188 inline bool
189 to_bool(const std::string &value, bool &retval)
190 {
191  std::string s = to_lower(value);
192 
193  if (s == "true") {
194  retval = true;
195  return true;
196  } else if (s == "false") {
197  retval = false;
198  return true;
199  }
200 
201  return false;
202 }
203 
204 // Put quotes around string arg if it contains spaces.
205 inline std::string
206 quote(const std::string &s)
207 {
208  std::string ret;
209  bool quote = s.find(' ') != std::string::npos;
210 
211  if (quote)
212  ret = '"';
213 
214  ret += s;
215 
216  if (quote)
217  ret += '"';
218 
219  return ret;
220 }
221 
222 
226 inline bool
227 startswith(const char *s, const char *prefix)
228 {
229  return (strncmp(s, prefix, strlen(prefix)) == 0);
230 }
231 
232 
236 inline bool
237 startswith(const std::string &s, const char *prefix)
238 {
239  return (s.compare(0, strlen(prefix), prefix) == 0);
240 }
241 
242 
246 inline bool
247 startswith(const std::string &s, const std::string &prefix)
248 {
249  return (s.compare(0, prefix.size(), prefix) == 0);
250 }
251 
252 
253 #endif //__BASE_STR_HH__
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:167
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:189
void eat_end_white(std::string &s)
Definition: str.hh:59
void tokenize(std::vector< std::string > &vector, const std::string &s, char token, bool ign=true)
std::enable_if< std::is_integral< T >::value &&std::is_signed< T >::value, T >::type __to_number(const std::string &value)
Definition: str.hh:116
unsigned int size_type
Definition: types.hh:56
uint8_t type
Definition: inet.hh:333
Bitfield< 4 > s
Bitfield< 18, 16 > len
Bitfield< 15, 8 > vector
Definition: intmessage.hh:46
bool startswith(const char *s, const char *prefix)
Return true if &#39;s&#39; starts with the prefix string &#39;prefix&#39;.
Definition: str.hh:227
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
std::string quote(const std::string &s)
Definition: str.hh:206
Bitfield< 29 > c
void eat_white(std::string &s)
Definition: str.hh:67
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:171
std::string to_lower(const std::string &s)
Definition: str.hh:74
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)

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13