File indexing completed on 2024-04-28 05:50:09

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2020 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 
0006 #include "issuervalidator.h"
0007 #include "util.h"
0008 
0009 #include <QRegularExpression>
0010 #include <QString>
0011 
0012 static const QRegularExpression& match_pattern(void)
0013 {
0014     /*
0015      * Pattern to check that issuer names:
0016      *
0017      *  - do not contain colons
0018      *  - start and end with a character which is not whitespace
0019      *  - do not contain any other whitespace besides spaces
0020      *  - have at most one space between non-whitespace characters
0021      */
0022     static const QRegularExpression re(QLatin1String("^[^\\s:]+( [^\\s:]+)*$"));
0023     re.optimize();
0024     return re;
0025 }
0026 
0027 namespace validators
0028 {
0029     IssuerValidator::IssuerValidator(QObject *parent):
0030         QValidator(parent),
0031         m_pattern(match_pattern())
0032     {
0033     }
0034 
0035     void IssuerValidator::fixup(QString &input) const
0036     {
0037         input = validators::simplify_spaces(input.remove(QLatin1Char(':')));
0038     }
0039 
0040     QValidator::State IssuerValidator::validate(QString &input, int &cursor) const
0041     {
0042         return input.isEmpty() ? QValidator::Acceptable : m_pattern.validate(input, cursor);
0043     }
0044 }