32 #ifndef __BASE_STR_HH__
33 #define __BASE_STR_HH__
46 if (off != std::string::npos) {
47 std::string::iterator begin = s.begin();
48 s.erase(begin, begin + off);
56 if (off != std::string::npos)
57 s.erase(s.begin() + off + 1, s.end());
75 for (
const auto &
c : s)
76 lower.push_back(std::tolower(
c));
84 split_first(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
89 split_last(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
97 char token,
bool ign =
true);
106 typename std::enable_if<std::is_integral<T>::value &&
107 std::is_signed<T>::value, T>
::type
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
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
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);
154 retval = __to_number<T>(value);
156 }
catch (
const std::out_of_range&) {
158 }
catch (
const std::invalid_argument&) {
167 to_bool(
const std::string &value,
bool &retval)
174 }
else if (s ==
"false") {
187 bool quote = s.find(
' ') != std::string::npos;
207 return (strncmp(s, prefix, strlen(prefix)) == 0);
217 return (s.compare(0, strlen(prefix), prefix) == 0);
227 return (s.compare(0, prefix.size(), prefix) == 0);
231 #endif //__BASE_STR_HH__
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
void eat_end_white(std::string &s)
void tokenize(std::vector< std::string > &vector, const std::string &s, char token, bool ign=true)
std::enable_if< std::is_integral< T >::value &&std::is_signed< T >::value, T >::type __to_number(const std::string &value)
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
std::string quote(const std::string &s)
void eat_white(std::string &s)
bool to_number(const std::string &value, T &retval)
Turn a string representation of a number, either integral or floating point, into an actual number...
std::string to_lower(const std::string &s)
void eat_lead_white(std::string &s)
bool split_first(const std::string &s, std::string &lhs, std::string &rhs, char c)