From 15d70c90537642e01f4a2d8cdee9c1566d5e060d Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 16 Sep 2020 16:47:49 +0200 Subject: QDateTimeParser: skip some needless braces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Simplified the logic around reconciling hour12, ampm and hour in the process. Mindless coding-style tidy-up. Change-Id: I0b7cabc57539d0d7201fef33e0120b84f4bb4994 Reviewed-by: Andrei Golubev Reviewed-by: MÃ¥rten Nordheim --- src/corelib/time/qdatetimeparser.cpp | 74 ++++++++++++++---------------------- 1 file changed, 28 insertions(+), 46 deletions(-) diff --git a/src/corelib/time/qdatetimeparser.cpp b/src/corelib/time/qdatetimeparser.cpp index ba26398772..efc8ea70d7 100644 --- a/src/corelib/time/qdatetimeparser.cpp +++ b/src/corelib/time/qdatetimeparser.cpp @@ -354,13 +354,12 @@ static QString unquote(QStringView str) const int max = str.size(); for (int i=0; i 0 && newFormat.at(i - 1) != slash) { + else if (i > 0 && newFormat.at(i - 1) != slash) status = zero; - } } else if (status != quote) { const char sect = newFormat.at(i).toLatin1(); switch (sect) { @@ -532,9 +529,8 @@ bool QDateTimeParser::parseFormat(const QString &newFormat) } } } - if (newSectionNodes.isEmpty() && context == DateTimeEdit) { + if (newSectionNodes.isEmpty() && context == DateTimeEdit) return false; - } if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) { const int count = newSectionNodes.size(); @@ -1306,9 +1302,8 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, needfixday = true; } if (needfixday) { - if (context == FromString) { + if (context == FromString) return StateNode(); - } if (state == Acceptable && fixday) { day = qMin(day, calendar.daysInMonth(month, year)); @@ -1335,25 +1330,18 @@ QDateTimeParser::scanString(const QDateTime &defaultValue, if (parserType != QMetaType::QDate) { if (isSet & Hour12Section) { const bool hasHour = isSet & Hour24Section; - if (ampm == -1) { - if (hasHour) { - ampm = (hour < 12 ? 0 : 1); - } else { - ampm = 0; // no way to tell if this is am or pm so I assume am - } - } - hour12 = (ampm == 0 ? hour12 % 12 : (hour12 % 12) + 12); - if (!hasHour) { + if (ampm == -1) // If we don't know from hour, assume am: + ampm = !hasHour || hour < 12 ? 0 : 1; + hour12 = hour12 % 12 + ampm * 12; + if (!hasHour) hour = hour12; - } else if (hour != hour12) { + else if (hour != hour12) conflicts = true; - } } else if (ampm != -1) { - if (!(isSet & (Hour24Section))) { - hour = (12 * ampm); // special case. Only ap section - } else if ((ampm == 0) != (hour < 12)) { + if (!(isSet & (Hour24Section))) + hour = 12 * ampm; // Special case: only ap section + else if ((ampm == 0) != (hour < 12)) conflicts = true; - } } } @@ -1469,9 +1457,9 @@ QDateTimeParser::parse(QString input, int position, const QDateTime &defaultValu int toMax; if (sn.type & TimeSectionMask) { - if (scan.value.daysTo(minimum) != 0) { + if (scan.value.daysTo(minimum) != 0) break; - } + const QTime time = scan.value.time(); toMin = time.msecsTo(minimum.time()); if (scan.value.daysTo(maximum) > 0) @@ -1525,9 +1513,8 @@ QDateTimeParser::parse(QString input, int position, const QDateTime &defaultValu Q_ASSERT(maximum.date().toJulianDay() == 5373484); if (scan.value.date().toJulianDay() > 5373484) scan.state = Invalid; - } else { - if (scan.value > maximum) - scan.state = Invalid; + } else if (scan.value > maximum) { + scan.state = Invalid; } QDTPDEBUG << "not checking intermediate because scanned value is" << scan.value << minimum << maximum; @@ -1785,9 +1772,9 @@ QDateTimeParser::AmPmFinder QDateTimeParser::findAmPm(QString &str, int sectionI } if (used) *used = str.size(); - if (QStringView(str).trimmed().isEmpty()) { + if (QStringView(str).trimmed().isEmpty()) return PossibleBoth; - } + const QLatin1Char space(' '); int size = sectionMaxSize(sectionIndex); @@ -1970,9 +1957,9 @@ QString QDateTimeParser::SectionNode::format() const bool QDateTimeParser::potentialValue(QStringView str, int min, int max, int index, const QDateTime ¤tValue, int insert) const { - if (str.isEmpty()) { + if (str.isEmpty()) return true; - } + const int size = sectionMaxSize(index); int val = (int)locale().toUInt(str); const SectionNode &sn = sectionNode(index); @@ -1980,13 +1967,10 @@ bool QDateTimeParser::potentialValue(QStringView str, int min, int max, int inde const int year = currentValue.date().year(calendar); val += year - (year % 100); } - if (val >= min && val <= max && str.size() == size) { + if (val >= min && val <= max && str.size() == size) return true; - } else if (val > max) { - return false; - } else if (str.size() == size && val < min) { + if (val > max || (str.size() == size && val < min)) return false; - } const int len = size - str.size(); for (int i=0; i