str.hh (9142:e9b713df4e1d) str.hh (10386:c81407818741)
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
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 __STR_HH__
33#define __STR_HH__
32#ifndef __BASE_STR_HH__
33#define __BASE_STR_HH__
34
34
35#include <cctype>
36#include <cstring>
35#include <cstring>
37#include <sstream>
36#include <limits>
37#include <locale>
38#include <stdexcept>
38#include <string>
39#include <vector>
40
39#include <string>
40#include <vector>
41
41template<class> class Hash;
42template<>
43class Hash<std::string> {
44public:
45 unsigned operator()(const std::string &s) {
46 std::string::const_iterator i = s.begin();
47 std::string::const_iterator end = s.end();
48 unsigned hash = 5381;
49
50 while (i < end)
51 hash = ((hash << 5) + hash) + *i++;
52
53 return hash;
54 }
55};
56
57inline void
58eat_lead_white(std::string &s)
59{
60 std::string::size_type off = s.find_first_not_of(' ');
61 if (off != std::string::npos) {
62 std::string::iterator begin = s.begin();
63 s.erase(begin, begin + off);
64 }

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

82inline std::string
83to_lower(const std::string &s)
84{
85 std::string lower;
86 int len = s.size();
87
88 lower.reserve(len);
89
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
90 for (int i = 0; i < len; ++i)
91 lower.push_back(tolower(s[i]));
75 for (const auto &c : s)
76 lower.push_back(std::tolower(c));
92
93 return lower;
94}
95
96// Split the string s into lhs and rhs on the first occurence of the
97// character c.
98bool
99split_first(const std::string &s, std::string &lhs, std::string &rhs, char c);

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

106// Tokenize the string <s> splitting on the character <token>, and
107// place the result in the string vector <vector>. If <ign> is true,
108// then empty result strings (due to trailing tokens, or consecutive
109// tokens) are skipped.
110void
111tokenize(std::vector<std::string> &vector, const std::string &s,
112 char token, bool ign = true);
113
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
114template <class T> bool
115to_number(const std::string &value, T &retval);
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>
116
117template <class T>
118inline std::string
119to_string(const T &value)
118typename std::enable_if<std::is_integral<T>::value &&
119 !std::is_signed<T>::value, T>::type
120__to_number(const std::string &value)
120{
121{
121 std::stringstream str;
122 str << value;
123 return str.str();
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);
124}
125
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
126// Put quotes around string arg if it contains spaces.
127inline std::string
128quote(const std::string &s)
129{
130 std::string ret;
131 bool quote = s.find(' ') != std::string::npos;
132
133 if (quote)

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

167 */
168inline bool
169startswith(const std::string &s, const std::string &prefix)
170{
171 return (s.compare(0, prefix.size(), prefix) == 0);
172}
173
174
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
175#endif //__STR_HH__
231#endif //__BASE_STR_HH__