fix: shorten humanized units and remove dead code (#1393)

* Refactor: remove unreachable code

* Fix: shortening of humanized units
This commit is contained in:
Yinghao
2025-12-21 18:13:03 +00:00
committed by GitHub
parent 1be5603206
commit e81c2b4b44
2 changed files with 13 additions and 17 deletions

View File

@@ -436,20 +436,12 @@ namespace Tools {
if (mega) {
while (value >= 100000) {
value /= 1000;
if (value < 100) {
out = fmt::format("{}", value);
break;
}
start++;
}
}
else {
while (value >= 102400) {
value >>= 10;
if (value < 100) {
out = fmt::format("{}", value);
break;
}
start++;
}
}
@@ -471,16 +463,20 @@ namespace Tools {
}
if (shorten) {
auto f_pos = out.find(".");
if (f_pos == 1 and out.size() > 3) {
auto has_sep = out.find(".") != string::npos;
if (has_sep) {
out = fmt::format("{:.1f}", stod(out));
}
else if (f_pos != string::npos) {
out = fmt::format("{:.0f}", stod(out));
}
if (out.size() > 3) {
out = fmt::format("{:d}.0", out[0] - '0');
start++;
// if out is of the form xy.z
if (has_sep) {
out = fmt::format("{:.0f}", stod(out));
}
// if out is of the form xyzw (only when not using base 10)
else {
out = fmt::format("{:d}.0", out[0] - '0');
start++;
}
}
out.push_back(units[start][0]);
}

View File

@@ -349,9 +349,9 @@ namespace Tools {
string sec_to_dhms(size_t seconds, bool no_days = false, bool no_seconds = false);
//* Scales up in steps of 1024 to highest positive value unit and returns string with unit suffixed
//* bit=True or defaults to bytes
//* bit=true or defaults to bytes
//* start=int to set 1024 multiplier starting unit
//* short=True always returns 0 decimals and shortens unit to 1 character
//* shorten=true shortens value to at most 3 characters and shortens unit to 1 character
string floating_humanizer(uint64_t value, bool shorten = false, size_t start = 0, bool bit = false, bool per_second = false);
//* Add std::string operator * : Repeat string <str> <n> number of times