Commit bca988d8 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

DiffEditor: Some more regexp cleanup

Change-Id: I3a423d71c03c624d7ba1bde8b8e7d02e3109f8e8
Reviewed-by: default avatarTobias Hunger <tobias.hunger@digia.com>
Reviewed-by: default avatarJarek Kobus <jaroslaw.kobus@digia.com>
parent 09ee5c74
...@@ -704,13 +704,13 @@ static QList<ChunkData> readChunks(const QString &patch, ...@@ -704,13 +704,13 @@ static QList<ChunkData> readChunks(const QString &patch,
{ {
const QRegExp chunkRegExp(QLatin1String( const QRegExp chunkRegExp(QLatin1String(
// beginning of the line // beginning of the line
"((?:\\n|^)" "(?:\\n|^)"
// @@ -leftPos[,leftCount] +rightPos[,rightCount] @@ // @@ -leftPos[,leftCount] +rightPos[,rightCount] @@
"@@ -(\\d+)(?:,\\d+)? \\+(\\d+)(?:,\\d+)? @@" "@@ -(\\d+)(?:,\\d+)? \\+(\\d+)(?:,\\d+)? @@"
// optional hint (e.g. function name) // optional hint (e.g. function name)
"(\\ +[^\\n]*)?" "(\\ +[^\\n]*)?"
// end of line (need to be followed by text line) // end of line (need to be followed by text line)
"(?:\\n))")); "\\n"));
bool readOk = false; bool readOk = false;
...@@ -721,10 +721,10 @@ static QList<ChunkData> readChunks(const QString &patch, ...@@ -721,10 +721,10 @@ static QList<ChunkData> readChunks(const QString &patch,
int endOfLastChunk = 0; int endOfLastChunk = 0;
do { do {
const QStringList capturedTexts = chunkRegExp.capturedTexts(); const QStringList capturedTexts = chunkRegExp.capturedTexts();
const QString captured = capturedTexts.at(1); const QString captured = capturedTexts.at(0);
const int leftStartingPos = capturedTexts.at(2).toInt(); const int leftStartingPos = capturedTexts.at(1).toInt();
const int rightStartingPos = capturedTexts.at(3).toInt(); const int rightStartingPos = capturedTexts.at(2).toInt();
const QString contextInfo = capturedTexts.at(4); const QString contextInfo = capturedTexts.at(3);
if (endOfLastChunk > 0) { if (endOfLastChunk > 0) {
const QString lines = patch.mid(endOfLastChunk, const QString lines = patch.mid(endOfLastChunk,
pos - endOfLastChunk); pos - endOfLastChunk);
...@@ -770,26 +770,26 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks, ...@@ -770,26 +770,26 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks,
bool readOk = false; bool readOk = false;
const QRegExp leftFileRegExp(QLatin1String( const QRegExp leftFileRegExp(QLatin1String(
"((?:\\n|^)-{3} " // "--- " "(?:\\n|^)-{3} " // "--- "
"([^\\t\\n]+)" // "fileName1" "([^\\t\\n]+)" // "fileName1"
"(?:\\t[^\\n]*)*\\n)")); // optionally followed by: \t anything \t anything ...) "(?:\\t[^\\n]*)*\\n")); // optionally followed by: \t anything \t anything ...)
const QRegExp rightFileRegExp(QLatin1String( const QRegExp rightFileRegExp(QLatin1String(
"(^\\+{3} " // "+++ " "^\\+{3} " // "+++ "
"([^\\t\\n]+)" // "fileName2" "([^\\t\\n]+)" // "fileName2"
"(?:\\t[^\\n]*)*\\n)")); // optionally followed by: \t anything \t anything ...) "(?:\\t[^\\n]*)*\\n")); // optionally followed by: \t anything \t anything ...)
const QRegExp binaryRegExp(QLatin1String("(^Binary files ([^\\t\\n]+) and ([^\\t\\n]+) differ$)")); const QRegExp binaryRegExp(QLatin1String("^Binary files ([^\\t\\n]+) and ([^\\t\\n]+) differ$"));
// followed either by leftFileRegExp or by binaryRegExp // followed either by leftFileRegExp or by binaryRegExp
if (leftFileRegExp.indexIn(patch, 0) == 0) { if (leftFileRegExp.indexIn(patch, 0) == 0) {
const QStringList leftCaptured = leftFileRegExp.capturedTexts(); const QStringList leftCaptured = leftFileRegExp.capturedTexts();
patch = patch.mid(leftCaptured.at(1).count()); patch = patch.mid(leftCaptured.at(0).count());
fileData.leftFileInfo.fileName = leftCaptured.at(2); fileData.leftFileInfo.fileName = leftCaptured.at(1);
// followed by rightFileRegExp // followed by rightFileRegExp
if (rightFileRegExp.indexIn(patch, 0) == 0) { if (rightFileRegExp.indexIn(patch, 0) == 0) {
const QStringList rightCaptured = rightFileRegExp.capturedTexts(); const QStringList rightCaptured = rightFileRegExp.capturedTexts();
patch = patch.mid(rightCaptured.at(1).count()); patch = patch.mid(rightCaptured.at(0).count());
fileData.rightFileInfo.fileName = rightCaptured.at(2); fileData.rightFileInfo.fileName = rightCaptured.at(1);
fileData.chunks = readChunks(patch, fileData.chunks = readChunks(patch,
ignoreWhitespace, ignoreWhitespace,
...@@ -798,8 +798,8 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks, ...@@ -798,8 +798,8 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks,
} }
} else if (binaryRegExp.indexIn(patch, 0) == 0) { } else if (binaryRegExp.indexIn(patch, 0) == 0) {
const QStringList binaryCaptured = binaryRegExp.capturedTexts(); const QStringList binaryCaptured = binaryRegExp.capturedTexts();
fileData.leftFileInfo.fileName = binaryCaptured.at(2); fileData.leftFileInfo.fileName = binaryCaptured.at(1);
fileData.rightFileInfo.fileName = binaryCaptured.at(3); fileData.rightFileInfo.fileName = binaryCaptured.at(2);
fileData.binaryFiles = true; fileData.binaryFiles = true;
readOk = true; readOk = true;
} }
...@@ -818,8 +818,7 @@ static QList<FileData> readDiffPatch(const QString &patch, ...@@ -818,8 +818,7 @@ static QList<FileData> readDiffPatch(const QString &patch,
bool ignoreWhitespace, bool ignoreWhitespace,
bool *ok) bool *ok)
{ {
const QRegExp diffRegExp(QLatin1String("(" // capture all const QRegExp diffRegExp(QLatin1String("(?:\\n|^)" // new line of the beginning of a patch
"(?:\\n|^)" // new line of the beginning of a patch
"(" // either "(" // either
"-{3} " // --- "-{3} " // ---
"[^\\t\\n]+" // filename1 "[^\\t\\n]+" // filename1
...@@ -833,8 +832,7 @@ static QList<FileData> readDiffPatch(const QString &patch, ...@@ -833,8 +832,7 @@ static QList<FileData> readDiffPatch(const QString &patch,
" and " " and "
"[^\\t\\n]+" // filename2 "[^\\t\\n]+" // filename2
" differ" " differ"
")" // end of or ")")); // end of or
")")); // end of capture all
bool readOk = false; bool readOk = false;
...@@ -845,8 +843,7 @@ static QList<FileData> readDiffPatch(const QString &patch, ...@@ -845,8 +843,7 @@ static QList<FileData> readDiffPatch(const QString &patch,
readOk = true; readOk = true;
int lastPos = -1; int lastPos = -1;
do { do {
const QStringList capturedTexts = diffRegExp.capturedTexts(); const QString captured = diffRegExp.cap();
const QString captured = capturedTexts.at(1);
if (lastPos >= 0) { if (lastPos >= 0) {
const QString headerAndChunks = patch.mid(lastPos, const QString headerAndChunks = patch.mid(lastPos,
pos - lastPos); pos - lastPos);
...@@ -901,12 +898,12 @@ static FileData readGitHeaderAndChunks(const QString &headerAndChunks, ...@@ -901,12 +898,12 @@ static FileData readGitHeaderAndChunks(const QString &headerAndChunks,
const QString devNull(QLatin1String("/dev/null")); const QString devNull(QLatin1String("/dev/null"));
// will be followed by: index 0000000..shasha, file "a" replaced by "/dev/null", @@ -0,0 +m,n @@ // will be followed by: index 0000000..shasha, file "a" replaced by "/dev/null", @@ -0,0 +m,n @@
const QRegExp newFileMode(QLatin1String("(^new file mode \\d+\\n)")); // new file mode octal const QRegExp newFileMode(QLatin1String("^new file mode \\d+\\n")); // new file mode octal
// will be followed by: index shasha..0000000, file "b" replaced by "/dev/null", @@ -m,n +0,0 @@ // will be followed by: index shasha..0000000, file "b" replaced by "/dev/null", @@ -m,n +0,0 @@
const QRegExp deletedFileMode(QLatin1String("(^deleted file mode \\d+\\n)")); // deleted file mode octal const QRegExp deletedFileMode(QLatin1String("^deleted file mode \\d+\\n")); // deleted file mode octal
const QRegExp indexRegExp(QLatin1String("(^index (\\w+)\\.{2}(\\w+)(?: \\d+)?(\\n|$))")); // index cap2..cap3(optionally: octal) const QRegExp indexRegExp(QLatin1String("^index (\\w+)\\.{2}(\\w+)(?: \\d+)?(\\n|$)")); // index cap2..cap3(optionally: octal)
QString leftFileName = QLatin1String("a/") + fileName; QString leftFileName = QLatin1String("a/") + fileName;
QString rightFileName = QLatin1String("b/") + fileName; QString rightFileName = QLatin1String("b/") + fileName;
...@@ -914,43 +911,43 @@ static FileData readGitHeaderAndChunks(const QString &headerAndChunks, ...@@ -914,43 +911,43 @@ static FileData readGitHeaderAndChunks(const QString &headerAndChunks,
if (newFileMode.indexIn(patch, 0) == 0) { if (newFileMode.indexIn(patch, 0) == 0) {
fileData.fileOperation = FileData::NewFile; fileData.fileOperation = FileData::NewFile;
leftFileName = devNull; leftFileName = devNull;
patch = patch.mid(newFileMode.capturedTexts().at(1).count()); patch = patch.mid(newFileMode.cap().count());
} else if (deletedFileMode.indexIn(patch, 0) == 0) { } else if (deletedFileMode.indexIn(patch, 0) == 0) {
fileData.fileOperation = FileData::DeleteFile; fileData.fileOperation = FileData::DeleteFile;
rightFileName = devNull; rightFileName = devNull;
patch = patch.mid(deletedFileMode.capturedTexts().at(1).count()); patch = patch.mid(deletedFileMode.cap().count());
} }
if (indexRegExp.indexIn(patch, 0) == 0) { if (indexRegExp.indexIn(patch, 0) == 0) {
const QStringList capturedTexts = indexRegExp.capturedTexts(); const QStringList capturedTexts = indexRegExp.capturedTexts();
const QString captured = capturedTexts.at(1); const QString captured = capturedTexts.at(0);
fileData.leftFileInfo.typeInfo = capturedTexts.at(2); fileData.leftFileInfo.typeInfo = capturedTexts.at(1);
fileData.rightFileInfo.typeInfo = capturedTexts.at(3); fileData.rightFileInfo.typeInfo = capturedTexts.at(2);
patch = patch.mid(captured.count()); patch = patch.mid(captured.count());
const QRegExp leftFileRegExp(QLatin1String("(^-{3} ") // "--- " const QRegExp leftFileRegExp(QLatin1String("^-{3} ") // "--- "
+ leftFileName // "a/fileName" or "/dev/null" + leftFileName // "a/fileName" or "/dev/null"
+ QLatin1String("(?:\\t[^\\n]*)*\\n)")); // optionally followed by: \t anything \t anything ...) + QLatin1String("(?:\\t[^\\n]*)*\\n")); // optionally followed by: \t anything \t anything ...)
const QRegExp rightFileRegExp(QLatin1String("(^\\+{3} ") // "+++ " const QRegExp rightFileRegExp(QLatin1String("^\\+{3} ") // "+++ "
+ rightFileName // "b/fileName" or "/dev/null" + rightFileName // "b/fileName" or "/dev/null"
+ QLatin1String("(?:\\t[^\\n]*)*\\n)")); // optionally followed by: \t anything \t anything ...) + QLatin1String("(?:\\t[^\\n]*)*\\n")); // optionally followed by: \t anything \t anything ...)
const QRegExp binaryRegExp(QLatin1String("(^Binary files ") const QRegExp binaryRegExp(QLatin1String("^Binary files ")
+ leftFileName + leftFileName
+ QLatin1String(" and ") + QLatin1String(" and ")
+ rightFileName + rightFileName
+ QLatin1String(" differ$)")); + QLatin1String(" differ$"));
// empty or followed either by leftFileRegExp or by binaryRegExp // empty or followed either by leftFileRegExp or by binaryRegExp
if (patch.isEmpty() && (fileData.fileOperation == FileData::NewFile if (patch.isEmpty() && (fileData.fileOperation == FileData::NewFile
|| fileData.fileOperation == FileData::DeleteFile)) { || fileData.fileOperation == FileData::DeleteFile)) {
readOk = true; readOk = true;
} else if (leftFileRegExp.indexIn(patch, 0) == 0) { } else if (leftFileRegExp.indexIn(patch, 0) == 0) {
patch = patch.mid(leftFileRegExp.capturedTexts().at(1).count()); patch = patch.mid(leftFileRegExp.cap().count());
// followed by rightFileRegExp // followed by rightFileRegExp
if (rightFileRegExp.indexIn(patch, 0) == 0) { if (rightFileRegExp.indexIn(patch, 0) == 0) {
patch = patch.mid(rightFileRegExp.capturedTexts().at(1).count()); patch = patch.mid(rightFileRegExp.cap().count());
fileData.chunks = readChunks(patch, fileData.chunks = readChunks(patch,
ignoreWhitespace, ignoreWhitespace,
...@@ -987,7 +984,7 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks, ...@@ -987,7 +984,7 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks,
QString patch = copyRenameChunks; QString patch = copyRenameChunks;
bool readOk = false; bool readOk = false;
const QRegExp indexRegExp(QLatin1String("(^index (\\w+)\\.{2}(\\w+)(?: \\d+)?(\\n|$))")); // index cap2..cap3(optionally: octal) const QRegExp indexRegExp(QLatin1String("^index (\\w+)\\.{2}(\\w+)(?: \\d+)?(\\n|$)")); // index cap2..cap3(optionally: octal)
QString leftGitFileName = QLatin1String("a/") + leftFileName; QString leftGitFileName = QLatin1String("a/") + leftFileName;
QString rightGitFileName = QLatin1String("b/") + rightFileName; QString rightGitFileName = QLatin1String("b/") + rightFileName;
...@@ -995,26 +992,26 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks, ...@@ -995,26 +992,26 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks,
if (fileOperation == FileData::CopyFile || fileOperation == FileData::RenameFile) { if (fileOperation == FileData::CopyFile || fileOperation == FileData::RenameFile) {
if (indexRegExp.indexIn(patch, 0) == 0) { if (indexRegExp.indexIn(patch, 0) == 0) {
const QStringList capturedTexts = indexRegExp.capturedTexts(); const QStringList capturedTexts = indexRegExp.capturedTexts();
const QString captured = capturedTexts.at(1); const QString captured = capturedTexts.at(0);
fileData.leftFileInfo.typeInfo = capturedTexts.at(2); fileData.leftFileInfo.typeInfo = capturedTexts.at(1);
fileData.rightFileInfo.typeInfo = capturedTexts.at(3); fileData.rightFileInfo.typeInfo = capturedTexts.at(2);
patch = patch.mid(captured.count()); patch = patch.mid(captured.count());
const QRegExp leftFileRegExp(QLatin1String("(^-{3} ") // "--- " const QRegExp leftFileRegExp(QLatin1String("^-{3} ") // "--- "
+ leftGitFileName // "a/fileName" or "/dev/null" + leftGitFileName // "a/fileName" or "/dev/null"
+ QLatin1String("(?:\\t[^\\n]*)*\\n)")); // optionally followed by: \t anything \t anything ...) + QLatin1String("(?:\\t[^\\n]*)*\\n")); // optionally followed by: \t anything \t anything ...)
const QRegExp rightFileRegExp(QLatin1String("(^\\+{3} ") // "+++ " const QRegExp rightFileRegExp(QLatin1String("^\\+{3} ") // "+++ "
+ rightGitFileName // "b/fileName" or "/dev/null" + rightGitFileName // "b/fileName" or "/dev/null"
+ QLatin1String("(?:\\t[^\\n]*)*\\n)")); // optionally followed by: \t anything \t anything ...) + QLatin1String("(?:\\t[^\\n]*)*\\n")); // optionally followed by: \t anything \t anything ...)
// followed by leftFileRegExp // followed by leftFileRegExp
if (leftFileRegExp.indexIn(patch, 0) == 0) { if (leftFileRegExp.indexIn(patch, 0) == 0) {
patch = patch.mid(leftFileRegExp.capturedTexts().at(1).count()); patch = patch.mid(leftFileRegExp.cap().count());
// followed by rightFileRegExp // followed by rightFileRegExp
if (rightFileRegExp.indexIn(patch, 0) == 0) { if (rightFileRegExp.indexIn(patch, 0) == 0) {
patch = patch.mid(rightFileRegExp.capturedTexts().at(1).count()); patch = patch.mid(rightFileRegExp.cap().count());
fileData.chunks = readChunks(patch, fileData.chunks = readChunks(patch,
ignoreWhitespace, ignoreWhitespace,
...@@ -1038,13 +1035,13 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks, ...@@ -1038,13 +1035,13 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks,
static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace, bool *ok) static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace, bool *ok)
{ {
const QRegExp simpleGitRegExp(QLatin1String("((?:\\n|^)diff --git a/([^\\n]+) b/\\2\\n)")); // diff --git a/cap2 b/cap2 const QRegExp simpleGitRegExp(QLatin1String("(?:\\n|^)diff --git a/([^\\n]+) b/\\1\\n")); // diff --git a/cap2 b/cap2
const QRegExp similarityRegExp(QLatin1String( const QRegExp similarityRegExp(QLatin1String(
"((?:\\n|^)diff --git a/([^\\n]+) b/([^\\n]+)\\n" // diff --git a/cap2 b/cap3 "(?:\\n|^)diff --git a/([^\\n]+) b/([^\\n]+)\\n" // diff --git a/cap2 b/cap3
"(?:dis)?similarity index \\d{1,3}%\\n" // similarity / dissimilarity index xxx% (100% max) "(?:dis)?similarity index \\d{1,3}%\\n" // similarity / dissimilarity index xxx% (100% max)
"(copy|rename) from \\2\\n" // copy / rename from cap2 "(copy|rename) from \\1\\n" // copy / rename from cap2
"\\4 to \\3\\n)")); // copy / rename (cap4) to cap3 "\\3 to \\2\\n")); // copy / rename (cap4) to cap3
bool readOk = false; bool readOk = false;
...@@ -1099,8 +1096,8 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace, ...@@ -1099,8 +1096,8 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace,
if (simpleGitMatched) { if (simpleGitMatched) {
const QStringList capturedTexts = simpleGitRegExp.capturedTexts(); const QStringList capturedTexts = simpleGitRegExp.capturedTexts();
const QString captured = capturedTexts.at(1); const QString captured = capturedTexts.at(0);
const QString fileName = capturedTexts.at(2); const QString fileName = capturedTexts.at(1);
pos += captured.count(); pos += captured.count();
endOfLastHeader = pos; endOfLastHeader = pos;
lastLeftFileName = fileName; lastLeftFileName = fileName;
...@@ -1108,10 +1105,10 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace, ...@@ -1108,10 +1105,10 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace,
lastOperation = FileData::ChangeFile; lastOperation = FileData::ChangeFile;
} else { } else {
const QStringList capturedTexts = similarityRegExp.capturedTexts(); const QStringList capturedTexts = similarityRegExp.capturedTexts();
const QString captured = capturedTexts.at(1); const QString captured = capturedTexts.at(0);
const QString leftFileName = capturedTexts.at(2); const QString leftFileName = capturedTexts.at(1);
const QString rightFileName = capturedTexts.at(3); const QString rightFileName = capturedTexts.at(2);
const QString operation = capturedTexts.at(4); const QString operation = capturedTexts.at(3);
pos += captured.count(); pos += captured.count();
endOfLastHeader = pos; endOfLastHeader = pos;
lastLeftFileName = leftFileName; lastLeftFileName = leftFileName;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment