Add wxRegEx::QuoteMeta() helper

Quote all characters special for wxRegEx in the given string: this can
be useful when searching for a literal string using wxRegEx.

Closes https://github.com/wxWidgets/wxWidgets/pull/1489
This commit is contained in:
Tomay
2019-08-24 22:26:50 +01:00
committed by Vadim Zeitlin
parent 363cdc5fdc
commit 050ca4ce3a
4 changed files with 62 additions and 0 deletions

View File

@@ -690,4 +690,29 @@ int wxRegEx::Replace(wxString *pattern,
return m_impl->Replace(pattern, replacement, maxMatches);
}
wxString wxRegEx::QuoteMeta(const wxString& str)
{
static const wxString s_strMetaChars = wxS("\\^$.|?*+()[]{}");
wxString strEscaped;
// This is the maximal possible length of the resulting string, if every
// character were escaped.
strEscaped.reserve(str.length() * 2);
for ( wxString::const_iterator it = str.begin(); it != str.end(); ++it )
{
if ( s_strMetaChars.find(*it) != wxString::npos )
{
strEscaped += wxS('\\');
}
strEscaped += *it;
}
strEscaped.shrink_to_fit();
return strEscaped;
}
#endif // wxUSE_REGEX