32#ifndef __BASE_STR_HH__
33#define __BASE_STR_HH__
52 std::string::size_type off =
s.find_first_not_of(
' ');
53 if (off != std::string::npos) {
54 std::string::iterator begin =
s.begin();
55 s.erase(begin, begin + off);
62 std::string::size_type off =
s.find_last_not_of(
' ');
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));
92split_first(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
98split_last(
const std::string &
s, std::string &lhs, std::string &rhs,
char c);
106 char token,
bool ign =
true);
116typename 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);
140typename std::enable_if_t<std::is_enum_v<T>, T>
144 return static_cast<T
>(
r);
148typename 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);
170inline 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>
179 }
catch (
const std::out_of_range&) {
181 }
catch (
const std::invalid_argument&) {
184 panic(
"Unrecognized exception.\n");
192to_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;
259 std::replace(replaced.begin(), replaced.end(), from,
to);
#define panic(...)
This implements a cprintf based panic() function.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
std::enable_if_t< std::is_integral_v< T >, T > __to_number(const std::string &value)
std::string to_lower(const std::string &s)
bool to_number(const std::string &value, Pixel &retval)
bool startswith(const char *s, const char *prefix)
Return true if 's' starts with the prefix string 'prefix'.
bool to_bool(const std::string &value, bool &retval)
Turn a string representation of a boolean into a boolean value.
std::string quote(const std::string &s)
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
bool split_last(const std::string &s, std::string &lhs, std::string &rhs, char c)
void eat_end_white(std::string &s)
std::string replace(const std::string &s, char from, char to)
void eat_white(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)