From 9ded5ae81007fd16536a033c63a8c4a2b824e713 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Fri, 29 Sep 2023 16:41:03 +0200 Subject: [PATCH] Remove custom data size formatting 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 --- src/common/utility.cpp | 46 ++++++++++-------------------------------- test/testutility.cpp | 32 ++++++++++++++--------------- 2 files changed, 27 insertions(+), 51 deletions(-) diff --git a/src/common/utility.cpp b/src/common/utility.cpp index 392aafc1258..4ed66dee956 100644 --- a/src/common/utility.cpp +++ b/src/common/utility.cpp @@ -84,44 +84,20 @@ 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) - if (value > 9.95) - round = true; + using namespace FileSystem::SizeLiterals; - if (round) - return s.arg(qRound(value)); + // We do what macOS 10.8 and above do: 0 fraction digits for bytes and KB; 1 fraction digits for MB; 2 for GB and above. + // See also https://developer.apple.com/documentation/foundation/nsbytecountformatter/1417887-adaptive + int precision = 0; + if (quint64(octets) >= 1_gb) { + precision = 2; + } else if (quint64(octets) >= 1_mb) { + precision = 1; + } - return s.arg(value, 0, 'g', 2); + return QLocale().formattedDataSize(octets, precision, QLocale::DataSizeTraditionalFormat); } // Qtified version of get_platforms() in csync_owncloud.c diff --git a/test/testutility.cpp b/test/testutility.cpp index 09a8c431305..06e6f042f51 100644 --- a/test/testutility.cpp +++ b/test/testutility.cpp @@ -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()