2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
32 #ifndef __BASE_STR_HH__
33 #define __BASE_STR_HH__
43 eat_lead_white(std::string &s)
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);
53 eat_end_white(std::string &s)
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());
61 eat_white(std::string &s)
68 to_lower(const std::string &s)
75 for (const auto &c : s)
76 lower.push_back(std::tolower(c));
81 // Split the string s into lhs and rhs on the first occurence of the
84 split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
86 // Split the string s into lhs and rhs on the last occurence of the
89 split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
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.
96 tokenize(std::vector<std::string> &vector, const std::string &s,
97 char token, bool ign = true);
102 * @name String to number helper functions for signed and unsigned
103 * integeral type, as well as floating-point types.
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)
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);
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)
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);
130 typename std::enable_if<std::is_floating_point<T>::value, T>::type
131 __to_number(const std::string &value)
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);
142 * Turn a string representation of a number, either integral or
143 * floating point, into an actual number.
145 * @param value The string representing the number
146 * @param retval The resulting value
147 * @return True if the parsing was successful
151 to_number(const std::string &value, T &retval)
154 retval = __to_number<T>(value);
156 } catch (const std::out_of_range&) {
158 } catch (const std::invalid_argument&) {
164 * Turn a string representation of a boolean into a boolean value.
167 to_bool(const std::string &value, bool &retval)
169 std::string s = to_lower(value);
174 } else if (s == "false") {
182 // Put quotes around string arg if it contains spaces.
184 quote(const std::string &s)
187 bool quote = s.find(' ') != std::string::npos;
202 * Return true if 's' starts with the prefix string 'prefix'.
205 startswith(const char *s, const char *prefix)
207 return (strncmp(s, prefix, strlen(prefix)) == 0);
212 * Return true if 's' starts with the prefix string 'prefix'.
215 startswith(const std::string &s, const char *prefix)
217 return (s.compare(0, strlen(prefix), prefix) == 0);
222 * Return true if 's' starts with the prefix string 'prefix'.
225 startswith(const std::string &s, const std::string &prefix)
227 return (s.compare(0, prefix.size(), prefix) == 0);
231 #endif //__BASE_STR_HH__