From f0b5f8121f6dc7e05140839db2bdb21919fa7a45 Mon Sep 17 00:00:00 2001 From: Johan Ouwerkerk Date: Wed, 2 Oct 2019 21:31:07 +0200 Subject: [PATCH] Refactor: extract strip_spaces() utility function. --- src/validators/CMakeLists.txt | 1 + src/validators/secretvalidator.cpp | 11 +++-------- src/validators/util.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/validators/util.h | 29 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 src/validators/util.cpp create mode 100644 src/validators/util.h diff --git a/src/validators/CMakeLists.txt b/src/validators/CMakeLists.txt index fda744b..b73f52d 100644 --- a/src/validators/CMakeLists.txt +++ b/src/validators/CMakeLists.txt @@ -7,6 +7,7 @@ set(validator_SRCS qmlsupport.cpp namevalidator.cpp secretvalidator.cpp + util.cpp ) add_library(validator_lib STATIC ${validator_SRCS}) diff --git a/src/validators/secretvalidator.cpp b/src/validators/secretvalidator.cpp index 41b83fd..755092b 100644 --- a/src/validators/secretvalidator.cpp +++ b/src/validators/secretvalidator.cpp @@ -18,6 +18,8 @@ #include "secretvalidator.h" +#include "util.h" + #include #include @@ -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); diff --git a/src/validators/util.cpp b/src/validators/util.cpp new file mode 100644 index 0000000..644727a --- /dev/null +++ b/src/validators/util.cpp @@ -0,0 +1,38 @@ +/***************************************************************************** + * Copyright: 2019 Johan Ouwerkerk * + * * + * 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 . * + * * + ****************************************************************************/ + +#include "util.h" + +#include + +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("")); + } +} diff --git a/src/validators/util.h b/src/validators/util.h new file mode 100644 index 0000000..a31f237 --- /dev/null +++ b/src/validators/util.h @@ -0,0 +1,29 @@ +/***************************************************************************** + * Copyright: 2019 Johan Ouwerkerk * + * * + * 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 . * + * * + ****************************************************************************/ + +#ifndef VALIDATOR_UTIL_H +#define VALIDATOR_UTIL_H + +#include + +namespace validators +{ + QString strip_spaces(QString &input); +} + +#endif