summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEdward Welbourne <edward.welbourne@qt.io>2017-09-26 17:14:24 +0200
committerEdward Welbourne <edward.welbourne@qt.io>2017-11-30 20:19:35 +0000
commit81ad5bc838538084d14e4627ad405262026b72a2 (patch)
tree73f5c3453d8b9461c2108f01f79854af600ed93e /src
parentb4ef4469ba0a47eb5f2b395f9934a0d41139a85d (diff)
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r--src/corelib/tools/qdatetimeparser.cpp19
1 files changed, 10 insertions, 9 deletions
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 &currentValue, int sectionIndex,
break;
}
Q_FALLTHROUGH();
+ // All numeric:
case DaySection:
case YearSection:
case YearSection2Digits:
@@ -816,9 +817,9 @@ QDateTimeParser::parseSection(const QDateTime &currentValue, 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 &currentValue, 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);