35bool ParseUint(
const char* s, T* out, T max = std::numeric_limits<T>::max(),
36 bool allow_suffixes =
false) {
37 static_assert(std::is_unsigned<T>::value,
"ParseUint can only be used with unsigned types");
50 int base = (s[0] ==
'0' && (s[1] ==
'x' || s[1] ==
'X')) ? 16 : 10;
53 unsigned long long int result = strtoull(s, &end, base);
54 if (errno != 0)
return false;
60 const char* suffixes =
"bkmgtpe";
62 if ((!allow_suffixes || (suffix = strchr(suffixes, tolower(*end))) ==
nullptr) ||
63 __builtin_mul_overflow(result, 1ULL << (10 * (suffix - suffixes)), &result)) {
73 *out =
static_cast<T
>(result);
80bool ParseUint(
const std::string& s, T* out, T max = std::numeric_limits<T>::max(),
81 bool allow_suffixes =
false) {
82 return ParseUint(s.c_str(), out, max, allow_suffixes);
86bool ParseByteCount(
const char* s, T* out, T max = std::numeric_limits<T>::max()) {
92bool ParseByteCount(
const std::string& s, T* out, T max = std::numeric_limits<T>::max()) {
103 T max = std::numeric_limits<T>::max()) {
104 static_assert(std::is_signed<T>::value,
"ParseInt can only be used with signed types");
105 while (isspace(*s)) {
109 int base = (s[0] ==
'0' && (s[1] ==
'x' || s[1] ==
'X')) ? 16 : 10;
112 long long int result = strtoll(s, &end, base);
116 if (s == end || *end !=
'\0') {
120 if (result <
min || max < result) {
124 if (out !=
nullptr) {
125 *out =
static_cast<T
>(result);
134 T max = std::numeric_limits<T>::max()) {
#define min(a, b)
Definition: ext4_utils.h:44
bool ParseInt(const char *s, T *out, T min=std::numeric_limits< T >::min(), T max=std::numeric_limits< T >::max())
Definition: parseint.h:101
bool ParseByteCount(const char *s, T *out, T max=std::numeric_limits< T >::max())
Definition: parseint.h:86
bool ParseUint(const char *s, T *out, T max=std::numeric_limits< T >::max(), bool allow_suffixes=false)
Definition: parseint.h:35