QPlainTestLogger::formatResult: use QByteArray instead of QString

Avoids operating in UTF-16 and having to convert.

Drive-by fix int to qsizetype.

Change-Id: I3c79b7e08fa346988dfefffd17203b044914d68d
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Thiago Macieira 2022-10-21 16:59:11 -07:00
parent dbc9467ca3
commit a90f041a0a

View File

@ -109,15 +109,15 @@ namespace QTest {
} }
// Pretty-prints a benchmark result using the given number of digits. // Pretty-prints a benchmark result using the given number of digits.
template <typename T> QString formatResult(T number, int significantDigits) template <typename T> QByteArray formatResult(T number, int significantDigits)
{ {
if (number < T(0)) if (number < T(0))
return u"NAN"_s; return "NAN";
if (number == T(0)) if (number == T(0))
return u"0"_s; return "0";
QString beforeDecimalPoint = QString::number(qint64(number), 'f', 0); QByteArray beforeDecimalPoint = QByteArray::number(qint64(number), 'f', 0);
QString afterDecimalPoint = QString::number(number, 'f', 20); QByteArray afterDecimalPoint = QByteArray::number(number, 'f', 20);
afterDecimalPoint.remove(0, beforeDecimalPoint.size() + 1); afterDecimalPoint.remove(0, beforeDecimalPoint.size() + 1);
int beforeUse = qMin(beforeDecimalPoint.size(), significantDigits); int beforeUse = qMin(beforeDecimalPoint.size(), significantDigits);
@ -132,11 +132,11 @@ namespace QTest {
int afterUse = significantDigits - beforeUse; int afterUse = significantDigits - beforeUse;
// leading zeroes after the decimal point does not count towards the digit use. // leading zeroes after the decimal point does not count towards the digit use.
if (beforeDecimalPoint == u'0' && !afterDecimalPoint.isEmpty()) { if (beforeDecimalPoint == "0" && !afterDecimalPoint.isEmpty()) {
++afterUse; ++afterUse;
int i = 0; int i = 0;
while (i < afterDecimalPoint.size() && afterDecimalPoint.at(i) == u'0') while (i < afterDecimalPoint.size() && afterDecimalPoint.at(i) == '0')
++i; ++i;
afterUse += i; afterUse += i;
@ -145,8 +145,8 @@ namespace QTest {
int afterRemove = afterDecimalPoint.size() - afterUse; int afterRemove = afterDecimalPoint.size() - afterUse;
afterDecimalPoint.chop(afterRemove); afterDecimalPoint.chop(afterRemove);
QChar separator = u','; char separator = ',';
QChar decimalPoint = u'.'; char decimalPoint = '.';
// insert thousands separators // insert thousands separators
int length = beforeDecimalPoint.size(); int length = beforeDecimalPoint.size();
@ -155,7 +155,7 @@ namespace QTest {
beforeDecimalPoint.insert(i, separator); beforeDecimalPoint.insert(i, separator);
} }
QString print; QByteArray print;
print = beforeDecimalPoint; print = beforeDecimalPoint;
if (afterUse > 0) if (afterUse > 0)
print.append(decimalPoint); print.append(decimalPoint);
@ -167,11 +167,11 @@ namespace QTest {
} }
template <typename T> template <typename T>
int formatResult(char * buffer, int bufferSize, T number, int significantDigits) qsizetype formatResult(char * buffer, int bufferSize, T number, int significantDigits)
{ {
QString result = formatResult(number, significantDigits); QByteArray result = formatResult(number, significantDigits);
int size = result.size(); qsizetype size = result.size();
qstrncpy(buffer, std::move(result).toLatin1().constData(), bufferSize); qstrncpy(buffer, result.constData(), bufferSize);
return size; return size;
} }
} }