Instantiate QRegularExpression instances statically

This commit is contained in:
Ilya Fedin
2023-12-22 03:50:04 +04:00
committed by John Preston
parent 4b297bfa09
commit e6b9a07163
18 changed files with 74 additions and 47 deletions
@@ -152,11 +152,12 @@ EditDocumentScheme GetDocumentScheme(
};
using Result = std::optional<QString>;
const auto NameValidate = [](const QString &value) -> Result {
static const auto RegExp = QRegularExpression(
"^[a-zA-Z0-9\\.,/&\\-' ]+$"
);
if (value.isEmpty() || value.size() > kMaxNameSize) {
return QString();
} else if (!QRegularExpression(
"^[a-zA-Z0-9\\.,/&\\-' ]+$"
).match(value).hasMatch()) {
} else if (!RegExp.match(value).hasMatch()) {
return tr::lng_passport_bad_name(tr::now);
}
return std::nullopt;
@@ -167,14 +168,16 @@ EditDocumentScheme GetDocumentScheme(
const auto StreetValidate = LimitedValidate(kMaxStreetSize);
const auto CityValidate = LimitedValidate(kMaxCitySize, kMinCitySize);
const auto PostcodeValidate = FromBoolean([](const QString &value) {
return QRegularExpression(
static const auto RegExp = QRegularExpression(
QString("^[a-zA-Z0-9\\-]{2,%1}$").arg(kMaxPostcodeSize)
).match(value).hasMatch();
);
return RegExp.match(value).hasMatch();
});
const auto DateValidateBoolean = [](const QString &value) {
return QRegularExpression(
static const auto RegExp = QRegularExpression(
"^\\d{2}\\.\\d{2}\\.\\d{4}$"
).match(value).hasMatch();
);
return RegExp.match(value).hasMatch();
};
const auto DateValidate = FromBoolean(DateValidateBoolean);
const auto DateOrEmptyValidate = FromBoolean([=](const QString &value) {
@@ -479,9 +482,8 @@ EditContactScheme GetContactScheme(Scope::Type type) {
result.newHeader = tr::lng_passport_new_phone(tr::now);
result.aboutNew = tr::lng_passport_new_phone_code(tr::now);
result.validate = [](const QString &value) {
return QRegularExpression(
"^\\d{2,12}$"
).match(value).hasMatch();
static const auto RegExp = QRegularExpression("^\\d{2,12}$");
return RegExp.match(value).hasMatch();
};
result.format = [](const QString &value) {
return Ui::FormatPhone(value);
@@ -51,7 +51,8 @@ PostcodeInput::PostcodeInput(
rpl::producer<QString> placeholder,
const QString &val)
: MaskedInputField(parent, st, std::move(placeholder), val) {
if (!QRegularExpression("^[a-zA-Z0-9\\-]+$").match(val).hasMatch()) {
static const auto RegExp = QRegularExpression("^[a-zA-Z0-9\\-]+$");
if (!RegExp.match(val).hasMatch()) {
setText(QString());
}
}
@@ -414,8 +415,9 @@ void CountryRow::chooseCountry() {
}
QDate ValidateDate(const QString &value) {
const auto match = QRegularExpression(
"^([0-9]{2})\\.([0-9]{2})\\.([0-9]{4})$").match(value);
static const auto RegExp = QRegularExpression(
"^([0-9]{2})\\.([0-9]{2})\\.([0-9]{4})$");
const auto match = RegExp.match(value);
if (!match.hasMatch()) {
return QDate();
}