Disintermediate QDateTimeParser::SectionNode operations.

The name, format and maxChange of a Section depend only on the
section, not on the details of the currently parsed text it matches;
so we don't need the parser object's list of all sections to work them
out.  Move these methods to the SectionNode and act directly on that
instead of going via the section list.

Make the name take a Section enum instead of an int.
Likewise, make stateName take a State enum instead of an int.

Change-Id: Ie340d042ab95aec517013c4dcc30901d40305c78
Reviewed-by: Simon Hausmann <simon.hausmann@theqtcompany.com>
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@theqtcompany.com>
bb10
Edward Welbourne 2016-01-18 18:19:21 +01:00
parent a965d505a0
commit 72b57a5dfa
3 changed files with 46 additions and 60 deletions

View File

@ -155,7 +155,7 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
case AmPmSection: hour = (newVal == 0 ? hour % 12 : (hour % 12) + 12); break;
default:
qWarning("QDateTimeParser::setDigit() Internal error (%s)",
qPrintable(sectionName(node.type)));
qPrintable(node.name()));
break;
}
@ -206,7 +206,7 @@ int QDateTimeParser::absoluteMax(int s, const QDateTime &cur) const
default: break;
}
qWarning("QDateTimeParser::absoluteMax() Internal error (%s)",
qPrintable(sectionName(sn.type)));
qPrintable(sn.name()));
return -1;
}
@ -235,7 +235,7 @@ int QDateTimeParser::absoluteMin(int s) const
default: break;
}
qWarning("QDateTimeParser::absoluteMin() Internal error (%s, %0x)",
qPrintable(sectionName(sn.type)), sn.type);
qPrintable(sn.name()), sn.type);
return -1;
}
@ -290,7 +290,7 @@ int QDateTimeParser::sectionPos(const SectionNode &sn) const
default: break;
}
if (sn.pos == -1) {
qWarning("QDateTimeParser::sectionPos Internal error (%s)", qPrintable(sectionName(sn.type)));
qWarning("QDateTimeParser::sectionPos Internal error (%s)", qPrintable(sn.name()));
return -1;
}
return sn.pos;
@ -514,7 +514,7 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
last.pos = -1;
// for (int i=0; i<sectionNodes.size(); ++i) {
// QDTPDEBUG << sectionName(sectionNodes.at(i).type) << sectionNodes.at(i).count;
// QDTPDEBUG << sectionNodes.at(i).name() << sectionNodes.at(i).count;
// }
QDTPDEBUG << newFormat << displayFormat;
@ -625,7 +625,7 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const
case TimeSectionMask:
case DateSectionMask:
qWarning("QDateTimeParser::sectionMaxSize: Invalid section %s",
sectionName(s).toLatin1().constData());
SectionNode::name(s).toLatin1().constData());
case NoSectionIndex:
case FirstSectionIndex:
@ -691,7 +691,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
const SectionNode &sn = sectionNode(sectionIndex);
if ((sn.type & Internal) == Internal) {
qWarning("QDateTimeParser::parseSection Internal error (%s %d)",
qPrintable(sectionName(sn.type)), sectionIndex);
qPrintable(sn.name()), sectionIndex);
return -1;
}
@ -699,7 +699,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
QString sectiontext = text.mid(index, sectionmaxsize);
int sectiontextSize = sectiontext.size();
QDTPDEBUG << "sectionValue for" << sectionName(sn.type)
QDTPDEBUG << "sectionValue for" << sn.name()
<< "with text" << text << "and st" << sectiontext
<< text.mid(index, sectionmaxsize)
<< index;
@ -849,7 +849,7 @@ int QDateTimeParser::parseSection(const QDateTime &currentValue, int sectionInde
break; }
default:
qWarning("QDateTimeParser::parseSection Internal error (%s %d)",
qPrintable(sectionName(sn.type)), sectionIndex);
qPrintable(sn.name()), sectionIndex);
return -1;
}
@ -910,7 +910,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
int used;
num = parseSection(currentValue, index, input, cursorPosition, pos, tmpstate, &used);
QDTPDEBUG << "sectionValue" << sectionName(sectionType(index)) << input
QDTPDEBUG << "sectionValue" << sn.name() << input
<< "pos" << pos << "used" << used << stateName(tmpstate);
if (fixup && tmpstate == Intermediate && used < sn.count) {
const FieldInfo fi = fieldInfo(index);
@ -928,7 +928,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
break;
}
QDTPDEBUG << index << sectionName(sectionType(index)) << "is set to"
QDTPDEBUG << index << sn.name() << "is set to"
<< pos << "state is" << stateName(state);
@ -948,7 +948,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
case AmPmSection: current = &ampm; break;
default:
qWarning("QDateTimeParser::parse Internal error (%s)",
qPrintable(sectionName(sn.type)));
qPrintable(sn.name()));
break;
}
if (!current) {
@ -956,7 +956,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
return StateNode();
}
if (isSet & sn.type && *current != num) {
QDTPDEBUG << "CONFLICT " << sectionName(sn.type) << *current << num;
QDTPDEBUG << "CONFLICT " << sn.name() << *current << num;
conflicts = true;
if (index != currentSectionIndex || num == -1) {
continue;
@ -1040,15 +1040,16 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
const QLocale loc = locale();
for (int i=0; i<sectionNodesCount; ++i) {
const Section thisSectionType = sectionType(i);
if (thisSectionType & (DaySection)) {
input.replace(sectionPos(i), sectionSize(i), loc.toString(day));
} else if (thisSectionType & (DayOfWeekSectionShort|DayOfWeekSectionLong)) {
const SectionNode sn = sectionNode(i);
if (sn.type & DaySection) {
input.replace(sectionPos(sn), sectionSize(i), loc.toString(day));
} else if (sn.type & (DayOfWeekSectionShort | DayOfWeekSectionLong)) {
const int dayOfWeek = QDate(year, month, day).dayOfWeek();
const QLocale::FormatType dayFormat = (thisSectionType == DayOfWeekSectionShort
? QLocale::ShortFormat : QLocale::LongFormat);
const QLocale::FormatType dayFormat =
(sn.type == DayOfWeekSectionShort
? QLocale::ShortFormat : QLocale::LongFormat);
const QString dayName(loc.dayName(dayOfWeek, dayFormat));
input.replace(sectionPos(i), sectionSize(i), dayName);
input.replace(sectionPos(sn), sectionSize(i), dayName);
}
}
} else {
@ -1162,7 +1163,7 @@ end:
toMin = newCurrentValue.daysTo(minimum);
toMax = newCurrentValue.daysTo(maximum);
}
const int maxChange = QDateTimeParser::maxChange(i);
const int maxChange = sn.maxChange();
if (toMin > maxChange) {
QDTPDEBUG << "invalid because toMin > maxChange" << toMin
<< maxChange << t << newCurrentValue << minimum;
@ -1176,7 +1177,7 @@ end:
const int min = getDigit(minimum, i);
if (min == -1) {
qWarning("QDateTimeParser::parse Internal error 4 (%s)",
qPrintable(sectionName(sn.type)));
qPrintable(sn.name()));
state = Invalid;
done = true;
break;
@ -1188,7 +1189,7 @@ end:
pos = -1;
if (!potentialValue(t.simplified(), min, max, i, newCurrentValue, pos)) {
QDTPDEBUG << "invalid because potentialValue(" << t.simplified() << min << max
<< sectionName(sn.type) << "returned" << toMax << toMin << pos;
<< sn.name() << "returned" << toMax << toMin << pos;
state = Invalid;
done = true;
break;
@ -1446,10 +1447,9 @@ int QDateTimeParser::findAmPm(QString &str, int sectionIndex, int *used) const
Max number of units that can be changed by this section.
*/
int QDateTimeParser::maxChange(int index) const
int QDateTimeParser::SectionNode::maxChange() const
{
const SectionNode &sn = sectionNode(index);
switch (sn.type) {
switch (type) {
// Time. unit is msec
case MSecSection: return 999;
case SecondSection: return 59 * 1000;
@ -1465,7 +1465,7 @@ int QDateTimeParser::maxChange(int index) const
case YearSection2Digits: return 100 * 365;
default:
qWarning("QDateTimeParser::maxChange() Internal error (%s)",
qPrintable(sectionName(sectionType(index))));
qPrintable(name()));
}
return -1;
@ -1475,8 +1475,7 @@ QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
{
FieldInfo ret = 0;
const SectionNode &sn = sectionNode(index);
const Section s = sn.type;
switch (s) {
switch (sn.type) {
case MSecSection:
ret |= Fraction;
// fallthrough
@ -1487,7 +1486,7 @@ QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
case YearSection:
case YearSection2Digits:
ret |= Numeric;
if (s != YearSection) {
if (sn.type != YearSection) {
ret |= AllowPartial;
}
if (sn.count != 1) {
@ -1515,30 +1514,16 @@ QDateTimeParser::FieldInfo QDateTimeParser::fieldInfo(int index) const
break;
default:
qWarning("QDateTimeParser::fieldInfo Internal error 2 (%d %s %d)",
index, qPrintable(sectionName(sn.type)), sn.count);
index, qPrintable(sn.name()), sn.count);
break;
}
return ret;
}
/*!
\internal
Get a number that str can become which is between min
and max or -1 if this is not possible.
*/
QString QDateTimeParser::sectionFormat(int index) const
{
const SectionNode &sn = sectionNode(index);
return sectionFormat(sn.type, sn.count);
}
QString QDateTimeParser::sectionFormat(Section s, int count) const
QString QDateTimeParser::SectionNode::format() const
{
QChar fillChar;
switch (s) {
switch (type) {
case AmPmSection: return count == 1 ? QLatin1String("AP") : QLatin1String("ap");
case MSecSection: fillChar = QLatin1Char('z'); break;
case SecondSection: fillChar = QLatin1Char('s'); break;
@ -1553,7 +1538,7 @@ QString QDateTimeParser::sectionFormat(Section s, int count) const
case YearSection: fillChar = QLatin1Char('y'); break;
default:
qWarning("QDateTimeParser::sectionFormat Internal error (%s)",
qPrintable(sectionName(s)));
qPrintable(name(type)));
return QString();
}
if (fillChar.isNull()) {
@ -1651,7 +1636,7 @@ bool QDateTimeParser::skipToNextSection(int index, const QDateTime &current, con
For debugging. Returns the name of the section \a s.
*/
QString QDateTimeParser::sectionName(int s) const
QString QDateTimeParser::SectionNode::name(QDateTimeParser::Section s)
{
switch (s) {
case QDateTimeParser::AmPmSection: return QLatin1String("AmPmSection");
@ -1669,7 +1654,7 @@ QString QDateTimeParser::sectionName(int s) const
case QDateTimeParser::NoSection: return QLatin1String("NoSection");
case QDateTimeParser::FirstSection: return QLatin1String("FirstSection");
case QDateTimeParser::LastSection: return QLatin1String("LastSection");
default: return QLatin1String("Unknown section ") + QString::number(s);
default: return QLatin1String("Unknown section ") + QString::number(int(s));
}
}
@ -1678,7 +1663,7 @@ QString QDateTimeParser::sectionName(int s) const
For debugging. Returns the name of the state \a s.
*/
QString QDateTimeParser::stateName(int s) const
QString QDateTimeParser::stateName(State s) const
{
switch (s) {
case Invalid: return QLatin1String("Invalid");

View File

@ -135,11 +135,16 @@ public:
}; // duplicated from qdatetimeedit.h
Q_DECLARE_FLAGS(Sections, Section)
struct SectionNode {
struct Q_CORE_EXPORT SectionNode {
Section type;
mutable int pos;
int count;
int zeroesAdded;
static QString name(Section s);
QString name() const { return name(type); }
QString format() const;
int maxChange() const;
};
enum State { // duplicated from QValidator
@ -197,15 +202,11 @@ public:
QString *dayName = 0, int *used = 0) const;
#endif
int findAmPm(QString &str1, int index, int *used = 0) const;
int maxChange(int s) const;
bool potentialValue(const QString &str, int min, int max, int index,
const QDateTime &currentValue, int insert) const;
bool skipToNextSection(int section, const QDateTime &current, const QString &sectionText) const;
QString sectionName(int s) const;
QString stateName(int s) const;
QString sectionFormat(int index) const;
QString sectionFormat(Section s, int count) const;
QString stateName(State s) const;
enum FieldInfoFlag {
Numeric = 0x01,

View File

@ -873,7 +873,7 @@ void QDateTimeEdit::setDisplayFormat(const QString &format)
d->displayFormat.clear();
for (int i=d->sectionNodes.size() - 1; i>=0; --i) {
d->displayFormat += d->separators.at(i + 1);
d->displayFormat += d->sectionFormat(i);
d->displayFormat += d->sectionNode(i).format();
}
d->displayFormat += d->separators.at(0);
d->separators = reverse(d->separators);
@ -2214,9 +2214,9 @@ void QDateTimeEditPrivate::_q_editorCursorPositionChanged(int oldpos, int newpos
}
}
QDTEDEBUG << "currentSectionIndex is set to" << sectionName(sectionType(s))
QDTEDEBUG << "currentSectionIndex is set to" << sectionNode(s).name()
<< oldpos << newpos
<< "was" << sectionName(sectionType(currentSectionIndex));
<< "was" << sectionNode(currentSectionIndex).name();
currentSectionIndex = s;
Q_ASSERT_X(currentSectionIndex < sectionNodes.size(),