summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qdatetimeparser.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/tools/qdatetimeparser.cpp')
-rw-r--r--src/corelib/tools/qdatetimeparser.cpp150
1 files changed, 80 insertions, 70 deletions
diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp
index 5b7bf0d3d4..36ab93df1e 100644
--- a/src/corelib/tools/qdatetimeparser.cpp
+++ b/src/corelib/tools/qdatetimeparser.cpp
@@ -1,31 +1,37 @@
/****************************************************************************
**
-** Copyright (C) 2015 The Qt Company Ltd.
-** Contact: http://www.qt.io/licensing/
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
-** $QT_BEGIN_LICENSE:LGPL21$
+** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see http://www.qt.io/terms-conditions. For further
-** information use the contact form at http://www.qt.io/contact-us.
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 or version 3 as published by the Free
-** Software Foundation and appearing in the file LICENSE.LGPLv21 and
-** LICENSE.LGPLv3 included in the packaging of this file. Please review the
-** following information to ensure the GNU Lesser General Public License
-** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
-** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** General Public License version 3 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL3 included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 3 requirements
+** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
-** As a special exception, The Qt Company gives you certain additional
-** rights. These rights are described in The Qt Company LGPL Exception
-** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 2.0 or (at your option) the GNU General
+** Public license version 3 or any later version approved by the KDE Free
+** Qt Foundation. The licenses are as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-2.0.html and
+** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
@@ -125,13 +131,15 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
}
const SectionNode &node = sectionNodes.at(index);
- int year = v.date().year();
- int month = v.date().month();
- int day = v.date().day();
- int hour = v.time().hour();
- int minute = v.time().minute();
- int second = v.time().second();
- int msec = v.time().msec();
+ const QDate date = v.date();
+ const QTime time = v.time();
+ int year = date.year();
+ int month = date.month();
+ int day = date.day();
+ int hour = time.hour();
+ int minute = time.minute();
+ int second = time.second();
+ int msec = time.msec();
switch (node.type) {
case Hour24Section: case Hour12Section: hour = newVal; break;
@@ -305,7 +313,7 @@ int QDateTimeParser::sectionPos(const SectionNode &sn) const
*/
-static QString unquote(const QString &str)
+static QString unquote(const QStringRef &str)
{
const QChar quote(QLatin1Char('\''));
const QChar slash(QLatin1Char('\\'));
@@ -349,10 +357,8 @@ static inline int countRepeat(const QString &str, int index, int maxCount)
static inline void appendSeparator(QStringList *list, const QString &string, int from, int size, int lastQuote)
{
- QString str(string.mid(from, size));
- if (lastQuote >= from)
- str = unquote(str);
- list->append(str);
+ const QStringRef separator = string.midRef(from, size);
+ list->append(lastQuote >= from ? unquote(separator) : separator.toString());
}
@@ -463,7 +469,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
if (parserType != QVariant::Time) {
const SectionNode sn = { MonthSection, i - add, countRepeat(newFormat, i, 4), 0 };
newSectionNodes.append(sn);
- newSeparators.append(unquote(newFormat.mid(index, i - index)));
+ newSeparators.append(unquote(newFormat.midRef(index, i - index)));
i += sn.count - 1;
index = i + 1;
newDisplay |= MonthSection;
@@ -545,19 +551,20 @@ int QDateTimeParser::sectionSize(int sectionIndex) const
// is the previous value and displayText() is the new value.
// The size difference is always due to leading zeroes.
int sizeAdjustment = 0;
- if (displayText().size() != text.size()) {
+ const int displayTextSize = displayText().size();
+ if (displayTextSize != text.size()) {
// Any zeroes added before this section will affect our size.
int preceedingZeroesAdded = 0;
if (sectionNodes.size() > 1 && context == DateTimeEdit) {
- for (QVector<SectionNode>::ConstIterator sectionIt = sectionNodes.constBegin();
- sectionIt != sectionNodes.constBegin() + sectionIndex; ++sectionIt) {
+ const auto begin = sectionNodes.cbegin();
+ const auto end = begin + sectionIndex;
+ for (auto sectionIt = begin; sectionIt != end; ++sectionIt)
preceedingZeroesAdded += sectionIt->zeroesAdded;
- }
}
sizeAdjustment = preceedingZeroesAdded;
}
- return displayText().size() + sizeAdjustment - sectionPos(sectionIndex) - separators.last().size();
+ return displayTextSize + sizeAdjustment - sectionPos(sectionIndex) - separators.last().size();
} else {
return sectionPos(sectionIndex + 1) - sectionPos(sectionIndex)
- separators.at(sectionIndex + 1).size();
@@ -701,17 +708,18 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
}
const int sectionmaxsize = sectionMaxSize(sectionIndex);
- QString sectiontext = text.mid(index, sectionmaxsize);
- int sectiontextSize = sectiontext.size();
+ QStringRef sectionTextRef = text.midRef(index, sectionmaxsize);
+ int sectiontextSize = sectionTextRef.size();
QDTPDEBUG << "sectionValue for" << sn.name()
- << "with text" << text << "and st" << sectiontext
- << text.mid(index, sectionmaxsize)
+ << "with text" << text << "and st" << sectionTextRef
+ << text.midRef(index, sectionmaxsize)
<< index;
int used = 0;
switch (sn.type) {
case AmPmSection: {
+ QString sectiontext = sectionTextRef.toString();
const int ampm = findAmPm(sectiontext, sectionIndex, &used);
switch (ampm) {
case AM: // sectiontext == AM
@@ -736,14 +744,14 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
QDTPDEBUGN("This should never happen (findAmPm returned %d)", ampm);
break;
}
- if (state != Invalid) {
- text.replace(index, used, sectiontext.left(used));
- }
+ if (state != Invalid)
+ text.replace(index, used, sectiontext.constData(), used);
break; }
case MonthSection:
case DayOfWeekSectionShort:
case DayOfWeekSectionLong:
if (sn.count >= 3) {
+ QString sectiontext = sectionTextRef.toString();
if (sn.type == MonthSection) {
int min = 1;
const QDate minDate = getMinimum().date();
@@ -757,7 +765,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
if (num != -1) {
state = (used == sectiontext.size() ? Acceptable : Intermediate);
- text.replace(index, used, sectiontext.left(used));
+ text.replace(index, used, sectiontext.constData(), used);
} else {
state = Intermediate;
}
@@ -782,7 +790,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
int last = -1;
used = -1;
- QString digitsStr(sectiontext);
+ QStringRef digitsStr = sectionTextRef;
for (int i = 0; i < sectiontextSize; ++i) {
if (digitsStr.at(i).isSpace()) {
sectiontextSize = i;
@@ -803,7 +811,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
}
}
if (ok && tmp <= absMax) {
- QDTPDEBUG << sectiontext.left(digits) << tmp << digits;
+ QDTPDEBUG << sectionTextRef.left(digits) << tmp << digits;
last = tmp;
used = digits;
break;
@@ -811,13 +819,13 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
}
if (last == -1) {
- QChar first(sectiontext.at(0));
+ QChar first(sectionTextRef.at(0));
if (separators.at(sectionIndex + 1).startsWith(first)) {
used = 0;
state = Intermediate;
} else {
state = Invalid;
- QDTPDEBUG << "invalid because" << sectiontext << "can't become a uint" << last << ok;
+ QDTPDEBUG << "invalid because" << sectionTextRef << "can't become a uint" << last << ok;
}
} else {
num += last;
@@ -839,7 +847,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
if (skipToNextSection(sectionIndex, currentValue, digitsStr)) {
state = Acceptable;
const int missingZeroes = sectionmaxsize - digitsStr.size();
- text.insert(index, QString().fill(QLatin1Char('0'), missingZeroes));
+ text.insert(index, QString(missingZeroes, QLatin1Char('0')));
used = sectionmaxsize;
cursorPosition += missingZeroes;
++(const_cast<QDateTimeParser*>(this)->sectionNodes[sectionIndex].zeroesAdded);
@@ -886,14 +894,16 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
QDTPDEBUG << "parse" << input;
{
int year, month, day;
- currentValue.date().getDate(&year, &month, &day);
+ const QDate currentDate = currentValue.date();
+ const QTime currentTime = currentValue.time();
+ currentDate.getDate(&year, &month, &day);
int year2digits = year % 100;
- int hour = currentValue.time().hour();
+ int hour = currentTime.hour();
int hour12 = -1;
- int minute = currentValue.time().minute();
- int second = currentValue.time().second();
- int msec = currentValue.time().msec();
- int dayofweek = currentValue.date().dayOfWeek();
+ int minute = currentTime.minute();
+ int second = currentTime.second();
+ int msec = currentTime.msec();
+ int dayofweek = currentDate.dayOfWeek();
int ampm = -1;
Sections isSet = NoSection;
@@ -902,7 +912,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
for (int index=0; state != Invalid && index<sectionNodesCount; ++index) {
if (QStringRef(&input, pos, separators.at(index).size()) != separators.at(index)) {
- QDTPDEBUG << "invalid because" << input.mid(pos, separators.at(index).size())
+ QDTPDEBUG << "invalid because" << input.midRef(pos, separators.at(index).size())
<< "!=" << separators.at(index)
<< index << pos << currentSectionIndex;
state = Invalid;
@@ -974,7 +984,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
}
if (state != Invalid && QStringRef(&input, pos, input.size() - pos) != separators.last()) {
- QDTPDEBUG << "invalid because" << input.mid(pos)
+ QDTPDEBUG << "invalid because" << input.midRef(pos)
<< "!=" << separators.last() << pos;
state = Invalid;
}
@@ -1130,10 +1140,11 @@ end:
}
case MonthSection:
if (sn.count >= 3) {
- int tmp = newCurrentValue.date().month();
+ const int currentMonth = newCurrentValue.date().month();
+ int tmp = currentMonth;
// I know the first possible month makes the date too early
while ((tmp = findMonth(t, tmp + 1, i)) != -1) {
- const QDateTime copy(newCurrentValue.addMonths(tmp - newCurrentValue.date().month()));
+ const QDateTime copy(newCurrentValue.addMonths(tmp - currentMonth));
if (copy >= minimum && copy <= maximum)
break; // break out of while
}
@@ -1153,11 +1164,12 @@ end:
if (newCurrentValue.daysTo(minimum) != 0) {
break;
}
- toMin = newCurrentValue.time().msecsTo(minimum.time());
+ const QTime time = newCurrentValue.time();
+ toMin = time.msecsTo(minimum.time());
if (newCurrentValue.daysTo(maximum) > 0) {
toMax = -1; // can't get to max
} else {
- toMax = newCurrentValue.time().msecsTo(maximum.time());
+ toMax = time.msecsTo(maximum.time());
}
} else {
toMin = newCurrentValue.daysTo(minimum);
@@ -1247,7 +1259,8 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI
QLocale l = locale();
for (int month=startMonth; month<=12; ++month) {
- QString str2 = l.monthName(month, type).toLower();
+ const QString monthName = l.monthName(month, type);
+ QString str2 = monthName.toLower();
if (str1.startsWith(str2)) {
if (used) {
@@ -1255,7 +1268,7 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI
*used = str2.size();
}
if (usedMonth)
- *usedMonth = l.monthName(month, type);
+ *usedMonth = monthName;
return month;
}
@@ -1280,7 +1293,7 @@ int QDateTimeParser::findMonth(const QString &str1, int startMonth, int sectionI
if (used)
*used = limit;
if (usedMonth)
- *usedMonth = l.monthName(month, type);
+ *usedMonth = monthName;
return month;
}
}
@@ -1372,7 +1385,7 @@ QDateTimeParser::AmPmFinder QDateTimeParser::findAmPm(QString &str, int sectionI
}
if (used)
*used = str.size();
- if (str.trimmed().isEmpty()) {
+ if (QStringRef(&str).trimmed().isEmpty()) {
return PossibleBoth;
}
const QLatin1Char space(' ');
@@ -1543,10 +1556,7 @@ QString QDateTimeParser::SectionNode::format() const
qWarning("QDateTimeParser::sectionFormat Internal error 2");
return QString();
}
-
- QString str;
- str.fill(fillChar, count);
- return str;
+ return QString(count, fillChar);
}
@@ -1557,7 +1567,7 @@ QString QDateTimeParser::SectionNode::format() const
number that is within min and max.
*/
-bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int index,
+bool QDateTimeParser::potentialValue(const QStringRef &str, int min, int max, int index,
const QDateTime &currentValue, int insert) const
{
if (str.isEmpty()) {
@@ -1567,7 +1577,8 @@ bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int i
int val = (int)locale().toUInt(str);
const SectionNode &sn = sectionNode(index);
if (sn.type == YearSection2Digits) {
- val += currentValue.date().year() - (currentValue.date().year() % 100);
+ const int year = currentValue.date().year();
+ val += year - (year % 100);
}
if (val >= min && val <= max && str.size() == size) {
return true;
@@ -1583,8 +1594,7 @@ bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int i
if (potentialValue(str + QLatin1Char('0' + j), min, max, index, currentValue, insert)) {
return true;
} else if (insert >= 0) {
- QString tmp = str;
- tmp.insert(insert, QLatin1Char('0' + j));
+ const QString tmp = str.left(insert) + QLatin1Char('0' + j) + str.mid(insert);
if (potentialValue(tmp, min, max, index, currentValue, insert))
return true;
}
@@ -1594,7 +1604,7 @@ bool QDateTimeParser::potentialValue(const QString &str, int min, int max, int i
return false;
}
-bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, const QString &text) const
+bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, const QStringRef &text) const
{
Q_ASSERT(current >= getMinimum() && current <= getMaximum());