Skip to content

Commit

Permalink
Make the 'Match similar lines' option work for 3-way comparisons (#1051)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdottaka authored Nov 21, 2021
1 parent 1024c2c commit e4759fd
Show file tree
Hide file tree
Showing 106 changed files with 1,183 additions and 97 deletions.
23 changes: 22 additions & 1 deletion Src/Common/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ String to_charstr(TCHAR ch)
* This function searches for a string inside another string an if found,
* replaces it with another string. Function can replace several instances
* of the string inside one string.
* @param [in] target A string containing another string to replace.
* @param [in,out] target A string containing another string to replace.
* @param [in] find A string to search and replace with another (@p replace).
* @param [in] replace A string used to replace original (@p find).
*/
Expand All @@ -119,6 +119,27 @@ void replace(String &target, const String &find, const String &replace)
}
}

/**
* @brief Replace the characters that matche characters specified in its arguments
* @param [in,out] str - A string containing another string to replace.
* @param [in] chars - characters to search for
* @param [in] rep - String to replace
*/
void replace_chars(String& str, const TCHAR* chars, const TCHAR *rep)
{
String::size_type pos = 0;
size_t replen = _tcslen(rep);
while ((pos = str.find_first_of(chars, pos)) != std::string::npos)
{
std::string::size_type posend = str.find_first_not_of(chars, pos);
if (posend != String::npos)
str.replace(pos, posend - pos, rep);
else
str.replace(pos, str.length() - pos, rep);
pos += replen;
}
}

/**
* @brief Compare two strings ignoring the character casing.
* @param [in] str1 First string to compare.
Expand Down
1 change: 1 addition & 0 deletions Src/Common/UnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TCHAR from_charstr(const String& str);
String to_charstr(TCHAR ch);

void replace(String &target, const String &find, const String &replace);
void replace_chars(String& str, const TCHAR* chars, const TCHAR* rep);

// Comparing
int compare_nocase(const String &str1, const String &str2);
Expand Down
4 changes: 2 additions & 2 deletions Src/DiffWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ static void ReplaceSpaces(std::string & str, const char *rep)
if (posend != String::npos)
str.replace(pos, posend - pos, rep);
else
str.replace(pos, 1, rep);
str.replace(pos, str.length() - pos, rep);
pos += replen;
}
}
Expand All @@ -325,7 +325,7 @@ static void ReplaceNumbers(std::string& str, const char* rep)
if (posend != String::npos)
str.replace(pos, posend - pos, rep);
else
str.replace(pos, 1, rep);
str.replace(pos, str.length() - pos, rep);
pos += replen;
}
}
Expand Down
9 changes: 7 additions & 2 deletions Src/MergeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,13 @@ int CMergeDoc::Rescan(bool &bBinary, IDENTLEVEL &identical,
m_ptBuf[nBuffer]->prepareForRescan();

// Divide diff blocks to match lines.
if (GetOptionsMgr()->GetBool(OPT_CMP_MATCH_SIMILAR_LINES) && m_nBuffers < 3)
AdjustDiffBlocks();
if (GetOptionsMgr()->GetBool(OPT_CMP_MATCH_SIMILAR_LINES))
{
if (m_nBuffers < 3)
AdjustDiffBlocks();
else
AdjustDiffBlocks3way();
}

// Analyse diff-list (updating real line-numbers)
// this operation does not change the modified flag
Expand Down
11 changes: 8 additions & 3 deletions Src/MergeDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class CMergeDoc : public CDocument, public IMergeDoc
void AddToSubstitutionFilters(CMergeEditView* pView, bool bReversed = false);
std::vector<WordDiff> GetWordDiffArrayInDiffBlock(int nDiff);
std::vector<WordDiff> GetWordDiffArray(int nLineIndex);
std::vector<WordDiff> GetWordDiffArrayInRange(const int begin[3], const int end[3]);
std::vector<WordDiff> GetWordDiffArrayInRange(const int begin[3], const int end[3], int pane1 = -1, int pane2 = -1);
void ClearWordDiffCache(int nDiff = -1);
private:
void Computelinediff(CMergeEditView *pView, CRect rc[], bool bReversed);
Expand Down Expand Up @@ -431,8 +431,13 @@ class CMergeDoc : public CDocument, public IMergeDoc
void PrimeTextBuffers();
void HideLines();
void AdjustDiffBlocks();
void AdjustDiffBlock(DiffMap & diffmap, const DIFFRANGE & diffrange, const std::vector<WordDiff>& worddiffs, int lo0, int hi0, int lo1, int hi1);
int GetMatchCost(int line0, int line1, const std::vector<WordDiff>& worddiffs);
void AdjustDiffBlocks3way();
void AdjustDiffBlock(DiffMap & diffmap, const DIFFRANGE & diffrange,
const std::vector<WordDiff>& worddiffs,
int i0, int i1, int lo0, int hi0, int lo1, int hi1);
int GetMatchCost(const DIFFRANGE& dr, int i0, int i1, int line0, int line1, const std::vector<WordDiff>& worddiffs);
OP_TYPE ComputeOpType3way(const std::vector<std::array<int, 3>>& vlines, size_t index,
const DIFFRANGE& diffrange, const DIFFOPTIONS& diffOptions);
void FlagTrivialLines();
void FlagMovedLines();
String GetFileExt(LPCTSTR sFileName, LPCTSTR sDescription) const;
Expand Down
Loading

0 comments on commit e4759fd

Please sign in to comment.