OSDN Git Service

remove non-operational QDateTimeParser bits
authorIvailo Monev <xakepa10@gmail.com>
Wed, 2 Aug 2023 02:35:26 +0000 (05:35 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Wed, 2 Aug 2023 02:35:26 +0000 (05:35 +0300)
with the QDateTimeEdit rewrite (see the previous commit) the parser has
only one mode

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
cmake/modules/KatieBuildMacros.cmake
src/core/tools/qdatetime.cpp
src/core/tools/qdatetime_p.h
src/core/tools/qlocale.cpp

index 96dbced..3e37536 100644 (file)
@@ -267,7 +267,7 @@ endfunction()
 # depend on KtNetwork depend on plugins that depend on it too
 add_custom_target(plugins_dependant_tests)
 function(KATIE_SETUP_PLUGIN FORPLUGIN)
-    add_dependencies(plugins_dependant_tests ${FORPLUGIN})
+    add_dependencies(plugins_dependant_tests ${FORPLUGIN})
 endfunction()
 
 # a macro to remove conditional code from headers which is only relevant to the
index 1a8e352..8251e9b 100644 (file)
@@ -1134,7 +1134,7 @@ QDate QDate::fromString(const QString &string, const QString &format)
 {
     QDate date;
 #ifndef QT_BOOTSTRAPPED
-    QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
+    QDateTimeParser dt(QVariant::Date);
     if (Q_LIKELY(dt.parseFormat(format)))
         dt.fromString(string, &date, 0);
 #else
@@ -1738,7 +1738,7 @@ QTime QTime::fromString(const QString &string, const QString &format)
 {
     QTime time;
 #ifndef QT_BOOTSTRAPPED
-    QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString);
+    QDateTimeParser dt(QVariant::Time);
     if (Q_LIKELY(dt.parseFormat(format)))
         dt.fromString(string, 0, &time);
 #else
@@ -3152,7 +3152,7 @@ QDateTime QDateTime::fromString(const QString &string, const QString &format)
     QTime time;
     QDate date;
 #ifndef QT_BOOTSTRAPPED
-    QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString);
+    QDateTimeParser dt(QVariant::DateTime);
     if (Q_LIKELY(dt.parseFormat(format)))
         dt.fromString(string, &date, &time);
 #else
@@ -3616,7 +3616,7 @@ bool QDateTimeParser::setDigit(QDateTime &v, int index, int newVal) const
         }
     }
     if (QDate::isValid(year, month, day) && QTime::isValid(hour, minute, second, msec)) {
-        v = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec);
+        v = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), QDATETIMEEDIT_SPEC);
         v.setUtcOffset(offset);
         return true;
     }
@@ -3940,9 +3940,6 @@ bool QDateTimeParser::parseFormat(const QString &newFormat)
             }
         }
     }
-    if (newSectionNodes.isEmpty() && context == DateTimeEdit) {
-        return false;
-    }
 
     if ((newDisplay & (AmPmSection|Hour12Section)) == Hour12Section) {
         const int count = newSectionNodes.size();
@@ -4377,7 +4374,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
             pos += qMax(0, used);
 
             state = qMin<State>(state, tmpstate);
-            if (state == Intermediate && context == FromString) {
+            if (state == Intermediate) {
                 state = Invalid;
                 break;
             }
@@ -4483,22 +4480,8 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
                     }
                 }
                 if (needfixday) {
-                    if (context == FromString) {
-                        state = Invalid;
-                        goto end;
-                    }
-                    if (state == Acceptable && fixday) {
-                        day = qMin<int>(day, QDate(year, month, 1).daysInMonth());
-
-                        const QLocale loc = locale();
-                        for (int i=0; i<sectionNodesCount; ++i) {
-                            if (sectionType(i) & (DaySection|DayOfWeekSection)) {
-                                input.replace(sectionPos(i), sectionSize(i), loc.toString(day));
-                            }
-                        }
-                    } else {
-                        state = qMin(Intermediate, state);
-                    }
+                    state = Invalid;
+                    goto end;
                 }
             }
 
@@ -4528,7 +4511,7 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
 
             }
 
-            newCurrentValue = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), spec);
+            newCurrentValue = QDateTime(QDate(year, month, day), QTime(hour, minute, second, msec), QDATETIMEEDIT_SPEC);
             QDTPDEBUG << year << month << day << hour << minute << second << msec;
         }
         QDTPDEBUGN("'%s' => '%s'(%s)", input.toLatin1().constData(),
@@ -4537,132 +4520,18 @@ QDateTimeParser::StateNode QDateTimeParser::parse(QString &input, int &cursorPos
     }
 end:
     if (newCurrentValue.isValid()) {
-        if (context != FromString && state != Invalid && newCurrentValue < minimum) {
-            static const QLatin1Char space(' ');
-            if (Q_UNLIKELY(newCurrentValue >= minimum))
-                qWarning("QDateTimeParser::parse Internal error 3 (%s %s)",
-                         qPrintable(newCurrentValue.toString()), qPrintable(minimum.toString()));
-
-            bool done = false;
+        // optimization
+        Q_ASSERT(getMaximum().date().toJulianDay() == 4642999);
+        if (newCurrentValue.date().toJulianDay() > 4642999)
             state = Invalid;
-            for (int i=0; i<sectionNodesCount && !done; ++i) {
-                const SectionNode &sn = sectionNodes.at(i);
-                QString t = sectionText(input, i, sn.pos).toLower();
-                if ((t.size() < sectionMaxSize(i) && (((int)fieldInfo(i) & (FixedWidth|Numeric)) != Numeric))
-                    || t.contains(space)) {
-                    switch (sn.type) {
-                    case AmPmSection:
-                        switch (findAmPm(t, i)) {
-                        case AM:
-                        case PM:
-                            state = Acceptable;
-                            done = true;
-                            break;
-                        case Neither:
-                            state = Invalid;
-                            done = true;
-                            break;
-                        case PossibleAM:
-                        case PossiblePM:
-                        case PossibleBoth: {
-                            const QDateTime copy(newCurrentValue.addSecs(12 * 60 * 60));
-                            if (copy >= minimum && copy <= maximum) {
-                                state = Intermediate;
-                                done = true;
-                            }
-                            break; }
-                        }
-                    case MonthSection:
-                        if (sn.count >= 3) {
-                            int tmp = newCurrentValue.date().month();
-                            // 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()));
-                                if (copy >= minimum && copy <= maximum)
-                                    break; // break out of while
-                            }
-                            if (tmp == -1) {
-                                break;
-                            }
-                            state = Intermediate;
-                            done = true;
-                            break;
-                        }
-                        // fallthrough
-                    default: {
-                        int toMin;
-                        int toMax;
-
-                        if (sn.type & TimeSectionMask) {
-                            if (newCurrentValue.daysTo(minimum) != 0) {
-                                break;
-                            }
-                            toMin = newCurrentValue.time().msecsTo(minimum.time());
-                            if (newCurrentValue.daysTo(maximum) > 0) {
-                                toMax = -1; // can't get to max
-                            } else {
-                                toMax = newCurrentValue.time().msecsTo(maximum.time());
-                            }
-                        } else {
-                            toMin = newCurrentValue.daysTo(minimum);
-                            toMax = newCurrentValue.daysTo(maximum);
-                        }
-                        const int maxChange = QDateTimeParser::maxChange(i);
-                        if (toMin > maxChange) {
-                            QDTPDEBUG << "invalid because toMin > maxChange" << toMin
-                                      << maxChange << t << newCurrentValue << minimum;
-                            state = Invalid;
-                            done = true;
-                            break;
-                        } else if (toMax > maxChange) {
-                            toMax = -1; // can't get to max
-                        }
-
-                        const int min = getDigit(minimum, i);
-                        if (Q_UNLIKELY(min == -1)) {
-                            qWarning("QDateTimeParser::parse Internal error 4 (%s)",
-                                     qPrintable(sectionName(sn.type)));
-                            state = Invalid;
-                            done = true;
-                            break;
-                        }
-
-                        int max = toMax != -1 ? getDigit(maximum, i) : absoluteMax(i, newCurrentValue);
-                        int pos = cursorPosition - sn.pos;
-                        if (pos < 0 || pos >= t.size())
-                            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;
-                            state = Invalid;
-                            done = true;
-                            break;
-                        }
-                        state = Intermediate;
-                        done = true;
-                        break; }
-                    }
-                }
-            }
-        } else {
-            if (context == FromString) {
-                // optimization
-                Q_ASSERT(getMaximum().date().toJulianDay() == 4642999);
-                if (newCurrentValue.date().toJulianDay() > 4642999)
-                    state = Invalid;
-            } else {
-                if (newCurrentValue > getMaximum())
-                    state = Invalid;
-            }
 
-            QDTPDEBUG << "not checking intermediate because newCurrentValue is" << newCurrentValue << getMinimum() << getMaximum();
-        }
+        QDTPDEBUG << "not checking intermediate because newCurrentValue is" << newCurrentValue << getMinimum() << getMaximum();
     }
     StateNode node;
     node.input = input;
     node.state = state;
     node.conflicts = conflicts;
-    node.value = newCurrentValue.toTimeSpec(spec);
+    node.value = newCurrentValue.toTimeSpec(QDATETIMEEDIT_SPEC);
     text = input;
     return node;
 }
@@ -4796,48 +4665,8 @@ int QDateTimeParser::findAmPm(QString &str, int sectionIndex, int *used) const
     } else if (str.indexOf(ampm[pmindex], 0, Qt::CaseInsensitive) == 0) {
         str = ampm[pmindex];
         return PM;
-    } else if (context == FromString || (str.count(space) == 0 && str.size() >= size)) {
-        return Neither;
     }
-    size = qMin(size, str.size());
-
-    bool broken[2] = {false, false};
-    for (int i=0; i<size; ++i) {
-        if (str.at(i) != space) {
-            for (int j=0; j<2; ++j) {
-                if (!broken[j]) {
-                    int index = ampm[j].indexOf(str.at(i));
-                    QDTPDEBUG << "looking for" << str.at(i)
-                              << "in" << ampm[j] << "and got" << index;
-                    if (index == -1) {
-                        if (str.at(i).category() == QChar::Letter_Uppercase) {
-                            index = ampm[j].indexOf(str.at(i).toLower());
-                            QDTPDEBUG << "trying with" << str.at(i).toLower()
-                                      << "in" << ampm[j] << "and got" << index;
-                        } else if (str.at(i).category() == QChar::Letter_Lowercase) {
-                            index = ampm[j].indexOf(str.at(i).toUpper());
-                            QDTPDEBUG << "trying with" << str.at(i).toUpper()
-                                      << "in" << ampm[j] << "and got" << index;
-                        }
-                        if (index == -1) {
-                            broken[j] = true;
-                            if (broken[amindex] && broken[pmindex]) {
-                                QDTPDEBUG << str << "didn't make it";
-                                return Neither;
-                            }
-                            continue;
-                        } else {
-                            str[i] = ampm[j].at(index); // fix case
-                        }
-                    }
-                    ampm[j].remove(index, 1);
-                }
-            }
-        }
-    }
-    if (!broken[pmindex] && !broken[amindex])
-        return PossibleBoth;
-    return (!broken[amindex] ? PossibleAM : PossiblePM);
+    return Neither;
 }
 
 /*!
@@ -5111,12 +4940,12 @@ bool QDateTimeParser::fromString(const QString &format, QDate *date, QTime *time
 
 QDateTime QDateTimeParser::getMinimum() const
 {
-    return QDateTime(QDATETIMEEDIT_DATE_MIN, QDATETIMEEDIT_TIME_MIN, spec);
+    return QDateTime(QDATETIMEEDIT_DATE_MIN, QDATETIMEEDIT_TIME_MIN, QDATETIMEEDIT_SPEC);
 }
 
 QDateTime QDateTimeParser::getMaximum() const
 {
-    return QDateTime(QDATETIMEEDIT_DATE_MAX, QDATETIMEEDIT_TIME_MAX, spec);
+    return QDateTime(QDATETIMEEDIT_DATE_MAX, QDATETIMEEDIT_TIME_MAX, QDATETIMEEDIT_SPEC);
 }
 
 QString QDateTimeParser::getAmPmText(AmPm ap, Case cs) const
index e503dac..99f4bd9 100644 (file)
@@ -39,7 +39,7 @@
 #include "QtCore/qlocale.h"
 #include "QtCore/qvector.h"
 
-
+#define QDATETIMEEDIT_SPEC Qt::LocalTime
 #define QDATETIMEEDIT_TIME_MIN QTime(0, 0, 0, 0)
 #define QDATETIMEEDIT_TIME_MAX QTime(23, 59, 59, 999)
 #define QDATETIMEEDIT_DATE_MIN QDate(100, 1, 1)
@@ -85,13 +85,8 @@ public:
 class Q_CORE_EXPORT QDateTimeParser
 {
 public:
-    enum Context {
-        FromString,
-        DateTimeEdit
-    };
-    QDateTimeParser(QVariant::Type t, Context ctx)
-        : currentSectionIndex(-1), display(0), cachedDay(-1), parserType(t),
-        fixday(false), spec(Qt::LocalTime), context(ctx)
+    QDateTimeParser(QVariant::Type t)
+        : currentSectionIndex(-1), display(0), cachedDay(-1), parserType(t)
     {
         defaultLocale = QLocale::system();
         first.type = FirstSection;
@@ -104,7 +99,7 @@ public:
         none.pos = -1;
         none.count = -1;
     }
-    virtual ~QDateTimeParser() {}
+
     enum {
         Neither = -1,
         AM = 0,
@@ -224,12 +219,12 @@ public:
 
     FieldInfo fieldInfo(int index) const;
 
-    virtual QDateTime getMinimum() const;
-    virtual QDateTime getMaximum() const;
-    virtual int cursorPosition() const { return -1; }
-    virtual QString displayText() const { return text; }
-    virtual QString getAmPmText(AmPm ap, Case cs) const;
-    virtual QLocale locale() const { return defaultLocale; }
+    QDateTime getMinimum() const;
+    QDateTime getMaximum() const;
+    int cursorPosition() const { return -1; }
+    QString displayText() const { return text; }
+    QString getAmPmText(AmPm ap, Case cs) const;
+    QLocale locale() const { return defaultLocale; }
 
     int currentSectionIndex;
     Sections display;
@@ -241,11 +236,6 @@ public:
     QString displayFormat;
     QLocale defaultLocale;
     QVariant::Type parserType;
-
-    bool fixday;
-
-    Qt::TimeSpec spec; // spec if used by QDateTimeEdit
-    Context context;
 };
 
 Q_CORE_EXPORT bool operator==(const QDateTimeParser::SectionNode &s1, const QDateTimeParser::SectionNode &s2);
index 303057c..b778515 100644 (file)
@@ -1119,7 +1119,7 @@ QTime QLocale::toTime(const QString &string, const QString &format) const
 {
     QTime time;
 #ifndef QT_BOOTSTRAPPED
-    QDateTimeParser dt(QVariant::Time, QDateTimeParser::FromString);
+    QDateTimeParser dt(QVariant::Time);
     dt.defaultLocale = *this;
     if (Q_LIKELY(dt.parseFormat(format)))
         dt.fromString(string, 0, &time);
@@ -1150,7 +1150,7 @@ QDate QLocale::toDate(const QString &string, const QString &format) const
 {
     QDate date;
 #ifndef QT_BOOTSTRAPPED
-    QDateTimeParser dt(QVariant::Date, QDateTimeParser::FromString);
+    QDateTimeParser dt(QVariant::Date);
     dt.defaultLocale = *this;
     if (Q_LIKELY(dt.parseFormat(format)))
         dt.fromString(string, &date, 0);
@@ -1182,7 +1182,7 @@ QDateTime QLocale::toDateTime(const QString &string, const QString &format) cons
     QTime time;
     QDate date;
 #ifndef QT_BOOTSTRAPPED
-    QDateTimeParser dt(QVariant::DateTime, QDateTimeParser::FromString);
+    QDateTimeParser dt(QVariant::DateTime);
     dt.defaultLocale = *this;
     if (Q_LIKELY(dt.parseFormat(format)))
         dt.fromString(string, &date, &time);