mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-10 04:50:31 -04:00
fix(util): improve error handling in parse_(size|time)_with_unit
This commit is contained in:
parent
b87db2d15a
commit
abbbadd30b
@ -73,37 +73,41 @@ std::string time_with_unit(double sec) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t parse_size_with_unit(std::string const& str) {
|
size_t parse_size_with_unit(std::string const& str) {
|
||||||
size_t end = 0;
|
size_t value;
|
||||||
size_t size = std::stoul(str, &end);
|
auto [ptr, ec]{std::from_chars(str.data(), str.data() + str.size(), value)};
|
||||||
|
|
||||||
if (str[end] == '\0') {
|
if (ec != std::errc()) {
|
||||||
return size;
|
DWARFS_THROW(runtime_error, "cannot parse size value");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str[end + 1] == '\0') {
|
if (ptr[0] == '\0') {
|
||||||
switch (str[end]) {
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ptr[1] == '\0') {
|
||||||
|
switch (ptr[0]) {
|
||||||
case 't':
|
case 't':
|
||||||
case 'T':
|
case 'T':
|
||||||
size <<= 10;
|
value <<= 10;
|
||||||
[[fallthrough]];
|
[[fallthrough]];
|
||||||
case 'g':
|
case 'g':
|
||||||
case 'G':
|
case 'G':
|
||||||
size <<= 10;
|
value <<= 10;
|
||||||
[[fallthrough]];
|
[[fallthrough]];
|
||||||
case 'm':
|
case 'm':
|
||||||
case 'M':
|
case 'M':
|
||||||
size <<= 10;
|
value <<= 10;
|
||||||
[[fallthrough]];
|
[[fallthrough]];
|
||||||
case 'k':
|
case 'k':
|
||||||
case 'K':
|
case 'K':
|
||||||
size <<= 10;
|
value <<= 10;
|
||||||
return size;
|
return value;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DWARFS_THROW(runtime_error, "invalid size suffix");
|
DWARFS_THROW(runtime_error, "unsupported size suffix");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::chrono::milliseconds parse_time_with_unit(std::string const& str) {
|
std::chrono::milliseconds parse_time_with_unit(std::string const& str) {
|
||||||
@ -129,8 +133,12 @@ std::chrono::milliseconds parse_time_with_unit(std::string const& str) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '\0':
|
|
||||||
case 's':
|
case 's':
|
||||||
|
if (ptr[1] != '\0') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
[[fallthrough]];
|
||||||
|
case '\0':
|
||||||
return std::chrono::seconds(value);
|
return std::chrono::seconds(value);
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user