Skip to content

Commit

Permalink
Remove custom data size formatting
Browse files Browse the repository at this point in the history
It is replaced by `QLocale::formattedDataSize`. The precision is now:
0 fraction digits for bytes and KB, 1 for MB, and 2 for GB and above.

Fixes: #11024
  • Loading branch information
erikjv committed Oct 19, 2023
1 parent 86bbc60 commit 8c24249
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 52 deletions.
45 changes: 9 additions & 36 deletions src/common/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,44 +84,17 @@ QString Utility::formatFingerprint(const QByteArray &fmhash, bool colonSeparated

QString Utility::octetsToString(qint64 octets)
{
#define THE_FACTOR 1024
static const qint64 kb = THE_FACTOR;
static const qint64 mb = THE_FACTOR * kb;
static const qint64 gb = THE_FACTOR * mb;

QString s;
qreal value = octets;

// Whether we care about decimals: only for GB/MB and only
// if it's less than 10 units.
bool round = true;

// do not display terra byte with the current units, as when
// the MB, GB and KB units were made, there was no TB,
// see the JEDEC standard
// https://en.wikipedia.org/wiki/JEDEC_memory_standards
if (octets >= gb) {
s = QCoreApplication::translate("Utility", "%L1 GB");
value /= gb;
round = false;
} else if (octets >= mb) {
s = QCoreApplication::translate("Utility", "%L1 MB");
value /= mb;
round = false;
} else if (octets >= kb) {
s = QCoreApplication::translate("Utility", "%L1 KB");
value /= kb;
} else {
s = QCoreApplication::translate("Utility", "%L1 B");
OC_ASSERT(octets >= 0)

using namespace FileSystem::SizeLiterals;
int precesion = 0; // for bytes and kb use precision 0
if (quint64(octets) >= 1_gb) {
precesion = 2;
} else if (quint64(octets) >= 1_mb) {
precesion = 1;
}

if (value > 9.95)
round = true;

if (round)
return s.arg(qRound(value));

return s.arg(value, 0, 'g', 2);
return QLocale().formattedDataSize(octets, precesion, QLocale::DataSizeTraditionalFormat);
}

// Qtified version of get_platforms() in csync_owncloud.c
Expand Down
32 changes: 16 additions & 16 deletions test/testutility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ private slots:
void testOctetsToString()
{
QLocale::setDefault(QLocale(QStringLiteral("en")));
QCOMPARE(octetsToString(999), QString::fromLatin1("999 B"));
QCOMPARE(octetsToString(1024), QString::fromLatin1("1 KB"));
QCOMPARE(octetsToString(1364), QString::fromLatin1("1 KB"));
QCOMPARE(octetsToString(999), QString::fromLatin1("999 bytes"));
QCOMPARE(octetsToString(1024), QString::fromLatin1("1 kB"));
QCOMPARE(octetsToString(1364), QString::fromLatin1("1 kB"));

QCOMPARE(octetsToString(9110), QString::fromLatin1("9 KB"));
QCOMPARE(octetsToString(9910), QString::fromLatin1("10 KB"));
QCOMPARE(octetsToString(10240), QString::fromLatin1("10 KB"));
QCOMPARE(octetsToString(9110), QString::fromLatin1("9 kB"));
QCOMPARE(octetsToString(9910), QString::fromLatin1("10 kB"));
QCOMPARE(octetsToString(10240), QString::fromLatin1("10 kB"));

QCOMPARE(octetsToString(123456), QString::fromLatin1("121 KB"));
QCOMPARE(octetsToString(123456), QString::fromLatin1("121 kB"));
QCOMPARE(octetsToString(1234567), QString::fromLatin1("1.2 MB"));
QCOMPARE(octetsToString(12345678), QString::fromLatin1("12 MB"));
QCOMPARE(octetsToString(123456789), QString::fromLatin1("118 MB"));
QCOMPARE(octetsToString(1000LL * 1000 * 1000 * 5), QString::fromLatin1("4.7 GB"));

QCOMPARE(octetsToString(1), QString::fromLatin1("1 B"));
QCOMPARE(octetsToString(2), QString::fromLatin1("2 B"));
QCOMPARE(octetsToString(1024), QString::fromLatin1("1 KB"));
QCOMPARE(octetsToString(1024 * 1024), QString::fromLatin1("1 MB"));
QCOMPARE(octetsToString(1024LL * 1024 * 1024), QString::fromLatin1("1 GB"));
QCOMPARE(octetsToString(12345678), QString::fromLatin1("11.8 MB"));
QCOMPARE(octetsToString(123456789), QString::fromLatin1("117.7 MB"));
QCOMPARE(octetsToString(1000LL * 1000 * 1000 * 5), QString::fromLatin1("4.66 GB"));

QCOMPARE(octetsToString(1), QString::fromLatin1("1 bytes"));
QCOMPARE(octetsToString(2), QString::fromLatin1("2 bytes"));
QCOMPARE(octetsToString(1024), QString::fromLatin1("1 kB"));
QCOMPARE(octetsToString(1024 * 1024), QString::fromLatin1("1.0 MB"));
QCOMPARE(octetsToString(1024LL * 1024 * 1024), QString::fromLatin1("1.00 GB"));
}

void testLaunchOnStartup()
Expand Down

0 comments on commit 8c24249

Please sign in to comment.