QDateTimeParser: skip some needless braces

Simplified the logic around reconciling hour12, ampm and hour in the process.
Mindless coding-style tidy-up.

Change-Id: I0b7cabc57539d0d7201fef33e0120b84f4bb4994
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Edward Welbourne 2020-09-16 16:47:49 +02:00
parent 461b28c89d
commit 15d70c9053

View File

@ -354,13 +354,12 @@ static QString unquote(QStringView str)
const int max = str.size(); const int max = str.size();
for (int i=0; i<max; ++i) { for (int i=0; i<max; ++i) {
if (str.at(i) == quote) { if (str.at(i) == quote) {
if (status != quote) { if (status != quote)
status = quote; status = quote;
} else if (!ret.isEmpty() && str.at(i - 1) == slash) { else if (!ret.isEmpty() && str.at(i - 1) == slash)
ret[ret.size() - 1] = quote; ret[ret.size() - 1] = quote;
} else { else
status = zero; status = zero;
}
} else { } else {
ret += str.at(i); ret += str.at(i);
} }
@ -396,9 +395,8 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
const QLatin1Char quote('\''); const QLatin1Char quote('\'');
const QLatin1Char slash('\\'); const QLatin1Char slash('\\');
const QLatin1Char zero('0'); const QLatin1Char zero('0');
if (newFormat == displayFormat && !newFormat.isEmpty()) { if (newFormat == displayFormat && !newFormat.isEmpty())
return true; return true;
}
QDTPDEBUGN("parseFormat: %s", newFormat.toLatin1().constData()); QDTPDEBUGN("parseFormat: %s", newFormat.toLatin1().constData());
@ -414,11 +412,10 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
if (newFormat.at(i) == quote) { if (newFormat.at(i) == quote) {
lastQuote = i; lastQuote = i;
++add; ++add;
if (status != quote) { if (status != quote)
status = quote; status = quote;
} else if (i > 0 && newFormat.at(i - 1) != slash) { else if (i > 0 && newFormat.at(i - 1) != slash)
status = zero; status = zero;
}
} else if (status != quote) { } else if (status != quote) {
const char sect = newFormat.at(i).toLatin1(); const char sect = newFormat.at(i).toLatin1();
switch (sect) { switch (sect) {
@ -532,9 +529,8 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
} }
} }
} }
if (newSectionNodes.isEmpty() && context == DateTimeEdit) { if (newSectionNodes.isEmpty() && context == DateTimeEdit)
return false; return false;
}
if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) { if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) {
const int count = newSectionNodes.size(); const int count = newSectionNodes.size();
@ -1306,9 +1302,8 @@ QDateTimeParser::scanString(const QDateTime &defaultValue,
needfixday = true; needfixday = true;
} }
if (needfixday) { if (needfixday) {
if (context == FromString) { if (context == FromString)
return StateNode(); return StateNode();
}
if (state == Acceptable && fixday) { if (state == Acceptable && fixday) {
day = qMin<int>(day, calendar.daysInMonth(month, year)); day = qMin<int>(day, calendar.daysInMonth(month, year));
@ -1335,25 +1330,18 @@ QDateTimeParser::scanString(const QDateTime &defaultValue,
if (parserType != QMetaType::QDate) { if (parserType != QMetaType::QDate) {
if (isSet & Hour12Section) { if (isSet & Hour12Section) {
const bool hasHour = isSet & Hour24Section; const bool hasHour = isSet & Hour24Section;
if (ampm == -1) { if (ampm == -1) // If we don't know from hour, assume am:
if (hasHour) { ampm = !hasHour || hour < 12 ? 0 : 1;
ampm = (hour < 12 ? 0 : 1); hour12 = hour12 % 12 + ampm * 12;
} else { if (!hasHour)
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) {
hour = hour12; hour = hour12;
} else if (hour != hour12) { else if (hour != hour12)
conflicts = true; conflicts = true;
}
} else if (ampm != -1) { } else if (ampm != -1) {
if (!(isSet & (Hour24Section))) { if (!(isSet & (Hour24Section)))
hour = (12 * ampm); // special case. Only ap section hour = 12 * ampm; // Special case: only ap section
} else if ((ampm == 0) != (hour < 12)) { else if ((ampm == 0) != (hour < 12))
conflicts = true; conflicts = true;
}
} }
} }
@ -1469,9 +1457,9 @@ QDateTimeParser::parse(QString input, int position, const QDateTime &defaultValu
int toMax; int toMax;
if (sn.type & TimeSectionMask) { if (sn.type & TimeSectionMask) {
if (scan.value.daysTo(minimum) != 0) { if (scan.value.daysTo(minimum) != 0)
break; break;
}
const QTime time = scan.value.time(); const QTime time = scan.value.time();
toMin = time.msecsTo(minimum.time()); toMin = time.msecsTo(minimum.time());
if (scan.value.daysTo(maximum) > 0) 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); Q_ASSERT(maximum.date().toJulianDay() == 5373484);
if (scan.value.date().toJulianDay() > 5373484) if (scan.value.date().toJulianDay() > 5373484)
scan.state = Invalid; scan.state = Invalid;
} else { } else if (scan.value > maximum) {
if (scan.value > maximum) scan.state = Invalid;
scan.state = Invalid;
} }
QDTPDEBUG << "not checking intermediate because scanned value is" << scan.value << minimum << maximum; 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) if (used)
*used = str.size(); *used = str.size();
if (QStringView(str).trimmed().isEmpty()) { if (QStringView(str).trimmed().isEmpty())
return PossibleBoth; return PossibleBoth;
}
const QLatin1Char space(' '); const QLatin1Char space(' ');
int size = sectionMaxSize(sectionIndex); int size = sectionMaxSize(sectionIndex);
@ -1970,9 +1957,9 @@ QString QDateTimeParser::SectionNode::format() const
bool QDateTimeParser::potentialValue(QStringView str, int min, int max, int index, bool QDateTimeParser::potentialValue(QStringView str, int min, int max, int index,
const QDateTime &currentValue, int insert) const const QDateTime &currentValue, int insert) const
{ {
if (str.isEmpty()) { if (str.isEmpty())
return true; return true;
}
const int size = sectionMaxSize(index); const int size = sectionMaxSize(index);
int val = (int)locale().toUInt(str); int val = (int)locale().toUInt(str);
const SectionNode &sn = sectionNode(index); 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); const int year = currentValue.date().year(calendar);
val += year - (year % 100); val += year - (year % 100);
} }
if (val >= min && val <= max && str.size() == size) { if (val >= min && val <= max && str.size() == size)
return true; return true;
} else if (val > max) { if (val > max || (str.size() == size && val < min))
return false; return false;
} else if (str.size() == size && val < min) {
return false;
}
const int len = size - str.size(); const int len = size - str.size();
for (int i=0; i<len; ++i) { for (int i=0; i<len; ++i) {
@ -2090,17 +2074,15 @@ bool QDateTimeParser::fromString(const QString &t, QDate *date, QTime *time) con
if (time) { if (time) {
const QTime t = datetime.time(); const QTime t = datetime.time();
if (!t.isValid()) { if (!t.isValid())
return false; return false;
}
*time = t; *time = t;
} }
if (date) { if (date) {
const QDate d = datetime.date(); const QDate d = datetime.date();
if (!d.isValid()) { if (!d.isValid())
return false; return false;
}
*date = d; *date = d;
} }
return true; return true;