commit
f08e42856e
2 changed files with 12 additions and 7 deletions
|
@ -257,15 +257,16 @@ namespace string_util {
|
||||||
/**
|
/**
|
||||||
* Create a filesize string by converting given bytes to highest unit possible
|
* Create a filesize string by converting given bytes to highest unit possible
|
||||||
*/
|
*/
|
||||||
string filesize(unsigned long long kbytes, size_t precision, bool fixed, const string& locale) {
|
string filesize(unsigned long long bytes, size_t precision, bool fixed, const string& locale) {
|
||||||
vector<string> suffixes{"TB", "GB", "MB"};
|
vector<string> suffixes{"TB", "GB", "MB", "KB"};
|
||||||
string suffix{"KB"};
|
string suffix{"B"};
|
||||||
double value = kbytes;
|
double value = bytes;
|
||||||
while (!suffixes.empty() && (value /= 1024.0) >= 1024.0) {
|
while (!suffixes.empty() && value >= 1024.0) {
|
||||||
suffix = suffixes.back();
|
suffix = suffixes.back();
|
||||||
suffixes.pop_back();
|
suffixes.pop_back();
|
||||||
|
value /= 1024.0;
|
||||||
}
|
}
|
||||||
return floating_point(value, precision, fixed, locale) + " GB";
|
return floating_point(value, precision, fixed, locale) + " " + suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -98,7 +98,11 @@ int main() {
|
||||||
expect(string_util::filesize_gb(3 * 1024 * 1024 + 200 * 1024, 3) == "3.195 GB");
|
expect(string_util::filesize_gb(3 * 1024 * 1024 + 200 * 1024, 3) == "3.195 GB");
|
||||||
expect(string_util::filesize_gb(3 * 1024 * 1024 + 400 * 1024) == "3 GB");
|
expect(string_util::filesize_gb(3 * 1024 * 1024 + 400 * 1024) == "3 GB");
|
||||||
expect(string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024) == "4 GB");
|
expect(string_util::filesize_gb(3 * 1024 * 1024 + 800 * 1024) == "4 GB");
|
||||||
expect(string_util::filesize(3 * 1024 * 1024) == "3 GB");
|
expect(string_util::filesize(3) == "3 B");
|
||||||
|
expect(string_util::filesize(3 * 1024) == "3 KB");
|
||||||
|
expect(string_util::filesize(3 * 1024 * 1024) == "3 MB");
|
||||||
|
expect(string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024) == "3 GB");
|
||||||
|
expect(string_util::filesize((unsigned long long)3 * 1024 * 1024 * 1024 * 1024) == "3 TB");
|
||||||
};
|
};
|
||||||
|
|
||||||
"sstream"_test = [] {
|
"sstream"_test = [] {
|
||||||
|
|
Loading…
Reference in a new issue