From fd5720af2cb1e641c433c73fd977c8ba9d278305 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Wed, 20 Jan 2016 09:42:34 +0100 Subject: QDateTimeParser: new Section mask values simplify code. Various |s of existing section flags were used repeatedly; naming these masks makes the relevant code easier to read. In QDateTimeEdit, add a comment to make clear that its Section enum is based on QDTP's. Change-Id: Ifd8364cd396a6d0d5ed7ae7dc4d31690f77edd30 Reviewed-by: Marc Mutz --- src/corelib/tools/qdatetimeparser.cpp | 18 +++++++++++------- src/corelib/tools/qdatetimeparser_p.h | 12 +++++++++--- 2 files changed, 20 insertions(+), 10 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp index cc09992b88..180f76bcc1 100644 --- a/src/corelib/tools/qdatetimeparser.cpp +++ b/src/corelib/tools/qdatetimeparser.cpp @@ -159,7 +159,7 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const break; } - if (!(node.type & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong))) { + if (!(node.type & DaySectionMask)) { if (day < cachedDay) day = cachedDay; const int max = QDate(year, month, 1).daysInMonth(); @@ -625,6 +625,10 @@ int QDateTimeParser::sectionMaxSize(Section s, int count) const case Internal: case TimeSectionMask: case DateSectionMask: + case HourSectionMask: + case YearSectionMask: + case DayOfWeekSectionMask: + case DaySectionMask: qWarning("QDateTimeParser::sectionMaxSize: Invalid section %s", SectionNode::name(s).toLatin1().constData()); @@ -993,12 +997,11 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos const QDate date(year, month, day); const int diff = dayofweek - date.dayOfWeek(); - if (diff != 0 && state == Acceptable - && isSet & (DayOfWeekSectionShort | DayOfWeekSectionLong)) { + if (diff != 0 && state == Acceptable && isSet & DayOfWeekSectionMask) { if (isSet & DaySection) conflicts = true; const SectionNode &sn = sectionNode(currentSectionIndex); - if (sn.type & (DayOfWeekSectionShort|DayOfWeekSectionLong) || currentSectionIndex == -1) { + if (sn.type & DayOfWeekSectionMask || currentSectionIndex == -1) { // dayofweek should be preferred day += diff; if (day <= 0) { @@ -1010,8 +1013,9 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos << diff << QDate(year, month, day).dayOfWeek(); } } + bool needfixday = false; - if (sectionType(currentSectionIndex) & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong)) { + if (sectionType(currentSectionIndex) & DaySectionMask) { cachedDay = day; } else if (cachedDay > day) { day = cachedDay; @@ -1039,7 +1043,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos const SectionNode sn = sectionNode(i); if (sn.type & DaySection) { input.replace(sectionPos(sn), sectionSize(i), loc.toString(day)); - } else if (sn.type & (DayOfWeekSectionShort | DayOfWeekSectionLong)) { + } else if (sn.type & DayOfWeekSectionMask) { const int dayOfWeek = QDate(year, month, day).dayOfWeek(); const QLocale::FormatType dayFormat = (sn.type == DayOfWeekSectionShort @@ -1296,7 +1300,7 @@ int QDateTimeParser::findDay(const QString &str1, int startDay, int sectionIndex int bestCount = 0; if (!str1.isEmpty()) { const SectionNode &sn = sectionNode(sectionIndex); - if (!(sn.type & (DaySection|DayOfWeekSectionShort|DayOfWeekSectionLong))) { + if (!(sn.type & DaySectionMask)) { qWarning("QDateTimeParser::findDay Internal error"); return -1; } diff --git a/src/corelib/tools/qdatetimeparser_p.h b/src/corelib/tools/qdatetimeparser_p.h index d6c4d615db..90c6a8acba 100644 --- a/src/corelib/tools/qdatetimeparser_p.h +++ b/src/corelib/tools/qdatetimeparser_p.h @@ -114,14 +114,20 @@ public: MinuteSection = 0x00008, Hour12Section = 0x00010, Hour24Section = 0x00020, - TimeSectionMask = (AmPmSection|MSecSection|SecondSection|MinuteSection|Hour12Section|Hour24Section), + HourSectionMask = (Hour12Section | Hour24Section), + TimeSectionMask = (MSecSection | SecondSection | MinuteSection | + HourSectionMask | AmPmSection), + DaySection = 0x00100, MonthSection = 0x00200, YearSection = 0x00400, YearSection2Digits = 0x00800, + YearSectionMask = YearSection | YearSection2Digits, DayOfWeekSectionShort = 0x01000, DayOfWeekSectionLong = 0x02000, - DateSectionMask = (DaySection|MonthSection|YearSection|YearSection2Digits|DayOfWeekSectionShort|DayOfWeekSectionLong), + DayOfWeekSectionMask = DayOfWeekSectionShort | DayOfWeekSectionLong, + DaySectionMask = DaySection | DayOfWeekSectionMask, + DateSectionMask = DaySectionMask | MonthSection | YearSectionMask, Internal = 0x10000, FirstSection = 0x20000 | Internal, @@ -132,7 +138,7 @@ public: FirstSectionIndex = -2, LastSectionIndex = -3, CalendarPopupIndex = -4 - }; // duplicated from qdatetimeedit.h + }; // extending qdatetimeedit.h's equivalent Q_DECLARE_FLAGS(Sections, Section) struct Q_CORE_EXPORT SectionNode { -- cgit v1.2.3