32 #ifndef __BASE_STR_HH__
33 #define __BASE_STR_HH__
41 #include <type_traits>
53 if (off != std::string::npos) {
54 std::string::iterator begin =
s.begin();
55 s.erase(begin, begin + off);
63 if (off != std::string::npos)
64 s.erase(
s.begin() + off + 1,
s.end());
82 for (
const auto &
c :
s)
83 lower.push_back(std::tolower(
c));
92 split_first(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
98 split_last(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
106 char token,
bool ign =
true);
116 typename std::enable_if_t<std::is_integral_v<T>, T>
120 if (value.find(
'e') != std::string::npos) {
121 throw std::invalid_argument(
"Cannot convert scientific to integral");
124 if constexpr (std::is_signed_v<T>) {
125 long long r = std::stoll(value,
nullptr, 0);
126 if (
r < std::numeric_limits<T>::lowest()
127 ||
r > std::numeric_limits<T>::max()) {
128 throw std::out_of_range(
"Out of range");
130 return static_cast<T
>(
r);
132 unsigned long long r = std::stoull(value,
nullptr, 0);
133 if (
r > std::numeric_limits<T>::max())
134 throw std::out_of_range(
"Out of range");
135 return static_cast<T
>(
r);
140 typename std::enable_if_t<std::is_enum_v<T>, T>
143 auto r = __to_number<typename std::underlying_type_t<T>>(value);
144 return static_cast<T
>(
r);
148 typename std::enable_if_t<std::is_floating_point_v<T>, T>
152 long double r = std::stold(value);
153 if (
r < std::numeric_limits<T>::lowest()
154 ||
r > std::numeric_limits<T>::max()) {
155 throw std::out_of_range(
"Out of range");
157 return static_cast<T
>(
r);
170 inline std::enable_if_t<(std::is_integral_v<T> ||
171 std::is_floating_point_v<T> ||
172 std::is_enum_v<T>) &&
173 !std::is_same_v<bool, T>,
bool>
177 retval = __to_number<T>(value);
179 }
catch (
const std::out_of_range&) {
181 }
catch (
const std::invalid_argument&) {
184 panic(
"Unrecognized exception.\n");
192 to_bool(
const std::string &value,
bool &retval)
199 }
else if (
s ==
"false") {
212 bool quote =
s.find(
' ') != std::string::npos;
232 return (strncmp(
s, prefix, strlen(prefix)) == 0);
242 return (
s.compare(0, strlen(prefix), prefix) == 0);
252 return (
s.compare(0, prefix.size(), prefix) == 0);
258 std::string replaced =
s;
265 #endif //__BASE_STR_HH__