From 2f011cfc1a1146f57c920f77db6e717ef401a029 Mon Sep 17 00:00:00 2001 From: Takashi Sawanaka Date: Sun, 15 Dec 2024 09:35:42 +0900 Subject: [PATCH] WIP --- Src/CompareEngines/ByteComparator.cpp | 48 +++- .../ByteCompare/ByteCompare_test.cpp | 208 +++++++++++++++++- 2 files changed, 247 insertions(+), 9 deletions(-) diff --git a/Src/CompareEngines/ByteComparator.cpp b/Src/CompareEngines/ByteComparator.cpp index 60c15efc1aa..bcd253c0819 100644 --- a/Src/CompareEngines/ByteComparator.cpp +++ b/Src/CompareEngines/ByteComparator.cpp @@ -354,8 +354,6 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers( if ((!m_eol0 || !m_eol1) && (orig0 == end0 || orig1 == end1)) { // one side had an end-of-line, but the other didn't - if (m_ignore_eof_newline_presence) - continue; result = RESULT_DIFF; goto exit; } @@ -384,16 +382,50 @@ ByteComparator::COMP_RESULT ByteComparator::CompareBuffers( { if (m_ignore_eof_newline_presence) { - if (eof0 || eof1) + if (eof0 && eof1) { - HandleSide0Eol((char **) &ptr0, end0, eof0); - HandleSide1Eol((char **) &ptr1, end1, eof1); - - if (m_cr0 || m_cr1) + 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 { - // these flags mean possible split CR/LF + 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; } + result = RESULT_DIFF; + goto exit; } } if (ptr0 == end0 && ptr1 == end1) diff --git a/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp b/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp index 1c1c6b23857..6c53c76282d 100644 --- a/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp +++ b/Testing/GoogleTest/ByteCompare/ByteCompare_test.cpp @@ -796,8 +796,214 @@ namespace EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); } } - } + TEST_F(ByteCompareTest, IgnoreEofNewlinePresenceAndIgnoreEOLDifference) + { + CompareEngines::ByteCompare bc; + QuickCompareOptions option; + std::string filename_left = "_tmp_.txt"; + std::string filename_right = "_tmp_2.txt"; + + option.m_bIgnoreEofNewlinePresence = true; + option.m_bIgnoreEOLDifference = true; + bc.SetCompareOptions(option); + + for (int i = 0; i < 3; i++) + { + {// same left: LF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 - 1 + i); + std::vector buf_right(WMCMPBUFF * 2 - 1 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[10] = '\n'; + buf_left[buf_left.size() - 1] = '\n'; + buf_right[10] = '\r'; + buf_right[11] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + + {// same left: no EOL - right: LF + std::vector buf_left(WMCMPBUFF * 2 - 1 + i); + std::vector buf_right(WMCMPBUFF * 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// same left: CRLF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + + {// same left: no EOL - right: CRLF + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::SAME, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// diff left: CR+A - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = 'A'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: CR+A + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = 'A'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// diff left: LF+LF - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[buf_left.size() - 2] = '\n'; + buf_left[buf_left.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: LF+LF + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[buf_right.size() - 2] = '\n'; + buf_right[buf_right.size() - 1] = '\n'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + + for (int i = 0; i < 3; i++) + { + {// diff left: CR+CR - right: no EOL + std::vector buf_left(WMCMPBUFF * 2 + i); + std::vector buf_right(WMCMPBUFF * 2 - 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_left[buf_left.size() - 2] = '\r'; + buf_left[buf_left.size() - 1] = '\r'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + + {// diff left: no EOL - right: CR+CR + std::vector buf_left(WMCMPBUFF * 2 - 2 + i); + std::vector buf_right(WMCMPBUFF * 2 + i); + + memset(buf_left.data(), 'A', buf_left.size()); + memset(buf_right.data(), 'A', buf_right.size()); + + buf_right[buf_right.size() - 2] = '\r'; + buf_right[buf_right.size() - 1] = '\r'; + + TempFile file_left(filename_left, buf_left.data(), buf_left.size()); + TempFile file_right(filename_right, buf_right.data(), buf_right.size()); + + FilePair pair(filename_left, filename_right); + + EXPECT_EQ(DIFFCODE::TEXT | DIFFCODE::DIFF, bc.CompareFiles(&pair.diffData)); + } + } + } } // namespace