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; 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. 15 * 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. 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
|
35#include <cctype>
|
35#include <cstring>
|
37#include <sstream>
|
36#include <limits> 37#include <locale> 38#include <stdexcept> |
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
|
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 } 50} 51 52inline void 53eat_end_white(std::string &s) 54{ 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()); 58} 59 60inline void 61eat_white(std::string &s) 62{ 63 eat_lead_white(s); 64 eat_end_white(s); 65} 66 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)); |
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); 85 86// Split the string s into lhs and rhs on the last occurence of the 87// character c. 88bool 89split_last(const std::string &s, std::string &lhs, std::string &rhs, char c); 90 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>
|
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) |
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); |
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) 190 ret = '"'; 191 192 ret += s; 193 194 if (quote) 195 ret += '"'; 196 197 return ret; 198} 199 200 201/** 202 * Return true if 's' starts with the prefix string 'prefix'. 203 */ 204inline bool 205startswith(const char *s, const char *prefix) 206{ 207 return (strncmp(s, prefix, strlen(prefix)) == 0); 208} 209 210 211/** 212 * Return true if 's' starts with the prefix string 'prefix'. 213 */ 214inline bool 215startswith(const std::string &s, const char *prefix) 216{ 217 return (s.compare(0, strlen(prefix), prefix) == 0); 218} 219 220 221/** 222 * Return true if 's' starts with the prefix string 'prefix'. 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__ |
|