gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
str.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2005 The Regents of The University of Michigan
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * Authors: Nathan Binkert
29  * Steve Reinhardt
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 <vector>
41 
42 inline void
43 eat_lead_white(std::string &s)
44 {
45  std::string::size_type off = s.find_first_not_of(' ');
46  if (off != std::string::npos) {
47  std::string::iterator begin = s.begin();
48  s.erase(begin, begin + off);
49  }
50 }
51 
52 inline void
53 eat_end_white(std::string &s)
54 {
55  std::string::size_type off = s.find_last_not_of(' ');
56  if (off != std::string::npos)
57  s.erase(s.begin() + off + 1, s.end());
58 }
59 
60 inline void
61 eat_white(std::string &s)
62 {
63  eat_lead_white(s);
64  eat_end_white(s);
65 }
66 
67 inline std::string
68 to_lower(const std::string &s)
69 {
70  std::string lower;
71  int len = s.size();
72 
73  lower.reserve(len);
74 
75  for (const auto &c : s)
76  lower.push_back(std::tolower(c));
77 
78  return lower;
79 }
80 
81 // Split the string s into lhs and rhs on the first occurence of the
82 // character c.
83 bool
84 split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
85 
86 // Split the string s into lhs and rhs on the last occurence of the
87 // character c.
88 bool
89 split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
90 
91 // Tokenize the string <s> splitting on the character <token>, and
92 // place the result in the string vector <vector>. If <ign> is true,
93 // then empty result strings (due to trailing tokens, or consecutive
94 // tokens) are skipped.
95 void
96 tokenize(std::vector<std::string> &vector, const std::string &s,
97  char token, bool ign = true);
98 
105 template <class T>
106 typename std::enable_if<std::is_integral<T>::value &&
107  std::is_signed<T>::value, T>::type
108 __to_number(const std::string &value)
109 {
110  // start big and narrow it down if needed, determine the base dynamically
111  long long r = std::stoll(value, nullptr, 0);
112  if (r < std::numeric_limits<T>::min() || r > std::numeric_limits<T>::max())
113  throw std::out_of_range("Out of range");
114  return static_cast<T>(r);
115 }
116 
117 template <class T>
118 typename std::enable_if<std::is_integral<T>::value &&
119  !std::is_signed<T>::value, T>::type
120 __to_number(const std::string &value)
121 {
122  // start big and narrow it down if needed, determine the base dynamically
123  unsigned long long r = std::stoull(value, nullptr, 0);
124  if (r > std::numeric_limits<T>::max())
125  throw std::out_of_range("Out of range");
126  return static_cast<T>(r);
127 }
128 
129 template <class T>
130 typename std::enable_if<std::is_floating_point<T>::value, T>::type
131 __to_number(const std::string &value)
132 {
133  // start big and narrow it down if needed
134  long double r = std::stold(value);
135  if (r < std::numeric_limits<T>::min() || r > std::numeric_limits<T>::max())
136  throw std::out_of_range("Out of range");
137  return static_cast<T>(r);
138 }
149 template <class T>
150 inline bool
151 to_number(const std::string &value, T &retval)
152 {
153  try {
154  retval = __to_number<T>(value);
155  return true;
156  } catch (const std::out_of_range&) {
157  return false;
158  } catch (const std::invalid_argument&) {
159  return false;
160  }
161 }
162 
166 inline bool
167 to_bool(const std::string &value, bool &retval)
168 {
169  std::string s = to_lower(value);
170 
171  if (s == "true") {
172  retval = true;
173  return true;
174  } else if (s == "false") {
175  retval = false;
176  return true;
177  }
178 
179  return false;
180 }
181 
182 // Put quotes around string arg if it contains spaces.
183 inline std::string
184 quote(const std::string &s)
185 {
186  std::string ret;
187  bool quote = s.find(' ') != std::string::npos;
188 
189  if (quote)
190  ret = '"';
191 
192  ret += s;
193 
194  if (quote)
195  ret += '"';
196 
197  return ret;
198 }
199 
200 
204 inline bool
205 startswith(const char *s, const char *prefix)
206 {
207  return (strncmp(s, prefix, strlen(prefix)) == 0);
208 }
209 
210 
214 inline bool
215 startswith(const std::string &s, const char *prefix)
216 {
217  return (s.compare(0, strlen(prefix), prefix) == 0);
218 }
219 
220 
224 inline bool
225 startswith(const std::string &s, const std::string &prefix)
226 {
227  return (s.compare(0, prefix.size(), prefix) == 0);
228 }
229 
230 
231 #endif //__BASE_STR_HH__
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
Definition: str.hh:167
void eat_end_white(std::string &s)
Definition: str.hh:53
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:108
unsigned int size_type
Definition: types.hh:56
Bitfield< 4 > s
Definition: miscregs.hh:1738
Bitfield< 15, 8 > vector
Definition: intmessage.hh:45
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
Definition: str.hh:205
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:184
type
Definition: misc.hh:728
Bitfield< 29 > c
Definition: miscregs.hh:1365
void eat_white(std::string &s)
Definition: str.hh:61
Bitfield< 18, 16 > len
Definition: miscregs.hh:1626
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:151
std::string to_lower(const std::string &s)
Definition: str.hh:68
void eat_lead_white(std::string &s)
Definition: str.hh:43
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)

Generated on Fri Jun 9 2017 13:03:41 for gem5 by doxygen 1.8.6