From 3b310a3ec13a743ae2258bc43d92a5df42c6ad37 Mon Sep 17 00:00:00 2001 From: Arttu Tarkiainen Date: Fri, 14 Jun 2019 16:15:16 +0300 Subject: Add regular expression support to replace operation Introduce search with regular expressions support to replace operation. Add unit tests and make associated documentation changes. Task-number: QTIFW-889 Change-Id: I82e30056030ebc900be49046fda1903b27a2824d Reviewed-by: Katja Marttila --- src/libs/installer/replaceoperation.cpp | 38 +++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/libs/installer') diff --git a/src/libs/installer/replaceoperation.cpp b/src/libs/installer/replaceoperation.cpp index 8cc1e0315..692bba715 100644 --- a/src/libs/installer/replaceoperation.cpp +++ b/src/libs/installer/replaceoperation.cpp @@ -31,6 +31,7 @@ #include #include #include +#include using namespace QInstaller; @@ -46,17 +47,45 @@ void ReplaceOperation::backup() bool ReplaceOperation::performOperation() { + static const QLatin1String stringMode("string"); + static const QLatin1String regexMode("regex"); + // Arguments: // 1. filename // 2. Source-String // 3. Replace-String - if (!checkArgumentCount(3)) + // 4. mode=string|regex + + // Calling with three arguments defaults to string search, + // this provides backward compatibility with the old syntax. + if (!checkArgumentCount(3, 4)) return false; const QStringList args = arguments(); const QString fileName = args.at(0); const QString before = args.at(1); const QString after = args.at(2); + QString mode = args.value(3); + + if (mode.isEmpty()) + mode = stringMode; + + if (before.isEmpty()) { + setError(InvalidArguments); + setErrorString(tr("Current search argument calling \"%1\" with " + "empty search argument is not supported.").arg(name())); + + return false; + } + + if (!(mode == stringMode || mode == regexMode)) { + setError(InvalidArguments); + setErrorString(tr("Current mode argument calling \"%1\" with " + "arguments \"%2\" is not supported. Please use string or regex.") + .arg(name(), arguments().join(QLatin1String("; ")))); + + return false; + } QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { @@ -78,7 +107,12 @@ bool ReplaceOperation::performOperation() } stream.setDevice(&file); - stream << replacedFileContent.replace(before, after); + if (mode == regexMode) { + QRegularExpression regex(before); + stream << replacedFileContent.replace(regex, after); + } else if (mode == stringMode) { + stream << replacedFileContent.replace(before, after); + } file.close(); return true; -- cgit v1.2.3