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

0001 /*
0002  * SPDX-License-Identifier: GPL-3.0-or-later
0003  * SPDX-FileCopyrightText: 2019 Johan Ouwerkerk <jm.ouwerkerk@gmail.com>
0004  */
0005 
0006 #include "util.h"
0007 
0008 #include <QRegularExpression>
0009 
0010 QRegularExpression spaces_pattern(void)
0011 {
0012     static const QRegularExpression re(QStringLiteral("\\s*"));
0013     re.optimize();
0014     return re;
0015 }
0016 
0017 namespace validators
0018 {
0019 
0020     QString simplify_spaces(QString &input)
0021     {
0022         QString fixed = input.simplified();
0023 
0024         // make sure the user can type in at least one space
0025         if (input.endsWith(QLatin1Char(' ')) && fixed.size() > 0) {
0026             fixed += QLatin1Char(' ');
0027         }
0028 
0029         return fixed;
0030     }
0031 
0032     QString strip_spaces(QString &input)
0033     {
0034         static const QRegularExpression re = spaces_pattern();
0035         return input.replace(re, QString());
0036     }
0037 }