QValidator: de-duplicate some code

QDoubleValidator didn't return Intermediate if the buffer only had one
character, - or +, but it makes sense to check that there too.

In a later commit that check will be moved to QLocaleData::validateChars
(which will return "Intermediate" if the last character in the result
buffer is -/+, in this case it's the last and only character in the
buffer).

Change-Id: I2f9f5b92880b7e9cc1a3ab36b5ec322f57291ee9
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
bb10
Ahmad Samir 2023-04-24 21:16:33 +02:00
parent 9d4cd4a63e
commit 6bb61f7e89
1 changed files with 24 additions and 22 deletions

View File

@ -362,6 +362,23 @@ static qlonglong pow10(int exp)
return result;
}
template <typename T> static inline
std::optional<QValidator::State> initialResultCheck(T min, T max, const QByteArray &buff)
{
if (buff.isEmpty())
return QValidator::Intermediate;
char ch = buff[0];
const bool signConflicts = (min >= 0 && ch == '-') || (max < 0 && ch == '+');
if (signConflicts)
return QValidator::Invalid;
if (buff.size() == 1 && (ch == '-' || ch == '+'))
return QValidator::Intermediate;
return std::nullopt;
}
QValidator::State QIntValidator::validate(QString & input, int&) const
{
QByteArray buff;
@ -370,19 +387,9 @@ QValidator::State QIntValidator::validate(QString & input, int&) const
return Invalid;
}
if (buff.isEmpty())
return Intermediate;
const bool startsWithMinus(buff[0] == '-');
if (b >= 0 && startsWithMinus)
return Invalid;
const bool startsWithPlus(buff[0] == '+');
if (t < 0 && startsWithPlus)
return Invalid;
if (buff.size() == 1 && (startsWithPlus || startsWithMinus))
return Intermediate;
std::optional<QValidator::State> opt = initialResultCheck(b, t, buff);
if (opt)
return *opt;
bool ok;
qlonglong entered = QLocaleData::bytearrayToLongLong(buff, 10, &ok);
@ -401,7 +408,7 @@ QValidator::State QIntValidator::validate(QString & input, int&) const
// of a number of digits equal to or less than the max value as intermediate.
int buffLength = buff.size();
if (startsWithPlus)
if (buff[0] == '+')
buffLength--;
const int tLength = t != 0 ? static_cast<int>(std::log10(qAbs(t))) + 1 : 1;
@ -651,14 +658,9 @@ QValidator::State QDoubleValidatorPrivate::validateWithLocale(QString &input, QL
return QValidator::Invalid;
}
if (buff.isEmpty())
return QValidator::Intermediate;
if (q->b >= 0 && buff.startsWith('-'))
return QValidator::Invalid;
if (q->t < 0 && buff.startsWith('+'))
return QValidator::Invalid;
std::optional<QValidator::State> opt = initialResultCheck(q->b, q->t, buff);
if (opt)
return *opt;
bool ok = false;
double i = locale.toDouble(input, &ok); // returns 0.0 if !ok