Skip to content

Commit

Permalink
set the replacement placeholder values for ${filepath}, ${filename} a…
Browse files Browse the repository at this point in the history
…nd ${fileext}
  • Loading branch information
stefankueng committed Apr 6, 2024
1 parent cf2a2b2 commit 84e3117
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/SearchDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3971,6 +3971,19 @@ int CSearchDlg::SearchOnTextFile(CSearchInfo& sInfo, const std::wstring& searchR
if (m_bReplace) // synchronize Replace and Search for cancellation and reducing repetitive work on huge files
{
m_backupAndTempFiles.insert(filePathTemp);
replaceFmt.SetReplacePair(L"${filepath}", sInfo.filePath);
std::wstring fileNameFullW = sInfo.filePath.substr(sInfo.filePath.find_last_of('\\') + 1);
auto dotPosW = fileNameFullW.find_last_of('.');
if (dotPosW != std::string::npos)
{
std::wstring filename = fileNameFullW.substr(0, dotPosW);
replaceFmt.SetReplacePair(L"${filename}", filename);
if (fileNameFullW.size() > dotPosW)
{
std::wstring fileExt = fileNameFullW.substr(dotPosW + 1);
replaceFmt.SetReplacePair(L"${fileext}", fileExt);
}
}
}
do
{
Expand Down Expand Up @@ -4219,6 +4232,40 @@ int CSearchDlg::SearchByFilePath(CSearchInfo& sInfo, const std::wstring& searchR
return -1;
}
outFileBufA.sputn(inData, skipSize);

if constexpr (sizeof(CharT) > 1)
{
replaceFmt.SetReplacePair(L"${filepath}", sInfo.filePath);
std::wstring fileNameFullW = sInfo.filePath.substr(sInfo.filePath.find_last_of('\\') + 1);
auto dotPosW = fileNameFullW.find_last_of('.');
if (dotPosW != std::string::npos)
{
std::wstring filename = fileNameFullW.substr(0, dotPosW);
replaceFmt.SetReplacePair(L"${filename}", filename);
if (fileNameFullW.size() > dotPosW)
{
std::wstring fileExt = fileNameFullW.substr(dotPosW + 1);
replaceFmt.SetReplacePair(L"${fileext}", fileExt);
}
}
}
else
{
std::basic_string<CharT> filePathA = ConvertToString<CharT>(sInfo.filePath, sInfo.encoding);
replaceFmt.SetReplacePair("${filepath}", filePathA);
std::string fileNameFullA = filePathA.substr(filePathA.find_last_of('\\') + 1);
auto dotPosA = fileNameFullA.find_last_of('.');
if (dotPosA != std::string::npos)
{
std::string filename = fileNameFullA.substr(0, dotPosA);
replaceFmt.SetReplacePair("${filename}", filename);
if (fileNameFullA.size() > dotPosA)
{
std::string fileExt = fileNameFullA.substr(dotPosA + 1);
replaceFmt.SetReplacePair("${fileext}", fileExt);
}
}
}
}

do
Expand Down

1 comment on commit 84e3117

@lifenjoiner
Copy link
Collaborator

@lifenjoiner lifenjoiner commented on 84e3117 Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

grepWin/src/SearchDlg.cpp

Lines 4412 to 4419 in cf2a2b2

if (m_bUseRegex)
{
replaceGrepWinFilePathVariables(searchExpression, sInfo.filePath);
if (m_bReplace)
{
replaceGrepWinFilePathVariables(replaceExpression, sInfo.filePath);
}
}
is there already.

What about reusing it like this?

diff --git a/src/SearchDlg.cpp b/src/SearchDlg.cpp
index e60989a..8c16a12 100644
--- a/src/SearchDlg.cpp
+++ b/src/SearchDlg.cpp
@@ -3993,7 +3993,7 @@ int CSearchDlg::SearchOnTextFile(CSearchInfo& sInfo, const std::wstring& searchR
             long lenMatch  = static_cast<long>(whatC[0].length());
             if (m_bCaptureSearch)
             {
-                auto out = whatC.format(m_replaceString, mFlags);
+                auto out = whatC.format(replaceExpression, mFlags);
                 sInfo.matchLines.push_back(out);
                 sInfo.matchLinesNumbers.push_back(lineStart);
                 sInfo.matchColumnsNumbers.push_back(colMatch);
@@ -4412,7 +4412,7 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot)
     if (m_bUseRegex)
     {
         replaceGrepWinFilePathVariables(searchExpression, sInfo.filePath);
-        if (m_bReplace)
+        if (m_bReplace || m_bCaptureSearch)
         {
             replaceGrepWinFilePathVariables(replaceExpression, sInfo.filePath);
         }

BTW:
Preprocessing in one time reduces the work of multiple occurrences.

Please sign in to comment.