Commit 2545212f authored by Michael Reeves's avatar Michael Reeves

Complete transition to one loop.

parent 4cc686cb
......@@ -189,7 +189,7 @@ const char* SourceData::getBuf() const
const QString& SourceData::getText() const
{
return m_normalData.m_unicodeBuf;
return *m_normalData.m_unicodeBuf;
}
bool SourceData::isText()
......@@ -251,6 +251,11 @@ bool SourceData::FileData::readFile(FileAccess& file)
m_pBuf = nullptr;
m_size = 0;
}
//null terminate buffer
pBuf[m_size + 1] = 0;
pBuf[m_size + 2] = 0;
pBuf[m_size + 3] = 0;
pBuf[m_size + 4] = 0;
return bSuccess;
}
......@@ -523,10 +528,10 @@ QStringList SourceData::readAndPreprocess(QTextCodec* pEncoding, bool bAutoDetec
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_v.push_back(LineData(m_lmppData.m_unicodeBuf, m_lmppData.m_unicodeBuf->length()));
}
m_lmppData.m_vSize = m_normalData.m_vSize;
......@@ -559,12 +564,16 @@ bool SourceData::FileData::preprocess(bool bPreserveCR, QTextCodec* pEncoding)
if(pEncoding == nullptr)
return false;
qint64 i;
QString line;
QChar curChar;
LineCount lineCount = 0;
qint64 lastOffset = 0;
qint64 skipBytes = 0;
// detect line end style
QVector<e_LineEndStyle> vOrigDataLineEndStyle;
m_eLineEndStyle = eLineEndStyleUndefined;
qint64 skipBytes = 0;
QTextCodec* pCodec = detectEncoding(m_pBuf, m_size, skipBytes);
if(pCodec != pEncoding)
skipBytes = 0;
......@@ -576,16 +585,10 @@ bool SourceData::FileData::preprocess(bool bPreserveCR, QTextCodec* pEncoding)
QTextStream ts(ba, QIODevice::ReadOnly); //Don't use text mode we need to see the actual line ending.
ts.setCodec(pEncoding);
ts.setAutoDetectUnicode(false);
QString line;
QChar curChar;
LineCount lineCount = 0;
qint32 lineLength = 0;
bool bNonWhiteFound = false;
qint32 whiteLength = 0;
qint64 lastOffset = 0;
m_bIncompleteConversion = false;
m_unicodeBuf.clear();
m_unicodeBuf->clear();
Q_ASSERT(m_unicodeBuf->length() == 0);
while(!ts.atEnd())
{
......@@ -593,9 +596,9 @@ bool SourceData::FileData::preprocess(bool bPreserveCR, QTextCodec* pEncoding)
if(lineCount >= TYPE_MAX(LineCount) - 5)
return false;
m_v.push_back(LineData(lastOffset));
ts >> curChar;
quint32 firstNonwhite=0;
//QTextStream::readLine doesn't tell us abount line endings.
while(curChar != '\n' && curChar != '\r')
{
......@@ -605,6 +608,9 @@ bool SourceData::FileData::preprocess(bool bPreserveCR, QTextCodec* pEncoding)
if(curChar == QChar::ReplacementCharacter)
m_bIncompleteConversion = true;
if(!curChar.isSpace())
firstNonwhite = line.length();
line.append(curChar);
if(ts.atEnd())
......@@ -645,71 +651,24 @@ bool SourceData::FileData::preprocess(bool bPreserveCR, QTextCodec* pEncoding)
break;
}
//kdiff3 internally uses only unix style endings for simplicity.
//line.append('\n');
m_v[lineCount - 1].setLine(line);
m_unicodeBuf.append(line).append('\n');
m_v.push_back(LineData(m_unicodeBuf, lastOffset, line.length()));
m_unicodeBuf->append(line).append('\n');
lastOffset = m_unicodeBuf->length();
}
m_v.push_back(LineData(lastOffset));
m_v.push_back(LineData(m_unicodeBuf, lastOffset));
Q_ASSERT(m_v[m_v.size() - 1].getOffset() != m_v[m_v.size() - 2].getOffset());
m_bIsText = true;
if(!vOrigDataLineEndStyle.isEmpty())
m_eLineEndStyle = vOrigDataLineEndStyle[0];
int ucSize = m_unicodeBuf.length();
const QChar* p = m_unicodeBuf.unicode();
m_v.push_back(LineData());
int lineIdx = 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].getRawLine() + std::min(whiteLength, lineLength));
if(lineIdx < vOrigDataLineEndStyle.count() && bPreserveCR && i < ucSize)
{
++lineLength;
const_cast<QChar*>(pLine)[lineLength] = '\r';
//switch ( vOrigDataLineEndStyle[lineIdx] )
//{
//case eLineEndStyleUnix: const_cast<QChar*>(pLine)[lineLength] = '\n'; break;
//case eLineEndStyleDos: const_cast<QChar*>(pLine)[lineLength] = '\r'; break;
//case eLineEndStyleUndefined: const_cast<QChar*>(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 == lineCount || lineIdx == lineCount + 1);//account for buggy old behavior of always appending an extra line. New loop only does this if the file does not end with a EOL marker.
m_vSize = lineCount;
return true;
}
// Depriated
// Depriated - move to comment handler
// 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".
......@@ -759,7 +718,7 @@ void SourceData::FileData::checkLineForComments(
if(!bWhite)
{
size = i - commentStart;
m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size));
m_unicodeBuf->replace(commentStart, size, QString(" ").repeated(size));
}
return;
}
......@@ -781,7 +740,7 @@ void SourceData::FileData::checkLineForComments(
if(!bWhite)
{
size = i - commentStart;
m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size));
m_unicodeBuf->replace(commentStart, size, QString(" ").repeated(size));
}
return;
}
......@@ -808,9 +767,9 @@ void SourceData::FileData::checkLineForComments(
void SourceData::FileData::removeComments()
{//TODO: Phase out
int line = 0;
const QChar* p = m_unicodeBuf.unicode();
const QChar* p = m_unicodeBuf->unicode();
bool bWithinComment = false;
int size = m_unicodeBuf.length();
int size = m_unicodeBuf->length();
qCDebug(kdiffCore) << "m_v.size() = " << m_v.size() << ", size = " << size;
Q_ASSERT(m_v.size() > 0);
......@@ -836,7 +795,7 @@ void SourceData::FileData::removeComments()
if(!bWhite)
{
size = i - commentStart;
m_unicodeBuf.replace(commentStart, size, QString(" ").repeated(size));
m_unicodeBuf->replace(commentStart, size, QString(" ").repeated(size));
}
break;
}
......
......@@ -83,7 +83,7 @@ class SourceData
const char* m_pBuf = nullptr; //TODO: Phase out needlessly wastes memmory and time by keeping second copy of file data.
qint64 m_size = 0;
qint64 m_vSize = 0; // Nr of lines in m_pBuf1 and size of m_v1, m_dv12 and m_dv13
QString m_unicodeBuf;
QSharedPointer<QString> m_unicodeBuf=QSharedPointer<QString>::create();
QVector<LineData> m_v;
bool m_bIsText = false;
bool m_bIncompleteConversion = false;
......
......@@ -27,6 +27,7 @@
int LineData::width(int tabSize) const
{
QString pLine = getLine();
int w = 0;
int j = 0;
for(int i = 0; i < size(); ++i)
......@@ -563,10 +564,10 @@ static bool runDiff(const QVector<LineData>* p1, const qint32 index1, LineRef si
GnuDiff::comparison comparisonInput;
memset(&comparisonInput, 0, sizeof(comparisonInput));
comparisonInput.parent = nullptr;
comparisonInput.file[0].buffer = (*p1)[index1].getRawLine(); //ptr to buffer
comparisonInput.file[0].buffered = ((*p1)[size1 - 1].getRawLine() - (*p1)[index1].getRawLine() + (*p1)[size1 - 1].size()); // size of buffer
comparisonInput.file[1].buffer = (*p2)[index2].getRawLine(); //ptr to buffer
comparisonInput.file[1].buffered = ((*p2)[size2 - 1].getRawLine() - (*p2)[index2].getRawLine() + (*p2)[size2 - 1].size()); // size of buffer
comparisonInput.file[0].buffer = (*p1)[index1].getBuffer()->unicode() + (*p1)[index1].getOffset(); //ptr to buffer
comparisonInput.file[0].buffered = ((*p1)[size1].getOffset() - 1); // size of buffer
comparisonInput.file[1].buffer = (*p2)[index2].getBuffer()->unicode() + (*p2)[index2].getOffset(); //ptr to buffer
comparisonInput.file[1].buffered = ((*p2)[size2].getOffset() - 1); // size of buffer
gnuDiff.ignore_white_space = GnuDiff::IGNORE_ALL_SPACE; // I think nobody needs anything else ...
gnuDiff.bIgnoreWhiteSpace = true;
......
......@@ -76,34 +76,37 @@ typedef std::list<Diff> DiffList;
class LineData
{
private:
QString pLine;
quint32 pFirstNonWhiteChar = 0;
Q_DECL_DEPRECATED const QChar* pLine_old = nullptr;
Q_DECL_DEPRECATED const QChar* pFirstNonWhiteChar_old = nullptr;
int mSize = 0;
QSharedPointer<QString> mBuffer;
//QString pLine;
QtNumberType mFirstNonWhiteChar = 0;
qint64 mOffset = 0;
QtNumberType mSize = 0;
Q_DECL_DEPRECATED bool bContainsPureComment = false;
public:
LineData() = default;
inline LineData(const qint64 inOffset) { mOffset = inOffset; }
Q_REQUIRED_RESULT inline int size() const { Q_ASSERT(pLine.length() == mSize); return pLine.length(); }
Q_DECL_DEPRECATED inline void setSize(const int newSize) { mSize = newSize; }
Q_DECL_DEPRECATED inline void setFirstNonWhiteChar(const QChar* firstNonWhiteChar) { pFirstNonWhiteChar_old = firstNonWhiteChar;}
Q_DECL_DEPRECATED inline const QChar* getFirstNonWhiteChar() const { return pFirstNonWhiteChar_old; }
Q_REQUIRED_RESULT inline const QString getLine() const { return pLine; }
Q_DECL_DEPRECATED Q_REQUIRED_RESULT inline const QChar* getRawLine() const { return pLine_old; }
Q_DECL_DEPRECATED inline void setLine(const QChar* line) { pLine_old = line;}
inline void setLine(const QString& line) { pLine = line;}
inline qint64 getOffset() { return mOffset; }
inline void setOffset(qint64 inOffset) { mOffset = inOffset; }
int width(int tabSize) const; // Calcs width considering tabs.
explicit LineData() = default; // needed for QtInternal reasons should not be used.
inline LineData(const QSharedPointer<QString> &buffer, const qint64 inOffset, QtNumberType inSize = 0)
{
mBuffer = buffer;
mOffset = inOffset;
mSize = inSize;
}
Q_REQUIRED_RESULT inline int size() const { return mSize; }
inline void setFirstNonWhiteChar(const qint32 firstNonWhiteChar) { mFirstNonWhiteChar = firstNonWhiteChar;}
Q_REQUIRED_RESULT inline qint32 getFirstNonWhiteChar() const { return mFirstNonWhiteChar; }
/*
QString::fromRawData allows us to create a light weight QString backed by the buffer memmory.
*/
Q_REQUIRED_RESULT inline const QString getLine() const { return QString::fromRawData(mBuffer->data() + mOffset, mSize); }
//inline void setLine(const QString& line) { pLine = line;}
Q_REQUIRED_RESULT const QSharedPointer<QString>& getBuffer() const { return mBuffer; }
Q_REQUIRED_RESULT inline qint64 getOffset() const { return mOffset; }
Q_REQUIRED_RESULT int width(int tabSize) const; // Calcs width considering tabs.
//int occurrences;
bool whiteLine() const { return pFirstNonWhiteChar_old - pLine_old == mSize; }
bool whiteLine() const { return mFirstNonWhiteChar == mSize - 1; }
bool isPureComment() const { return bContainsPureComment; }
void setPureComment(const bool bPureComment) { bContainsPureComment = bPureComment; }
......@@ -189,7 +192,7 @@ class Diff3Line
if(src == C && lineC >= 0) return &(*m_pDiffBufferInfo->m_pLineDataC)[lineC];
return nullptr;
}
QString getString(const e_SrcSelector src) const
const QString getString(const e_SrcSelector src) const
{
const LineData* pld = getLineData(src);
if(pld)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment