Skip to content

Commit

Permalink
core: config: CFG_TYPE_SIZE64: fixes result, when value is 0
Browse files Browse the repository at this point in the history
When the size is 0, a zero should be printed, not an empty string.
Also some cleanup.
  • Loading branch information
joergsteffens committed Oct 15, 2020
1 parent fe7d4d3 commit 69eb8f0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
11 changes: 4 additions & 7 deletions core/src/lib/edit.cc
Expand Up @@ -424,7 +424,8 @@ std::string SizeAsSiPrefixFormat(uint64_t value_in)
/*
* convert default value string to numeric value
*/
static const char* modifier[] = {"e", "p", "t", "g", "m", "k", "", NULL};
static const char* modifier[] = {" e", " p", " t", " g",
" m", " k", "", NULL};
const uint64_t multiplier[] = {1152921504606846976, // EiB Exbibyte
1125899906842624, // PiB Pebibyte
1099511627776, // TiB Tebibyte
Expand All @@ -436,19 +437,16 @@ std::string SizeAsSiPrefixFormat(uint64_t value_in)
if (value == 0) {
result += "0";
} else {
for (int t = 0; modifier[t]; t++) {
for (int t = 0; modifier[t] && (value > 0); t++) {
factor = value / multiplier[t];
value = value % multiplier[t];
if (factor > 0) {
result += std::to_string(factor);
result += " ";
result += modifier[t];
if (!(bstrcmp(modifier[t], ""))) { result += " "; }
if (value > 0) { result += " "; }
}
if (value == 0) { break; }
}
}
result.pop_back();
return result;
}

Expand Down Expand Up @@ -699,4 +697,3 @@ bool IsAclEntryValid(const char* acl)
std::vector<char> msg;
return IsAclEntryValid(acl, msg);
}

3 changes: 3 additions & 0 deletions core/src/tests/test_edit.cc
Expand Up @@ -30,8 +30,11 @@

TEST(edit, convert_number_to_siunits)
{
ASSERT_STREQ(SizeAsSiPrefixFormat(0).c_str(), "0");
ASSERT_STREQ(SizeAsSiPrefixFormat(1).c_str(), "1");
ASSERT_STREQ(SizeAsSiPrefixFormat(123).c_str(), "123");
ASSERT_STREQ(SizeAsSiPrefixFormat(1024).c_str(), "1 k");
ASSERT_STREQ(SizeAsSiPrefixFormat(2050).c_str(), "2 k 2");
ASSERT_STREQ(SizeAsSiPrefixFormat(1048576).c_str(), "1 m");
ASSERT_STREQ(SizeAsSiPrefixFormat(1073741824).c_str(), "1 g");
ASSERT_STREQ(SizeAsSiPrefixFormat(1099511627776).c_str(), "1 t");
Expand Down

0 comments on commit 69eb8f0

Please sign in to comment.