QValidator: return State::Intermediate for certain trailing characters

E.g. a group separator, a -/+ sign, or 'e' (exponent). Input ending with
one of these characters now returns Intermediate, as it can become
Acceptable if the user types more characters.

Remove a check from initialResultCheck(), as it's now covered by
QLocaleData::validateChars() checking that the last character in the
buffer is -/+, this works the same if buffer's size is 1.

Extended unittests based on the linked bug report; the other cases for
"last" are already covered by existing unittests.

Task-number: QTBUG-111371
Change-Id: I9b6979c29f07a5f57b040004cd3dbf4e27147c21
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Ahmad Samir 2023-03-18 11:58:05 +02:00
parent 3d284d9b8d
commit f107773742
3 changed files with 8 additions and 4 deletions

View File

@ -4297,6 +4297,12 @@ QLocaleData::validateChars(QStringView str, NumberMode numMode, int decDigits,
}
result.state = ParsingResult::Acceptable;
// Intermediate if it ends with any character that requires a digit after
// it to be valid e.g. group separator, sign, or exponent
if (last == ',' || last == '-' || last == '+' || last == 'e')
result.state = ParsingResult::Intermediate;
return result;
}

View File

@ -377,9 +377,6 @@ std::optional<QValidator::State> initialResultCheck(T min, T max, const ParsingR
if (signConflicts)
return QValidator::Invalid;
if (buff.size() == 1 && (ch == '-' || ch == '+'))
return QValidator::Intermediate;
if (result.state == ParsingResult::Intermediate)
return QValidator::Intermediate;

View File

@ -167,7 +167,8 @@ void tst_QIntValidator::validateFrench()
int i;
// Grouping separator is a narrow no-break space; QLocale accepts a space as it.
QString s = QLatin1String("1 ");
QCOMPARE(validator.validate(s, i), QValidator::Acceptable);
// Shouldn't end with a group separator
QCOMPARE(validator.validate(s, i), QValidator::Intermediate);
validator.fixup(s);
QCOMPARE(s, s);