14#ifndef OPENVPN_COMMON_NUMBER_H
15#define OPENVPN_COMMON_NUMBER_H
37 const bool nondigit_term =
false)
43 if (std::numeric_limits<T>::min() < 0 && str[0] ==
'-')
51 const char c = str[i++];
52 if (c >=
'0' && c <=
'9')
57 else if (!c || nondigit_term)
59 retval = neg ? -ret : ret;
71 return parse_number<T>(str.c_str(), retval);
78 if (parse_number<T>(str.c_str(), ret))
80 throw number_parse_exception(error);
87 if (parse_number<T>(str.c_str(), ret))
89 throw number_parse_exception(std::string(error));
96 if (parse_number<T>(str, ret))
98 throw number_parse_exception(std::string(error));
103 const size_t max_len,
106 T *value_return =
nullptr)
108 if (numstr.length() <= max_len)
111 if (parse_number<T>(numstr.c_str(), value))
113 if (value >= minimum && value <= maximum)
116 *value_return = value;
126 return !str.empty() && std::ranges::all_of(str, [](
const unsigned char c)
127 {
return std::isdigit(c); });
#define OPENVPN_EXCEPTION(C)
bool is_number(std::string_view str)
bool parse_number_validate(const std::string &numstr, const size_t max_len, const T minimum, const T maximum, T *value_return=nullptr)
bool parse_number(const char *str, T &retval, const bool nondigit_term=false)
T parse_number_throw(const std::string &str, const std::string &error)