Refactor: extract strip_spaces() utility function.

master
Johan Ouwerkerk 2019-10-02 21:31:07 +02:00
parent 668e912dfe
commit f0b5f8121f
4 changed files with 71 additions and 8 deletions

View File

@ -7,6 +7,7 @@ set(validator_SRCS
qmlsupport.cpp
namevalidator.cpp
secretvalidator.cpp
util.cpp
)
add_library(validator_lib STATIC ${validator_SRCS})

View File

@ -18,6 +18,8 @@
#include "secretvalidator.h"
#include "util.h"
#include <QRegularExpression>
#include <QString>
@ -38,13 +40,6 @@ static QValidator::State check_padding(int length)
}
}
static QString strip_spaces(QString &input)
{
static const QRegularExpression re("\\s*");
re.optimize();
return input.replace(re, QLatin1String(""));
}
static const QRegularExpression& match_pattern(void)
{
static const QRegularExpression re(QLatin1String("^[A-Za-z2-7]+=*$"));
@ -62,7 +57,7 @@ namespace validators
void Base32Validator::fixup(QString &input) const
{
input = strip_spaces(input);
input = validators::strip_spaces(input);
input = input.toUpper();
m_pattern.fixup(input);

38
src/validators/util.cpp Normal file
View File

@ -0,0 +1,38 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This project is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#include "util.h"
#include <QRegularExpression>
QRegularExpression spaces_patern(void)
{
static const QRegularExpression re("\\s*");
re.optimize();
return re;
}
namespace validators
{
QString strip_spaces(QString &input)
{
static const QRegularExpression re = spaces_patern();
return input.replace(re, QLatin1String(""));
}
}

29
src/validators/util.h Normal file
View File

@ -0,0 +1,29 @@
/*****************************************************************************
* Copyright: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com> *
* *
* This project is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This project is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#ifndef VALIDATOR_UTIL_H
#define VALIDATOR_UTIL_H
#include <QString>
namespace validators
{
QString strip_spaces(QString &input);
}
#endif