From 81ad5bc838538084d14e4627ad405262026b72a2 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 26 Sep 2017 17:14:24 +0200 Subject: Tidy up QDateTimeParser code to make it easier to reason about Note that the relevant cases are all numeric, eliminate a redundant variable (the min of two others, one of which was provably <= the other), invert and rename a boolean (that was always used negated), eliminate a case that couldn't arise (and assert this). Change-Id: I9ef9cedbeb608c7cd56ddc618ddfb921966edfbf Reviewed-by: Thiago Macieira --- src/corelib/tools/qdatetimeparser.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qdatetimeparser.cpp b/src/corelib/tools/qdatetimeparser.cpp index ff62eab4cc..f0c8b5a799 100644 --- a/src/corelib/tools/qdatetimeparser.cpp +++ b/src/corelib/tools/qdatetimeparser.cpp @@ -794,6 +794,7 @@ QDateTimeParser::parseSection(const QDateTime ¤tValue, int sectionIndex, break; } Q_FALLTHROUGH(); + // All numeric: case DaySection: case YearSection: case YearSection2Digits: @@ -816,9 +817,9 @@ QDateTimeParser::parseSection(const QDateTime ¤tValue, int sectionIndex, bool ok = true; int last = -1, used = -1; - const int max = qMin(sectionmaxsize, sectiontextSize); - QStringRef digitsStr = sectionTextRef.left(max); - for (int digits = max; digits >= 1; --digits) { + Q_ASSERT(sectiontextSize <= sectionmaxsize); + QStringRef digitsStr = sectionTextRef.left(sectiontextSize); + for (int digits = sectiontextSize; digits >= 1; --digits) { digitsStr.truncate(digits); int tmp = (int)loc.toUInt(digitsStr, &ok); if (ok && sn.type == Hour12Section) { @@ -845,20 +846,20 @@ QDateTimeParser::parseSection(const QDateTime ¤tValue, int sectionIndex, QDTPDEBUG << "invalid because" << sectionTextRef << "can't become a uint" << last << ok; } else { const FieldInfo fi = fieldInfo(sectionIndex); - const bool done = (used == sectionmaxsize); - if (!done && fi & Fraction) { // typing 2 in a zzz field should be .200, not .002 + const bool unfilled = used < sectionmaxsize; + if (unfilled && fi & Fraction) { // typing 2 in a zzz field should be .200, not .002 for (int i = used; i < sectionmaxsize; ++i) last *= 10; } + // Even those *= 10s can't take last above absMax: + Q_ASSERT(last <= absMax); const int absMin = absoluteMin(sectionIndex); if (last < absMin) { - if (!done) // reversed test to dodge QDTPDEBUG ugliness ! + if (unfilled) result = ParsedSection(Intermediate, last, used); else QDTPDEBUG << "invalid because" << last << "is less than absoluteMin" << absMin; - } else if (last > absMax) { - result = ParsedSection(Intermediate, last, used); - } else if (!done && (fi & (FixedWidth|Numeric)) == (FixedWidth|Numeric)) { + } else if (unfilled && (fi & (FixedWidth|Numeric)) == (FixedWidth|Numeric)) { if (skipToNextSection(sectionIndex, currentValue, digitsStr)) { const int missingZeroes = sectionmaxsize - digitsStr.size(); result = ParsedSection(Acceptable, last, sectionmaxsize, missingZeroes); -- cgit v1.2.3