QNetworkCookie: Clean up

Rewrite the regex to not capture the things we ignore anyway.
Use capturedView to avoid allocating a string just to turn it into an
int.
Fix a (usually) ifdef-ed out piece of code that was still calling
a QRegExp function.
Make the QRegularExpression static const to save it from having to
recompile every time.

Change-Id: I2f4841a2bc35df4e6cea44aec72432410583f770
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Mårten Nordheim 2021-08-25 16:18:04 +02:00
parent 9935dfe289
commit b33ccd99e1

View File

@ -664,7 +664,8 @@ static QDateTime parseDateString(const QByteArray &dateString)
int zoneOffset = -1;
// hour:minute:second.ms pm
QRegularExpression timeRx(QLatin1String("(\\d{1,2}):(\\d{1,2})(:(\\d{1,2})|)(\\.(\\d{1,3})|)((\\s{0,}(am|pm))|)"));
static const QRegularExpression timeRx(
u"(\\d\\d?):(\\d\\d?)(?::(\\d\\d?)(?:\\.(\\d{1,3}))?)?(?:\\s*(am|pm))?"_qs);
int at = 0;
while (at < dateString.length()) {
@ -748,18 +749,17 @@ static QDateTime parseDateString(const QByteArray &dateString)
// or else we get use-after-free issues:
QString dateToString = QString::fromLatin1(dateString);
if (auto match = timeRx.match(dateToString, at); match.hasMatch()) {
QStringList list = match.capturedTexts();
int h = match.captured(1).toInt();
int m = match.captured(2).toInt();
int s = match.captured(4).toInt();
int ms = match.captured(6).toInt();
QString ampm = match.captured(9);
int h = match.capturedView(1).toInt();
int m = match.capturedView(2).toInt();
int s = match.capturedView(3).toInt();
int ms = match.capturedView(4).toInt();
QStringView ampm = match.capturedView(5);
if (h < 12 && !ampm.isEmpty())
if (ampm == QLatin1String("pm"))
h += 12;
time = QTime(h, m, s, ms);
#ifdef PARSEDATESTRINGDEBUG
qDebug() << "Time:" << list << timeRx.matchedLength();
qDebug() << "Time:" << match.capturedTexts() << match.capturedLength();
#endif
at += match.capturedLength();
continue;