misc: Merge branch v20.1.0.3 hotfix into develop
[gem5.git] / src / base / str.hh
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 {
66 eat_lead_white(s);
67 eat_end_white(s);
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
104 /**
105 * @{
106 *
107 * @name String to number helper functions for signed and unsigned
108 * integeral type, as well as enums and floating-point types.
109 */
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 // 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_t<std::is_integral<T>::value &&
126 !std::is_signed<T>::value, T>
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_t<std::is_enum<T>::value, T>
138 __to_number(const std::string &value)
139 {
140 auto r = __to_number<typename std::underlying_type<T>::type>(value);
141 return static_cast<T>(r);
142 }
143
144 template <class T>
145 typename std::enable_if_t<std::is_floating_point<T>::value, T>
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 }
156 /** @} */
157
158 /**
159 * Turn a string representation of a number, either integral, floating point,
160 * or enum into an actual number. Use to_bool for booleans.
161 *
162 * @param value The string representing the number
163 * @param retval The resulting value
164 * @return True if the parsing was successful
165 */
166 template <class T>
167 inline std::enable_if_t<(std::is_integral<T>::value ||
168 std::is_floating_point<T>::value ||
169 std::is_enum<T>::value) &&
170 !std::is_same<bool, T>::value, 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
185 /**
186 * Turn a string representation of a boolean into a boolean value.
187 */
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
223 /**
224 * Return true if 's' starts with the prefix string 'prefix'.
225 */
226 inline bool
227 startswith(const char *s, const char *prefix)
228 {
229 return (strncmp(s, prefix, strlen(prefix)) == 0);
230 }
231
232
233 /**
234 * Return true if 's' starts with the prefix string 'prefix'.
235 */
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
243 /**
244 * Return true if 's' starts with the prefix string 'prefix'.
245 */
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__