TimeZone: optimize offsetFromUtcString
Use view types more to avoid needless allocations Change-Id: Ifdf5c8f6fecc54a0583444fbf3fe151c2c20002e Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
2d6c4b5ee7
commit
f76af5f78c
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <qdatastream.h>
|
#include <qdatastream.h>
|
||||||
#include <qdebug.h>
|
#include <qdebug.h>
|
||||||
|
#include <qstring.h>
|
||||||
|
|
||||||
#include <private/qcalendarmath_p.h>
|
#include <private/qcalendarmath_p.h>
|
||||||
#include <private/qnumeric_p.h>
|
#include <private/qnumeric_p.h>
|
||||||
@ -20,6 +21,7 @@ QT_BEGIN_NAMESPACE
|
|||||||
|
|
||||||
using namespace QtMiscUtils;
|
using namespace QtMiscUtils;
|
||||||
using namespace QtTimeZoneCldr;
|
using namespace QtTimeZoneCldr;
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
// For use with std::is_sorted() in assertions:
|
// For use with std::is_sorted() in assertions:
|
||||||
[[maybe_unused]]
|
[[maybe_unused]]
|
||||||
@ -838,7 +840,7 @@ QUtcTimeZonePrivate::QUtcTimeZonePrivate(const QByteArray &id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
qint64 QUtcTimeZonePrivate::offsetFromUtcString(const QByteArray &id)
|
qint64 QUtcTimeZonePrivate::offsetFromUtcString(QByteArrayView id)
|
||||||
{
|
{
|
||||||
// Convert reasonable UTC[+-]\d+(:\d+){,2} to offset in seconds.
|
// Convert reasonable UTC[+-]\d+(:\d+){,2} to offset in seconds.
|
||||||
// Assumption: id has already been tried as a CLDR UTC offset ID (notably
|
// Assumption: id has already been tried as a CLDR UTC offset ID (notably
|
||||||
@ -850,21 +852,22 @@ qint64 QUtcTimeZonePrivate::offsetFromUtcString(const QByteArray &id)
|
|||||||
return invalidSeconds(); // No sign
|
return invalidSeconds(); // No sign
|
||||||
const int sign = signChar == '-' ? -1 : 1;
|
const int sign = signChar == '-' ? -1 : 1;
|
||||||
|
|
||||||
const auto offsets = id.mid(4).split(':');
|
|
||||||
if (offsets.isEmpty() || offsets.size() > 3)
|
|
||||||
return invalidSeconds(); // No numbers, or too many.
|
|
||||||
|
|
||||||
qint32 seconds = 0;
|
qint32 seconds = 0;
|
||||||
int prior = 0; // Number of fields parsed thus far
|
int prior = 0; // Number of fields parsed thus far
|
||||||
for (const auto &offset : offsets) {
|
for (auto offset : QLatin1StringView(id.mid(4)).tokenize(':'_L1)) {
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
unsigned short field = offset.toUShort(&ok);
|
unsigned short field = offset.toUShort(&ok);
|
||||||
// Bound hour above at 24, minutes and seconds at 60:
|
// Bound hour above at 24, minutes and seconds at 60:
|
||||||
if (!ok || field >= (prior ? 60 : 24))
|
if (!ok || field >= (prior ? 60 : 24))
|
||||||
return invalidSeconds();
|
return invalidSeconds();
|
||||||
seconds = seconds * 60 + field;
|
seconds = seconds * 60 + field;
|
||||||
++prior;
|
if (++prior > 3)
|
||||||
|
return invalidSeconds(); // Too many numbers
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!prior)
|
||||||
|
return invalidSeconds(); // No numbers
|
||||||
|
|
||||||
while (prior++ < 3)
|
while (prior++ < 3)
|
||||||
seconds *= 60;
|
seconds *= 60;
|
||||||
|
|
||||||
|
@ -161,7 +161,7 @@ public:
|
|||||||
virtual ~QUtcTimeZonePrivate();
|
virtual ~QUtcTimeZonePrivate();
|
||||||
|
|
||||||
// Fall-back for UTC[+-]\d+(:\d+){,2} IDs.
|
// Fall-back for UTC[+-]\d+(:\d+){,2} IDs.
|
||||||
static qint64 offsetFromUtcString(const QByteArray &id);
|
static qint64 offsetFromUtcString(QByteArrayView id);
|
||||||
|
|
||||||
QUtcTimeZonePrivate *clone() const override;
|
QUtcTimeZonePrivate *clone() const override;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user