diff --git a/src/SourceData.cpp b/src/SourceData.cpp index 02aa541..81c6331 100644 --- a/src/SourceData.cpp +++ b/src/SourceData.cpp @@ -1,937 +1,937 @@ /*************************************************************************** * 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()) { 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()) { 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()) { 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)); } 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 - || isEndOfLine(p[i]) // Normal end of line + || 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) { 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); else 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(". - * + * */ #ifndef UTILS_H #define UTILS_H #include #include class Utils{ public: static bool wildcardMultiMatch(const QString& wildcard, const QString& testString, bool bCaseSensitive); static QString getArguments(QString cmd, QString& program, QStringList& args); + inline static bool isEndOfLine( QChar c ) { return c=='\n' || c=='\r' || c=='\x0b'; } }; #endif diff --git a/src/gnudiff_diff.h b/src/gnudiff_diff.h index 4669cc8..9c76ecc 100644 --- a/src/gnudiff_diff.h +++ b/src/gnudiff_diff.h @@ -1,379 +1,375 @@ /* Shared definitions for GNU DIFF Modified for KDiff3 by Joachim Eibl 2003, 2004, 2005. The original file was part of GNU DIFF. Copyright (C) 1988, 1989, 1991, 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. */ #ifndef GNUDIFF_DIFF_H #define GNUDIFF_DIFF_H +#include "LineRef.h" +#include "Utils.h" #include #include #include #include #include #include #include #include #include #include #include -#include "LineRef.h" /* The integer type of a line number. */ typedef qint64 GNULineRef; #define GNULINEREF_MAX std::numeric_limits::max() static_assert(sizeof(int) >= sizeof(qint32), "Legacy LP32 systems/compilers not supported");// e.g. Windows 16-bit static_assert(std::is_signed::value, "GNULineRef must be signed."); static_assert(sizeof(GNULineRef) >= sizeof(size_t),"GNULineRef must be able to receive size_t values."); -inline bool isEndOfLine( QChar c ) -{ - return c=='\n' || c=='\r' || c=='\x0b'; -} - #define TAB_WIDTH 8 class GnuDiff { public: /* What kind of changes a hunk contains. */ enum changes { /* No changes: lines common to both files. */ UNCHANGED, /* Deletes only: lines taken from just the first file. */ OLD, /* Inserts only: lines taken from just the second file. */ NEW, /* Both deletes and inserts: a hunk containing both old and new lines. */ CHANGED }; /* Variables for command line options */ /* Nonzero if output cannot be generated for identical files. */ bool no_diff_means_no_output; /* Number of lines of context to show in each set of diffs. This is zero when context is not to be shown. */ GNULineRef context; /* Consider all files as text files (-a). Don't interpret codes over 0177 as implying a "binary file". */ bool text; /* The significance of white space during comparisons. */ enum { /* All white space is significant (the default). */ IGNORE_NO_WHITE_SPACE, /* Ignore changes due to tab expansion (-E). */ IGNORE_TAB_EXPANSION, /* Ignore changes in horizontal white space (-b). */ IGNORE_SPACE_CHANGE, /* Ignore all horizontal white space (-w). */ IGNORE_ALL_SPACE } ignore_white_space; /* Ignore changes that affect only blank lines (-B). */ bool ignore_blank_lines; /* Ignore changes that affect only numbers. (J. Eibl) */ bool bIgnoreNumbers; bool bIgnoreWhiteSpace; /* Files can be compared byte-by-byte, as if they were binary. This depends on various options. */ bool files_can_be_treated_as_binary; /* Ignore differences in case of letters (-i). */ bool ignore_case; /* Ignore differences in case of letters in file names. */ bool ignore_file_name_case; /* Regexp to identify function-header lines (-F). */ //struct re_pattern_buffer function_regexp; /* Ignore changes that affect only lines matching this regexp (-I). */ //struct re_pattern_buffer ignore_regexp; /* Say only whether files differ, not how (-q). */ bool brief; /* Expand tabs in the output so the text lines up properly despite the characters added to the front of each line (-t). */ bool expand_tabs; /* Use a tab in the output, rather than a space, before the text of an input line, so as to keep the proper alignment in the input line without changing the characters in it (-T). */ bool initial_tab; /* In directory comparison, specify file to start with (-S). This is used for resuming an aborted comparison. All file names less than this name are ignored. */ const QChar *starting_file; /* Pipe each file's output through pr (-l). */ bool paginate; /* Line group formats for unchanged, old, new, and changed groups. */ const QChar *group_format[CHANGED + 1]; /* Line formats for unchanged, old, and new lines. */ const QChar *line_format[NEW + 1]; /* If using OUTPUT_SDIFF print extra information to help the sdiff filter. */ bool sdiff_merge_assist; /* Tell OUTPUT_SDIFF to show only the left version of common lines. */ bool left_column; /* Tell OUTPUT_SDIFF to not show common lines. */ bool suppress_common_lines; /* The half line width and column 2 offset for OUTPUT_SDIFF. */ unsigned int sdiff_half_width; unsigned int sdiff_column2_offset; /* Use heuristics for better speed with large files with a small density of changes. */ bool speed_large_files; /* Patterns that match file names to be excluded. */ struct exclude *excluded; /* Don't discard lines. This makes things slower (sometimes much slower) but will find a guaranteed minimal set of changes. */ bool minimal; /* The result of comparison is an "edit script": a chain of `struct change'. Each `struct change' represents one place where some lines are deleted and some are inserted. 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. */ struct change { struct change *link; /* Previous or next edit command */ GNULineRef inserted; /* # lines of file 1 changed here. */ GNULineRef deleted; /* # lines of file 0 changed here. */ GNULineRef line0; /* Line number of 1st deleted line. */ GNULineRef line1; /* Line number of 1st inserted line. */ bool ignore; /* Flag used in context.c. */ }; /* Structures that describe the input files. */ /* Data on one input file being compared. */ struct file_data { /* Buffer in which text of file is read. */ const QChar* buffer; /* Allocated size of buffer, in QChars. Always a multiple of sizeof *buffer. */ size_t bufsize; /* Number of valid bytes now in the buffer. */ size_t buffered; /* Array of pointers to lines in the file. */ const QChar **linbuf; /* linbuf_base <= buffered_lines <= valid_lines <= alloc_lines. linebuf[linbuf_base ... buffered_lines - 1] are possibly differing. linebuf[linbuf_base ... valid_lines - 1] contain valid data. linebuf[linbuf_base ... alloc_lines - 1] are allocated. */ GNULineRef linbuf_base, buffered_lines, valid_lines, alloc_lines; /* Pointer to end of prefix of this file to ignore when hashing. */ const QChar *prefix_end; /* Count of lines in the prefix. There are this many lines in the file before linbuf[0]. */ GNULineRef prefix_lines; /* Pointer to start of suffix of this file to ignore when hashing. */ const QChar *suffix_begin; /* Vector, indexed by line number, containing an equivalence code for each line. It is this vector that is actually compared with that of another file to generate differences. */ GNULineRef *equivs; /* Vector, like the previous one except that the elements for discarded lines have been squeezed out. */ GNULineRef *undiscarded; /* Vector mapping virtual line numbers (not counting discarded lines) to real ones (counting those lines). Both are origin-0. */ GNULineRef *realindexes; /* Total number of nondiscarded lines. */ GNULineRef nondiscarded_lines; /* Vector, indexed by real origin-0 line number, containing TRUE for a line that is an insertion or a deletion. The results of comparison are stored here. */ bool *changed; /* 1 if at end of file. */ bool eof; /* 1 more than the maximum equivalence value used for this or its sibling file. */ GNULineRef equiv_max; }; /* Data on two input files being compared. */ struct comparison { struct file_data file[2]; struct comparison const *parent; /* parent, if a recursive comparison */ }; /* Describe the two files currently being compared. */ struct file_data files[2]; /* Stdio stream to output diffs to. */ FILE *outfile; /* Declare various functions. */ /* analyze.c */ struct change* diff_2_files (struct comparison *); /* context.c */ void print_context_header (struct file_data[], bool); void print_context_script (struct change *, bool); /* dir.c */ int diff_dirs (struct comparison const *, int (*) (struct comparison const *, const QChar *, const QChar *)); /* ed.c */ void print_ed_script (struct change *); void pr_forward_ed_script (struct change *); /* ifdef.c */ void print_ifdef_script (struct change *); /* io.c */ void file_block_read (struct file_data *, size_t); bool read_files (struct file_data[], bool); /* normal.c */ void print_normal_script (struct change *); /* rcs.c */ void print_rcs_script (struct change *); /* side.c */ void print_sdiff_script (struct change *); /* util.c */ QChar *concat (const QChar *, const QChar *, const QChar *); bool lines_differ ( const QChar *, size_t, const QChar *, size_t ); GNULineRef translate_line_number (struct file_data const *, GNULineRef); struct change *find_change (struct change *); struct change *find_reverse_change (struct change *); void *zalloc (size_t); enum changes analyze_hunk (struct change *, GNULineRef *, GNULineRef *, GNULineRef *, GNULineRef *); void begin_output (void); void debug_script (struct change *); void finish_output (void); void message (const QChar *, const QChar *, const QChar *); void message5 (const QChar *, const QChar *, const QChar *, const QChar *, const QChar *); void output_1_line (const QChar *, const QChar *, const QChar *, const QChar *); void perror_with_name (const QChar *); void setup_output (const QChar *, const QChar *, bool); void translate_range (struct file_data const *, GNULineRef, GNULineRef, long *, long *); /* version.c */ //extern const QChar version_string[]; private: // gnudiff_analyze.cpp GNULineRef diag (GNULineRef xoff, GNULineRef xlim, GNULineRef yoff, GNULineRef ylim, bool find_minimal, struct partition *part); void compareseq (GNULineRef xoff, GNULineRef xlim, GNULineRef yoff, GNULineRef ylim, bool find_minimal); void discard_confusing_lines (struct file_data filevec[]); void shift_boundaries (struct file_data filevec[]); struct change * add_change (GNULineRef line0, GNULineRef line1, GNULineRef deleted, GNULineRef inserted, struct change *old); struct change * build_reverse_script (struct file_data const filevec[]); struct change* build_script (struct file_data const filevec[]); // gnudiff_io.cpp void find_and_hash_each_line (struct file_data *current); void find_identical_ends (struct file_data filevec[]); // gnudiff_xmalloc.cpp void *xmalloc (size_t n); void *xrealloc(void *p, size_t n); void xalloc_die (void); inline bool isWhite( QChar c ) { return c==' ' || c=='\t' || c=='\r'; } }; // class GnuDiff # define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) # define XREALLOC(Ptr, Type, N_items) \ ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) /* Declare and alloc memory for VAR of type TYPE. */ # define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) /* Free VAR only if non NULL. */ # define XFREE(Var) \ do { \ if (Var) \ free (Var); \ } while (0) /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ # define CCLONE(Src, Num) \ (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) /* Return a malloc'ed copy of SRC. */ # define CLONE(Src) CCLONE (Src, 1) #endif diff --git a/src/gnudiff_io.cpp b/src/gnudiff_io.cpp index 7bc1f2d..ccb753c 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. */ 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 struct 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(struct 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); struct 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 && !isEndOfLine(c = *p)) + 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 && !isEndOfLine(c = *p)) + 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 && !isEndOfLine(c = *p)) + 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 && !isEndOfLine(c = *p)) + 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 && !isEndOfLine(*p++)) + 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(struct 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 && !isEndOfLine(p0[-1])) + 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 && !isEndOfLine(*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 && !isEndOfLine(*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(struct 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; }