Skip to content

Commit bab67e2

Browse files
committed
Ignore numbers when the "Ignore numbers" comparison option is specified
1 parent 992ac65 commit bab67e2

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

src/WinWebDiffLib/DiffHighlighter.hpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ struct TextSegments
130130
return true;
131131
}
132132
}
133-
void Make(const std::wstring& text)
133+
void Make(const std::wstring& text, bool ignoreNumbers)
134134
{
135135
allText = text;
136136
int charTypePrev = -1;
@@ -143,6 +143,8 @@ struct TextSegments
143143
charType = 1;
144144
else if (isWordBreak(ch))
145145
charType = 2;
146+
else if (ignoreNumbers && iswdigit(ch))
147+
charType = 3;
146148
if (charType == 2 || charType != charTypePrev)
147149
{
148150
if (i > 0)
@@ -200,7 +202,7 @@ class DataForDiff
200202
bool equals(const char* scanline1, unsigned size1,
201203
const char* scanline2, unsigned size2) const
202204
{
203-
if (!m_diffOptions.ignoreCase && m_diffOptions.ignoreWhitespace == 0)
205+
if (!m_diffOptions.ignoreCase && m_diffOptions.ignoreWhitespace == 0 && !m_diffOptions.ignoreNumbers)
204206
{
205207
if (size1 != size2)
206208
return false;
@@ -223,6 +225,13 @@ class DataForDiff
223225
i1++;
224226
while (i2 < s2 && iswspace(l2[i2]))
225227
i2++;
228+
if (m_diffOptions.ignoreNumbers)
229+
{
230+
while (i1 < s1 && iswdigit(l1[i1]))
231+
i1++;
232+
while (i2 < s2 && iswdigit(l2[i2]))
233+
i2++;
234+
}
226235
}
227236
}
228237
else if (m_diffOptions.ignoreWhitespace == 1)
@@ -238,6 +247,15 @@ class DataForDiff
238247
i2++;
239248
continue;
240249
}
250+
if (m_diffOptions.ignoreNumbers)
251+
{
252+
while (i1 < s1 && iswdigit(l1[i1]))
253+
i1++;
254+
while (i2 < s2 && iswdigit(l2[i2]))
255+
i2++;
256+
if (i1 >= s1 || i2 >= s2)
257+
continue;
258+
}
241259
if (!match_a_wchar(l1[i1++], l2[i2++]))
242260
return false;
243261
}
@@ -246,6 +264,15 @@ class DataForDiff
246264
{
247265
while (i1 < s1 && i2 < s2)
248266
{
267+
if (m_diffOptions.ignoreNumbers)
268+
{
269+
while (i1 < s1 && iswdigit(l1[i1]))
270+
i1++;
271+
while (i2 < s2 && iswdigit(l2[i2]))
272+
i2++;
273+
if (i1 >= s1 || i2 >= s2)
274+
continue;
275+
}
249276
if (!match_a_wchar(l1[i1++], l2[i2++]))
250277
return false;
251278
}
@@ -265,7 +292,7 @@ class DataForDiff
265292
const wchar_t* begin = reinterpret_cast<const wchar_t*>(scanline);
266293
const wchar_t* end = reinterpret_cast<const wchar_t*>(this->next(scanline));
267294

268-
if (!m_diffOptions.ignoreCase && m_diffOptions.ignoreWhitespace == 0)
295+
if (!m_diffOptions.ignoreCase && m_diffOptions.ignoreWhitespace == 0 && !m_diffOptions.ignoreNumbers)
269296
{
270297
for (const auto* ptr = begin; ptr < end; ptr++)
271298
{
@@ -289,6 +316,8 @@ class DataForDiff
289316
}
290317
continue;
291318
}
319+
if (m_diffOptions.ignoreNumbers && iswdigit(*ptr))
320+
continue;
292321
ha += (ha << 5);
293322
ha ^= hash_a_wchar(*ptr);
294323
}
@@ -676,9 +705,9 @@ class Highlighter
676705
std::pair<WValue*, WValue*> pair = domutils::findNodeId(m_documents[pane][L"root"], diffInfo.nodeIds[pane]);
677706
pvalues[pane] = pair.first;
678707
if (diffInfo.nodePos[pane] == 0 && pvalues[pane])
679-
textSegments[pane].Make((*pvalues[pane])[L"nodeValue"].GetString());
708+
textSegments[pane].Make((*pvalues[pane])[L"nodeValue"].GetString(), m_diffOptions.ignoreNumbers);
680709
else
681-
textSegments[pane].Make(L"");
710+
textSegments[pane].Make(L"", m_diffOptions.ignoreNumbers);
682711
}
683712
if (m_showWordDifferences)
684713
wordDiffInfoList = Comparer::compare(m_diffOptions, textSegments);

src/WinWebDiffTest/WinWebDiffTest.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ const wchar_t* json2 = LR"(
278278
TEST_METHOD(TestMethod3)
279279
{
280280
std::vector<TextSegments> textSegments(2);
281-
textSegments[0].Make(L"abc");
282-
textSegments[1].Make(L"abc ");
281+
textSegments[0].Make(L"abc", false);
282+
textSegments[1].Make(L"abc ", false);
283283

284284
IWebDiffWindow::DiffOptions diffOptions1{};
285285
std::vector<DiffInfo> diffInfos1 = Comparer::compare(diffOptions1, textSegments);
@@ -289,7 +289,29 @@ const wchar_t* json2 = LR"(
289289
diffOptions2.ignoreWhitespace = 2;
290290
std::vector<DiffInfo> diffInfos2 = Comparer::compare(diffOptions2, textSegments);
291291
Assert::AreEqual((size_t)0, diffInfos2.size());
292+
}
293+
294+
TEST_METHOD(TestMethod4)
295+
{
296+
std::vector<TextSegments> textSegments(2);
297+
textSegments[0].Make(L"123AB", true);
298+
textSegments[1].Make(L"456AB ", true);
299+
300+
IWebDiffWindow::DiffOptions diffOptions1{};
301+
std::vector<DiffInfo> diffInfos1 = Comparer::compare(diffOptions1, textSegments);
302+
Assert::AreEqual((size_t)2, diffInfos1.size());
303+
304+
IWebDiffWindow::DiffOptions diffOptions2{};
305+
diffOptions2.ignoreWhitespace = 1;
306+
diffOptions2.ignoreNumbers = true;
307+
std::vector<DiffInfo> diffInfos2 = Comparer::compare(diffOptions2, textSegments);
308+
Assert::AreEqual((size_t)1, diffInfos2.size());
292309

310+
IWebDiffWindow::DiffOptions diffOptions3{};
311+
diffOptions3.ignoreWhitespace = 2;
312+
diffOptions3.ignoreNumbers = true;
313+
std::vector<DiffInfo> diffInfos3 = Comparer::compare(diffOptions3, textSegments);
314+
Assert::AreEqual((size_t)0, diffInfos3.size());
293315
}
294316
};
295317
}

0 commit comments

Comments
 (0)