Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove custom data size formatting #11252

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 11 additions & 35 deletions src/common/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Loading