Skip to content

Commit

Permalink
#3497 - better fix than 4992
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Sep 19, 2017
1 parent 4f75943 commit c96ca00
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 17 deletions.
12 changes: 6 additions & 6 deletions far/RegExp.cpp
Expand Up @@ -479,6 +479,7 @@ RegExp::RegExp():
minlength(),
errorcode(errNotCompiled),
errorpos(),
srcstart(),
ignorecase(),
bracketscount(),
maxbackref(),
Expand Down Expand Up @@ -642,18 +643,14 @@ int RegExp::CalcLength(const wchar_t* src,int srclength)

if (count)
{
errorpos=brackets[0];
errorcode=errBrackets;
return 0;
return SetError(errBrackets, brackets[0]);
}

return length;
}

int RegExp::Compile(const wchar_t* src,int options)
{
int srcstart=0,srclength/*=0*/,relength;

if (options&OP_CPPMODE)
{
slashChar='\\';
Expand All @@ -669,6 +666,8 @@ int RegExp::Compile(const wchar_t* src,int options)

code.clear();

int srclength;

if (options&OP_PERLSTYLE)
{
if (src[0]!=slashChar)return SetError(errSyntax,0);
Expand Down Expand Up @@ -711,11 +710,12 @@ int RegExp::Compile(const wchar_t* src,int options)
}
else
{
srcstart = 0;
srclength=(int)wcslen(src);
}

ignorecase=options&OP_IGNORECASE?1:0;
relength=CalcLength(src+srcstart,srclength);
int relength=CalcLength(src+srcstart,srclength);

if (!relength)
{
Expand Down
3 changes: 2 additions & 1 deletion far/RegExp.hpp
Expand Up @@ -128,6 +128,7 @@ class RegExp:noncopyable
// error info
mutable int errorcode;
mutable int errorpos;
int srcstart;

// options
int ignorecase;
Expand Down Expand Up @@ -226,7 +227,7 @@ class RegExp:noncopyable
\return position of the last error in the regexp source.
\sa LastError
*/
int ErrorPosition() const {return errorpos;}
int ErrorPosition() const { return srcstart + errorpos; }
/*! Get number of brackets in expression
\return number of brackets, excluding brackets of type (:expr)
and named brackets.
Expand Down
4 changes: 4 additions & 0 deletions far/changelog
@@ -1,3 +1,7 @@
drkns 19.09.2017 23:14:44 +0100 - build 5033

1. 0003497: регэкспы перестали работать (более правильное исправление, чем 4492).

drkns 16.09.2017 23:23:44 +0100 - build 5032

1. Продолжение 5030.3.
Expand Down
14 changes: 11 additions & 3 deletions far/editor.cpp
Expand Up @@ -3390,7 +3390,7 @@ bool Editor::Search(bool Next)
if (strSearchStr.empty())
return true;

const auto QuotedStr = quote_unconditional(strSearchStr);
string QuotedStr;

const auto FindAllList = VMenu2::create({}, nullptr, 0);
UINT AllRefLines = 0;
Expand Down Expand Up @@ -3432,14 +3432,22 @@ bool Editor::Search(bool Next)

if (Regexp)
{
const auto strSlash = InsertRegexpQuote(strSearchStr);

QuotedStr = strSlash;

// Q: что важнее: опция диалога или опция RegExp`а?
if (!re.Compile(strSearchStr.data(), OP_OPTIMIZE | (Case? 0 : OP_IGNORECASE)))
if (!re.Compile(strSlash.data(), OP_PERLSTYLE | OP_OPTIMIZE | (Case? 0 : OP_IGNORECASE)))
{
ReCompileErrorMessage(re, strSearchStr);
ReCompileErrorMessage(re, strSlash);
return false; //BUGBUG
}
m.resize(re.GetBracketsCount() * 2);
}
else
{
QuotedStr = quote_unconditional(strSearchStr);
}

const auto strSearchStrUpper = Case? strSearchStr : upper(strSearchStr);
const auto strSearchStrLower = Case? strSearchStr : lower(strSearchStr);
Expand Down
6 changes: 4 additions & 2 deletions far/help.cpp
Expand Up @@ -1925,10 +1925,12 @@ void Help::Search(const os::fs::file& HelpFile,uintptr_t nCodePage)

if (LastSearchRegexp)
{
const auto strSlash = InsertRegexpQuote(strLastSearchStr);

// Q: что важнее: опция диалога или опция RegExp`а?
if (!re.Compile(strLastSearchStr.data(), OP_OPTIMIZE | (LastSearchCase? 0 : OP_IGNORECASE)))
if (!re.Compile(strSlash.data(), OP_PERLSTYLE | OP_OPTIMIZE | (LastSearchCase? 0 : OP_IGNORECASE)))
{
ReCompileErrorMessage(re, strLastSearchStr);
ReCompileErrorMessage(re, strSlash);
return; //BUGBUG
}

Expand Down
12 changes: 12 additions & 0 deletions far/strmix.cpp
Expand Up @@ -107,6 +107,18 @@ wchar_t* QuoteSpace(wchar_t *Str)
return Str;
}

string InsertRegexpQuote(string strStr)
{
//выражение вида /regexp/i не дополняем слешами
if (!strStr.empty() && strStr[0] != L'/')
{
strStr.insert(0, 1, L'/');
strStr += L'/';
}

return strStr;
}

string &QuoteSpace(string &strStr)
{
if (strStr.find_first_of(Global->Opt->strQuotedSymbols) != string::npos)
Expand Down
1 change: 1 addition & 0 deletions far/strmix.hpp
Expand Up @@ -51,6 +51,7 @@ wchar_t* QuoteSpaceOnly(wchar_t *Str);

string &QuoteSpace(string &strStr);

string InsertRegexpQuote(string strStr);
void UnquoteExternal(string &strStr);
string& RemoveLeadingSpaces(string &strStr);
string& RemoveTrailingSpaces(string &strStr);
Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
@@ -1 +1 @@
m4_define(BUILD,5032)m4_dnl
m4_define(BUILD,5033)m4_dnl
12 changes: 8 additions & 4 deletions far/viewer.cpp
Expand Up @@ -3429,15 +3429,18 @@ void Viewer::Search(int Next,int FirstChar)
sd.ch_size = getCharSize();
sd.search_text = strSearchStr.data();

inplace::quote_unconditional(strMsgStr);

if (SearchRegexp)
{
WholeWords = false;
searcher = (ReverseSearch ? &Viewer::search_regex_backward : &Viewer::search_regex_forward);
if (!sd.InitRegEx(strSearchStr, OP_OPTIMIZE | (Case ? 0 : OP_IGNORECASE)))

const auto strSlash = InsertRegexpQuote(strSearchStr);

strMsgStr = strSlash;

if (!sd.InitRegEx(strSlash, OP_PERLSTYLE | OP_OPTIMIZE | (Case ? 0 : OP_IGNORECASE)))
{
ReCompileErrorMessage(*sd.pRex, strSearchStr);
ReCompileErrorMessage(*sd.pRex, strSlash);
return; // wrong regular expression...
}
sd.RexMatchCount = sd.pRex->GetBracketsCount();
Expand All @@ -3446,6 +3449,7 @@ void Viewer::Search(int Next,int FirstChar)
else
{
searcher = (ReverseSearch ? &Viewer::search_text_backward : &Viewer::search_text_forward);
inplace::quote_unconditional(strMsgStr);
}
}

Expand Down

0 comments on commit c96ca00

Please sign in to comment.