32 #ifndef __BASE_STR_HH__
33 #define __BASE_STR_HH__
40 #include <type_traits>
49 if (off != std::string::npos) {
50 std::string::iterator begin =
s.begin();
51 s.erase(begin, begin + off);
59 if (off != std::string::npos)
60 s.erase(
s.begin() + off + 1,
s.end());
78 for (
const auto &
c :
s)
79 lower.push_back(std::tolower(
c));
88 split_first(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
94 split_last(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
102 char token,
bool ign =
true);
111 typename std::enable_if_t<std::is_integral<T>::value &&
112 std::is_signed<T>::value, T>
116 if (value.find(
'e') != std::string::npos) {
117 throw std::invalid_argument(
"Cannot convert scientific to integral");
120 long long r = std::stoll(value,
nullptr, 0);
121 if (
r < std::numeric_limits<T>::lowest()
122 ||
r > std::numeric_limits<T>::max()) {
123 throw std::out_of_range(
"Out of range");
125 return static_cast<T
>(
r);
129 typename std::enable_if_t<std::is_integral<T>::value &&
130 !std::is_signed<T>::value, T>
134 if (value.find(
'e') != std::string::npos) {
135 throw std::invalid_argument(
"Cannot convert scientific to integral");
138 unsigned long long r = std::stoull(value,
nullptr, 0);
139 if (
r > std::numeric_limits<T>::max())
140 throw std::out_of_range(
"Out of range");
141 return static_cast<T
>(
r);
145 typename std::enable_if_t<std::is_enum<T>::value, T>
149 if (value.find(
'e') != std::string::npos) {
150 throw std::invalid_argument(
"Cannot convert scientific to integral");
153 return static_cast<T
>(
r);
157 typename std::enable_if_t<std::is_floating_point<T>::value, T>
161 long double r = std::stold(value);
162 if (
r < std::numeric_limits<T>::lowest()
163 ||
r > std::numeric_limits<T>::max()) {
164 throw std::out_of_range(
"Out of range");
166 return static_cast<T
>(
r);
179 inline std::enable_if_t<(std::is_integral<T>::value ||
180 std::is_floating_point<T>::value ||
181 std::is_enum<T>::value) &&
182 !std::is_same<bool, T>::value,
bool>
186 retval = __to_number<T>(value);
188 }
catch (
const std::out_of_range&) {
190 }
catch (
const std::invalid_argument&) {
193 panic(
"Unrecognized exception.\n");
201 to_bool(
const std::string &value,
bool &retval)
208 }
else if (
s ==
"false") {
221 bool quote =
s.find(
' ') != std::string::npos;
241 return (strncmp(
s, prefix, strlen(prefix)) == 0);
251 return (
s.compare(0, strlen(prefix), prefix) == 0);
261 return (
s.compare(0, prefix.size(), prefix) == 0);
265 #endif //__BASE_STR_HH__