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<std::is_integral<T>::value &&
112 std::is_signed<T>::value, T>
::type
116 long long r = std::stoll(value,
nullptr, 0);
117 if (
r < std::numeric_limits<T>::lowest()
118 ||
r > std::numeric_limits<T>::max()) {
119 throw std::out_of_range(
"Out of range");
121 return static_cast<T
>(
r);
125 typename std::enable_if<std::is_integral<T>::value &&
126 !std::is_signed<T>::value, T>
::type
130 unsigned long long r = std::stoull(value,
nullptr, 0);
131 if (
r > std::numeric_limits<T>::max())
132 throw std::out_of_range(
"Out of range");
133 return static_cast<T
>(
r);
137 typename std::enable_if<std::is_enum<T>::value, T>
::type
141 return static_cast<T
>(
r);
145 typename std::enable_if<std::is_floating_point<T>::value, T>
::type
149 long double r = std::stold(value);
150 if (
r < std::numeric_limits<T>::lowest()
151 ||
r > std::numeric_limits<T>::max()) {
152 throw std::out_of_range(
"Out of range");
154 return static_cast<T
>(
r);
171 retval = __to_number<T>(value);
173 }
catch (
const std::out_of_range&) {
175 }
catch (
const std::invalid_argument&) {
178 panic(
"Unrecognized exception.\n");
186 to_bool(
const std::string &value,
bool &retval)
193 }
else if (
s ==
"false") {
206 bool quote =
s.find(
' ') != std::string::npos;
226 return (strncmp(
s, prefix, strlen(prefix)) == 0);
236 return (
s.compare(0, strlen(prefix), prefix) == 0);
246 return (
s.compare(0, prefix.size(), prefix) == 0);
250 #endif //__BASE_STR_HH__