2 * Copyright (c) 2018 ARM Limited
5 * Copyright (c) 2001-2005 The Regents of The University of Michigan
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.
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.
32 #ifndef __BASE_STR_HH__
33 #define __BASE_STR_HH__
40 #include <type_traits>
43 #include "base/logging.hh"
46 eat_lead_white(std::string &s)
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);
56 eat_end_white(std::string &s)
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());
64 eat_white(std::string &s)
71 to_lower(const std::string &s)
78 for (const auto &c : s)
79 lower.push_back(std::tolower(c));
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.
88 split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);
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.
94 split_last(const std::string &s, std::string &lhs, std::string &rhs, char c);
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.
101 tokenize(std::vector<std::string> &vector, const std::string &s,
102 char token, bool ign = true);
107 * @name String to number helper functions for signed and unsigned
108 * integeral type, as well as enums and floating-point types.
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)
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");
121 return static_cast<T>(r);
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)
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);
137 typename std::enable_if_t<std::is_enum<T>::value, T>
138 __to_number(const std::string &value)
140 auto r = __to_number<typename std::underlying_type<T>::type>(value);
141 return static_cast<T>(r);
145 typename std::enable_if_t<std::is_floating_point<T>::value, T>
146 __to_number(const std::string &value)
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");
154 return static_cast<T>(r);
159 * Turn a string representation of a number, either integral, floating point,
160 * or enum into an actual number. Use to_bool for booleans.
162 * @param value The string representing the number
163 * @param retval The resulting value
164 * @return True if the parsing was successful
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)
174 retval = __to_number<T>(value);
176 } catch (const std::out_of_range&) {
178 } catch (const std::invalid_argument&) {
181 panic("Unrecognized exception.\n");
186 * Turn a string representation of a boolean into a boolean value.
189 to_bool(const std::string &value, bool &retval)
191 std::string s = to_lower(value);
196 } else if (s == "false") {
204 // Put quotes around string arg if it contains spaces.
206 quote(const std::string &s)
209 bool quote = s.find(' ') != std::string::npos;
224 * Return true if 's' starts with the prefix string 'prefix'.
227 startswith(const char *s, const char *prefix)
229 return (strncmp(s, prefix, strlen(prefix)) == 0);
234 * Return true if 's' starts with the prefix string 'prefix'.
237 startswith(const std::string &s, const char *prefix)
239 return (s.compare(0, strlen(prefix), prefix) == 0);
244 * Return true if 's' starts with the prefix string 'prefix'.
247 startswith(const std::string &s, const std::string &prefix)
249 return (s.compare(0, prefix.size(), prefix) == 0);
253 #endif //__BASE_STR_HH__