Skip to content

Commit

Permalink
WIP: Improve substitution filters (4)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka committed Sep 17, 2023
1 parent ca99ed1 commit fe4fbe0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 26 deletions.
65 changes: 40 additions & 25 deletions Src/DiffWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ extern "C" int is_blank_line(char const* pch, char const* limit);
static void CopyTextStats(const file_data * inf, FileTextStats * myTextStats);
static void CopyDiffutilTextStats(file_data *inf, DiffFileData * diffData);

constexpr char* FILTERED_LINE = "!" "c0d5089f" "-" "3d91" "-" "4d69" "-" "b406" "-" "dc5a5b51a4f8";

/**
* @brief Default constructor.
* Initializes members.
Expand Down Expand Up @@ -380,22 +382,6 @@ bool CDiffWrapper::PostFilter(PostFilterContext& ctxt, change* thisob, const fil
const int LineNumberLeft = trans_a0 - 1;
const int LineNumberRight = trans_a1 - 1;

if (m_pFilterList != nullptr && m_pFilterList->HasRegExps())
{
// Match lines against regular expression filters
// Our strategy is that every line in both sides must
// match regexp before we mark difference as ignored.
bool match2 = false;
bool match1 = RegExpFilter(LineNumberLeft + file_data_ary[0].linbuf_base, LineNumberLeft + file_data_ary[0].linbuf_base + QtyLinesLeft - 1, &file_data_ary[0]);
if (match1)
match2 = RegExpFilter(LineNumberRight + file_data_ary[1].linbuf_base, LineNumberRight + file_data_ary[1].linbuf_base + QtyLinesRight - 1, &file_data_ary[1]);
if (match1 && match2)
{
thisob->trivial = 1;
return true;
}
}

std::string LineDataLeft, LineDataRight;

if (m_options.m_filterCommentsLines)
Expand Down Expand Up @@ -423,6 +409,22 @@ bool CDiffWrapper::PostFilter(PostFilterContext& ctxt, change* thisob, const fil
- file_data_ary[1].linbuf[LineNumberRight + file_data_ary[1].linbuf_base]);
}

if (m_pFilterList != nullptr && m_pFilterList->HasRegExps())
{
// Match lines against regular expression filters
// Our strategy is that every line in both sides must
// match regexp before we mark difference as ignored.
bool match2 = false;
bool match1 = RegExpFilter(LineDataLeft);
if (match1)
match2 = RegExpFilter(LineDataRight);
if (match1 && match2)
{
thisob->trivial = 1;
return true;
}
}

if (m_pSubstitutionList)
{
LineDataLeft = m_pSubstitutionList->Subst(LineDataLeft);
Expand Down Expand Up @@ -945,7 +947,7 @@ CDiffWrapper::FreeDiffUtilsScript(struct change * & script)
* @param [in] FileNo File to match.
* return true if any of the expressions matches.
*/
bool CDiffWrapper::RegExpFilter(int StartPos, int EndPos, const file_data *pinf) const
bool CDiffWrapper::RegExpFilter(std::string& lines) const
{
if (m_pFilterList == nullptr)
{
Expand All @@ -954,19 +956,32 @@ bool CDiffWrapper::RegExpFilter(int StartPos, int EndPos, const file_data *pinf)
}

bool linesMatch = true; // set to false when non-matching line is found.
int line = StartPos;

while (line <= EndPos && linesMatch)
{
size_t len = pinf->linbuf[line + 1] - pinf->linbuf[line];
const char *string = pinf->linbuf[line];
size_t stringlen = linelen(string, len);
if (!m_pFilterList->Match(std::string(string, stringlen), m_codepage))
std::string replaced;
replaced.reserve(lines.length());
size_t pos = 0;
while (pos < lines.length())
{
const char* string = lines.c_str() + pos;
while (pos < lines.length() && (lines[pos] != '\r' && lines[pos] != '\n'))
pos++;
size_t stringlen = lines.c_str() + pos - string;
std::string line = std::string(string, stringlen);
if (!m_pFilterList->Match(line, m_codepage))
{
linesMatch = false;
replaced += line;
}
else
{
replaced += FILTERED_LINE;
}
++line;
std::string eol;
while (pos < lines.length() && (lines[pos] == '\r' || lines[pos] == '\n'))
eol += lines[pos++];
replaced += eol;
}
lines = replaced;
return linesMatch;
}

Expand Down
2 changes: 1 addition & 1 deletion Src/DiffWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class CDiffWrapper
struct change * script10, struct change * script12,
const file_data * inf10, const file_data * inf12);
static void FreeDiffUtilsScript(struct change * & script);
bool RegExpFilter(int StartPos, int EndPos, const file_data * pinf) const;
bool RegExpFilter(std::string& lines) const;

private:
DiffutilsOptions m_options;
Expand Down

0 comments on commit fe4fbe0

Please sign in to comment.