From 852a4b1a853017000324422e7dbb9bda1c464a69 Mon Sep 17 00:00:00 2001 From: sdottaka Date: Tue, 17 Dec 2024 07:22:11 +0900 Subject: [PATCH] WIP --- Src/CompareEngines/ByteComparator.cpp | 53 +++------------------------ Src/CompareEngines/ByteComparator.h | 1 - Src/CompareEngines/ByteCompare.cpp | 31 +++++++++++++++- 3 files changed, 35 insertions(+), 50 deletions(-) diff --git a/Src/CompareEngines/ByteComparator.cpp b/Src/CompareEngines/ByteComparator.cpp index 3567c1c30fd..03a3782b6b7 100644 --- a/Src/CompareEngines/ByteComparator.cpp +++ b/Src/CompareEngines/ByteComparator.cpp @@ -111,7 +111,6 @@ ByteComparator::ByteComparator(const QuickCompareOptions * options) , m_ignore_eol_diff(options->m_bIgnoreEOLDifference) , m_ignore_blank_lines(options->m_bIgnoreBlankLines) , m_ignore_numbers(options->m_bIgnoreNumbers) - , m_ignore_eof_newline_presence(options->m_bIgnoreEofNewlinePresence) // state , m_wsflag(false) , m_eol0(false) @@ -341,6 +340,8 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers( } else // don't skip blank lines, but still ignore eol difference { + const char* ptr0b = ptr0; + const char* ptr1b = ptr1; HandleSide0Eol((char **) &ptr0, end0, eof0); HandleSide1Eol((char **) &ptr1, end1, eof1); @@ -349,11 +350,13 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers( // these flags mean possible split CR/LF goto need_more; } - if (!m_ignore_eof_newline_presence && (m_eol0 || m_eol1)) + if (m_eol0 || m_eol1) { if ((!m_eol0 || !m_eol1) && (orig0 == end0 || orig1 == end1)) { // one side had an end-of-line, but the other didn't + ptr0 = ptr0b; + ptr1 = ptr1b; result = RESULT_DIFF; goto exit; } @@ -380,52 +383,6 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers( if (ptr0 == end0 || ptr1 == end1) { - if (m_ignore_eof_newline_presence) - { - if (eof0 && eof1) - { - const size_t rest0 = end0 - ptr0; - const size_t rest1 = end1 - ptr1; - if ((rest0 == 0 && rest1 == 0) || - (rest0 == 1 && ((!m_cr0 && *ptr0 == '\r') || ((m_cr0 || !m_eol0) && *ptr0 == '\n'))) || - (rest0 == 2 && (!m_eol0 && (*ptr0 == '\r' && *(ptr0 + 1) == '\n'))) || - (rest1 == 1 && ((!m_cr1 && *ptr1 == '\r') || ((m_cr1 || !m_eol1) && *ptr1 == '\n'))) || - (rest1 == 2 && (!m_eol1 && (*ptr1 == '\r' && *(ptr1 + 1) == '\n')))) - { - ptr0 = end0; - ptr1 = end1; - result = RESULT_SAME; - } - else - { - result = RESULT_DIFF; - } - goto exit; - } - else if (eof0 || eof1) - { - const size_t rest0 = end0 - ptr0; - const size_t rest1 = end1 - ptr1; - if ((rest0 == 0 && rest1 == 0) || - (rest0 == 1 && (*ptr0 == '\r' || *ptr0 == '\n')) || - (rest0 == 2 && (*ptr0 == '\r' && *(ptr0 + 1) == '\n')) || - (rest1 == 1 && (*ptr1 == '\r' || *ptr1 == '\n')) || - (rest1 == 2 && (*ptr1 == '\r' && *(ptr1 + 1) == '\n'))) - { - if (end0 - ptr0 >= 1) - { - m_eol0 = true; - ptr0 = end0; - } - if (end1 - ptr1 >= 1) - { - m_eol1 = true; - ptr1 = end1; - } - goto need_more; - } - } - } if (ptr0 == end0 && ptr1 == end1) { if (!eof0 || !eof1) diff --git a/Src/CompareEngines/ByteComparator.h b/Src/CompareEngines/ByteComparator.h index 23c60b2c95d..cf7ec209324 100644 --- a/Src/CompareEngines/ByteComparator.h +++ b/Src/CompareEngines/ByteComparator.h @@ -54,7 +54,6 @@ class ByteComparator bool m_ignore_all_space; /**< Ignore all whitespace changes */ bool m_ignore_eol_diff; /**< Ignore differences in EOL bytes */ bool m_ignore_blank_lines; /**< Ignore blank lines */ - bool m_ignore_eof_newline_presence; /**< Ignore end-of-file newline presence */ // state bool m_wsflag; /**< ignore_space_change & in a whitespace area */ bool m_eol0; /**< 0-side has an eol */ diff --git a/Src/CompareEngines/ByteCompare.cpp b/Src/CompareEngines/ByteCompare.cpp index c0f2b80be54..7f8c1a2b1d8 100644 --- a/Src/CompareEngines/ByteCompare.cpp +++ b/Src/CompareEngines/ByteCompare.cpp @@ -80,6 +80,7 @@ int ByteCompare::CompareFiles(DiffFileData* diffData) // because transform code converted any UCS-2 files to UTF-8 // We could compare directly in UCS-2LE here, as an optimization, in that case char buff[2][WMCMPBUFF]; // buffered access to files + std::string lasteol[2]; int i; unsigned diffcode = 0; @@ -126,11 +127,23 @@ int ByteCompare::CompareFiles(DiffFileData* diffData) if (rtn < space) eof[i] = true; bfend[i] += rtn; + if (m_pOptions->m_bIgnoreEofNewlinePresence) + { + for (int j = bfstart[i]; j < bfend[i]; ++j) + { + const char c = buff[i][j]; + if (c == '\r' || c == '\n') + lasteol[i].push_back(c); + else + lasteol[i].clear(); + } + } if (diffData->m_inf[0].desc == diffData->m_inf[1].desc) { bfstart[1] = bfstart[0]; bfend[1] = bfend[0]; eof[1] = eof[0]; + lasteol[1] = lasteol[0]; diffData->m_FileLocation[1] = diffData->m_FileLocation[0]; memcpy(&buff[1][bfend[1] - rtn], &buff[0][bfend[0] - rtn], rtn); break; @@ -165,7 +178,21 @@ int ByteCompare::CompareFiles(DiffFileData* diffData) } else { - diffcode |= DIFFCODE::DIFF; + if (m_pOptions->m_bIgnoreEofNewlinePresence) + { + if ((eof[0] || eof[1]) && + ((end0 - ptr0 <= 1 && (lasteol[0] == "\r" || lasteol[0] == "\n" || lasteol[0] == "\r\n") && (end1 == ptr1))) || + ((end0 - ptr0 == 2 && (lasteol[0] == "\r\n") && (end1 == ptr1))) || + ((end1 - ptr1 <= 1 && (lasteol[1] == "\r" || lasteol[1] == "\n" || lasteol[1] == "\r\n") && (end0 == ptr0))) || + ((end1 - ptr1 == 2 && (lasteol[1] == "\r\n") && (end0 == ptr0)))) + ; + else + diffcode |= DIFFCODE::DIFF; + } + else + { + diffcode |= DIFFCODE::DIFF; + } ptr0 = end0; ptr1 = end1; // move our current pointers over what we just compared @@ -250,7 +277,9 @@ int ByteCompare::CompareFiles(DiffFileData* diffData) // If either unfinished, they differ if (ptr0 != end0 || ptr1 != end1) + { diffcode = (diffcode & DIFFCODE::DIFF); + } if (diffcode & DIFFCODE::DIFF) return diffcode | DIFFCODE::DIFF; else