diff --git a/src/MergeFileInfos.h b/src/MergeFileInfos.h index a20e8a6..8606855 100644 --- a/src/MergeFileInfos.h +++ b/src/MergeFileInfos.h @@ -1,176 +1,194 @@ /*************************************************************************** * Copyright (C) 2003-2007 by Joachim Eibl * * Copyright (C) 2018 Michael Reeves reeves.87@gmail.com * * * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef MERGEFILEINFO_H #define MERGEFILEINFO_H #include "DirectoryInfo.h" #include "diff.h" #include "fileaccess.h" #include //class DirectoryInfo; enum e_MergeOperation { - eTitleId, - eNoOperation, - // Operations in sync mode (with only two directories): - eCopyAToB, eCopyBToA, eDeleteA, eDeleteB, eDeleteAB, eMergeToA, eMergeToB, eMergeToAB, - - // Operations in merge mode (with two or three directories) - eCopyAToDest, eCopyBToDest, eCopyCToDest, eDeleteFromDest, eMergeABCToDest, - eMergeABToDest, - eConflictingFileTypes, // Error - eChangedAndDeleted, // Error - eConflictingAges // Equal age but files are not! + eTitleId, + eNoOperation, + // Operations in sync mode (with only two directories): + eCopyAToB, + eCopyBToA, + eDeleteA, + eDeleteB, + eDeleteAB, + eMergeToA, + eMergeToB, + eMergeToAB, + + // Operations in merge mode (with two or three directories) + eCopyAToDest, + eCopyBToDest, + eCopyCToDest, + eDeleteFromDest, + eMergeABCToDest, + eMergeABToDest, + eConflictingFileTypes, // Error + eChangedAndDeleted, // Error + eConflictingAges // Equal age but files are not! }; -enum e_Age { eNew, eMiddle, eOld, eNotThere, eAgeEnd }; +enum e_Age +{ + eNew, + eMiddle, + eOld, + eNotThere, + eAgeEnd +}; enum e_OperationStatus { eOpStatusNone, eOpStatusDone, eOpStatusError, eOpStatusSkipped, eOpStatusNotSaved, eOpStatusInProgress, eOpStatusToDo }; class MergeFileInfos { public: MergeFileInfos(); ~MergeFileInfos(); //bool operator>( const MergeFileInfos& ); QString subPath() const; QString fileName() const; bool isDirA() const { return m_pFileInfoA ? m_pFileInfoA->isDir() : false; } bool isDirB() const { return m_pFileInfoB ? m_pFileInfoB->isDir() : false; } bool isDirC() const { return m_pFileInfoC ? m_pFileInfoC->isDir() : false; } bool isLinkA() const { return m_pFileInfoA ? m_pFileInfoA->isSymLink() : false; } bool isLinkB() const { return m_pFileInfoB ? m_pFileInfoB->isSymLink() : false; } bool isLinkC() const { return m_pFileInfoC ? m_pFileInfoC->isSymLink() : false; } bool existsInA() const { return m_pFileInfoA != nullptr; } bool existsInB() const { return m_pFileInfoB != nullptr; } bool existsInC() const { return m_pFileInfoC != nullptr; } bool conflictingFileTypes(); void sort(Qt::SortOrder order); inline MergeFileInfos* parent() const { return m_pParent; } inline void setParent(MergeFileInfos* inParent) { m_pParent = inParent; } inline const QList& children() const { return m_children; } inline void addChild(MergeFileInfos* child) { m_children.push_back(child); } inline void clear() { m_children.clear(); } FileAccess* getFileInfoA() const { return m_pFileInfoA; } FileAccess* getFileInfoB() const { return m_pFileInfoB; } FileAccess* getFileInfoC() const { return m_pFileInfoC; } void setFileInfoA(FileAccess* newInfo) { m_pFileInfoA = newInfo; } void setFileInfoB(FileAccess* newInfo) { m_pFileInfoB = newInfo; } void setFileInfoC(FileAccess* newInfo) { m_pFileInfoC = newInfo; } QString fullNameA() const; QString fullNameB() const; QString fullNameC() const; QString fullNameDest() const; inline QSharedPointer getDirectoryInfo() const { return m_dirInfo; } void setDirectoryInfo(const QSharedPointer& dirInfo) { m_dirInfo = dirInfo; } inline QString getDirNameA() const { return getDirectoryInfo()->dirA().prettyAbsPath(); } inline QString getDirNameB() const { return getDirectoryInfo()->dirB().prettyAbsPath(); } inline QString getDirNameC() const { return getDirectoryInfo()->dirC().prettyAbsPath(); } inline QString getDirNameDest() const { return getDirectoryInfo()->destDir().prettyAbsPath(); } inline TotalDiffStatus& diffStatus() { return m_totalDiffStatus; } inline e_MergeOperation getOperation() const { return m_eMergeOperation; } inline void setOperation(const e_MergeOperation op) { m_eMergeOperation = op; } inline e_OperationStatus getOpStatus() const { return m_eOpStatus; } inline void setOpStatus(const e_OperationStatus eOpStatus) { m_eOpStatus = eOpStatus; } inline e_Age getAgeA() const { return m_ageA; } inline void setAgeA(const e_Age inAge) { m_ageA = inAge; } inline e_Age getAgeB() const { return m_ageB; } inline void setAgeB(const e_Age inAge) { m_ageB = inAge; } inline e_Age getAgeC() const { return m_ageC; } inline void setAgeC(const e_Age inAge) { m_ageC = inAge; } private: MergeFileInfos* m_pParent; QList m_children; FileAccess* m_pFileInfoA; FileAccess* m_pFileInfoB; FileAccess* m_pFileInfoC; QSharedPointer m_dirInfo; - + TotalDiffStatus m_totalDiffStatus; - public: + public: e_MergeOperation m_eMergeOperation; e_OperationStatus m_eOpStatus; e_Age m_ageA; e_Age m_ageB; e_Age m_ageC; public: bool m_bOperationComplete; bool m_bSimOpComplete; bool m_bEqualAB; bool m_bEqualAC; bool m_bEqualBC; bool m_bConflictingAges; // Equal age but files are not! }; class MfiCompare { Qt::SortOrder mOrder; public: explicit MfiCompare(Qt::SortOrder order) { mOrder = order; } bool operator()(MergeFileInfos* pMFI1, MergeFileInfos* pMFI2) { bool bDir1 = pMFI1->isDirA() || pMFI1->isDirB() || pMFI1->isDirC(); bool bDir2 = pMFI2->isDirA() || pMFI2->isDirB() || pMFI2->isDirC(); if(bDir1 == bDir2) { if(mOrder == Qt::AscendingOrder) { return pMFI1->fileName().compare(pMFI2->fileName(), Qt::CaseInsensitive) < 0; } else { return pMFI1->fileName().compare(pMFI2->fileName(), Qt::CaseInsensitive) > 0; } } else return bDir1; } }; #endif // !MERGEFILEINFO_H diff --git a/src/SourceData.cpp b/src/SourceData.cpp index 81c6331..644559f 100644 --- a/src/SourceData.cpp +++ b/src/SourceData.cpp @@ -1,937 +1,941 @@ /*************************************************************************** * Copyright (C) 2003-2007 by Joachim Eibl * * Copyright (C) 2018 Michael Reeves reeves.87@gmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * ***************************************************************************/ /* Features of class SourceData: - Read a file (from the given URL) or accept data via a string. - Allocate and free buffers as necessary. - Run a preprocessor, when specified. - Run the line-matching preprocessor, when specified. - Run other preprocessing steps: Uppercase, ignore comments, remove carriage return, ignore numbers. Order of operation: 1. If data was given via a string then save it to a temp file. (see setData()) 2. If the specified file is nonlocal (URL) copy it to a temp file. 3. If a preprocessor was specified, run the input file through it. 4. Read the output of the preprocessor. 5. If Uppercase was specified: Turn the read data to uppercase. 6. Write the result to a temp file. 7. If a line-matching preprocessor was specified, run the temp file through it. 8. Read the output of the line-matching preprocessor. 9. If ignore numbers was specified, strip the LMPP-output of all numbers. 10. If ignore comments was specified, strip the LMPP-output of comments. Optimizations: Skip unneeded steps. */ #include "SourceData.h" #include "Utils.h" #include "diff.h" #include #include #include #include #include #include #include SourceData::SourceData() { m_pOptions = nullptr; reset(); } SourceData::~SourceData() { reset(); } void SourceData::reset() { m_pEncoding = nullptr; m_fileAccess = FileAccess(); m_normalData.reset(); m_lmppData.reset(); if(!m_tempInputFileName.isEmpty()) { m_tempFile.remove(); m_tempInputFileName = ""; } } void SourceData::setFilename(const QString& filename) { if(filename.isEmpty()) { reset(); } else { FileAccess fa(filename); setFileAccess(fa); } } bool SourceData::isEmpty() { return getFilename().isEmpty(); } bool SourceData::hasData() { return m_normalData.m_pBuf != nullptr; } bool SourceData::isValid() { return isEmpty() || hasData(); } void SourceData::setOptions(Options* pOptions) { m_pOptions = pOptions; } QString SourceData::getFilename() { return m_fileAccess.absoluteFilePath(); } QString SourceData::getAliasName() { return m_aliasName.isEmpty() ? m_fileAccess.prettyAbsPath() : m_aliasName; } void SourceData::setAliasName(const QString& name) { m_aliasName = name; } void SourceData::setFileAccess(const FileAccess& fileAccess) { m_fileAccess = fileAccess; m_aliasName = QString(); if(!m_tempInputFileName.isEmpty()) { m_tempFile.remove(); m_tempInputFileName = ""; } } void SourceData::setEncoding(QTextCodec* pEncoding) { m_pEncoding = pEncoding; } QStringList SourceData::setData(const QString& data) { QStringList errors; // Create a temp file for preprocessing: if(m_tempInputFileName.isEmpty()) { FileAccess::createTempFile(m_tempFile); m_tempInputFileName = m_tempFile.fileName(); } FileAccess f(m_tempInputFileName); QByteArray ba = QTextCodec::codecForName("UTF-8")->fromUnicode(data); bool bSuccess = f.writeFile(ba.constData(), ba.length()); if(!bSuccess) { errors.append(i18n("Writing clipboard data to temp file failed.")); } else { m_aliasName = i18n("From Clipboard"); m_fileAccess = FileAccess(""); // Effect: m_fileAccess.isValid() is false } return errors; } const LineData* SourceData::getLineDataForDiff() const { if(m_lmppData.m_pBuf == nullptr) return m_normalData.m_v.size() > 0 ? &m_normalData.m_v[0] : nullptr; else return m_lmppData.m_v.size() > 0 ? &m_lmppData.m_v[0] : nullptr; } const LineData* SourceData::getLineDataForDisplay() const { return m_normalData.m_v.size() > 0 ? &m_normalData.m_v[0] : nullptr; } LineRef SourceData::getSizeLines() const { return (LineRef)(m_normalData.m_vSize); } qint64 SourceData::getSizeBytes() const { return m_normalData.m_size; } const char* SourceData::getBuf() const { return m_normalData.m_pBuf; } const QString& SourceData::getText() const { return m_normalData.m_unicodeBuf; } bool SourceData::isText() { return m_normalData.isText(); } bool SourceData::isIncompleteConversion() { return m_normalData.m_bIncompleteConversion; } bool SourceData::isFromBuffer() { return !m_fileAccess.isValid(); } bool SourceData::isBinaryEqualWith(const SourceData& other) const { return m_fileAccess.exists() && other.m_fileAccess.exists() && getSizeBytes() == other.getSizeBytes() && (getSizeBytes() == 0 || memcmp(getBuf(), other.getBuf(), getSizeBytes()) == 0); } void SourceData::FileData::reset() { delete[](char*) m_pBuf; m_pBuf = nullptr; m_v.clear(); m_size = 0; m_vSize = 0; m_bIsText = true; m_bIncompleteConversion = false; m_eLineEndStyle = eLineEndStyleUndefined; } bool SourceData::FileData::readFile(FileAccess& file) { reset(); - if(file.fileName().isEmpty()) { + if(file.fileName().isEmpty()) + { return true; } //FileAccess fa(filename); if(!file.isNormal()) return true; m_size = file.sizeForReading(); char* pBuf; m_pBuf = pBuf = new char[m_size + 100]; // Alloc 100 byte extra: Safety hack, not nice but does no harm. // Some extra bytes at the end of the buffer are needed by // the diff algorithm. See also GnuDiff::diff_2_files(). bool bSuccess = file.readFile(pBuf, m_size); if(!bSuccess) { delete[] pBuf; m_pBuf = nullptr; m_size = 0; } return bSuccess; } bool SourceData::FileData::readFile(const QString& filename) { reset(); - if(filename.isEmpty()) { + if(filename.isEmpty()) + { return true; } FileAccess fa(filename); if(!fa.isNormal()) return true; m_size = fa.sizeForReading(); char* pBuf; m_pBuf = pBuf = new char[m_size + 100]; // Alloc 100 byte extra: Safety hack, not nice but does no harm. // Some extra bytes at the end of the buffer are needed by // the diff algorithm. See also GnuDiff::diff_2_files(). bool bSuccess = fa.readFile(pBuf, m_size); if(!bSuccess) { delete[] pBuf; m_pBuf = nullptr; m_size = 0; } return bSuccess; } bool SourceData::saveNormalDataAs(const QString& fileName) { return m_normalData.writeFile(fileName); } bool SourceData::FileData::writeFile(const QString& filename) { - if(filename.isEmpty()) { + if(filename.isEmpty()) + { return true; } FileAccess fa(filename); bool bSuccess = fa.writeFile(m_pBuf, m_size); return bSuccess; } void SourceData::FileData::copyBufFrom(const FileData& src) { reset(); char* pBuf; m_size = src.m_size; m_pBuf = pBuf = new char[m_size + 100]; Q_ASSERT(src.m_pBuf != nullptr); memcpy(pBuf, src.m_pBuf, m_size); } QTextCodec* SourceData::detectEncoding(const QString& fileName, QTextCodec* pFallbackCodec) { QFile f(fileName); if(f.open(QIODevice::ReadOnly)) { char buf[200]; qint64 size = f.read(buf, sizeof(buf)); qint64 skipBytes = 0; QTextCodec* pCodec = detectEncoding(buf, size, skipBytes); if(pCodec) return pCodec; } return pFallbackCodec; } QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetectUnicode) { m_pEncoding = pEncoding; QTemporaryFile fileIn1, fileOut1; QString fileNameIn1; QString fileNameOut1; QString fileNameIn2; QString fileNameOut2; QStringList errors; if(m_fileAccess.isValid() && !m_fileAccess.isNormal()) { errors.append(i18n("%1 is not a normal file.", m_fileAccess.prettyAbsPath())); return errors; } bool bTempFileFromClipboard = !m_fileAccess.isValid(); // Detect the input for the preprocessing operations if(!bTempFileFromClipboard) { if(m_fileAccess.isLocal()) { fileNameIn1 = m_fileAccess.absoluteFilePath(); } else // File is not local: create a temporary local copy: { if(m_tempInputFileName.isEmpty()) { m_fileAccess.createLocalCopy(); m_tempInputFileName = m_fileAccess.getTempName(); } fileNameIn1 = m_tempInputFileName; } if(bAutoDetectUnicode) { m_pEncoding = detectEncoding(fileNameIn1, pEncoding); } } else // The input was set via setData(), probably from clipboard. { fileNameIn1 = m_tempInputFileName; m_pEncoding = QTextCodec::codecForName("UTF-8"); } QTextCodec* pEncoding1 = m_pEncoding; QTextCodec* pEncoding2 = m_pEncoding; m_normalData.reset(); m_lmppData.reset(); FileAccess faIn(fileNameIn1); qint64 fileInSize = faIn.size(); if(faIn.exists()) { // Run the first preprocessor if(m_pOptions->m_PreProcessorCmd.isEmpty()) { // No preprocessing: Read the file directly: if(!m_normalData.readFile(faIn)) { errors.append(faIn.getStatusText()); return errors; } } else { QTemporaryFile tmpInPPFile; QString fileNameInPP = fileNameIn1; if(pEncoding1 != m_pOptions->m_pEncodingPP) { // Before running the preprocessor convert to the format that the preprocessor expects. FileAccess::createTempFile(tmpInPPFile); fileNameInPP = tmpInPPFile.fileName(); pEncoding1 = m_pOptions->m_pEncodingPP; convertFileEncoding(fileNameIn1, pEncoding, fileNameInPP, pEncoding1); } QString ppCmd = m_pOptions->m_PreProcessorCmd; FileAccess::createTempFile(fileOut1); fileNameOut1 = fileOut1.fileName(); QProcess ppProcess; ppProcess.setStandardInputFile(fileNameInPP); ppProcess.setStandardOutputFile(fileNameOut1); QString program; QStringList args; QString errorReason = Utils::getArguments(ppCmd, program, args); if(errorReason.isEmpty()) { ppProcess.start(program, args); ppProcess.waitForFinished(-1); } else errorReason = "\n(" + errorReason + ')'; bool bSuccess = errorReason.isEmpty() && m_normalData.readFile(fileNameOut1); if(fileInSize > 0 && (!bSuccess || m_normalData.m_size == 0)) { if(!m_normalData.readFile(faIn)) { errors.append(faIn.getStatusText()); errors.append(i18n(" Temp file is: %1", fileNameIn1)); return errors; } //Don't fail the preprocessor command if the file cann't be read. errors.append( i18n("Preprocessing possibly failed. Check this command:\n\n %1" "\n\nThe preprocessing command will be disabled now.", ppCmd) + errorReason); m_pOptions->m_PreProcessorCmd = ""; pEncoding1 = m_pEncoding; } } if(!m_normalData.preprocess(m_pOptions->m_bPreserveCarriageReturn, pEncoding1)) { errors.append(i18n("File %1 too large to process. Skipping.", fileNameIn1)); return errors; } //exit early for non text data further processing assumes a text file as input if(!m_normalData.isText()) return errors; // LineMatching Preprocessor if(!m_pOptions->m_LineMatchingPreProcessorCmd.isEmpty()) { QTemporaryFile tempOut2, fileInPP; fileNameIn2 = fileNameOut1.isEmpty() ? fileNameIn1 : fileNameOut1; QString fileNameInPP = fileNameIn2; pEncoding2 = pEncoding1; if(pEncoding2 != m_pOptions->m_pEncodingPP) { // Before running the preprocessor convert to the format that the preprocessor expects. FileAccess::createTempFile(fileInPP); fileNameInPP = fileInPP.fileName(); pEncoding2 = m_pOptions->m_pEncodingPP; convertFileEncoding(fileNameIn2, pEncoding1, fileNameInPP, pEncoding2); } QString ppCmd = m_pOptions->m_LineMatchingPreProcessorCmd; FileAccess::createTempFile(tempOut2); fileNameOut2 = tempOut2.fileName(); QProcess ppProcess; ppProcess.setStandardInputFile(fileNameInPP); ppProcess.setStandardOutputFile(fileNameOut2); QString program; QStringList args; QString errorReason = Utils::getArguments(ppCmd, program, args); if(errorReason.isEmpty()) { ppProcess.start(program, args); ppProcess.waitForFinished(-1); } else errorReason = "\n(" + errorReason + ')'; bool bSuccess = errorReason.isEmpty() && m_lmppData.readFile(fileNameOut2); if(FileAccess(fileNameIn2).size() > 0 && (!bSuccess || m_lmppData.m_size == 0)) { errors.append( i18n("The line-matching-preprocessing possibly failed. Check this command:\n\n %1" "\n\nThe line-matching-preprocessing command will be disabled now.", ppCmd) + errorReason); m_pOptions->m_LineMatchingPreProcessorCmd = ""; if(!m_lmppData.readFile(fileNameIn2)) { errors.append(i18n("Failed to read file: %1", fileNameIn2)); return errors; } } } else if(m_pOptions->m_bIgnoreComments || m_pOptions->m_bIgnoreCase) { // We need a copy of the normal data. m_lmppData.copyBufFrom(m_normalData); } } if(!m_lmppData.preprocess(false, pEncoding2)) { errors.append(i18n("File %1 too large to process. Skipping.", fileNameIn1)); return errors; } Q_ASSERT(m_lmppData.isText()); //TODO: Needed? if(m_lmppData.m_vSize < m_normalData.m_vSize) { // Preprocessing command may result in smaller data buffer so adjust size m_lmppData.m_v.resize((int)m_normalData.m_vSize); for(qint64 i = m_lmppData.m_vSize; i < m_normalData.m_vSize; ++i) { // Set all empty lines to point to the end of the buffer. m_lmppData.m_v[(int)i].setLine(m_lmppData.m_unicodeBuf.unicode() + m_lmppData.m_unicodeBuf.length()); } m_lmppData.m_vSize = m_normalData.m_vSize; } // Ignore comments if(m_pOptions->m_bIgnoreComments) { m_lmppData.removeComments(); qint64 vSize = std::min(m_normalData.m_vSize, m_lmppData.m_vSize); Q_ASSERT(vSize < std::numeric_limits::max()); for(int i = 0; i < vSize; ++i) { m_normalData.m_v[i].setPureComment(m_lmppData.m_v[i].isPureComment()); //Don't crash if vSize is too large. if(i == std::numeric_limits::max()) break; } } return errors; } /** Prepare the linedata vector for every input line.*/ bool SourceData::FileData::preprocess(bool bPreserveCR, QTextCodec* pEncoding) { qint64 i; // detect line end style QVector vOrigDataLineEndStyle; m_eLineEndStyle = eLineEndStyleUndefined; for(i = 0; i < m_size; ++i) { if(m_pBuf[i] == '\r') { if(i + 1 < m_size && m_pBuf[i + 1] == '\n') // not 16-bit unicode { vOrigDataLineEndStyle.push_back(eLineEndStyleDos); ++i; } else if(i > 0 && i + 2 < m_size && m_pBuf[i - 1] == '\0' && m_pBuf[i + 1] == '\0' && m_pBuf[i + 2] == '\n') // 16-bit unicode { vOrigDataLineEndStyle.push_back(eLineEndStyleDos); i += 2; } else // old mac line end style ? { vOrigDataLineEndStyle.push_back(eLineEndStyleUndefined); const_cast(m_pBuf)[i] = '\n'; // fix it in original data } } else if(m_pBuf[i] == '\n') { vOrigDataLineEndStyle.push_back(eLineEndStyleUnix); } } if(!vOrigDataLineEndStyle.isEmpty()) m_eLineEndStyle = vOrigDataLineEndStyle[0]; qint64 skipBytes = 0; QTextCodec* pCodec = detectEncoding(m_pBuf, m_size, skipBytes); if(pCodec != pEncoding) skipBytes = 0; if(m_size - skipBytes > INT_MAX) return false; QByteArray ba = QByteArray::fromRawData(m_pBuf + skipBytes, (int)(m_size - skipBytes)); QTextStream ts(ba, QIODevice::ReadOnly | QIODevice::Text); ts.setCodec(pEncoding); ts.setAutoDetectUnicode(false); m_unicodeBuf = ts.readAll(); ba.clear(); int ucSize = m_unicodeBuf.length(); const QChar* p = m_unicodeBuf.unicode(); m_bIsText = true; int lines = 1; m_bIncompleteConversion = false; for(i = 0; i < ucSize; ++i) { if(i >= ucSize || p[i] == '\n') { ++lines; } if(p[i].isNull()) { m_bIsText = false; } if(p[i] == QChar::ReplacementCharacter) { m_bIncompleteConversion = true; } } m_v.resize(lines + 5); int lineIdx = 0; int lineLength = 0; bool bNonWhiteFound = false; int whiteLength = 0; for(i = 0; i <= ucSize; ++i) { if(i >= ucSize || p[i] == '\n') { const QChar* pLine = &p[i - lineLength]; m_v[lineIdx].setLine(&p[i - lineLength]); while(/*!bPreserveCR &&*/ lineLength > 0 && m_v[lineIdx].getLine()[lineLength - 1] == '\r') { --lineLength; } m_v[lineIdx].setFirstNonWhiteChar(m_v[lineIdx].getLine() + std::min(whiteLength, lineLength)); if(lineIdx < vOrigDataLineEndStyle.count() && bPreserveCR && i < ucSize) { ++lineLength; const_cast(pLine)[lineLength] = '\r'; //switch ( vOrigDataLineEndStyle[lineIdx] ) //{ //case eLineEndStyleUnix: const_cast(pLine)[lineLength] = '\n'; break; //case eLineEndStyleDos: const_cast(pLine)[lineLength] = '\r'; break; //case eLineEndStyleUndefined: const_cast(pLine)[lineLength] = '\x0b'; break; //} } m_v[lineIdx].setSize(lineLength); lineLength = 0; bNonWhiteFound = false; whiteLength = 0; ++lineIdx; } else { ++lineLength; if(!bNonWhiteFound && isWhite(p[i])) ++whiteLength; else bNonWhiteFound = true; } } Q_ASSERT(lineIdx == lines); m_vSize = lines; return true; } // Must not be entered, when within a comment. // Returns either at a newline-character p[i]=='\n' or when i==size. // A line that contains only comments is still "white". // Comments in white lines must remain, while comments in // non-white lines are overwritten with spaces. void SourceData::FileData::checkLineForComments( const QChar* p, // pointer to start of buffer int& i, // index of current position (in, out) int size, // size of buffer bool& bWhite, // false if this line contains nonwhite characters (in, out) bool& bCommentInLine, // true if any comment is within this line (in, out) bool& bStartsOpenComment // true if the line ends within an comment (out) - ) +) { bStartsOpenComment = false; for(; i < size; ++i) { // A single apostroph ' has prio over a double apostroph " (e.g. '"') // (if not in a string) if(p[i] == '\'') { bWhite = false; ++i; for(; !isLineOrBufEnd(p, i, size) && p[i] != '\''; ++i) ; if(p[i] == '\'') ++i; } // Strings have priority over comments: e.g. "/* Not a comment, but a string. */" else if(p[i] == '"') { bWhite = false; ++i; for(; !isLineOrBufEnd(p, i, size) && !(p[i] == '"' && p[i - 1] != '\\'); ++i) ; if(p[i] == '"') ++i; } // C++-comment else if(p[i] == '/' && i + 1 < size && p[i + 1] == '/') { int commentStart = i; bCommentInLine = true; i += 2; for(; !isLineOrBufEnd(p, i, size); ++i) ; if(!bWhite) { size = i - commentStart; m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size)); } return; } // C-comment else if(p[i] == '/' && i + 1 < size && p[i + 1] == '*') { int commentStart = i; bCommentInLine = true; i += 2; for(; !isLineOrBufEnd(p, i, size); ++i) { if(i + 1 < size && p[i] == '*' && p[i + 1] == '/') // end of the comment { i += 2; // More comments in the line? checkLineForComments(p, i, size, bWhite, bCommentInLine, bStartsOpenComment); if(!bWhite) { size = i - commentStart; - m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size)); } + m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size)); + } return; } } bStartsOpenComment = true; return; } if(isLineOrBufEnd(p, i, size)) { return; } else if(!p[i].isSpace()) { bWhite = false; } } } // Modifies the input data, and replaces C/C++ comments with whitespace // when the line contains other data too. If the line contains only // a comment or white data, remember this in the flag bContainsPureComment. void SourceData::FileData::removeComments() { int line = 0; const QChar* p = m_unicodeBuf.unicode(); bool bWithinComment = false; int size = m_unicodeBuf.length(); for(int i = 0; i < size; ++i) { // std::cout << "2 " << std::string(&p[i], m_v[line].size) << std::endl; bool bWhite = true; bool bCommentInLine = false; if(bWithinComment) { int commentStart = i; bCommentInLine = true; for(; !isLineOrBufEnd(p, i, size); ++i) { if(i + 1 < size && p[i] == '*' && p[i + 1] == '/') // end of the comment { i += 2; // More comments in the line? checkLineForComments(p, i, size, bWhite, bCommentInLine, bWithinComment); if(!bWhite) { size = i - commentStart; m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size)); } break; } } } else { checkLineForComments(p, i, size, bWhite, bCommentInLine, bWithinComment); } // end of line Q_ASSERT(isLineOrBufEnd(p, i, size)); m_v[line].setPureComment(bCommentInLine && bWhite); /* std::cout << line << " : " << ( bCommentInLine ? "c" : " " ) << ( bWhite ? "w " : " ") << std::string(pLD[line].pLine, pLD[line].size) << std::endl;*/ ++line; } } bool SourceData::isLineOrBufEnd(const QChar* p, int i, int size) { - return i >= size // End of file + return i >= size // End of file || Utils::isEndOfLine(p[i]) // Normal end of line // No support for Mac-end of line yet, because incompatible with GNU-diff-routines. // || ( p[i]=='\r' && (i>=size-1 || p[i+1]!='\n') // && (i==0 || p[i-1]!='\n') ) // Special case: '\r' without '\n' ; } // Convert the input file from input encoding to output encoding and write it to the output file. bool SourceData::convertFileEncoding(const QString& fileNameIn, QTextCodec* pCodecIn, - const QString& fileNameOut, QTextCodec* pCodecOut) + const QString& fileNameOut, QTextCodec* pCodecOut) { QFile in(fileNameIn); if(!in.open(QIODevice::ReadOnly)) return false; QTextStream inStream(&in); inStream.setCodec(pCodecIn); inStream.setAutoDetectUnicode(false); QFile out(fileNameOut); if(!out.open(QIODevice::WriteOnly)) return false; QTextStream outStream(&out); outStream.setCodec(pCodecOut); QString data = inStream.readAll(); outStream << data; return true; } QTextCodec* SourceData::getEncodingFromTag(const QByteArray& s, const QByteArray& encodingTag) { int encodingPos = s.indexOf(encodingTag); if(encodingPos >= 0) { int apostrophPos = s.indexOf('"', encodingPos + encodingTag.length()); int apostroph2Pos = s.indexOf('\'', encodingPos + encodingTag.length()); char apostroph = '"'; if(apostroph2Pos >= 0 && (apostrophPos < 0 || apostroph2Pos < apostrophPos)) { apostroph = '\''; apostrophPos = apostroph2Pos; } int encodingEnd = s.indexOf(apostroph, apostrophPos + 1); if(encodingEnd >= 0) // e.g.: or { QByteArray encoding = s.mid(apostrophPos + 1, encodingEnd - (apostrophPos + 1)); return QTextCodec::codecForName(encoding); } else // e.g.: { QByteArray encoding = s.mid(encodingPos + encodingTag.length(), apostrophPos - (encodingPos + encodingTag.length())); return QTextCodec::codecForName(encoding); } } return nullptr; } QTextCodec* SourceData::detectEncoding(const char* buf, qint64 size, qint64& skipBytes) { if(size >= 2) { if(buf[0] == '\xFF' && buf[1] == '\xFE') { skipBytes = 2; return QTextCodec::codecForName("UTF-16LE"); } if(buf[0] == '\xFE' && buf[1] == '\xFF') { skipBytes = 2; return QTextCodec::codecForName("UTF-16BE"); } } if(size >= 3) { if(buf[0] == '\xEF' && buf[1] == '\xBB' && buf[2] == '\xBF') { skipBytes = 3; return QTextCodec::codecForName("UTF-8-BOM"); } } skipBytes = 0; QByteArray s; /* We don't need the whole file here just the header. ] */ if(size <= 5000) - s=QByteArray(buf, (int)size); + s = QByteArray(buf, (int)size); else - s=QByteArray(buf, 5000); + s = QByteArray(buf, 5000); int xmlHeaderPos = s.indexOf("= 0) { int xmlHeaderEnd = s.indexOf("?>", xmlHeaderPos); if(xmlHeaderEnd >= 0) { QTextCodec* pCodec = getEncodingFromTag(s.mid(xmlHeaderPos, xmlHeaderEnd - xmlHeaderPos), "encoding="); if(pCodec) return pCodec; } } else // HTML { int metaHeaderPos = s.indexOf("= 0) { int metaHeaderEnd = s.indexOf(">", metaHeaderPos); if(metaHeaderEnd >= 0) { QTextCodec* pCodec = getEncodingFromTag(s.mid(metaHeaderPos, metaHeaderEnd - metaHeaderPos), "charset="); if(pCodec) return pCodec; metaHeaderPos = s.indexOf(" * * Copyright (C) 2018 Michael Reeves reeves.87@gmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "diff.h" #include "Utils.h" #include "fileaccess.h" #include "gnudiff_diff.h" #include "options.h" #include "progress.h" #include #include #include -#include #include #include +#include int LineData::width(int tabSize) const { int w = 0; int j = 0; for(int i = 0; i < size(); ++i) { if(pLine[i] == '\t') { for(j %= tabSize; j < tabSize; ++j) ++w; j = 0; } else { ++w; ++j; } } return w; } // The bStrict flag is true during the test where a nonmatching area ends. // Then the equal()-function requires that the match has more than 2 nonwhite characters. // This is to avoid matches on trivial lines (e.g. with white space only). // This choice is good for C/C++. bool LineData::equal(const LineData& l1, const LineData& l2, bool bStrict) { if(l1.getLine() == nullptr || l2.getLine() == nullptr) return false; if(bStrict && g_bIgnoreTrivialMatches) return false; // Ignore white space diff const QChar* p1 = l1.getLine(); const QChar* p1End = p1 + l1.size(); const QChar* p2 = l2.getLine(); const QChar* p2End = p2 + l2.size(); if(g_bIgnoreWhiteSpace) { int nonWhite = 0; for(;;) { while(isWhite(*p1) && p1 != p1End) ++p1; while(isWhite(*p2) && p2 != p2End) ++p2; if(p1 == p1End && p2 == p2End) { if(bStrict && g_bIgnoreTrivialMatches) { // Then equality is not enough return nonWhite > 2; } else // equality is enough return true; } else if(p1 == p1End || p2 == p2End) return false; if(*p1 != *p2) return false; ++p1; ++p2; ++nonWhite; } } else { return (l1.size() == l2.size() && memcmp(p1, p2, l1.size()) == 0); } } // First step void calcDiff3LineListUsingAB( const DiffList* pDiffListAB, Diff3LineList& d3ll) { // First make d3ll for AB (from pDiffListAB) DiffList::const_iterator i = pDiffListAB->begin(); LineRef::LineType lineA = 0; LineRef::LineType lineB = 0; Diff d(0, 0, 0); for(;;) { if(d.nofEquals == 0 && d.diff1 == 0 && d.diff2 == 0) { if(i != pDiffListAB->end()) { d = *i; ++i; } else break; } Diff3Line d3l; if(d.nofEquals > 0) { d3l.bAEqB = true; d3l.setLineA(lineA); d3l.setLineB(lineB); --d.nofEquals; ++lineA; ++lineB; } else if(d.diff1 > 0 && d.diff2 > 0) { d3l.setLineA(lineA); d3l.setLineB(lineB); --d.diff1; --d.diff2; ++lineA; ++lineB; } else if(d.diff1 > 0) { d3l.setLineA(lineA); --d.diff1; ++lineA; } else if(d.diff2 > 0) { d3l.setLineB(lineB); --d.diff2; ++lineB; } Q_ASSERT(d.nofEquals >= 0); d3ll.push_back(d3l); } } // Second step void calcDiff3LineListUsingAC( const DiffList* pDiffListAC, Diff3LineList& d3ll) { //////////////// // Now insert data from C using pDiffListAC DiffList::const_iterator i = pDiffListAC->begin(); Diff3LineList::iterator i3 = d3ll.begin(); LineRef::LineType lineA = 0; LineRef::LineType lineC = 0; Diff d(0, 0, 0); for(;;) { if(d.nofEquals == 0 && d.diff1 == 0 && d.diff2 == 0) { if(i != pDiffListAC->end()) { d = *i; ++i; } else break; } Diff3Line d3l; if(d.nofEquals > 0) { // Find the corresponding lineA while(i3->getLineA() != lineA) ++i3; i3->setLineC(lineC); i3->bAEqC = true; i3->bBEqC = i3->isEqualAB(); --d.nofEquals; ++lineA; ++lineC; ++i3; } else if(d.diff1 > 0 && d.diff2 > 0) { d3l.setLineC(lineC); d3ll.insert(i3, d3l); --d.diff1; --d.diff2; ++lineA; ++lineC; } else if(d.diff1 > 0) { --d.diff1; ++lineA; } else if(d.diff2 > 0) { d3l.setLineC(lineC); d3ll.insert(i3, d3l); --d.diff2; ++lineC; } } } // Third step void calcDiff3LineListUsingBC( const DiffList* pDiffListBC, Diff3LineList& d3ll) { //////////////// // Now improve the position of data from C using pDiffListBC // If a line from C equals a line from A then it is in the // same Diff3Line already. // If a line from C equals a line from B but not A, this // information will be used here. DiffList::const_iterator i = pDiffListBC->begin(); Diff3LineList::iterator i3b = d3ll.begin(); Diff3LineList::iterator i3c = d3ll.begin(); LineRef::LineType lineB = 0; LineRef::LineType lineC = 0; Diff d(0, 0, 0); for(;;) { if(d.nofEquals == 0 && d.diff1 == 0 && d.diff2 == 0) { if(i != pDiffListBC->end()) { d = *i; ++i; } else break; } Diff3Line d3l; if(d.nofEquals > 0) { // Find the corresponding lineB and lineC while(i3b != d3ll.end() && i3b->getLineB() != lineB) ++i3b; while(i3c != d3ll.end() && i3c->getLineC() != lineC) ++i3c; Q_ASSERT(i3b != d3ll.end()); Q_ASSERT(i3c != d3ll.end()); if(i3b == i3c) { Q_ASSERT(i3b->getLineC() == lineC); i3b->bBEqC = true; } else { // Is it possible to move this line up? // Test if no other B's are used between i3c and i3b // First test which is before: i3c or i3b ? Diff3LineList::iterator i3c1 = i3c; Diff3LineList::iterator i3b1 = i3b; while(i3c1 != i3b && i3b1 != i3c) { Q_ASSERT(i3b1 != d3ll.end() || i3c1 != d3ll.end()); if(i3c1 != d3ll.end()) ++i3c1; if(i3b1 != d3ll.end()) ++i3b1; } if(i3c1 == i3b && !i3b->isEqualAB()) // i3c before i3b { Diff3LineList::iterator i3 = i3c; int nofDisturbingLines = 0; while(i3 != i3b && i3 != d3ll.end()) { if(i3->getLineB().isValid()) ++nofDisturbingLines; ++i3; } if(nofDisturbingLines > 0) //&& nofDisturbingLines < d.nofEquals*d.nofEquals+4 ) { Diff3LineList::iterator i3_last_equal_A = d3ll.end(); i3 = i3c; while(i3 != i3b) { if(i3->isEqualAB()) { i3_last_equal_A = i3; } ++i3; } /* If i3_last_equal_A isn't still set to d3ll.end(), then * we've found a line in A that is equal to one in B * somewhere between i3c and i3b */ bool before_or_on_equal_line_in_A = (i3_last_equal_A != d3ll.end()); // Move the disturbing lines up, out of sight. i3 = i3c; while(i3 != i3b) { if(i3->getLineB().isValid() || (before_or_on_equal_line_in_A && i3->getLineA().isValid())) { d3l.setLineB(i3->getLineB()); i3->getLineB().invalidate(); // Move A along if it matched B if(before_or_on_equal_line_in_A) { d3l.setLineA(i3->getLineA()); d3l.bAEqB = i3->isEqualAB(); i3->getLineA().invalidate(); i3->bAEqC = false; } i3->bAEqB = false; i3->bBEqC = false; d3ll.insert(i3c, d3l); } if(i3 == i3_last_equal_A) { before_or_on_equal_line_in_A = false; } ++i3; } nofDisturbingLines = 0; } if(nofDisturbingLines == 0) { // Yes, the line from B can be moved. i3b->getLineB().invalidate(); // This might leave an empty line: removed later. i3b->bAEqB = false; i3b->bBEqC = false; i3c->setLineB(lineB); i3c->bBEqC = true; i3c->bAEqB = i3c->isEqualAC(); } } else if(i3b1 == i3c && !i3c->isEqualAC()) { Diff3LineList::iterator i3 = i3b; int nofDisturbingLines = 0; while(i3 != i3c && i3 != d3ll.end()) { if(i3->getLineC().isValid()) ++nofDisturbingLines; ++i3; } if(nofDisturbingLines > 0) //&& nofDisturbingLines < d.nofEquals*d.nofEquals+4 ) { Diff3LineList::iterator i3_last_equal_A = d3ll.end(); i3 = i3b; while(i3 != i3c) { if(i3->isEqualAC()) { i3_last_equal_A = i3; } ++i3; } /* If i3_last_equal_A isn't still set to d3ll.end(), then * we've found a line in A that is equal to one in C * somewhere between i3b and i3c */ bool before_or_on_equal_line_in_A = (i3_last_equal_A != d3ll.end()); // Move the disturbing lines up. i3 = i3b; while(i3 != i3c) { if(i3->getLineC().isValid() || (before_or_on_equal_line_in_A && i3->getLineA().isValid())) { d3l.setLineC(i3->getLineC()); i3->getLineC().invalidate(); // Move A along if it matched C if(before_or_on_equal_line_in_A) { d3l.setLineA(i3->getLineA()); d3l.bAEqC = i3->isEqualAC(); i3->getLineA().invalidate(); i3->bAEqB = false; } i3->bAEqC = false; i3->bBEqC = false; d3ll.insert(i3b, d3l); } if(i3 == i3_last_equal_A) { before_or_on_equal_line_in_A = false; } ++i3; } nofDisturbingLines = 0; } if(nofDisturbingLines == 0) { // Yes, the line from C can be moved. i3c->getLineC().invalidate(); // This might leave an empty line: removed later. i3c->bAEqC = false; i3c->bBEqC = false; i3b->setLineC(lineC); i3b->bBEqC = true; i3b->bAEqC = i3b->isEqualAB(); } } } --d.nofEquals; ++lineB; ++lineC; ++i3b; ++i3c; } else if(d.diff1 > 0) { Diff3LineList::iterator i3 = i3b; while(i3->getLineB() != lineB) ++i3; if(i3 != i3b && !i3->isEqualAB()) { // Take B from this line and move it up as far as possible d3l.setLineB(lineB); d3ll.insert(i3b, d3l); i3->getLineB().invalidate(); } else { i3b = i3; } --d.diff1; ++lineB; ++i3b; if(d.diff2 > 0) { --d.diff2; ++lineC; } } else if(d.diff2 > 0) { --d.diff2; ++lineC; } } /* Diff3LineList::iterator it = d3ll.begin(); int li=0; for( ; it!=d3ll.end(); ++it, ++li ) { printf( "%4d %4d %4d %4d A%c=B A%c=C B%c=C\n", li, it->getLineA(), it->getLineB(), it->getLineC(), it->isEqualAB() ? '=' : '!', it->isEqualAC() ? '=' : '!', it->isEqualBC() ? '=' : '!' ); } printf("\n");*/ } // Test if the move would pass a barrier. Return true if not. bool ManualDiffHelpList::isValidMove(int line1, int line2, e_SrcSelector winIdx1, e_SrcSelector winIdx2) const { if(line1 >= 0 && line2 >= 0) { ManualDiffHelpList::const_iterator i; for(i = begin(); i != end(); ++i) { const ManualDiffHelpEntry& mdhe = *i; if(!mdhe.isValidMove(line1, line2, winIdx1, winIdx2)) return false; } } return true; // no barrier passed. } bool ManualDiffHelpEntry::isValidMove(int line1, int line2, e_SrcSelector winIdx1, e_SrcSelector winIdx2) const { // Barrier int l1 = winIdx1 == A ? lineA1 : winIdx1 == B ? lineB1 : lineC1; int l2 = winIdx2 == A ? lineA1 : winIdx2 == B ? lineB1 : lineC1; if(l1 >= 0 && l2 >= 0) { if((line1 >= l1 && line2 < l2) || (line1 < l1 && line2 >= l2)) return false; l1 = winIdx1 == A ? lineA2 : winIdx1 == B ? lineB2 : lineC2; l2 = winIdx2 == A ? lineA2 : winIdx2 == B ? lineB2 : lineC2; ++l1; ++l2; if((line1 >= l1 && line2 < l2) || (line1 < l1 && line2 >= l2)) return false; } return true; } static bool runDiff(const LineData* p1, LineRef size1, const LineData* p2, LineRef size2, DiffList& diffList, Options* pOptions) { ProgressProxy pp; static GnuDiff gnuDiff; // All values are initialized with zeros. pp.setCurrent(0); diffList.clear(); if(p1 == nullptr || p1[0].getLine() == nullptr || p2 == nullptr || p2[0].getLine() == nullptr || size1 == 0 || size2 == 0) { Diff d(0, 0, 0); if(p1 != nullptr && p2 != nullptr && p1[0].getLine() == nullptr && p2[0].getLine() == nullptr && size1 == size2) d.nofEquals = size1; else { d.diff1 = size1; d.diff2 = size2; } diffList.push_back(d); } else { GnuDiff::comparison comparisonInput; memset(&comparisonInput, 0, sizeof(comparisonInput)); comparisonInput.parent = nullptr; - comparisonInput.file[0].buffer = p1[0].getLine(); //ptr to buffer + comparisonInput.file[0].buffer = p1[0].getLine(); //ptr to buffer comparisonInput.file[0].buffered = (p1[size1 - 1].getLine() - p1[0].getLine() + p1[size1 - 1].size()); // size of buffer - comparisonInput.file[1].buffer = p2[0].getLine(); //ptr to buffer + comparisonInput.file[1].buffer = p2[0].getLine(); //ptr to buffer comparisonInput.file[1].buffered = (p2[size2 - 1].getLine() - p2[0].getLine() + p2[size2 - 1].size()); // size of buffer gnuDiff.ignore_white_space = GnuDiff::IGNORE_ALL_SPACE; // I think nobody needs anything else ... gnuDiff.bIgnoreWhiteSpace = true; gnuDiff.bIgnoreNumbers = pOptions->m_bIgnoreNumbers; gnuDiff.minimal = pOptions->m_bTryHard; gnuDiff.ignore_case = false; GnuDiff::change* script = gnuDiff.diff_2_files(&comparisonInput); LineRef equalLinesAtStart = (LineRef)comparisonInput.file[0].prefix_lines; LineRef currentLine1 = 0; LineRef currentLine2 = 0; GnuDiff::change* p = nullptr; for(GnuDiff::change* e = script; e; e = p) { Diff d(0, 0, 0); d.nofEquals = (LineRef)(e->line0 - currentLine1); Q_ASSERT(d.nofEquals == e->line1 - currentLine2); d.diff1 = e->deleted; d.diff2 = e->inserted; currentLine1 += (LineRef)(d.nofEquals + d.diff1); currentLine2 += (LineRef)(d.nofEquals + d.diff2); diffList.push_back(d); p = e->link; free(e); } if(diffList.empty()) { Diff d(0, 0, 0); d.nofEquals = std::min(size1, size2); d.diff1 = size1 - d.nofEquals; d.diff2 = size2 - d.nofEquals; diffList.push_back(d); } else { diffList.front().nofEquals += equalLinesAtStart; currentLine1 += equalLinesAtStart; currentLine2 += equalLinesAtStart; LineRef nofEquals = std::min(size1 - currentLine1, size2 - currentLine2); if(nofEquals == 0) { diffList.back().diff1 += size1 - currentLine1; diffList.back().diff2 += size2 - currentLine2; } else { Diff d(nofEquals, size1 - currentLine1 - nofEquals, size2 - currentLine2 - nofEquals); diffList.push_back(d); } } } // Verify difflist { LineRef::LineType l1 = 0; LineRef::LineType l2 = 0; DiffList::iterator i; for(i = diffList.begin(); i != diffList.end(); ++i) { l1 += i->nofEquals + i->diff1; l2 += i->nofEquals + i->diff2; } Q_ASSERT(l1 == size1 && l2 == size2); } pp.setCurrent(1); return true; } bool ManualDiffHelpList::runDiff(const LineData* p1, LineRef size1, const LineData* p2, LineRef size2, DiffList& diffList, - e_SrcSelector winIdx1, e_SrcSelector winIdx2, - Options* pOptions) + e_SrcSelector winIdx1, e_SrcSelector winIdx2, + Options* pOptions) { diffList.clear(); DiffList diffList2; int l1begin = 0; int l2begin = 0; ManualDiffHelpList::const_iterator i; for(i = begin(); i != end(); ++i) { const ManualDiffHelpEntry& mdhe = *i; int l1end = mdhe.getLine1(winIdx1); int l2end = mdhe.getLine1(winIdx2); if(l1end >= 0 && l2end >= 0) { ::runDiff(p1 + l1begin, l1end - l1begin, p2 + l2begin, l2end - l2begin, diffList2, pOptions); diffList.splice(diffList.end(), diffList2); l1begin = l1end; l2begin = l2end; l1end = mdhe.getLine2(winIdx1); l2end = mdhe.getLine2(winIdx2); if(l1end >= 0 && l2end >= 0) { ++l1end; // point to line after last selected line ++l2end; ::runDiff(p1 + l1begin, l1end - l1begin, p2 + l2begin, l2end - l2begin, diffList2, pOptions); diffList.splice(diffList.end(), diffList2); l1begin = l1end; l2begin = l2end; } } } ::runDiff(p1 + l1begin, size1 - l1begin, p2 + l2begin, size2 - l2begin, diffList2, pOptions); diffList.splice(diffList.end(), diffList2); return true; } void correctManualDiffAlignment(Diff3LineList& d3ll, ManualDiffHelpList* pManualDiffHelpList) { if(pManualDiffHelpList->empty()) return; // If a line appears unaligned in comparison to the manual alignment, correct this. ManualDiffHelpList::iterator iMDHL; for(iMDHL = pManualDiffHelpList->begin(); iMDHL != pManualDiffHelpList->end(); ++iMDHL) { Diff3LineList::iterator i3 = d3ll.begin(); e_SrcSelector missingWinIdx = None; int alignedSum = (iMDHL->getLine1(A) < 0 ? 0 : 1) + (iMDHL->getLine1(B) < 0 ? 0 : 1) + (iMDHL->getLine1(C) < 0 ? 0 : 1); if(alignedSum == 2) { // If only A & B are aligned then let C rather be aligned with A // If only A & C are aligned then let B rather be aligned with A // If only B & C are aligned then let A rather be aligned with B missingWinIdx = iMDHL->getLine1(A) < 0 ? A : (iMDHL->getLine1(B) < 0 ? B : C); } else if(alignedSum <= 1) { return; } // At the first aligned line, move up the two other lines into new d3ls until the second input is aligned // Then move up the third input until all three lines are aligned. int wi = None; for(; i3 != d3ll.end(); ++i3) { for(wi = A; wi <= Max; ++wi) { if(i3->getLineInFile((e_SrcSelector)wi) >= 0 && iMDHL->firstLine((e_SrcSelector)wi) == i3->getLineInFile((e_SrcSelector)wi)) break; } if(wi <= Max) break; } if(wi >= A && wi <= Max) { // Found manual alignment for one source Diff3LineList::iterator iDest = i3; // Move lines up until the next firstLine is found. Omit wi from move and search. int wi2 = None; for(; i3 != d3ll.end(); ++i3) { for(wi2 = A; wi2 <= C; ++wi2) { if(wi != wi2 && i3->getLineInFile((e_SrcSelector)wi2) >= 0 && iMDHL->firstLine((e_SrcSelector)wi2) == i3->getLineInFile((e_SrcSelector)wi2)) break; } if(wi2 > C) { // Not yet found // Move both others up Diff3Line d3l; // Move both up if(wi == A) // Move B and C up { d3l.bBEqC = i3->isEqualBC(); d3l.setLineB(i3->getLineB()); d3l.setLineC(i3->getLineC()); i3->getLineB().invalidate(); i3->getLineC().invalidate(); } if(wi == B) // Move A and C up { d3l.bAEqC = i3->isEqualAC(); d3l.setLineA(i3->getLineA()); d3l.setLineC(i3->getLineC()); i3->getLineA().invalidate(); i3->getLineC().invalidate(); } if(wi == C) // Move A and B up { d3l.bAEqB = i3->isEqualAB(); d3l.setLineA(i3->getLineA()); d3l.setLineB(i3->getLineB()); i3->getLineA().invalidate(); i3->getLineB().invalidate(); } i3->bAEqB = false; i3->bAEqC = false; i3->bBEqC = false; d3ll.insert(iDest, d3l); } else { // align the found line with the line we already have here if(i3 != iDest) { if(wi2 == A) { iDest->setLineA(i3->getLineA()); i3->getLineA().invalidate(); i3->bAEqB = false; i3->bAEqC = false; } else if(wi2 == B) { iDest->setLineB(i3->getLineB()); i3->getLineB().invalidate(); i3->bAEqB = false; i3->bBEqC = false; } else if(wi2 == C) { iDest->setLineC(i3->getLineC()); i3->getLineC().invalidate(); i3->bBEqC = false; i3->bAEqC = false; } } if(missingWinIdx != 0) { for(; i3 != d3ll.end(); ++i3) { e_SrcSelector wi3 = missingWinIdx; if(i3->getLineInFile((e_SrcSelector)wi3) >= 0) { // not found, move the line before iDest Diff3Line d3l; if(wi3 == A) { if(i3->isEqualAB()) // Stop moving lines up if one equal is found. break; d3l.setLineA(i3->getLineA()); i3->getLineA().invalidate(); i3->bAEqB = false; i3->bAEqC = false; } if(wi3 == B) { if(i3->isEqualAB()) break; d3l.setLineB(i3->getLineB()); i3->getLineB().invalidate(); i3->bAEqB = false; i3->bBEqC = false; } if(wi3 == C) { if(i3->isEqualAC()) break; d3l.setLineC(i3->getLineC()); i3->getLineC().invalidate(); i3->bAEqC = false; i3->bBEqC = false; } d3ll.insert(iDest, d3l); } } // for(), searching for wi3 } break; } } // for(), searching for wi2 } // if, wi found } // for (iMDHL) } // Fourth step void calcDiff3LineListTrim( Diff3LineList& d3ll, const LineData* pldA, const LineData* pldB, const LineData* pldC, ManualDiffHelpList* pManualDiffHelpList) { const Diff3Line d3l_empty; d3ll.remove(d3l_empty); Diff3LineList::iterator i3 = d3ll.begin(); Diff3LineList::iterator i3A = d3ll.begin(); Diff3LineList::iterator i3B = d3ll.begin(); Diff3LineList::iterator i3C = d3ll.begin(); int line = 0; // diff3line counters int lineA = 0; // int lineB = 0; int lineC = 0; ManualDiffHelpList::iterator iMDHL = pManualDiffHelpList->begin(); // The iterator i3 and the variable line look ahead. // The iterators i3A, i3B, i3C and corresponding lineA, lineB and lineC stop at empty lines, if found. // If possible, then the texts from the look ahead will be moved back to the empty places. for(; i3 != d3ll.end(); ++i3, ++line) { if(iMDHL != pManualDiffHelpList->end()) { if((i3->getLineA().isValid() && i3->getLineA() == iMDHL->getLine1(A)) || (i3->getLineB().isValid() && i3->getLineB() == iMDHL->getLine1(B)) || (i3->getLineC().isValid() && i3->getLineC() == iMDHL->getLine1(C))) { i3A = i3; i3B = i3; i3C = i3; lineA = line; lineB = line; lineC = line; ++iMDHL; } } if(line > lineA && i3->getLineA().isValid() && (*i3A).getLineB().isValid() && (*i3A).isEqualBC() && LineData::equal(pldA[i3->getLineA()], pldB[(*i3A).getLineB()], false) && pManualDiffHelpList->isValidMove(i3->getLineA(), (*i3A).getLineB(), A, B) && pManualDiffHelpList->isValidMove(i3->getLineA(), (*i3A).getLineC(), A, C)) { // Empty space for A. A matches B and C in the empty line. Move it up. (*i3A).setLineA(i3->getLineA()); (*i3A).bAEqB = true; (*i3A).bAEqC = true; i3->getLineA().invalidate(); i3->bAEqB = false; i3->bAEqC = false; ++i3A; ++lineA; } if(line > lineB && i3->getLineB().isValid() && (*i3B).getLineA().isValid() && (*i3B).isEqualAC() && LineData::equal(pldB[i3->getLineB()], pldA[(*i3B).getLineA()], false) && pManualDiffHelpList->isValidMove(i3->getLineB(), (*i3B).getLineA(), B, A) && pManualDiffHelpList->isValidMove(i3->getLineB(), (*i3B).getLineC(), B, C)) { // Empty space for B. B matches A and C in the empty line. Move it up. (*i3B).setLineB(i3->getLineB()); (*i3B).bAEqB = true; (*i3B).bBEqC = true; i3->getLineB().invalidate(); i3->bAEqB = false; i3->bBEqC = false; ++i3B; ++lineB; } if(line > lineC && i3->getLineC().isValid() && (*i3C).getLineA().isValid() && (*i3C).isEqualAB() && LineData::equal(pldC[i3->getLineC()], pldA[(*i3C).getLineA()], false) && pManualDiffHelpList->isValidMove(i3->getLineC(), (*i3C).getLineA(), C, A) && pManualDiffHelpList->isValidMove(i3->getLineC(), (*i3C).getLineB(), C, B)) { // Empty space for C. C matches A and B in the empty line. Move it up. (*i3C).setLineC(i3->getLineC()); (*i3C).bAEqC = true; (*i3C).bBEqC = true; i3->getLineC().invalidate(); i3->bAEqC = false; i3->bBEqC = false; ++i3C; ++lineC; } if(line > lineA && i3->getLineA().isValid() && !i3->isEqualAB() && !i3->isEqualAC() && pManualDiffHelpList->isValidMove(i3->getLineA(), (*i3A).getLineB(), A, B) && - pManualDiffHelpList->isValidMove(i3->getLineA(), (*i3A).getLineC(), A, C)) { + pManualDiffHelpList->isValidMove(i3->getLineA(), (*i3A).getLineC(), A, C)) + { // Empty space for A. A doesn't match B or C. Move it up. (*i3A).setLineA(i3->getLineA()); i3->getLineA().invalidate(); if(i3A->getLineB().isValid() && LineData::equal(pldA[i3A->getLineA()], pldB[i3A->getLineB()], false)) { i3A->bAEqB = true; } if((i3A->isEqualAB() && i3A->isEqualBC()) || (i3A->getLineC().isValid() && LineData::equal(pldA[i3A->getLineA()], pldC[i3A->getLineC()], false))) { i3A->bAEqC = true; } ++i3A; ++lineA; } if(line > lineB && i3->getLineB().isValid() && !i3->isEqualAB() && !i3->isEqualBC() && pManualDiffHelpList->isValidMove(i3->getLineB(), (*i3B).getLineA(), B, A) && pManualDiffHelpList->isValidMove(i3->getLineB(), (*i3B).getLineC(), B, C)) { // Empty space for B. B matches neither A nor C. Move B up. (*i3B).setLineB(i3->getLineB()); i3->getLineB().invalidate(); if(i3B->getLineA().isValid() && LineData::equal(pldA[i3B->getLineA()], pldB[i3B->getLineB()], false)) { i3B->bAEqB = true; } if((i3B->isEqualAB() && i3B->isEqualAC()) || (i3B->getLineC().isValid() && LineData::equal(pldB[i3B->getLineB()], pldC[i3B->getLineC()], false))) { i3B->bBEqC = true; } ++i3B; ++lineB; } if(line > lineC && i3->getLineC().isValid() && !i3->isEqualAC() && !i3->isEqualBC() && - pManualDiffHelpList->isValidMove( i3->getLineC(), (*i3C).getLineA(), C, A) && - pManualDiffHelpList->isValidMove( i3->getLineC(), (*i3C).getLineB(), C, B)) + pManualDiffHelpList->isValidMove(i3->getLineC(), (*i3C).getLineA(), C, A) && + pManualDiffHelpList->isValidMove(i3->getLineC(), (*i3C).getLineB(), C, B)) { // Empty space for C. C matches neither A nor B. Move C up. (*i3C).setLineC(i3->getLineC()); i3->getLineC().invalidate(); if(i3C->getLineA().isValid() && LineData::equal(pldA[i3C->getLineA()], pldC[i3C->getLineC()], false)) { i3C->bAEqC = true; } if((i3C->isEqualAC() && i3C->isEqualAB()) || (i3C->getLineB().isValid() && LineData::equal(pldB[i3C->getLineB()], pldC[i3C->getLineC()], false))) { i3C->bBEqC = true; } ++i3C; ++lineC; } if(line > lineA && line > lineB && i3->getLineA().isValid() && i3->isEqualAB() && !i3->isEqualAC()) { // Empty space for A and B. A matches B, but not C. Move A & B up. Diff3LineList::iterator i = lineA > lineB ? i3A : i3B; int l = lineA > lineB ? lineA : lineB; - if(pManualDiffHelpList->isValidMove( i->getLineC(), i3->getLineA(), C, A) && - pManualDiffHelpList->isValidMove( i->getLineC(), i3->getLineB(), C, B)) + if(pManualDiffHelpList->isValidMove(i->getLineC(), i3->getLineA(), C, A) && + pManualDiffHelpList->isValidMove(i->getLineC(), i3->getLineB(), C, B)) { (*i).setLineA(i3->getLineA()); (*i).setLineB(i3->getLineB()); (*i).bAEqB = true; if(i->getLineC().isValid() && LineData::equal(pldA[i->getLineA()], pldC[i->getLineC()], false)) { (*i).bAEqC = true; (*i).bBEqC = true; } i3->getLineA().invalidate(); i3->getLineB().invalidate(); i3->bAEqB = false; i3A = i; i3B = i; ++i3A; ++i3B; lineA = l + 1; lineB = l + 1; } } else if(line > lineA && line > lineC && i3->getLineA().isValid() && i3->isEqualAC() && !i3->isEqualAB()) { // Empty space for A and C. A matches C, but not B. Move A & C up. Diff3LineList::iterator i = lineA > lineC ? i3A : i3C; int l = lineA > lineC ? lineA : lineC; if(pManualDiffHelpList->isValidMove(i->getLineB(), i3->getLineA(), B, A) && pManualDiffHelpList->isValidMove(i->getLineB(), i3->getLineC(), B, C)) { (*i).setLineA(i3->getLineA()); (*i).setLineC(i3->getLineC()); (*i).bAEqC = true; if(i->getLineB().isValid() && LineData::equal(pldA[i->getLineA()], pldB[i->getLineB()], false)) { (*i).bAEqB = true; (*i).bBEqC = true; } i3->getLineA().invalidate(); i3->getLineC().invalidate(); i3->bAEqC = false; i3A = i; i3C = i; ++i3A; ++i3C; lineA = l + 1; lineC = l + 1; } } else if(line > lineB && line > lineC && i3->getLineB().isValid() && i3->isEqualBC() && !i3->isEqualAC()) { // Empty space for B and C. B matches C, but not A. Move B & C up. Diff3LineList::iterator i = lineB > lineC ? i3B : i3C; int l = lineB > lineC ? lineB : lineC; - if(pManualDiffHelpList->isValidMove( i->getLineA(), i3->getLineB(), A, B) && - pManualDiffHelpList->isValidMove( i->getLineA(), i3->getLineC(), A, C)) + if(pManualDiffHelpList->isValidMove(i->getLineA(), i3->getLineB(), A, B) && + pManualDiffHelpList->isValidMove(i->getLineA(), i3->getLineC(), A, C)) { (*i).setLineB(i3->getLineB()); (*i).setLineC(i3->getLineC()); (*i).bBEqC = true; if(i->getLineA().isValid() && LineData::equal(pldA[i->getLineA()], pldB[i->getLineB()], false)) { (*i).bAEqB = true; (*i).bAEqC = true; } i3->getLineB().invalidate(); i3->getLineC().invalidate(); i3->bBEqC = false; i3B = i; i3C = i; ++i3B; ++i3C; lineB = l + 1; lineC = l + 1; } } if(i3->getLineA().isValid()) { lineA = line + 1; i3A = i3; ++i3A; } if(i3->getLineB().isValid()) { lineB = line + 1; i3B = i3; ++i3B; } if(i3->getLineC().isValid()) { lineC = line + 1; i3C = i3; ++i3C; } } d3ll.remove(d3l_empty); /* Diff3LineList::iterator it = d3ll.begin(); int li=0; for( ; it!=d3ll.end(); ++it, ++li ) { printf( "%4d %4d %4d %4d A%c=B A%c=C B%c=C\n", li, it->getLineA(), it->getLineB(), it->getLineC(), it->isEqualAB() ? '=' : '!', it->isEqualAC() ? '=' : '!', it->isEqualBC() ? '=' : '!' ); } */ } void DiffBufferInfo::init(Diff3LineList* pD3ll, const Diff3LineVector* pD3lv, const LineData* pldA, LineCount sizeA, const LineData* pldB, LineCount sizeB, const LineData* pldC, LineCount sizeC) { m_pDiff3LineList = pD3ll; m_pDiff3LineVector = pD3lv; m_pLineDataA = pldA; m_pLineDataB = pldB; m_pLineDataC = pldC; m_sizeA = sizeA; m_sizeB = sizeB; m_sizeC = sizeC; Diff3LineList::iterator i3 = pD3ll->begin(); for(; i3 != pD3ll->end(); ++i3) { i3->m_pDiffBufferInfo = this; } } void Diff3LineList::calcWhiteDiff3Lines( const LineData* pldA, const LineData* pldB, const LineData* pldC) { Diff3LineList::iterator i3; - for(i3=begin(); i3 != end(); ++i3) + for(i3 = begin(); i3 != end(); ++i3) { i3->bWhiteLineA = (!i3->getLineA().isValid() || pldA == nullptr || pldA[i3->getLineA()].whiteLine() || pldA[i3->getLineA()].isPureComment()); i3->bWhiteLineB = (!i3->getLineA().isValid() || pldB == nullptr || pldB[i3->getLineB()].whiteLine() || pldB[i3->getLineB()].isPureComment()); i3->bWhiteLineC = (!i3->getLineC().isValid() || pldC == nullptr || pldC[i3->getLineC()].whiteLine() || pldC[i3->getLineC()].isPureComment()); } } // My own diff-invention: void calcDiff(const QChar* p1, LineRef size1, const QChar* p2, LineRef size2, DiffList& diffList, int match, int maxSearchRange) { diffList.clear(); const QChar* p1start = p1; const QChar* p2start = p2; const QChar* p1end = p1 + size1; const QChar* p2end = p2 + size2; for(;;) { int nofEquals = 0; while(p1 != p1end && p2 != p2end && *p1 == *p2) { ++p1; ++p2; ++nofEquals; } bool bBestValid = false; int bestI1 = 0; int bestI2 = 0; int i1 = 0; int i2 = 0; for(i1 = 0;; ++i1) { if(&p1[i1] == p1end || (bBestValid && i1 >= bestI1 + bestI2)) { break; } for(i2 = 0; i2 < maxSearchRange; ++i2) { if(&p2[i2] == p2end || (bBestValid && i1 + i2 >= bestI1 + bestI2)) { break; } else if(p2[i2] == p1[i1] && (match == 1 || abs(i1 - i2) < 3 || (&p2[i2 + 1] == p2end && &p1[i1 + 1] == p1end) || (&p2[i2 + 1] != p2end && &p1[i1 + 1] != p1end && p2[i2 + 1] == p1[i1 + 1]))) { if(i1 + i2 < bestI1 + bestI2 || !bBestValid) { bestI1 = i1; bestI2 = i2; bBestValid = true; break; } } } } // The match was found using the strict search. Go back if there are non-strict // matches. while(bestI1 >= 1 && bestI2 >= 1 && p1[bestI1 - 1] == p2[bestI2 - 1]) { --bestI1; --bestI2; } bool bEndReached = false; if(bBestValid) { // continue somehow Diff d(nofEquals, bestI1, bestI2); diffList.push_back(d); p1 += bestI1; p2 += bestI2; } else { // Nothing else to match. Diff d(nofEquals, p1end - p1, p2end - p2); diffList.push_back(d); bEndReached = true; //break; } // Sometimes the algorithm that chooses the first match unfortunately chooses // a match where later actually equal parts don't match anymore. // A different match could be achieved, if we start at the end. // Do it, if it would be a better match. int nofUnmatched = 0; const QChar* pu1 = p1 - 1; const QChar* pu2 = p2 - 1; while(pu1 >= p1start && pu2 >= p2start && *pu1 == *pu2) { ++nofUnmatched; --pu1; --pu2; } Diff d = diffList.back(); if(nofUnmatched > 0) { // We want to go backwards the nofUnmatched elements and redo // the matching d = diffList.back(); Diff origBack = d; diffList.pop_back(); while(nofUnmatched > 0) { if(d.diff1 > 0 && d.diff2 > 0) { --d.diff1; --d.diff2; --nofUnmatched; } else if(d.nofEquals > 0) { --d.nofEquals; --nofUnmatched; } if(d.nofEquals == 0 && (d.diff1 == 0 || d.diff2 == 0) && nofUnmatched > 0) { if(diffList.empty()) break; d.nofEquals += diffList.back().nofEquals; d.diff1 += diffList.back().diff1; d.diff2 += diffList.back().diff2; diffList.pop_back(); bEndReached = false; } } if(bEndReached) diffList.push_back(origBack); else { p1 = pu1 + 1 + nofUnmatched; p2 = pu2 + 1 + nofUnmatched; diffList.push_back(d); } } if(bEndReached) break; } // Verify difflist { LineRef l1 = 0; LineRef l2 = 0; DiffList::iterator i; for(i = diffList.begin(); i != diffList.end(); ++i) { l1 += (LineRef)(i->nofEquals + i->diff1); l2 += (LineRef)(i->nofEquals + i->diff2); } Q_ASSERT(l1 == size1 && l2 == size2); } } bool Diff3Line::fineDiff(bool inBTextsTotalEqual, const e_SrcSelector selector, const LineData* v1, const LineData* v2) { LineRef k1 = 0; LineRef k2 = 0; int maxSearchLength = 500; bool bTextsTotalEqual = inBTextsTotalEqual; Q_ASSERT(selector == A || selector == B || selector == C); if(selector == A) { k1 = getLineA(); k2 = getLineB(); } else if(selector == B) { k1 = getLineB(); k2 = getLineC(); } else if(selector == C) { k1 = getLineC(); k2 = getLineA(); } if((k1 == -1 && k2 != -1) || (k1 != -1 && k2 == -1)) bTextsTotalEqual = false; if(k1 != -1 && k2 != -1) { if(v1[k1].size() != v2[k2].size() || memcmp(v1[k1].getLine(), v2[k2].getLine(), v1[k1].size() << 1) != 0) { bTextsTotalEqual = false; DiffList* pDiffList = new DiffList; calcDiff(v1[k1].getLine(), v1[k1].size(), v2[k2].getLine(), v2[k2].size(), *pDiffList, 2, maxSearchLength); // Optimize the diff list. DiffList::iterator dli; bool bUsefulFineDiff = false; for(dli = pDiffList->begin(); dli != pDiffList->end(); ++dli) { if(dli->nofEquals >= 4) { bUsefulFineDiff = true; break; } } for(dli = pDiffList->begin(); dli != pDiffList->end(); ++dli) { if(dli->nofEquals < 4 && (dli->diff1 > 0 || dli->diff2 > 0) && !(bUsefulFineDiff && dli == pDiffList->begin())) { dli->diff1 += dli->nofEquals; dli->diff2 += dli->nofEquals; dli->nofEquals = 0; } } setFineDiff(selector, pDiffList); } if((v1[k1].isPureComment() || v1[k1].whiteLine()) && (v2[k2].isPureComment() || v2[k2].whiteLine())) { if(selector == A) { bAEqB = true; } else if(selector == B) { bBEqC = true; } else if(selector == C) { bAEqC = true; } } } return bTextsTotalEqual; } void Diff3Line::getLineInfo(const e_SrcSelector winIdx, const bool isTriple, int& lineIdx, - DiffList*& pFineDiff1, DiffList*& pFineDiff2, // return values - int& changed, int& changed2) const + DiffList*& pFineDiff1, DiffList*& pFineDiff2, // return values + int& changed, int& changed2) const { changed = 0; changed2 = 0; bool bAEqualB = this->isEqualAB() || (bWhiteLineA && bWhiteLineB); bool bAEqualC = this->isEqualAC() || (bWhiteLineA && bWhiteLineC); bool bBEqualC = this->isEqualBC() || (bWhiteLineB && bWhiteLineC); Q_ASSERT(winIdx >= A && winIdx <= C); - if(winIdx == A) { + if(winIdx == A) + { lineIdx = getLineA(); pFineDiff1 = pFineAB; pFineDiff2 = pFineCA; changed |= ((!getLineB().isValid()) != (lineIdx == -1) ? 1 : 0) + ((!getLineC().isValid()) != (lineIdx == -1) && isTriple ? 2 : 0); changed2 |= (bAEqualB ? 0 : 1) + (bAEqualC || !isTriple ? 0 : 2); } else if(winIdx == B) { lineIdx = getLineB(); pFineDiff1 = pFineBC; pFineDiff2 = pFineAB; changed |= ((!getLineC().isValid()) != (lineIdx == -1) && isTriple ? 1 : 0) + ((!getLineA().isValid()) != (lineIdx == -1) ? 2 : 0); changed2 |= (bBEqualC || !isTriple ? 0 : 1) + (bAEqualB ? 0 : 2); } else if(winIdx == C) { lineIdx = getLineC(); pFineDiff1 = pFineCA; pFineDiff2 = pFineBC; changed |= ((!getLineA().isValid()) != (lineIdx == -1) ? 1 : 0) + ((!getLineB().isValid()) != (lineIdx == -1) ? 2 : 0); changed2 |= (bAEqualC ? 0 : 1) + (bBEqualC ? 0 : 2); } } bool Diff3LineList::fineDiff(const e_SrcSelector selector, const LineData* v1, const LineData* v2) { // Finetuning: Diff each line with deltas ProgressProxy pp; Diff3LineList::iterator i; bool bTextsTotalEqual = true; int listSize = size(); pp.setMaxNofSteps(listSize); int listIdx = 0; for(i = begin(); i != end(); ++i) { bTextsTotalEqual = i->fineDiff(bTextsTotalEqual, selector, v1, v2); ++listIdx; pp.step(); } return bTextsTotalEqual; } // Convert the list to a vector of pointers void Diff3LineList::calcDiff3LineVector(Diff3LineVector& d3lv) { d3lv.resize(size()); Diff3LineList::iterator i; int j = 0; for(i = begin(); i != end(); ++i, ++j) { d3lv[j] = &(*i); } Q_ASSERT(j == (int)d3lv.size()); } diff --git a/src/gnudiff_analyze.cpp b/src/gnudiff_analyze.cpp index 55d85a1..9f21b07 100644 --- a/src/gnudiff_analyze.cpp +++ b/src/gnudiff_analyze.cpp @@ -1,860 +1,860 @@ /* Analyze file differences for GNU DIFF. Modified for KDiff3 by Joachim Eibl 2003. The original file was part of GNU DIFF. Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002 Free Software Foundation, Inc. GNU DIFF is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU DIFF is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ /* The basic algorithm is described in: "An O(ND) Difference Algorithm and its Variations", Eugene Myers, Algorithmica Vol. 1 No. 2, 1986, pp. 251-266; see especially section 4.2, which describes the variation used below. Unless the --minimal option is specified, this code uses the TOO_EXPENSIVE heuristic, by Paul Eggert, to limit the cost to O(N**1.5 log N) at the price of producing suboptimal output for large inputs with many differences. The basic algorithm was independently discovered as described in: "Algorithms for Approximate String Matching", E. Ukkonen, Information and Control Vol. 64, 1985, pp. 100-118. */ #define GDIFF_MAIN #include "common.h" #include "gnudiff_diff.h" //#include #include static GNULineRef *xvec, *yvec; /* Vectors being compared. */ static GNULineRef *fdiag; /* Vector, indexed by diagonal, containing 1 + the X coordinate of the point furthest along the given diagonal in the forward search of the edit matrix. */ static GNULineRef *bdiag; /* Vector, indexed by diagonal, containing the X coordinate of the point furthest along the given diagonal in the backward search of the edit matrix. */ static GNULineRef too_expensive; /* Edit scripts longer than this are too expensive to compute. */ #define SNAKE_LIMIT 20 /* Snakes bigger than this are considered `big'. */ struct partition { - GNULineRef xmid, ymid; /* Midpoints of this partition. */ - bool lo_minimal; /* Nonzero if low half will be analyzed minimally. */ - bool hi_minimal; /* Likewise for high half. */ + GNULineRef xmid, ymid; /* Midpoints of this partition. */ + bool lo_minimal; /* Nonzero if low half will be analyzed minimally. */ + bool hi_minimal; /* Likewise for high half. */ }; /* Find the midpoint of the shortest edit script for a specified portion of the two files. Scan from the beginnings of the files, and simultaneously from the ends, doing a breadth-first search through the space of edit-sequence. When the two searches meet, we have found the midpoint of the shortest edit sequence. If FIND_MINIMAL is nonzero, find the minimal edit script regardless of expense. Otherwise, if the search is too expensive, use heuristics to stop the search and report a suboptimal answer. Set PART->(xmid,ymid) to the midpoint (XMID,YMID). The diagonal number XMID - YMID equals the number of inserted lines minus the number of deleted lines (counting only lines before the midpoint). Return the approximate edit cost; this is the total number of lines inserted or deleted (counting only lines before the midpoint), unless a heuristic is used to terminate the search prematurely. Set PART->lo_minimal to true iff the minimal edit script for the left half of the partition is known; similarly for PART->hi_minimal. This function assumes that the first lines of the specified portions of the two files do not match, and likewise that the last lines do not match. The caller must trim matching lines from the beginning and end of the portions it is going to specify. If we return the "wrong" partitions, the worst this can do is cause suboptimal diff output. It cannot cause incorrect diff output. */ GNULineRef GnuDiff::diag(GNULineRef xoff, GNULineRef xlim, GNULineRef yoff, GNULineRef ylim, bool find_minimal, - partition *part) + partition *part) { GNULineRef *const fd = fdiag; /* Give the compiler a chance. */ GNULineRef *const bd = bdiag; /* Additional help for the compiler. */ GNULineRef const *const xv = xvec; /* Still more help for the compiler. */ GNULineRef const *const yv = yvec; /* And more and more . . . */ GNULineRef const dmin = xoff - ylim; /* Minimum valid diagonal. */ GNULineRef const dmax = xlim - yoff; /* Maximum valid diagonal. */ GNULineRef const fmid = xoff - yoff; /* Center diagonal of top-down search. */ GNULineRef const bmid = xlim - ylim; /* Center diagonal of bottom-up search. */ GNULineRef fmin = fmid, fmax = fmid; /* Limits of top-down search. */ GNULineRef bmin = bmid, bmax = bmid; /* Limits of bottom-up search. */ GNULineRef c; /* Cost. */ - bool odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd + bool odd = (fmid - bmid) & 1; /* True if southeast corner is on an odd diagonal with respect to the northwest. */ fd[fmid] = xoff; bd[bmid] = xlim; for(c = 1;; ++c) { GNULineRef d; /* Active diagonal. */ bool big_snake = false; /* Extend the top-down search by an edit step in each diagonal. */ fmin > dmin ? fd[--fmin - 1] = -1 : ++fmin; fmax < dmax ? fd[++fmax + 1] = -1 : --fmax; for(d = fmax; d >= fmin; d -= 2) { GNULineRef x, y, oldx, tlo = fd[d - 1], thi = fd[d + 1]; if(tlo >= thi) x = tlo + 1; else x = thi; oldx = x; y = x - d; while(x < xlim && y < ylim && xv[x] == yv[y]) ++x, ++y; if(x - oldx > SNAKE_LIMIT) big_snake = true; fd[d] = x; if(odd && bmin <= d && d <= bmax && bd[d] <= x) { part->xmid = x; part->ymid = y; part->lo_minimal = part->hi_minimal = true; return 2 * c - 1; } } /* Similarly extend the bottom-up search. */ bmin > dmin ? bd[--bmin - 1] = GNULINEREF_MAX : ++bmin; bmax < dmax ? bd[++bmax + 1] = GNULINEREF_MAX : --bmax; for(d = bmax; d >= bmin; d -= 2) { GNULineRef x, y, oldx, tlo = bd[d - 1], thi = bd[d + 1]; if(tlo < thi) x = tlo; else x = thi - 1; oldx = x; y = x - d; while(x > xoff && y > yoff && xv[x - 1] == yv[y - 1]) --x, --y; if(oldx - x > SNAKE_LIMIT) big_snake = true; bd[d] = x; if(!odd && fmin <= d && d <= fmax && x <= fd[d]) { part->xmid = x; part->ymid = y; part->lo_minimal = part->hi_minimal = true; return 2 * c; } } if(find_minimal) continue; /* Heuristic: check occasionally for a diagonal that has made lots of progress compared with the edit distance. If we have any such, find the one that has made the most progress and return it as if it had succeeded. With this heuristic, for files with a constant small density of changes, the algorithm is linear in the file size. */ if(200 < c && big_snake && speed_large_files) { GNULineRef best; best = 0; for(d = fmax; d >= fmin; d -= 2) { GNULineRef dd = d - fmid; GNULineRef x = fd[d]; GNULineRef y = x - d; GNULineRef v = (x - xoff) * 2 - dd; if(v > 12 * (c + (dd < 0 ? -dd : dd))) { if(v > best && xoff + SNAKE_LIMIT <= x && x < xlim && yoff + SNAKE_LIMIT <= y && y < ylim) { /* We have a good enough best diagonal; now insist that it end with a significant snake. */ int k; for(k = 1; xv[x - k] == yv[y - k]; k++) if(k == SNAKE_LIMIT) { best = v; part->xmid = x; part->ymid = y; break; } } } } if(best > 0) { part->lo_minimal = true; part->hi_minimal = false; return 2 * c - 1; } best = 0; for(d = bmax; d >= bmin; d -= 2) { GNULineRef dd = d - bmid; GNULineRef x = bd[d]; GNULineRef y = x - d; GNULineRef v = (xlim - x) * 2 + dd; if(v > 12 * (c + (dd < 0 ? -dd : dd))) { if(v > best && xoff < x && x <= xlim - SNAKE_LIMIT && yoff < y && y <= ylim - SNAKE_LIMIT) { /* We have a good enough best diagonal; now insist that it end with a significant snake. */ int k; for(k = 0; xv[x + k] == yv[y + k]; k++) if(k == SNAKE_LIMIT - 1) { best = v; part->xmid = x; part->ymid = y; break; } } } } if(best > 0) { part->lo_minimal = false; part->hi_minimal = true; return 2 * c - 1; } } /* Heuristic: if we've gone well beyond the call of duty, give up and report halfway between our best results so far. */ if(c >= too_expensive) { GNULineRef fxybest, fxbest; GNULineRef bxybest, bxbest; fxbest = bxbest = 0; /* Pacify `gcc -Wall'. */ /* Find forward diagonal that maximizes X + Y. */ fxybest = -1; for(d = fmax; d >= fmin; d -= 2) { GNULineRef x = std::min(fd[d], xlim); GNULineRef y = x - d; if(ylim < y) x = ylim + d, y = ylim; if(fxybest < x + y) { fxybest = x + y; fxbest = x; } } /* Find backward diagonal that minimizes X + Y. */ bxybest = GNULINEREF_MAX; for(d = bmax; d >= bmin; d -= 2) { GNULineRef x = std::max(xoff, bd[d]); GNULineRef y = x - d; if(y < yoff) x = yoff + d, y = yoff; if(x + y < bxybest) { bxybest = x + y; bxbest = x; } } /* Use the better of the two diagonals. */ if((xlim + ylim) - bxybest < fxybest - (xoff + yoff)) { part->xmid = fxbest; part->ymid = fxybest - fxbest; part->lo_minimal = true; part->hi_minimal = false; } else { part->xmid = bxbest; part->ymid = bxybest - bxbest; part->lo_minimal = false; part->hi_minimal = true; } return 2 * c - 1; } } } /* Compare in detail contiguous subsequences of the two files which are known, as a whole, to match each other. The results are recorded in the vectors files[N].changed, by storing 1 in the element for each line that is an insertion or deletion. The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1. Note that XLIM, YLIM are exclusive bounds. All line numbers are origin-0 and discarded lines are not counted. If FIND_MINIMAL, find a minimal difference no matter how expensive it is. */ void GnuDiff::compareseq(GNULineRef xoff, GNULineRef xlim, GNULineRef yoff, GNULineRef ylim, bool find_minimal) { GNULineRef *const xv = xvec; /* Help the compiler. */ GNULineRef *const yv = yvec; /* Slide down the bottom initial diagonal. */ while(xoff < xlim && yoff < ylim && xv[xoff] == yv[yoff]) ++xoff, ++yoff; /* Slide up the top initial diagonal. */ while(xlim > xoff && ylim > yoff && xv[xlim - 1] == yv[ylim - 1]) --xlim, --ylim; /* Handle simple cases. */ if(xoff == xlim) while(yoff < ylim) files[1].changed[files[1].realindexes[yoff++]] = true; else if(yoff == ylim) while(xoff < xlim) files[0].changed[files[0].realindexes[xoff++]] = true; else { GNULineRef c; partition part; /* Find a point of correspondence in the middle of the files. */ c = diag(xoff, xlim, yoff, ylim, find_minimal, &part); if(c == 1) { /* This should be impossible, because it implies that one of the two subsequences is empty, and that case was handled above without calling `diag'. Let's verify that this is true. */ abort(); #if 0 /* The two subsequences differ by a single insert or delete; record it and we are done. */ if (part.xmid - part.ymid < xoff - yoff) files[1].changed[files[1].realindexes[part.ymid - 1]] = 1; else files[0].changed[files[0].realindexes[part.xmid]] = 1; #endif } else { /* Use the partitions to split this problem into subproblems. */ compareseq(xoff, part.xmid, yoff, part.ymid, part.lo_minimal); compareseq(part.xmid, xlim, part.ymid, ylim, part.hi_minimal); } } } /* Discard lines from one file that have no matches in the other file. A line which is discarded will not be considered by the actual comparison algorithm; it will be as if that line were not in the file. The file's `realindexes' table maps virtual line numbers (which don't count the discarded lines) into real line numbers; this is how the actual comparison algorithm produces results that are comprehensible when the discarded lines are counted. When we discard a line, we also mark it as a deletion or insertion so that it will be printed in the output. */ void GnuDiff::discard_confusing_lines(file_data filevec[]) { int f; GNULineRef i; char *discarded[2]; GNULineRef *equiv_count[2]; GNULineRef *p; /* Allocate our results. */ p = (GNULineRef *)xmalloc((filevec[0].buffered_lines + filevec[1].buffered_lines) * (2 * sizeof *p)); for(f = 0; f < 2; ++f) { filevec[f].undiscarded = p; p += filevec[f].buffered_lines; filevec[f].realindexes = p; p += filevec[f].buffered_lines; } /* Set up equiv_count[F][I] as the number of lines in file F that fall in equivalence class I. */ p = (GNULineRef *)zalloc(filevec[0].equiv_max * (2 * sizeof *p)); equiv_count[0] = p; equiv_count[1] = p + filevec[0].equiv_max; for(i = 0; i < filevec[0].buffered_lines; ++i) ++equiv_count[0][filevec[0].equivs[i]]; for(i = 0; i < filevec[1].buffered_lines; ++i) ++equiv_count[1][filevec[1].equivs[i]]; /* Set up tables of which lines are going to be discarded. */ discarded[0] = (char *)zalloc(filevec[0].buffered_lines + filevec[1].buffered_lines); discarded[1] = discarded[0] + filevec[0].buffered_lines; /* Mark to be discarded each line that matches no line of the other file. If a line matches many lines, mark it as provisionally discardable. */ for(f = 0; f < 2; ++f) { size_t end = filevec[f].buffered_lines; char *discards = discarded[f]; GNULineRef *counts = equiv_count[1 - f]; GNULineRef *equivs = filevec[f].equivs; size_t many = 5; size_t tem = end / 64; /* Multiply MANY by approximate square root of number of lines. That is the threshold for provisionally discardable lines. */ while((tem = tem >> 2) > 0) many *= 2; for(i = 0; i < (GNULineRef)end; ++i) { GNULineRef nmatch; if(equivs[i] == 0) continue; nmatch = counts[equivs[i]]; if(nmatch == 0) discards[i] = 1; else if(nmatch > (GNULineRef)many) discards[i] = 2; } } /* Don't really discard the provisional lines except when they occur in a run of discardables, with nonprovisionals at the beginning and end. */ for(f = 0; f < 2; ++f) { GNULineRef end = filevec[f].buffered_lines; char *discards = discarded[f]; for(i = 0; i < end; ++i) { /* Cancel provisional discards not in middle of run of discards. */ if(discards[i] == 2) discards[i] = 0; else if(discards[i] != 0) { /* We have found a nonprovisional discard. */ GNULineRef j; GNULineRef length; GNULineRef provisional = 0; /* Find end of this run of discardable lines. Count how many are provisionally discardable. */ for(j = i; j < end; ++j) { if(discards[j] == 0) break; if(discards[j] == 2) ++provisional; } /* Cancel provisional discards at end, and shrink the run. */ while(j > i && discards[j - 1] == 2) discards[--j] = 0, --provisional; /* Now we have the length of a run of discardable lines whose first and last are not provisional. */ length = j - i; /* If 1/4 of the lines in the run are provisional, cancel discarding of all provisional lines in the run. */ if(provisional * 4 > length) { while(j > i) if(discards[--j] == 2) discards[j] = 0; } else { GNULineRef consec; GNULineRef minimum = 1; GNULineRef tem = length >> 2; /* MINIMUM is approximate square root of LENGTH/4. A subrun of two or more provisionals can stand when LENGTH is at least 16. A subrun of 4 or more can stand when LENGTH >= 64. */ while(0 < (tem >>= 2)) minimum <<= 1; minimum++; /* Cancel any subrun of MINIMUM or more provisionals within the larger run. */ for(j = 0, consec = 0; j < length; ++j) if(discards[i + j] != 2) consec = 0; else if(minimum == ++consec) /* Back up to start of subrun, to cancel it all. */ j -= consec; else if(minimum < consec) discards[i + j] = 0; /* Scan from beginning of run until we find 3 or more nonprovisionals in a row or until the first nonprovisional at least 8 lines in. Until that point, cancel any provisionals. */ for(j = 0, consec = 0; j < length; ++j) { if(j >= 8 && discards[i + j] == 1) break; if(discards[i + j] == 2) consec = 0, discards[i + j] = 0; else if(discards[i + j] == 0) consec = 0; else consec++; if(consec == 3) break; } /* I advances to the last line of the run. */ i += length - 1; /* Same thing, from end. */ for(j = 0, consec = 0; j < length; ++j) { if(j >= 8 && discards[i - j] == 1) break; if(discards[i - j] == 2) consec = 0, discards[i - j] = 0; else if(discards[i - j] == 0) consec = 0; else consec++; if(consec == 3) break; } } } } } /* Actually discard the lines. */ for(f = 0; f < 2; ++f) { char *discards = discarded[f]; GNULineRef end = filevec[f].buffered_lines; GNULineRef j = 0; for(i = 0; i < end; ++i) if(minimal || discards[i] == 0) { filevec[f].undiscarded[j] = filevec[f].equivs[i]; filevec[f].realindexes[j++] = i; } else filevec[f].changed[i] = true; filevec[f].nondiscarded_lines = j; } free(discarded[0]); free(equiv_count[0]); } /* Adjust inserts/deletes of identical lines to join changes as much as possible. We do something when a run of changed lines include a line at one end and have an excluded, identical line at the other. We are free to choose which identical line is included. `compareseq' usually chooses the one at the beginning, but usually it is cleaner to consider the following identical line to be the "change". */ void GnuDiff::shift_boundaries(file_data filevec[]) { int f; for(f = 0; f < 2; ++f) { bool *changed = filevec[f].changed; bool const *other_changed = filevec[1 - f].changed; GNULineRef const *equivs = filevec[f].equivs; GNULineRef i = 0; GNULineRef j = 0; GNULineRef i_end = filevec[f].buffered_lines; while(true) { GNULineRef runlength, start, corresponding; /* Scan forwards to find beginning of another run of changes. Also keep track of the corresponding point in the other file. */ while(i < i_end && !changed[i]) { while(other_changed[j++]) continue; i++; } if(i == i_end) break; start = i; /* Find the end of this run of changes. */ while(changed[++i]) continue; while(other_changed[j]) j++; do { /* Record the length of this run of changes, so that we can later determine whether the run has grown. */ runlength = i - start; /* Move the changed region back, so long as the previous unchanged line matches the last changed one. This merges with previous changed regions. */ while(start && equivs[start - 1] == equivs[i - 1]) { changed[--start] = true; changed[--i] = false; while(changed[start - 1]) start--; while(other_changed[--j]) continue; } /* Set CORRESPONDING to the end of the changed run, at the last point where it corresponds to a changed run in the other file. CORRESPONDING == I_END means no such point has been found. */ corresponding = other_changed[j - 1] ? i : i_end; /* Move the changed region forward, so long as the first changed line matches the following unchanged one. This merges with following changed regions. Do this second, so that if there are no merges, the changed region is moved forward as far as possible. */ while(i != i_end && equivs[start] == equivs[i]) { changed[start++] = false; changed[i++] = true; while(changed[i]) i++; while(other_changed[++j]) corresponding = i; } } while(runlength != i - start); /* If possible, move the fully-merged run of changes back to a corresponding run in the other file. */ while(corresponding < i) { changed[--start] = true; changed[--i] = false; while(other_changed[--j]) continue; } } } } /* Cons an additional entry onto the front of an edit script OLD. LINE0 and LINE1 are the first affected lines in the two files (origin 0). DELETED is the number of lines deleted here from file 0. INSERTED is the number of lines inserted here in file 1. If DELETED is 0 then LINE0 is the number of the line before which the insertion was done; vice versa for INSERTED and LINE1. */ GnuDiff::change *GnuDiff::add_change(GNULineRef line0, GNULineRef line1, GNULineRef deleted, GNULineRef inserted, change *old) { change *newChange = (change *)xmalloc(sizeof *newChange); newChange->line0 = line0; newChange->line1 = line1; newChange->inserted = inserted; newChange->deleted = deleted; newChange->link = old; return newChange; } /* Scan the tables of which lines are inserted and deleted, producing an edit script in reverse order. */ GnuDiff::change *GnuDiff::build_reverse_script(file_data const filevec[]) { change *script = nullptr; bool *changed0 = filevec[0].changed; bool *changed1 = filevec[1].changed; GNULineRef len0 = filevec[0].buffered_lines; GNULineRef len1 = filevec[1].buffered_lines; /* Note that changedN[len0] does exist, and is 0. */ GNULineRef i0 = 0, i1 = 0; while(i0 < len0 || i1 < len1) { if(changed0[i0] | changed1[i1]) { GNULineRef line0 = i0, line1 = i1; /* Find # lines changed here in each file. */ while(changed0[i0]) ++i0; while(changed1[i1]) ++i1; /* Record this change. */ script = add_change(line0, line1, i0 - line0, i1 - line1, script); } /* We have reached lines in the two files that match each other. */ i0++, i1++; } return script; } /* Scan the tables of which lines are inserted and deleted, producing an edit script in forward order. */ GnuDiff::change *GnuDiff::build_script(file_data const filevec[]) { change *script = nullptr; bool *changed0 = filevec[0].changed; bool *changed1 = filevec[1].changed; GNULineRef i0 = filevec[0].buffered_lines, i1 = filevec[1].buffered_lines; /* Note that changedN[-1] does exist, and is 0. */ while(i0 >= 0 || i1 >= 0) { if(changed0[i0 - 1] | changed1[i1 - 1]) { GNULineRef line0 = i0, line1 = i1; /* Find # lines changed here in each file. */ while(changed0[i0 - 1]) --i0; while(changed1[i1 - 1]) --i1; /* Record this change. */ script = add_change(i0, i1, line0 - i0, line1 - i1, script); } /* We have reached lines in the two files that match each other. */ i0--, i1--; } return script; } /* Report the differences of two files. */ GnuDiff::change *GnuDiff::diff_2_files(comparison *cmp) { GNULineRef diags; int f; change *script; read_files(cmp->file, files_can_be_treated_as_binary); { /* Allocate vectors for the results of comparison: a flag for each line of each file, saying whether that line is an insertion or deletion. Allocate an extra element, always 0, at each end of each vector. */ size_t s = cmp->file[0].buffered_lines + cmp->file[1].buffered_lines + 4; bool *flag_space = (bool *)zalloc(s * sizeof(*flag_space)); cmp->file[0].changed = flag_space + 1; cmp->file[1].changed = flag_space + cmp->file[0].buffered_lines + 3; /* Some lines are obviously insertions or deletions because they don't match anything. Detect them now, and avoid even thinking about them in the main comparison algorithm. */ discard_confusing_lines(cmp->file); /* Now do the main comparison algorithm, considering just the undiscarded lines. */ xvec = cmp->file[0].undiscarded; yvec = cmp->file[1].undiscarded; diags = (cmp->file[0].nondiscarded_lines + cmp->file[1].nondiscarded_lines + 3); fdiag = (GNULineRef *)xmalloc(diags * (2 * sizeof *fdiag)); bdiag = fdiag + diags; fdiag += cmp->file[1].nondiscarded_lines + 1; bdiag += cmp->file[1].nondiscarded_lines + 1; /* Set TOO_EXPENSIVE to be approximate square root of input size, bounded below by 256. */ too_expensive = 1; for(; diags != 0; diags >>= 2) too_expensive <<= 1; too_expensive = std::max((GNULineRef)256, too_expensive); files[0] = cmp->file[0]; files[1] = cmp->file[1]; compareseq(0, cmp->file[0].nondiscarded_lines, 0, cmp->file[1].nondiscarded_lines, minimal); free(fdiag - (cmp->file[1].nondiscarded_lines + 1)); /* Modify the results slightly to make them prettier in cases where that can validly be done. */ shift_boundaries(cmp->file); /* Get the results of comparison in the form of a chain of `change's -- an edit script. */ script = build_script(cmp->file); free(cmp->file[0].undiscarded); free(flag_space); for(f = 0; f < 2; ++f) { free(cmp->file[f].equivs); free(cmp->file[f].linbuf + cmp->file[f].linbuf_base); } } return script; } diff --git a/src/gnudiff_io.cpp b/src/gnudiff_io.cpp index 2ab1bf7..a0e919d 100644 --- a/src/gnudiff_io.cpp +++ b/src/gnudiff_io.cpp @@ -1,545 +1,545 @@ /* File I/O for GNU DIFF. Modified for KDiff3 by Joachim Eibl 2003, 2004, 2005. The original file was part of GNU DIFF. Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1998, 2001, 2002 Free Software Foundation, Inc. GNU DIFF is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU DIFF is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; see the file COPYING. If not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "gnudiff_diff.h" #include #include /* Rotate an unsigned value to the left. */ #define ROL(v, n) ((v) << (n) | (v) >> (sizeof(v) * CHAR_BIT - (n))) /* Given a hash value and a new character, return a new hash value. */ #define HASH(h, c) ((c) + ROL(h, 7)) /* The type of a hash value. */ typedef size_t hash_value; static_assert(std::is_unsigned::value, "hash_value must be signed."); /* Lines are put into equivalence classes of lines that match in lines_differ. Each equivalence class is represented by one of these structures, but only while the classes are being computed. Afterward, each class is represented by a number. */ struct equivclass { - GNULineRef next; /* Next item in this bucket. */ + GNULineRef next; /* Next item in this bucket. */ hash_value hash; /* Hash of lines in this class. */ const QChar *line; /* A line that fits this class. */ size_t length; /* That line's length, not counting its newline. */ }; /* Hash-table: array of buckets, each being a chain of equivalence classes. buckets[-1] is reserved for incomplete lines. */ static GNULineRef *buckets; /* Number of buckets in the hash table array, not counting buckets[-1]. */ static size_t nbuckets; /* Array in which the equivalence classes are allocated. The bucket-chains go through the elements in this array. The number of an equivalence class is its index in this array. */ static equivclass *equivs; /* Index of first free element in the array `equivs'. */ static GNULineRef equivs_index; /* Number of elements allocated in the array `equivs'. */ static GNULineRef equivs_alloc; /* Check for binary files and compare them for exact identity. */ /* Return 1 if BUF contains a non text character. SIZE is the number of characters in BUF. */ #define binary_file_p(buf, size) (memchr(buf, 0, size) != 0) /* Compare two lines (typically one from each input file) according to the command line options. For efficiency, this is invoked only when the lines do not match exactly but an option like -i might cause us to ignore the difference. Return nonzero if the lines differ. */ bool GnuDiff::lines_differ(const QChar *s1, size_t len1, const QChar *s2, size_t len2) { const QChar *t1 = s1; const QChar *t2 = s2; const QChar *s1end = s1 + len1; const QChar *s2end = s2 + len2; for(;; ++t1, ++t2) { /* Test for exact char equality first, since it's a common case. */ if(t1 != s1end && t2 != s2end && *t1 == *t2) continue; else { while(t1 != s1end && ((bIgnoreWhiteSpace && isWhite(*t1)) || (bIgnoreNumbers && (t1->isDigit() || *t1 == '-' || *t1 == '.')))) { ++t1; } while(t2 != s2end && ((bIgnoreWhiteSpace && isWhite(*t2)) || (bIgnoreNumbers && (t2->isDigit() || *t2 == '-' || *t2 == '.')))) { ++t2; } if(t1 != s1end && t2 != s2end) { if(ignore_case) { /* Lowercase comparison. */ if(t1->toLower() == t2->toLower()) continue; } else if(*t1 == *t2) continue; else return true; } else if(t1 == s1end && t2 == s2end) return false; else return true; } } return false; } /* Split the file into lines, simultaneously computing the equivalence class for each line. */ void GnuDiff::find_and_hash_each_line(file_data *current) { hash_value h; const QChar *p = current->prefix_end; QChar c; GNULineRef i, *bucket; size_t length; /* Cache often-used quantities in local variables to help the compiler. */ const QChar **linbuf = current->linbuf; GNULineRef alloc_lines = current->alloc_lines; GNULineRef line = 0; GNULineRef linbuf_base = current->linbuf_base; GNULineRef *cureqs = (GNULineRef *)xmalloc(alloc_lines * sizeof *cureqs); equivclass *eqs = equivs; GNULineRef eqs_index = equivs_index; GNULineRef eqs_alloc = equivs_alloc; const QChar *suffix_begin = current->suffix_begin; const QChar *bufend = current->buffer + current->buffered; bool diff_length_compare_anyway = ignore_white_space != IGNORE_NO_WHITE_SPACE || bIgnoreNumbers; bool same_length_diff_contents_compare_anyway = diff_length_compare_anyway | ignore_case; while(p < suffix_begin) { const QChar *ip = p; h = 0; /* Hash this line until we find a newline or bufend is reached. */ if(ignore_case) switch(ignore_white_space) { - case IGNORE_ALL_SPACE: - while(p < bufend && !Utils::isEndOfLine(c = *p)) - { - if(!(isWhite(c) || (bIgnoreNumbers && (c.isDigit() || c == '-' || c == '.')))) - h = HASH(h, c.toLower().unicode()); - ++p; - } - break; + case IGNORE_ALL_SPACE: + while(p < bufend && !Utils::isEndOfLine(c = *p)) + { + if(!(isWhite(c) || (bIgnoreNumbers && (c.isDigit() || c == '-' || c == '.')))) + h = HASH(h, c.toLower().unicode()); + ++p; + } + break; - default: - while(p < bufend && !Utils::isEndOfLine(c = *p)) - { - h = HASH(h, c.toLower().unicode()); - ++p; - } - break; + default: + while(p < bufend && !Utils::isEndOfLine(c = *p)) + { + h = HASH(h, c.toLower().unicode()); + ++p; + } + break; } else switch(ignore_white_space) { - case IGNORE_ALL_SPACE: - while(p < bufend && !Utils::isEndOfLine(c = *p)) - { - if(!(isWhite(c) || (bIgnoreNumbers && (c.isDigit() || c == '-' || c == '.')))) - h = HASH(h, c.unicode()); - ++p; - } - break; + case IGNORE_ALL_SPACE: + while(p < bufend && !Utils::isEndOfLine(c = *p)) + { + if(!(isWhite(c) || (bIgnoreNumbers && (c.isDigit() || c == '-' || c == '.')))) + h = HASH(h, c.unicode()); + ++p; + } + break; - default: - while(p < bufend && !Utils::isEndOfLine(c = *p)) - { - h = HASH(h, c.unicode()); - ++p; - } - break; + default: + while(p < bufend && !Utils::isEndOfLine(c = *p)) + { + h = HASH(h, c.unicode()); + ++p; + } + break; } bucket = &buckets[h % nbuckets]; length = p - ip; ++p; for(i = *bucket;; i = eqs[i].next) if(!i) { /* Create a new equivalence class in this bucket. */ i = eqs_index++; if(i == eqs_alloc) { if((GNULineRef)(GNULINEREF_MAX / (2 * sizeof *eqs)) <= eqs_alloc) xalloc_die(); eqs_alloc *= 2; eqs = (equivclass *)xrealloc(eqs, eqs_alloc * sizeof *eqs); } eqs[i].next = *bucket; eqs[i].hash = h; eqs[i].line = ip; eqs[i].length = length; *bucket = i; break; } else if(eqs[i].hash == h) { const QChar *eqline = eqs[i].line; /* Reuse existing class if lines_differ reports the lines equal. */ if(eqs[i].length == length) { /* Reuse existing equivalence class if the lines are identical. This detects the common case of exact identity faster than lines_differ would. */ if(memcmp(eqline, ip, length * sizeof(QChar)) == 0) break; if(!same_length_diff_contents_compare_anyway) continue; } else if(!diff_length_compare_anyway) continue; if(!lines_differ(eqline, eqs[i].length, ip, length)) break; } /* Maybe increase the size of the line table. */ if(line == alloc_lines) { /* Double (alloc_lines - linbuf_base) by adding to alloc_lines. */ if((GNULineRef)(GNULINEREF_MAX / 3) <= alloc_lines || (GNULineRef)(GNULINEREF_MAX / sizeof *cureqs) <= 2 * alloc_lines - linbuf_base || (GNULineRef)(GNULINEREF_MAX / sizeof *linbuf) <= alloc_lines - linbuf_base) xalloc_die(); alloc_lines = 2 * alloc_lines - linbuf_base; cureqs = (GNULineRef *)xrealloc(cureqs, alloc_lines * sizeof *cureqs); linbuf += linbuf_base; linbuf = (const QChar **)xrealloc(linbuf, (alloc_lines - linbuf_base) * sizeof *linbuf); linbuf -= linbuf_base; } linbuf[line] = ip; cureqs[line] = i; ++line; } current->buffered_lines = line; for(i = 0;; ++i) { /* Record the line start for lines in the suffix that we care about. Record one more line start than lines, so that we can compute the length of any buffered line. */ if(line == alloc_lines) { /* Double (alloc_lines - linbuf_base) by adding to alloc_lines. */ if((GNULineRef)(GNULINEREF_MAX / 3) <= alloc_lines || (GNULineRef)(GNULINEREF_MAX / sizeof *cureqs) <= 2 * alloc_lines - linbuf_base || (GNULineRef)(GNULINEREF_MAX / sizeof *linbuf) <= alloc_lines - linbuf_base) xalloc_die(); alloc_lines = 2 * alloc_lines - linbuf_base; linbuf += linbuf_base; linbuf = (const QChar **)xrealloc(linbuf, (alloc_lines - linbuf_base) * sizeof *linbuf); linbuf -= linbuf_base; } linbuf[line] = p; if(p >= bufend) break; if(context <= i && no_diff_means_no_output) break; line++; while(p < bufend && !Utils::isEndOfLine(*p++)) continue; } /* Done with cache in local variables. */ current->linbuf = linbuf; current->valid_lines = line; current->alloc_lines = alloc_lines; current->equivs = cureqs; equivs = eqs; equivs_alloc = eqs_alloc; equivs_index = eqs_index; } /* We have found N lines in a buffer of size S; guess the proportionate number of lines that will be found in a buffer of size T. However, do not guess a number of lines so large that the resulting line table might cause overflow in size calculations. */ static GNULineRef guess_lines(GNULineRef n, size_t s, size_t t) { size_t guessed_bytes_per_line = n < 10 ? 32 : s / (n - 1); size_t guessed_lines = std::max((size_t)1, t / guessed_bytes_per_line); return (GNULineRef)std::min((GNULineRef)guessed_lines, (GNULineRef)(GNULINEREF_MAX / (2 * sizeof(QChar *) + 1) - 5)) + 5; } /* Given a vector of two file_data objects, find the identical prefixes and suffixes of each object. */ void GnuDiff::find_identical_ends(file_data filevec[]) { /* Find identical prefix. */ const QChar *p0, *p1, *buffer0, *buffer1; p0 = buffer0 = filevec[0].buffer; p1 = buffer1 = filevec[1].buffer; size_t n0, n1; n0 = filevec[0].buffered; n1 = filevec[1].buffered; const QChar *const pEnd0 = p0 + n0; const QChar *const pEnd1 = p1 + n1; if(p0 == p1) /* The buffers are the same; sentinels won't work. */ p0 = p1 += n1; else { /* Loop until first mismatch, or end. */ while(p0 != pEnd0 && p1 != pEnd1 && *p0 == *p1) { p0++; p1++; } } /* Now P0 and P1 point at the first nonmatching characters. */ /* Skip back to last line-beginning in the prefix. */ while(p0 != buffer0 && !Utils::isEndOfLine(p0[-1])) p0--, p1--; /* Record the prefix. */ filevec[0].prefix_end = p0; filevec[1].prefix_end = p1; /* Find identical suffix. */ /* P0 and P1 point beyond the last chars not yet compared. */ p0 = buffer0 + n0; p1 = buffer1 + n1; const QChar *end0, *beg0; end0 = p0; /* Addr of last char in file 0. */ /* Get value of P0 at which we should stop scanning backward: this is when either P0 or P1 points just past the last char of the identical prefix. */ beg0 = filevec[0].prefix_end + (n0 < n1 ? 0 : n0 - n1); /* Scan back until chars don't match or we reach that point. */ for(; p0 != beg0; p0--, p1--) { if(*p0 != *p1) { /* Point at the first char of the matching suffix. */ beg0 = p0; break; } } // Go to the next line (skip last line with a difference) if(p0 != end0) { if(*p0 != *p1) ++p0; while(p0 < pEnd0 && !Utils::isEndOfLine(*p0++)) continue; } p1 += p0 - beg0; /* Record the suffix. */ filevec[0].suffix_begin = p0; filevec[1].suffix_begin = p1; /* Calculate number of lines of prefix to save. prefix_count == 0 means save the whole prefix; we need this for options like -D that output the whole file, or for enormous contexts (to avoid worrying about arithmetic overflow). We also need it for options like -F that output some preceding line; at least we will need to find the last few lines, but since we don't know how many, it's easiest to find them all. Otherwise, prefix_count != 0. Save just prefix_count lines at start of the line buffer; they'll be moved to the proper location later. Handle 1 more line than the context says (because we count 1 too many), rounded up to the next power of 2 to speed index computation. */ const QChar **linbuf0, **linbuf1; GNULineRef alloc_lines0, alloc_lines1; GNULineRef buffered_prefix, prefix_count, prefix_mask; GNULineRef middle_guess, suffix_guess; if(no_diff_means_no_output && context < (GNULineRef)(GNULINEREF_MAX / 4) && context < (GNULineRef)(n0)) { middle_guess = guess_lines(0, 0, p0 - filevec[0].prefix_end); suffix_guess = guess_lines(0, 0, buffer0 + n0 - p0); for(prefix_count = 1; prefix_count <= context; prefix_count *= 2) continue; alloc_lines0 = (prefix_count + middle_guess + std::min(context, suffix_guess)); } else { prefix_count = 0; alloc_lines0 = guess_lines(0, 0, n0); } prefix_mask = prefix_count - 1; GNULineRef lines = 0; linbuf0 = (const QChar **)xmalloc(alloc_lines0 * sizeof(*linbuf0)); p0 = buffer0; /* If the prefix is needed, find the prefix lines. */ if(!(no_diff_means_no_output && filevec[0].prefix_end == p0 && filevec[1].prefix_end == p1)) { end0 = filevec[0].prefix_end; while(p0 != end0) { GNULineRef l = lines++ & prefix_mask; if(l == alloc_lines0) { if((GNULineRef)(GNULINEREF_MAX / (2 * sizeof *linbuf0)) <= alloc_lines0) xalloc_die(); alloc_lines0 *= 2; linbuf0 = (const QChar **)xrealloc(linbuf0, alloc_lines0 * sizeof(*linbuf0)); } linbuf0[l] = p0; while(p0 < pEnd0 && !Utils::isEndOfLine(*p0++)) continue; } } buffered_prefix = prefix_count && context < lines ? context : lines; /* Allocate line buffer 1. */ middle_guess = guess_lines(lines, p0 - buffer0, p1 - filevec[1].prefix_end); suffix_guess = guess_lines(lines, p0 - buffer0, buffer1 + n1 - p1); alloc_lines1 = buffered_prefix + middle_guess + std::min(context, suffix_guess); if(alloc_lines1 < buffered_prefix || (GNULineRef)(GNULINEREF_MAX / sizeof *linbuf1) <= alloc_lines1) xalloc_die(); linbuf1 = (const QChar **)xmalloc(alloc_lines1 * sizeof(*linbuf1)); GNULineRef i; if(buffered_prefix != lines) { /* Rotate prefix lines to proper location. */ for(i = 0; i < buffered_prefix; ++i) linbuf1[i] = linbuf0[(lines - context + i) & prefix_mask]; for(i = 0; i < buffered_prefix; ++i) linbuf0[i] = linbuf1[i]; } /* Initialize line buffer 1 from line buffer 0. */ for(i = 0; i < buffered_prefix; ++i) linbuf1[i] = linbuf0[i] - buffer0 + buffer1; /* Record the line buffer, adjusted so that linbuf[0] points at the first differing line. */ filevec[0].linbuf = linbuf0 + buffered_prefix; filevec[1].linbuf = linbuf1 + buffered_prefix; filevec[0].linbuf_base = filevec[1].linbuf_base = -buffered_prefix; filevec[0].alloc_lines = alloc_lines0 - buffered_prefix; filevec[1].alloc_lines = alloc_lines1 - buffered_prefix; filevec[0].prefix_lines = filevec[1].prefix_lines = lines; } /* If 1 < k, then (2**k - prime_offset[k]) is the largest prime less than 2**k. This table is derived from Chris K. Caldwell's list . */ static unsigned char const prime_offset[] = { 0, 0, 1, 1, 3, 1, 3, 1, 5, 3, 3, 9, 3, 1, 3, 19, 15, 1, 5, 1, 3, 9, 3, 15, 3, 39, 5, 39, 57, 3, 35, 1, 5, 9, 41, 31, 5, 25, 45, 7, 87, 21, 11, 57, 17, 55, 21, 115, 59, 81, 27, 129, 47, 111, 33, 55, 5, 13, 27, 55, 93, 1, 57, 25}; /* Verify that this host's size_t is not too wide for the above table. */ static_assert(sizeof(size_t) * CHAR_BIT <= sizeof prime_offset, "Not enough primes in table"); /* Given a vector of two file_data objects, read the file associated with each one, and build the table of equivalence classes. Return nonzero if either file appears to be a binary file. If PRETEND_BINARY is nonzero, pretend they are binary regardless. */ bool GnuDiff::read_files(file_data filevec[], bool /*pretend_binary*/) { GNULineRef i; find_identical_ends(filevec); equivs_alloc = filevec[0].alloc_lines + filevec[1].alloc_lines + 1; if((GNULineRef)(GNULINEREF_MAX / sizeof *equivs) <= equivs_alloc) xalloc_die(); equivs = (equivclass *)xmalloc(equivs_alloc * sizeof *equivs); /* Equivalence class 0 is permanently safe for lines that were not hashed. Real equivalence classes start at 1. */ equivs_index = 1; /* Allocate (one plus) a prime number of hash buckets. Use a prime number between 1/3 and 2/3 of the value of equiv_allocs, approximately. */ for(i = 9; ((GNULineRef)1 << i) < equivs_alloc / 3; ++i) continue; nbuckets = ((GNULineRef)1 << i) - prime_offset[i]; if(GNULINEREF_MAX / sizeof *buckets <= nbuckets) xalloc_die(); buckets = (GNULineRef *)zalloc((nbuckets + 1) * sizeof *buckets); buckets++; for(i = 0; i < 2; ++i) find_and_hash_each_line(&filevec[i]); filevec[0].equiv_max = filevec[1].equiv_max = equivs_index; free(equivs); free(buckets - 1); return false; } diff --git a/src/merger.cpp b/src/merger.cpp index 807cb4d..e53b5ab 100644 --- a/src/merger.cpp +++ b/src/merger.cpp @@ -1,77 +1,77 @@ /*************************************************************************** * Copyright (C) 2003-2007 by Joachim Eibl * * Copyright (C) 2018 Michael Reeves reeves.87@gmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #include "merger.h" #include Merger::Merger(const DiffList* pDiffList1, const DiffList* pDiffList2) : md1(pDiffList1, 0), md2(pDiffList2, 1) { } Merger::MergeData::MergeData(const DiffList* p, int i) : d(0, 0, 0) { idx = i; pDiffList = p; if(p != nullptr) { it = p->begin(); update(); } } bool Merger::MergeData::eq() { return pDiffList == nullptr || d.nofEquals > 0; } bool Merger::MergeData::isEnd() { return (pDiffList == nullptr || (it == pDiffList->end() && d.nofEquals == 0 && - (idx == 0 ? d.diff1 == 0 : d.diff2 == 0))); + (idx == 0 ? d.diff1 == 0 : d.diff2 == 0))); } void Merger::MergeData::update() { if(d.nofEquals > 0) --d.nofEquals; else if(idx == 0 && d.diff1 > 0) --d.diff1; else if(idx == 1 && d.diff2 > 0) --d.diff2; while(d.nofEquals == 0 && ((idx == 0 && d.diff1 == 0) || (idx == 1 && d.diff2 == 0)) && pDiffList != nullptr && it != pDiffList->end()) { d = *it; ++it; } } void Merger::next() { md1.update(); md2.update(); } int Merger::whatChanged() { int changed = 0; changed |= md1.eq() ? 0 : 1; changed |= md2.eq() ? 0 : 2; return changed; } bool Merger::isEndReached() { return md1.isEnd() && md2.isEnd(); } diff --git a/src/merger.h b/src/merger.h index 20ed09b..7e730a5 100644 --- a/src/merger.h +++ b/src/merger.h @@ -1,55 +1,53 @@ /*************************************************************************** * Copyright (C) 2003-2007 by Joachim Eibl * * Copyright (C) 2018 Michael Reeves reeves.87@gmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * ***************************************************************************/ #ifndef MERGER_H #define MERGER_H #include "diff.h" - class Merger { -public: - - Merger( const DiffList* pDiffList1, const DiffList* pDiffList2 ); + public: + Merger(const DiffList* pDiffList1, const DiffList* pDiffList2); - /** Go one step. */ - void next(); + /** Go one step. */ + void next(); - /** Information about what changed. Can be used for coloring. + /** Information about what changed. Can be used for coloring. The return value is 0 if nothing changed here, bit 1 is set if a difference from pDiffList1 was detected, bit 2 is set if a difference from pDiffList2 was detected. */ - int whatChanged(); - - /** End of both diff lists reached. */ - bool isEndReached(); -private: - - struct MergeData - { - DiffList::const_iterator it; - const DiffList* pDiffList; - Diff d; - int idx; - - MergeData( const DiffList* p, int i ); - bool eq(); - void update(); - bool isEnd(); - }; - - MergeData md1; - MergeData md2; + int whatChanged(); + + /** End of both diff lists reached. */ + bool isEndReached(); + + private: + struct MergeData + { + DiffList::const_iterator it; + const DiffList* pDiffList; + Diff d; + int idx; + + MergeData(const DiffList* p, int i); + bool eq(); + void update(); + bool isEnd(); + }; + + MergeData md1; + MergeData md2; }; #endif