2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "filesearch.h"
|
2009-03-13 18:31:21 +01:00
|
|
|
#include <cctype>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QRegExp>
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
#include <QTextCodec>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-09 09:35:03 +01:00
|
|
|
#include "runextensions.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-10-05 11:06:05 +02:00
|
|
|
using namespace Utils;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2009-04-30 16:09:57 +02:00
|
|
|
static inline QString msgCanceled(const QString &searchTerm, int numMatches, int numFilesSearched)
|
|
|
|
|
{
|
2009-10-05 11:06:05 +02:00
|
|
|
return QCoreApplication::translate("Utils::FileSearch",
|
2009-04-30 16:09:57 +02:00
|
|
|
"%1: canceled. %n occurrences found in %2 files.",
|
2014-08-28 17:33:47 +02:00
|
|
|
0, numMatches).arg(searchTerm).arg(numFilesSearched);
|
2009-04-30 16:09:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline QString msgFound(const QString &searchTerm, int numMatches, int numFilesSearched)
|
|
|
|
|
{
|
2009-10-05 11:06:05 +02:00
|
|
|
return QCoreApplication::translate("Utils::FileSearch",
|
2009-04-30 16:09:57 +02:00
|
|
|
"%1: %n occurrences found in %2 files.",
|
2014-08-28 17:33:47 +02:00
|
|
|
0, numMatches).arg(searchTerm).arg(numFilesSearched);
|
2009-04-30 16:09:57 +02:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace {
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2011-10-27 14:25:18 +02:00
|
|
|
const int MAX_LINE_SIZE = 400;
|
|
|
|
|
|
|
|
|
|
QString clippedText(const QString &text, int maxLength)
|
|
|
|
|
{
|
|
|
|
|
if (text.length() > maxLength)
|
|
|
|
|
return text.left(maxLength) + QChar(0x2026); // '...'
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
void runFileSearch(QFutureInterface<FileSearchResultList> &future,
|
2008-12-02 14:09:21 +01:00
|
|
|
QString searchTerm,
|
2010-06-24 15:44:46 +02:00
|
|
|
FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags,
|
|
|
|
|
QMap<QString, QString> fileToContentsMap)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
|
int numFilesSearched = 0;
|
|
|
|
|
int numMatches = 0;
|
2010-06-25 10:18:44 +02:00
|
|
|
future.setProgressRange(0, files->maxProgress());
|
|
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2010-10-19 08:55:55 +02:00
|
|
|
const bool caseInsensitive = !(flags & QTextDocument::FindCaseSensitively);
|
|
|
|
|
const bool wholeWord = (flags & QTextDocument::FindWholeWords);
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2010-10-11 11:34:17 +02:00
|
|
|
const QString searchTermLower = searchTerm.toLower();
|
|
|
|
|
const QString searchTermUpper = searchTerm.toUpper();
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2010-10-19 08:55:55 +02:00
|
|
|
const int termLength = searchTerm.length();
|
|
|
|
|
const int termMaxIndex = termLength - 1;
|
2010-10-11 11:34:17 +02:00
|
|
|
const QChar *termData = searchTerm.constData();
|
|
|
|
|
const QChar *termDataLower = searchTermLower.constData();
|
|
|
|
|
const QChar *termDataUpper = searchTermUpper.constData();
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2009-08-05 12:43:47 +02:00
|
|
|
QFile file;
|
2010-10-11 11:34:17 +02:00
|
|
|
QString str;
|
|
|
|
|
QTextStream stream;
|
2010-06-25 10:18:44 +02:00
|
|
|
FileSearchResultList results;
|
2010-06-24 15:44:46 +02:00
|
|
|
while (files->hasNext()) {
|
|
|
|
|
const QString &s = files->next();
|
2008-12-02 14:09:21 +01:00
|
|
|
if (future.isPaused())
|
|
|
|
|
future.waitForResume();
|
|
|
|
|
if (future.isCanceled()) {
|
2010-06-24 15:44:46 +02:00
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgCanceled(searchTerm, numMatches, numFilesSearched));
|
2008-12-02 14:09:21 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2010-10-11 11:34:17 +02:00
|
|
|
|
|
|
|
|
bool needsToCloseFile = false;
|
2009-08-05 12:43:47 +02:00
|
|
|
if (fileToContentsMap.contains(s)) {
|
2010-10-11 11:34:17 +02:00
|
|
|
str = fileToContentsMap.value(s);
|
|
|
|
|
stream.setString(&str);
|
2009-08-05 12:43:47 +02:00
|
|
|
} else {
|
|
|
|
|
file.setFileName(s);
|
2010-10-11 11:34:17 +02:00
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
|
continue;
|
|
|
|
|
needsToCloseFile = true;
|
|
|
|
|
stream.setDevice(&file);
|
|
|
|
|
stream.setCodec(files->encoding());
|
2009-08-05 12:43:47 +02:00
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2010-10-14 19:19:40 +02:00
|
|
|
int lineNr = 0;
|
2010-10-11 11:34:17 +02:00
|
|
|
while (!stream.atEnd()) {
|
2010-10-14 19:19:40 +02:00
|
|
|
++lineNr;
|
|
|
|
|
const QString chunk = stream.readLine();
|
2011-10-27 14:25:18 +02:00
|
|
|
const QString resultItemText = clippedText(chunk, MAX_LINE_SIZE);
|
2010-10-11 11:34:17 +02:00
|
|
|
int chunkLength = chunk.length();
|
|
|
|
|
const QChar *chunkPtr = chunk.constData();
|
2010-10-14 19:19:40 +02:00
|
|
|
const QChar *chunkEnd = chunkPtr + chunkLength - 1;
|
|
|
|
|
for (const QChar *regionPtr = chunkPtr;
|
|
|
|
|
regionPtr + termMaxIndex <= chunkEnd;
|
2010-10-11 11:34:17 +02:00
|
|
|
++regionPtr) {
|
|
|
|
|
const QChar *regionEnd = regionPtr + termMaxIndex;
|
2010-10-14 19:19:40 +02:00
|
|
|
if ( /* optimization check for start and end of region */
|
2008-12-02 14:09:21 +01:00
|
|
|
// case sensitive
|
2010-10-11 11:34:17 +02:00
|
|
|
(!caseInsensitive && *regionPtr == termData[0] && *regionEnd == termData[termMaxIndex])
|
2008-12-02 14:09:21 +01:00
|
|
|
||
|
|
|
|
|
// case insensitive
|
2010-10-11 11:34:17 +02:00
|
|
|
(caseInsensitive && (*regionPtr == termDataLower[0] || *regionPtr == termDataUpper[0])
|
|
|
|
|
&& (*regionEnd == termDataLower[termMaxIndex] || *regionEnd == termDataUpper[termMaxIndex]))
|
2008-12-02 14:09:21 +01:00
|
|
|
) {
|
|
|
|
|
bool equal = true;
|
2010-10-11 11:34:17 +02:00
|
|
|
|
|
|
|
|
// whole word check
|
|
|
|
|
const QChar *beforeRegion = regionPtr - 1;
|
|
|
|
|
const QChar *afterRegion = regionEnd + 1;
|
|
|
|
|
if (wholeWord && (
|
|
|
|
|
((beforeRegion >= chunkPtr) && (beforeRegion->isLetterOrNumber() || ((*beforeRegion) == QLatin1Char('_')))) ||
|
2010-10-14 19:19:40 +02:00
|
|
|
((afterRegion <= chunkEnd) && (afterRegion->isLetterOrNumber() || ((*afterRegion) == QLatin1Char('_'))))
|
2010-10-11 11:34:17 +02:00
|
|
|
)) {
|
2008-12-02 14:09:21 +01:00
|
|
|
equal = false;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-10-11 11:34:17 +02:00
|
|
|
if (equal) {
|
|
|
|
|
// check all chars
|
|
|
|
|
int regionIndex = 1;
|
|
|
|
|
for (const QChar *regionCursor = regionPtr + 1; regionCursor < regionEnd; ++regionCursor, ++regionIndex) {
|
|
|
|
|
if ( // case sensitive
|
|
|
|
|
(!caseInsensitive && *regionCursor != termData[regionIndex])
|
|
|
|
|
||
|
|
|
|
|
// case insensitive
|
|
|
|
|
(caseInsensitive && *regionCursor != termData[regionIndex]
|
|
|
|
|
&& *regionCursor != termDataLower[regionIndex] && *regionCursor != termDataUpper[regionIndex])
|
|
|
|
|
) {
|
|
|
|
|
equal = false;
|
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
}
|
|
|
|
|
if (equal) {
|
2011-10-27 14:25:18 +02:00
|
|
|
results << FileSearchResult(s, lineNr, resultItemText,
|
2010-10-14 19:19:40 +02:00
|
|
|
regionPtr - chunkPtr, termLength,
|
|
|
|
|
QStringList());
|
2013-06-13 16:37:14 +02:00
|
|
|
regionPtr += termLength - 1; // another +1 done by for-loop
|
2010-10-14 19:19:40 +02:00
|
|
|
++numMatches;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-11 11:34:17 +02:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
++numFilesSearched;
|
2011-03-28 13:35:43 +02:00
|
|
|
if (future.isProgressUpdateNeeded()
|
|
|
|
|
|| future.progressValue() == 0 /*workaround for regression in Qt*/) {
|
2010-06-25 10:18:44 +02:00
|
|
|
if (!results.isEmpty()) {
|
|
|
|
|
future.reportResult(results);
|
|
|
|
|
results.clear();
|
|
|
|
|
}
|
|
|
|
|
future.setProgressRange(0, files->maxProgress());
|
2010-06-24 15:44:46 +02:00
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
|
2010-06-25 10:18:44 +02:00
|
|
|
}
|
2010-10-11 11:34:17 +02:00
|
|
|
|
|
|
|
|
// clean up
|
|
|
|
|
if (needsToCloseFile)
|
|
|
|
|
file.close();
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-06-25 10:18:44 +02:00
|
|
|
if (!results.isEmpty()) {
|
|
|
|
|
future.reportResult(results);
|
|
|
|
|
results.clear();
|
|
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
if (!future.isCanceled())
|
2010-06-24 15:44:46 +02:00
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
|
|
|
|
|
delete files;
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future.isPaused())
|
|
|
|
|
future.waitForResume();
|
2008-12-02 14:09:21 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
void runFileSearchRegExp(QFutureInterface<FileSearchResultList> &future,
|
2008-12-02 14:09:21 +01:00
|
|
|
QString searchTerm,
|
2010-06-24 15:44:46 +02:00
|
|
|
FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags,
|
|
|
|
|
QMap<QString, QString> fileToContentsMap)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
|
int numFilesSearched = 0;
|
|
|
|
|
int numMatches = 0;
|
2010-06-25 10:18:44 +02:00
|
|
|
future.setProgressRange(0, files->maxProgress());
|
|
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
|
2008-12-02 14:09:21 +01:00
|
|
|
if (flags & QTextDocument::FindWholeWords)
|
2009-04-30 16:09:57 +02:00
|
|
|
searchTerm = QString::fromLatin1("\\b%1\\b").arg(searchTerm);
|
|
|
|
|
const Qt::CaseSensitivity caseSensitivity = (flags & QTextDocument::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
2012-04-30 12:01:15 +02:00
|
|
|
QRegExp expression(searchTerm, caseSensitivity);
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2009-08-05 12:43:47 +02:00
|
|
|
QFile file;
|
|
|
|
|
QString str;
|
|
|
|
|
QTextStream stream;
|
2010-06-25 10:18:44 +02:00
|
|
|
FileSearchResultList results;
|
2010-06-24 15:44:46 +02:00
|
|
|
while (files->hasNext()) {
|
|
|
|
|
const QString &s = files->next();
|
2008-12-02 14:09:21 +01:00
|
|
|
if (future.isPaused())
|
|
|
|
|
future.waitForResume();
|
|
|
|
|
if (future.isCanceled()) {
|
2010-06-24 15:44:46 +02:00
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgCanceled(searchTerm, numMatches, numFilesSearched));
|
2008-12-02 14:09:21 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2009-08-05 12:43:47 +02:00
|
|
|
|
|
|
|
|
bool needsToCloseFile = false;
|
|
|
|
|
if (fileToContentsMap.contains(s)) {
|
|
|
|
|
str = fileToContentsMap.value(s);
|
|
|
|
|
stream.setString(&str);
|
|
|
|
|
} else {
|
|
|
|
|
file.setFileName(s);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
|
continue;
|
|
|
|
|
needsToCloseFile = true;
|
|
|
|
|
stream.setDevice(&file);
|
2010-10-11 11:34:17 +02:00
|
|
|
stream.setCodec(files->encoding());
|
2009-08-05 12:43:47 +02:00
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
int lineNr = 1;
|
|
|
|
|
QString line;
|
|
|
|
|
while (!stream.atEnd()) {
|
|
|
|
|
line = stream.readLine();
|
2011-10-27 14:25:18 +02:00
|
|
|
const QString resultItemText = clippedText(line, MAX_LINE_SIZE);
|
2011-06-23 16:53:06 +02:00
|
|
|
int lengthOfLine = line.size();
|
2008-12-02 14:09:21 +01:00
|
|
|
int pos = 0;
|
|
|
|
|
while ((pos = expression.indexIn(line, pos)) != -1) {
|
2011-10-27 14:25:18 +02:00
|
|
|
results << FileSearchResult(s, lineNr, resultItemText,
|
2010-06-11 08:57:38 +02:00
|
|
|
pos, expression.matchedLength(),
|
2010-06-25 09:16:30 +02:00
|
|
|
expression.capturedTexts());
|
|
|
|
|
++numMatches;
|
2011-06-23 16:53:06 +02:00
|
|
|
if (expression.matchedLength() == 0)
|
|
|
|
|
break;
|
2008-12-02 14:09:21 +01:00
|
|
|
pos += expression.matchedLength();
|
2011-06-23 16:53:06 +02:00
|
|
|
if (pos >= lengthOfLine)
|
|
|
|
|
break;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
++lineNr;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
++numFilesSearched;
|
2011-03-28 13:35:43 +02:00
|
|
|
if (future.isProgressUpdateNeeded()
|
|
|
|
|
|| future.progressValue() == 0 /*workaround for regression in Qt*/) {
|
2010-06-25 10:18:44 +02:00
|
|
|
if (!results.isEmpty()) {
|
|
|
|
|
future.reportResult(results);
|
|
|
|
|
results.clear();
|
|
|
|
|
}
|
|
|
|
|
future.setProgressRange(0, files->maxProgress());
|
2010-06-24 15:44:46 +02:00
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
|
2010-06-25 10:18:44 +02:00
|
|
|
}
|
2009-08-05 12:43:47 +02:00
|
|
|
if (needsToCloseFile)
|
|
|
|
|
file.close();
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-06-25 10:18:44 +02:00
|
|
|
if (!results.isEmpty()) {
|
|
|
|
|
future.reportResult(results);
|
|
|
|
|
results.clear();
|
|
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
if (!future.isCanceled())
|
2010-06-24 15:44:46 +02:00
|
|
|
future.setProgressValueAndText(files->currentProgress(), msgFound(searchTerm, numMatches, numFilesSearched));
|
|
|
|
|
delete files;
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future.isPaused())
|
|
|
|
|
future.waitForResume();
|
2008-12-02 14:09:21 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
QFuture<FileSearchResultList> Utils::findInFiles(const QString &searchTerm, FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-06-25 09:16:30 +02:00
|
|
|
return QtConcurrent::run<FileSearchResultList, QString, FileIterator *, QTextDocument::FindFlags, QMap<QString, QString> >
|
2009-08-05 12:43:47 +02:00
|
|
|
(runFileSearch, searchTerm, files, flags, fileToContentsMap);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-06-25 09:16:30 +02:00
|
|
|
QFuture<FileSearchResultList> Utils::findInFilesRegExp(const QString &searchTerm, FileIterator *files,
|
2009-08-05 12:43:47 +02:00
|
|
|
QTextDocument::FindFlags flags, QMap<QString, QString> fileToContentsMap)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2010-06-25 09:16:30 +02:00
|
|
|
return QtConcurrent::run<FileSearchResultList, QString, FileIterator *, QTextDocument::FindFlags, QMap<QString, QString> >
|
2009-08-05 12:43:47 +02:00
|
|
|
(runFileSearchRegExp, searchTerm, files, flags, fileToContentsMap);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-06-11 08:57:38 +02:00
|
|
|
|
|
|
|
|
QString Utils::expandRegExpReplacement(const QString &replaceText, const QStringList &capturedTexts)
|
|
|
|
|
{
|
2010-11-03 11:37:04 +01:00
|
|
|
// handles \1 \\ \& & \t \n
|
2010-06-11 08:57:38 +02:00
|
|
|
QString result;
|
2010-10-19 08:55:55 +02:00
|
|
|
const int numCaptures = capturedTexts.size() - 1;
|
2010-06-11 08:57:38 +02:00
|
|
|
for (int i = 0; i < replaceText.length(); ++i) {
|
|
|
|
|
QChar c = replaceText.at(i);
|
|
|
|
|
if (c == QLatin1Char('\\') && i < replaceText.length() - 1) {
|
|
|
|
|
c = replaceText.at(++i);
|
|
|
|
|
if (c == QLatin1Char('\\')) {
|
|
|
|
|
result += QLatin1Char('\\');
|
|
|
|
|
} else if (c == QLatin1Char('&')) {
|
|
|
|
|
result += QLatin1Char('&');
|
2010-10-06 16:17:30 +02:00
|
|
|
} else if (c == QLatin1Char('t')) {
|
|
|
|
|
result += QLatin1Char('\t');
|
2010-11-03 11:37:04 +01:00
|
|
|
} else if (c == QLatin1Char('n')) {
|
|
|
|
|
result += QLatin1Char('\n');
|
2010-06-11 08:57:38 +02:00
|
|
|
} else if (c.isDigit()) {
|
|
|
|
|
int index = c.unicode()-'1';
|
|
|
|
|
if (index < numCaptures) {
|
|
|
|
|
result += capturedTexts.at(index+1);
|
|
|
|
|
} else {
|
|
|
|
|
result += QLatin1Char('\\');
|
|
|
|
|
result += c;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
result += QLatin1Char('\\');
|
|
|
|
|
result += c;
|
|
|
|
|
}
|
|
|
|
|
} else if (c == QLatin1Char('&')) {
|
|
|
|
|
result += capturedTexts.at(0);
|
|
|
|
|
} else {
|
|
|
|
|
result += c;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2010-06-24 15:44:46 +02:00
|
|
|
|
2012-11-30 16:15:07 +01:00
|
|
|
namespace Utils {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
QString matchCaseReplacement(const QString &originalText, const QString &replaceText)
|
|
|
|
|
{
|
2014-03-03 14:27:42 +01:00
|
|
|
if (originalText.isEmpty() || replaceText.isEmpty())
|
2013-02-24 18:59:26 -05:00
|
|
|
return replaceText;
|
|
|
|
|
|
2012-11-30 16:15:07 +01:00
|
|
|
//Now proceed with actual case matching
|
|
|
|
|
bool firstIsUpperCase = originalText.at(0).isUpper();
|
|
|
|
|
bool firstIsLowerCase = originalText.at(0).isLower();
|
|
|
|
|
bool restIsLowerCase = true; // to be verified
|
|
|
|
|
bool restIsUpperCase = true; // to be verified
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < originalText.length(); ++i) {
|
|
|
|
|
if (originalText.at(i).isUpper())
|
|
|
|
|
restIsLowerCase = false;
|
|
|
|
|
else if (originalText.at(i).isLower())
|
|
|
|
|
restIsUpperCase = false;
|
|
|
|
|
|
|
|
|
|
if (!restIsLowerCase && !restIsUpperCase)
|
|
|
|
|
return replaceText; // mixed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (restIsLowerCase) {
|
|
|
|
|
QString res = replaceText.toLower();
|
|
|
|
|
if (firstIsUpperCase)
|
|
|
|
|
res.replace(0, 1, res.at(0).toUpper());
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (restIsUpperCase) {
|
|
|
|
|
QString res = replaceText.toUpper();
|
|
|
|
|
if (firstIsLowerCase)
|
|
|
|
|
res.replace(0, 1, res.at(0).toLower());
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return replaceText; // mixed
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Utils::matchCaseReplacement(const QString &originalText, const QString &replaceText)
|
|
|
|
|
{
|
|
|
|
|
if (originalText.isEmpty())
|
|
|
|
|
return replaceText;
|
|
|
|
|
|
|
|
|
|
//Find common prefix & suffix: these will be unaffected
|
|
|
|
|
const int replaceTextLen = replaceText.length();
|
|
|
|
|
const int originalTextLen = originalText.length();
|
|
|
|
|
|
|
|
|
|
int prefixLen = 0;
|
2013-02-24 18:59:26 -05:00
|
|
|
for (; prefixLen < replaceTextLen && prefixLen < originalTextLen; ++prefixLen)
|
2012-11-30 16:15:07 +01:00
|
|
|
if (replaceText.at(prefixLen).toLower() != originalText.at(prefixLen).toLower())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
int suffixLen = 0;
|
2013-02-24 18:59:26 -05:00
|
|
|
for (; suffixLen < replaceTextLen - prefixLen && suffixLen < originalTextLen - prefixLen; ++suffixLen)
|
2012-11-30 16:15:07 +01:00
|
|
|
if (replaceText.at(replaceTextLen - 1 - suffixLen).toLower() != originalText.at(originalTextLen- 1 - suffixLen).toLower())
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
//keep prefix and suffix, and do actual replacement on the 'middle' of the string
|
|
|
|
|
return originalText.left(prefixLen)
|
|
|
|
|
+ Internal::matchCaseReplacement(originalText.mid(prefixLen, originalTextLen - prefixLen - suffixLen),
|
|
|
|
|
replaceText.mid(prefixLen, replaceTextLen - prefixLen - suffixLen))
|
|
|
|
|
+ originalText.right(suffixLen);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-24 15:44:46 +02:00
|
|
|
// #pragma mark -- FileIterator
|
|
|
|
|
|
|
|
|
|
FileIterator::FileIterator()
|
|
|
|
|
: m_list(QStringList()),
|
|
|
|
|
m_iterator(0),
|
2010-10-11 11:34:17 +02:00
|
|
|
m_index(-1)
|
2010-06-24 15:44:46 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-11 11:34:17 +02:00
|
|
|
FileIterator::FileIterator(const QStringList &fileList,
|
|
|
|
|
const QList<QTextCodec *> encodings)
|
2010-06-24 15:44:46 +02:00
|
|
|
: m_list(fileList),
|
2010-10-11 11:34:17 +02:00
|
|
|
m_iterator(new QStringListIterator(m_list)),
|
|
|
|
|
m_encodings(encodings),
|
|
|
|
|
m_index(-1)
|
2010-06-24 15:44:46 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileIterator::~FileIterator()
|
|
|
|
|
{
|
|
|
|
|
if (m_iterator)
|
|
|
|
|
delete m_iterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileIterator::hasNext() const
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(m_iterator);
|
|
|
|
|
return m_iterator->hasNext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileIterator::next()
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(m_iterator);
|
|
|
|
|
++m_index;
|
|
|
|
|
return m_iterator->next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileIterator::maxProgress() const
|
|
|
|
|
{
|
|
|
|
|
return m_list.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileIterator::currentProgress() const
|
|
|
|
|
{
|
2010-10-11 11:34:17 +02:00
|
|
|
return m_index + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextCodec * FileIterator::encoding() const
|
|
|
|
|
{
|
|
|
|
|
if (m_index >= 0 && m_index < m_encodings.size())
|
|
|
|
|
return m_encodings.at(m_index);
|
|
|
|
|
return QTextCodec::codecForLocale();
|
2010-06-24 15:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// #pragma mark -- SubDirFileIterator
|
|
|
|
|
|
|
|
|
|
namespace {
|
2010-06-25 10:18:44 +02:00
|
|
|
const int MAX_PROGRESS = 1000;
|
2010-06-24 15:44:46 +02:00
|
|
|
}
|
|
|
|
|
|
2010-10-11 11:34:17 +02:00
|
|
|
SubDirFileIterator::SubDirFileIterator(const QStringList &directories, const QStringList &filters,
|
|
|
|
|
QTextCodec *encoding)
|
2010-06-24 15:44:46 +02:00
|
|
|
: m_filters(filters), m_progress(0)
|
|
|
|
|
{
|
2010-10-11 11:34:17 +02:00
|
|
|
m_encoding = (encoding == 0 ? QTextCodec::codecForLocale() : encoding);
|
2014-05-19 18:26:05 +03:00
|
|
|
qreal maxPer = qreal(MAX_PROGRESS) / directories.count();
|
2010-06-24 15:44:46 +02:00
|
|
|
foreach (const QString &directoryEntry, directories) {
|
|
|
|
|
if (!directoryEntry.isEmpty()) {
|
|
|
|
|
m_dirs.push(QDir(directoryEntry));
|
|
|
|
|
m_progressValues.push(maxPer);
|
|
|
|
|
m_processedValues.push(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SubDirFileIterator::hasNext() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_currentFiles.isEmpty())
|
|
|
|
|
return true;
|
2012-11-28 20:44:03 +02:00
|
|
|
while (!m_dirs.isEmpty() && m_currentFiles.isEmpty()) {
|
2010-06-24 15:44:46 +02:00
|
|
|
QDir dir = m_dirs.pop();
|
2010-10-19 08:55:55 +02:00
|
|
|
const qreal dirProgressMax = m_progressValues.pop();
|
|
|
|
|
const bool processed = m_processedValues.pop();
|
2010-06-24 15:44:46 +02:00
|
|
|
if (dir.exists()) {
|
|
|
|
|
QStringList subDirs;
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (!processed)
|
2010-06-24 15:44:46 +02:00
|
|
|
subDirs = dir.entryList(QDir::Dirs|QDir::Hidden|QDir::NoDotAndDotDot);
|
|
|
|
|
if (subDirs.isEmpty()) {
|
|
|
|
|
QStringList fileEntries = dir.entryList(m_filters,
|
|
|
|
|
QDir::Files|QDir::Hidden);
|
|
|
|
|
QStringListIterator it(fileEntries);
|
|
|
|
|
it.toBack();
|
|
|
|
|
while (it.hasPrevious()) {
|
|
|
|
|
const QString &file = it.previous();
|
|
|
|
|
m_currentFiles.append(dir.path()+ QLatin1Char('/') +file);
|
|
|
|
|
}
|
|
|
|
|
m_progress += dirProgressMax;
|
|
|
|
|
} else {
|
2010-06-25 10:18:44 +02:00
|
|
|
qreal subProgress = dirProgressMax/(subDirs.size()+1);
|
2010-06-24 15:44:46 +02:00
|
|
|
m_dirs.push(dir);
|
2010-06-25 10:18:44 +02:00
|
|
|
m_progressValues.push(subProgress);
|
2010-06-24 15:44:46 +02:00
|
|
|
m_processedValues.push(true);
|
|
|
|
|
QStringListIterator it(subDirs);
|
|
|
|
|
it.toBack();
|
|
|
|
|
while (it.hasPrevious()) {
|
|
|
|
|
const QString &directory = it.previous();
|
|
|
|
|
m_dirs.push(QDir(dir.path()+ QLatin1Char('/') + directory));
|
|
|
|
|
m_progressValues.push(subProgress);
|
|
|
|
|
m_processedValues.push(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
m_progress += dirProgressMax;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (m_currentFiles.isEmpty()) {
|
|
|
|
|
m_progress = MAX_PROGRESS;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString SubDirFileIterator::next()
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(!m_currentFiles.isEmpty());
|
|
|
|
|
return m_currentFiles.takeFirst();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SubDirFileIterator::maxProgress() const
|
|
|
|
|
{
|
|
|
|
|
return MAX_PROGRESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int SubDirFileIterator::currentProgress() const
|
|
|
|
|
{
|
2010-06-25 10:18:44 +02:00
|
|
|
return qMin(qRound(m_progress), MAX_PROGRESS);
|
2010-06-24 15:44:46 +02:00
|
|
|
}
|
2010-10-11 11:34:17 +02:00
|
|
|
|
|
|
|
|
QTextCodec * SubDirFileIterator::encoding() const
|
|
|
|
|
{
|
|
|
|
|
return m_encoding;
|
|
|
|
|
}
|