config/Parser: use std::size_t

This commit is contained in:
Max Kellermann 2020-10-08 20:25:54 +02:00
parent ab318200db
commit d64729065e
2 changed files with 12 additions and 12 deletions

View File

@ -70,28 +70,28 @@ ParsePositive(const char *s)
return (unsigned)value; return (unsigned)value;
} }
template<size_t OPERAND> template<std::size_t OPERAND>
static size_t static std::size_t
Multiply(size_t value) Multiply(std::size_t value)
{ {
static constexpr size_t MAX_VALUE = SIZE_MAX / OPERAND; static constexpr std::size_t MAX_VALUE = SIZE_MAX / OPERAND;
if (value > MAX_VALUE) if (value > MAX_VALUE)
throw std::runtime_error("Value too large"); throw std::runtime_error("Value too large");
return value * OPERAND; return value * OPERAND;
} }
size_t std::size_t
ParseSize(const char *s, size_t default_factor) ParseSize(const char *s, std::size_t default_factor)
{ {
char *endptr; char *endptr;
size_t value = strtoul(s, &endptr, 10); std::size_t value = strtoul(s, &endptr, 10);
if (endptr == s) if (endptr == s)
throw std::runtime_error("Failed to parse integer"); throw std::runtime_error("Failed to parse integer");
static constexpr size_t KILO = 1024; static constexpr std::size_t KILO = 1024;
static constexpr size_t MEGA = 1024 * KILO; static constexpr std::size_t MEGA = 1024 * KILO;
static constexpr size_t GIGA = 1024 * MEGA; static constexpr std::size_t GIGA = 1024 * MEGA;
s = StripLeft(endptr); s = StripLeft(endptr);

View File

@ -51,7 +51,7 @@ ParsePositive(const char *s);
* *
* Throws on error. * Throws on error.
*/ */
size_t std::size_t
ParseSize(const char *s, size_t default_factor=1); ParseSize(const char *s, std::size_t default_factor=1);
#endif #endif