Deleted Added
sdiff udiff text old ( 9142:e9b713df4e1d ) new ( 10386:c81407818741 )
full compact
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;

--- 15 unchanged lines hidden (view full) ---

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
42inline void
43eat_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 }

--- 17 unchanged lines hidden (view full) ---

67inline std::string
68to_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.
83bool
84split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);

--- 6 unchanged lines hidden (view full) ---

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.
95void
96tokenize(std::vector<std::string> &vector, const std::string &s,
97 char token, bool ign = true);
98
99/**
100 * @{
101 *
102 * @name String to number helper functions for signed and unsigned
103 * integeral type, as well as floating-point types.
104 */
105template <class T>
106typename 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
117template <class T>
118typename 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
129template <class T>
130typename 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}
139/** @} */
140
141/**
142 * Turn a string representation of a number, either integral or
143 * floating point, into an actual number.
144 *
145 * @param value The string representing the number
146 * @param retval The resulting value
147 * @return True if the parsing was successful
148 */
149template <class T>
150inline bool
151to_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
163/**
164 * Turn a string representation of a boolean into a boolean value.
165 */
166inline bool
167to_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.
183inline std::string
184quote(const std::string &s)
185{
186 std::string ret;
187 bool quote = s.find(' ') != std::string::npos;
188
189 if (quote)

--- 33 unchanged lines hidden (view full) ---

223 */
224inline bool
225startswith(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__