35 #ifndef __BASE_STR_HH__ 36 #define __BASE_STR_HH__ 43 #include <type_traits> 52 if (off != std::string::npos) {
53 std::string::iterator begin = s.begin();
54 s.erase(begin, begin + off);
62 if (off != std::string::npos)
63 s.erase(s.begin() + off + 1, s.end());
81 for (
const auto &
c : s)
82 lower.push_back(std::tolower(
c));
91 split_first(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
97 split_last(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
105 char token,
bool ign =
true);
114 typename std::enable_if<std::is_integral<T>::value &&
115 std::is_signed<T>::value, T>
::type 119 long long r = std::stoll(value,
nullptr, 0);
120 if (r < std::numeric_limits<T>::lowest()
121 || r > std::numeric_limits<T>::max()) {
122 throw std::out_of_range(
"Out of range");
124 return static_cast<T
>(
r);
128 typename std::enable_if<std::is_integral<T>::value &&
129 !std::is_signed<T>::value, T>
::type 133 unsigned long long r = std::stoull(value,
nullptr, 0);
134 if (r > std::numeric_limits<T>::max())
135 throw std::out_of_range(
"Out of range");
136 return static_cast<T
>(
r);
140 typename std::enable_if<std::is_enum<T>::value, T>
::type 144 return static_cast<T
>(
r);
148 typename std::enable_if<std::is_floating_point<T>::value, T>
::type 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);
174 retval = __to_number<T>(value);
176 }
catch (
const std::out_of_range&) {
178 }
catch (
const std::invalid_argument&) {
181 panic(
"Unrecognized exception.\n");
189 to_bool(
const std::string &value,
bool &retval)
196 }
else if (s ==
"false") {
209 bool quote = s.find(
' ') != std::string::npos;
229 return (strncmp(s, prefix, strlen(prefix)) == 0);
239 return (s.compare(0, strlen(prefix), prefix) == 0);
249 return (s.compare(0, prefix.size(), prefix) == 0);
253 #endif //__BASE_STR_HH__ #define panic(...)
This implements a cprintf based panic() function.
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)