Optimize formatTime utility (#3777)

Adds benchmarks and unit tests for the function

Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
This commit is contained in:
Kasia
2022-05-29 13:54:42 +02:00
committed by GitHub
parent 143f4ef2ec
commit 0ad66c0af4
5 changed files with 183 additions and 15 deletions
+10 -15
View File
@@ -1,12 +1,19 @@
#include "FormatTime.hpp"
namespace chatterino {
namespace {
void appendDuration(int count, QChar &&order, QString &outString)
void appendDuration(int count, QChar &&suffix, QString &out)
{
outString.append(QString::number(count));
outString.append(order);
if (!out.isEmpty())
{
out.append(' ');
}
out.append(QString::number(count));
out.append(suffix);
}
} // namespace
QString formatTime(int totalSeconds)
@@ -25,26 +32,14 @@ QString formatTime(int totalSeconds)
}
if (hours > 0)
{
if (!res.isEmpty())
{
res.append(" ");
}
appendDuration(hours, 'h', res);
}
if (minutes > 0)
{
if (!res.isEmpty())
{
res.append(" ");
}
appendDuration(minutes, 'm', res);
}
if (seconds > 0)
{
if (!res.isEmpty())
{
res.append(" ");
}
appendDuration(seconds, 's', res);
}
return res;