@@ -3835,21 +3835,6 @@ bool CSearchDlg::MatchPath(LPCTSTR pathBuf) const
38353835 return bPattern;
38363836}
38373837
3838- static long columnFromPosition (const std::wstring& textContent, long pos)
3839- {
3840- long i = pos;
3841- while (i > 0 && textContent[i] != L' \n ' && textContent[i] != L' \r ' )
3842- {
3843- --i;
3844- }
3845- if (pos == i)
3846- {
3847- // first/empty line starting
3848- return 1 ;
3849- }
3850- return pos - i;
3851- }
3852-
38533838void CSearchDlg::SearchFile (CSearchInfo sInfo , const std::wstring& searchRoot, bool bSearchAlways, bool bIncludeBinary, bool bUseRegex, bool bCaseSensitive, bool bDotMatchesNewline, const std::wstring& searchString, const std::wstring& searchStringUtf16Le, std::atomic_bool& bCancelled)
38543839{
38553840 int nFound = 0 ;
@@ -3929,11 +3914,14 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
39293914 nFound++;
39303915 if (m_bNotSearch)
39313916 break ;
3932- long posMatch = static_cast <long >(whatC[0 ].first - textFile.GetFileString ().begin ());
3933- long lineStart = textFile.LineFromPosition (posMatch);
3934- long lineEnd = textFile.LineFromPosition (static_cast <long >(whatC[0 ].second - textFile.GetFileString ().begin ()));
3935- long colMatch = columnFromPosition (textFile.GetFileString (), posMatch);
3936- long lenMatch = static_cast <long >(whatC[0 ].length ());
3917+ long posMatchHead = static_cast <long >(whatC[0 ].first - textFile.GetFileString ().begin ());
3918+ long posMatchTail = static_cast <long >(whatC[0 ].second - textFile.GetFileString ().begin ());
3919+ if (whatC[0 ].first < whatC[0 ].second ) // m[0].second is not part of the match
3920+ --posMatchTail;
3921+ long lineStart = textFile.LineFromPosition (posMatchHead);
3922+ long lineEnd = textFile.LineFromPosition (posMatchTail);
3923+ long colMatch = textFile.ColumnFromPosition (posMatchHead, lineStart);
3924+ long lenMatch = static_cast <long >(whatC[0 ].length ());
39373925 for (long l = lineStart; l <= lineEnd; ++l)
39383926 {
39393927 auto sLine = textFile.GetLineString (l);
@@ -3964,14 +3952,11 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
39643952 ++sInfo .matchCount ;
39653953 }
39663954 // update search position:
3967- if ( start == whatC[0 ].second )
3968- {
3969- if (start == end)
3970- break ;
3955+ start = whatC[0 ].second ;
3956+ if (start == end)
3957+ break ;
3958+ if (start == whatC[ 0 ]. first ) // ^$
39713959 ++start;
3972- }
3973- else
3974- start = whatC[0 ].second ;
39753960 // update flags:
39763961 flags |= boost::match_prev_avail;
39773962 flags |= boost::match_not_bob;
@@ -3989,11 +3974,14 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
39893974 nFound++;
39903975 if (m_bNotSearch)
39913976 break ;
3992- long posMatch = static_cast <long >(whatC[0 ].first - textFile.GetFileString ().begin ());
3993- long lineStart = textFile.LineFromPosition (posMatch);
3994- long lineEnd = textFile.LineFromPosition (static_cast <long >(whatC[0 ].second - textFile.GetFileString ().begin ()));
3995- long colMatch = columnFromPosition (textFile.GetFileString (), posMatch);
3996- long lenMatch = static_cast <long >(whatC[0 ].length ());
3977+ long posMatchHead = static_cast <long >(whatC[0 ].first - textFile.GetFileString ().begin ());
3978+ long posMatchTail = static_cast <long >(whatC[0 ].second - textFile.GetFileString ().begin ());
3979+ if (whatC[0 ].first < whatC[0 ].second ) // m[0].second is not part of the match
3980+ --posMatchTail;
3981+ long lineStart = textFile.LineFromPosition (posMatchHead);
3982+ long lineEnd = textFile.LineFromPosition (posMatchTail);
3983+ long colMatch = textFile.ColumnFromPosition (posMatchHead, lineStart);
3984+ long lenMatch = static_cast <long >(whatC[0 ].length ());
39973985 if (m_bCaptureSearch)
39983986 {
39993987 auto out = whatC.format (m_replaceString, flags);
@@ -4026,14 +4014,11 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
40264014 ++sInfo .matchCount ;
40274015 }
40284016 // update search position:
4029- if ( start == whatC[0 ].second )
4030- {
4031- if (start == end)
4032- break ;
4017+ start = whatC[0 ].second ;
4018+ if (start == end)
4019+ break ;
4020+ if (start == whatC[ 0 ]. first ) // ^$
40334021 ++start;
4034- }
4035- else
4036- start = whatC[0 ].second ;
40374022 // update flags:
40384023 flags |= boost::match_prev_avail;
40394024 flags |= boost::match_not_bob;
@@ -4192,7 +4177,7 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
41924177 boost::spirit::classic::file_iterator<> fBeg = start;
41934178 boost::spirit::classic::file_iterator<> end = start.make_end ();
41944179 boost::match_results<boost::spirit::classic::file_iterator<>> whatC;
4195- while (boost::regex_search (start, end, whatC, expression, flags) && !bCancelled )
4180+ while (boost::regex_search (start, end, whatC, expression, flags))
41964181 {
41974182 nFound++;
41984183 if (m_bNotSearch)
@@ -4202,6 +4187,10 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
42024187 ++sInfo .matchCount ;
42034188 // update search position:
42044189 start = whatC[0 ].second ;
4190+ if (start == end)
4191+ break ;
4192+ if (start == whatC[0 ].first ) // ^$
4193+ ++start;
42054194 // update flags:
42064195 flags |= boost::match_prev_avail;
42074196 flags |= boost::match_not_bob;
@@ -4227,6 +4216,10 @@ void CSearchDlg::SearchFile(CSearchInfo sInfo, const std::wstring& searchRoot, b
42274216 ++sInfo .matchCount ;
42284217 // update search position:
42294218 start = whatC[0 ].second ;
4219+ if (start == end)
4220+ break ;
4221+ if (start == whatC[0 ].first ) // ^$
4222+ ++start;
42304223 // update flags:
42314224 flags |= boost::match_prev_avail;
42324225 flags |= boost::match_not_bob;
0 commit comments