forked from qt-creator/qt-creator
Utils: Port ClassNameValidatingLineEdit to QRegularExpression
Task-number: 24098 Change-Id: Ifeae0a864455e99ec7d358d293507072abdf9057 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -28,7 +28,7 @@
|
|||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QRegExp>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\class Utils::ClassNameValidatingLineEdit
|
\class Utils::ClassNameValidatingLineEdit
|
||||||
@@ -40,22 +40,17 @@
|
|||||||
|
|
||||||
namespace Utils {
|
namespace Utils {
|
||||||
|
|
||||||
struct ClassNameValidatingLineEditPrivate {
|
// Match something like "Namespace1::Namespace2::ClassName".
|
||||||
ClassNameValidatingLineEditPrivate();
|
|
||||||
|
|
||||||
QRegExp m_nameRegexp;
|
struct ClassNameValidatingLineEditPrivate
|
||||||
QString m_namespaceDelimiter;
|
{
|
||||||
|
QRegularExpression m_nameRegexp;
|
||||||
|
QString m_namespaceDelimiter{"::"};
|
||||||
bool m_namespacesEnabled = false;
|
bool m_namespacesEnabled = false;
|
||||||
bool m_lowerCaseFileName = true;
|
bool m_lowerCaseFileName = true;
|
||||||
bool m_forceFirstCapitalLetter = false;
|
bool m_forceFirstCapitalLetter = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Match something like "Namespace1::Namespace2::ClassName".
|
|
||||||
ClassNameValidatingLineEditPrivate:: ClassNameValidatingLineEditPrivate() :
|
|
||||||
m_namespaceDelimiter(QLatin1String("::"))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------- ClassNameValidatingLineEdit
|
// --------------------- ClassNameValidatingLineEdit
|
||||||
ClassNameValidatingLineEdit::ClassNameValidatingLineEdit(QWidget *parent) :
|
ClassNameValidatingLineEdit::ClassNameValidatingLineEdit(QWidget *parent) :
|
||||||
FancyLineEdit(parent),
|
FancyLineEdit(parent),
|
||||||
@@ -112,7 +107,7 @@ bool ClassNameValidatingLineEdit::validateClassName(FancyLineEdit *edit, QString
|
|||||||
if (errorMessage)
|
if (errorMessage)
|
||||||
*errorMessage = tr("Please enter a class name.");
|
*errorMessage = tr("Please enter a class name.");
|
||||||
return false;
|
return false;
|
||||||
} else if (!d->m_nameRegexp.exactMatch(value)) {
|
} else if (!d->m_nameRegexp.match(value).hasMatch()) {
|
||||||
if (errorMessage)
|
if (errorMessage)
|
||||||
*errorMessage = tr("The class name contains invalid characters.");
|
*errorMessage = tr("The class name contains invalid characters.");
|
||||||
return false;
|
return false;
|
||||||
@@ -148,25 +143,26 @@ QString ClassNameValidatingLineEdit::fixInputString(const QString &string)
|
|||||||
|
|
||||||
void ClassNameValidatingLineEdit::updateRegExp() const
|
void ClassNameValidatingLineEdit::updateRegExp() const
|
||||||
{
|
{
|
||||||
const QString pattern(QLatin1String("%1(%2%1)*"));
|
const QString pattern = "^%1(%2%1)*$";
|
||||||
d->m_nameRegexp.setPattern(pattern.arg(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]*"))
|
d->m_nameRegexp.setPattern(pattern.arg("[a-zA-Z_][a-zA-Z0-9_]*")
|
||||||
.arg(QRegExp::escape(d->m_namespaceDelimiter)));
|
.arg(QRegularExpression::escape(d->m_namespaceDelimiter)));
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ClassNameValidatingLineEdit::createClassName(const QString &name)
|
QString ClassNameValidatingLineEdit::createClassName(const QString &name)
|
||||||
{
|
{
|
||||||
// Remove spaces and convert the adjacent characters to uppercase
|
// Remove spaces and convert the adjacent characters to uppercase
|
||||||
QString className = name;
|
QString className = name;
|
||||||
QRegExp spaceMatcher(QLatin1String(" +(\\w)"), Qt::CaseSensitive, QRegExp::RegExp2);
|
const QRegularExpression spaceMatcher(" +(\\w)");
|
||||||
QTC_CHECK(spaceMatcher.isValid());
|
QTC_CHECK(spaceMatcher.isValid());
|
||||||
int pos;
|
while (true) {
|
||||||
while ((pos = spaceMatcher.indexIn(className)) != -1) {
|
const QRegularExpressionMatch match = spaceMatcher.match(className);
|
||||||
className.replace(pos, spaceMatcher.matchedLength(),
|
if (!match.hasMatch())
|
||||||
spaceMatcher.cap(1).toUpper());
|
break;
|
||||||
|
className.replace(match.capturedStart(), match.capturedLength(), match.captured(1).toUpper());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out any remaining invalid characters
|
// Filter out any remaining invalid characters
|
||||||
className.remove(QRegExp(QLatin1String("[^a-zA-Z0-9_]")));
|
className.remove(QRegularExpression("[^a-zA-Z0-9_]"));
|
||||||
|
|
||||||
// If the first character is numeric, prefix the name with a "_"
|
// If the first character is numeric, prefix the name with a "_"
|
||||||
if (className.at(0).isNumber()) {
|
if (className.at(0).isNumber()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user