summaryrefslogtreecommitdiffstats
path: root/examples/serialbus/can/sendframebox.cpp
diff options
context:
space:
mode:
authorEvgeny Shtanov <shtanov_evgenii@mail.ru>2021-08-14 00:55:37 +0300
committerEvgeny Shtanov <shtanov_evgenii@mail.ru>2021-08-24 11:53:05 +0300
commit612fe9453a40a89bb2517400ce7ddaaf486e1ac6 (patch)
tree1092c9679b0329d5b396d6f7c12e4ec2a31b62fd /examples/serialbus/can/sendframebox.cpp
parentad53e8027a8691f0663e49e79ba23680720adc2d (diff)
CAN example, HexStringValidator: don't allow user to enter extra spaces
Fixes: QTBUG-95801 Change-Id: I3ec19aae58b42450470000e6380c5d41422bd034 Reviewed-by: André Hartmann <aha_1980@gmx.de>
Diffstat (limited to 'examples/serialbus/can/sendframebox.cpp')
-rw-r--r--examples/serialbus/can/sendframebox.cpp28
1 files changed, 24 insertions, 4 deletions
diff --git a/examples/serialbus/can/sendframebox.cpp b/examples/serialbus/can/sendframebox.cpp
index 3e1e2bb..9046add 100644
--- a/examples/serialbus/can/sendframebox.cpp
+++ b/examples/serialbus/can/sendframebox.cpp
@@ -134,13 +134,14 @@ QValidator::State HexStringValidator::validate(QString &input, int &pos) const
const int maxSize = 2 * m_maxLength;
const QChar space = QLatin1Char(' ');
QString data = input;
+
data.remove(space);
if (data.isEmpty())
return Intermediate;
- // limit maximum size and forbid trailing spaces
- if ((data.size() > maxSize) || (data.size() == maxSize && input.endsWith(space)))
+ // limit maximum size
+ if (data.size() > maxSize)
return Invalid;
// check if all input is valid
@@ -148,13 +149,32 @@ QValidator::State HexStringValidator::validate(QString &input, int &pos) const
if (!re.match(data).hasMatch())
return Invalid;
+ // don't allow user to enter more than one space
+ const QRegularExpression twoSpaces(QStringLiteral("([\\s]{2})"));
+ if (twoSpaces.match(input).hasMatch()) {
+ const QRegularExpressionMatch match = twoSpaces.match(input);
+ input.replace(match.capturedStart(), 2, ' ');
+ pos = match.capturedEnd() - 1;
+ }
+
// insert a space after every two hex nibbles
const QRegularExpression threeDigits(THREE_DIGITS);
while (threeDigits.match(input).hasMatch()) {
const QRegularExpressionMatch match = threeDigits.match(input);
- input.insert(match.capturedEnd() - 1, space);
- pos = match.capturedEnd() + 1;
+ if (pos == match.capturedStart() + 1) {
+ // add one hex nibble before two - Abc
+ input.insert(match.capturedStart() + 1, space);
+ pos = match.capturedStart() + 1;
+ } else if (pos == match.capturedStart() + 2) {
+ // add hex nibble in the middle - aBc
+ input.insert(match.capturedEnd() - 1, space);
+ pos = match.capturedEnd();
+ } else {
+ // add one hex nibble after two - abC
+ input.insert(match.capturedEnd() - 1, space);
+ pos = match.capturedEnd() + 1;
+ }
}
return Acceptable;