Move EscapeRegExp into StringUtil

This commit is contained in:
Birunthan Mohanathas 2014-01-04 19:01:21 +02:00
parent ce847ac12e
commit 9b5871f4dc
5 changed files with 24 additions and 18 deletions

View File

@ -62,4 +62,17 @@ std::wstring Widen(const char* str, int strLen, int cp)
return wideStr;
}
/*
** Escapes reserved PCRE regex metacharacters.
*/
void EscapeRegExp(std::wstring& str)
{
size_t start = 0;
while ((start = str.find_first_of(L"\\^$|()[{.+*?", start)) != std::wstring::npos)
{
str.insert(start, L"\\");
start += 2;
}
}
} // namespace StringUtil

View File

@ -36,6 +36,8 @@ inline std::wstring Widen(const std::string& str, int cp = CP_ACP) { return Wide
inline std::wstring WidenUTF8(const char* str, int strLen = -1) { return Widen(str, strLen, CP_UTF8); }
inline std::wstring WidenUTF8(const std::string& str) { return Widen(str.c_str(), (int)str.length(), CP_UTF8); }
void EscapeRegExp(std::wstring& str);
} // namespace StringUtil
#endif

View File

@ -39,6 +39,13 @@ public:
Assert::AreEqual("\xd0\xa2\xc4\x94st", NarrowUTF8(L"\u0422\u0114st").c_str());
Assert::AreEqual("\xd0\xa2", NarrowUTF8(L"\u0422\u0114st", 1).c_str());
}
TEST_METHOD(TestEscapeRegExp)
{
std::wstring str = L"\\^$|(test)[{. ing+*?";
EscapeRegExp(str);
Assert::AreEqual(L"\\\\\\^\\$\\|\\(test\\)\\[\\{\\. ing\\+\\*\\?", str.c_str());
}
};
} // namespace StringUtil

View File

@ -296,7 +296,7 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s
if (valueType == ValueType::EscapeRegExp)
{
strValue = measure->GetStringValue();
EscapeRegExp(strValue);
StringUtil::EscapeRegExp(strValue);
return true;
}
@ -310,7 +310,7 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s
if (*selectorSz == L'%') // Percentual
{
if (valueType == ValueType::Max || valueType == ValueType::Min)
if (valueType == ValueType::Max || valueType == ValueType::Min)
{
// '%' cannot be used with Max/Min values.
return false;
@ -379,20 +379,6 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s
return false;
}
/*
** Escapes reserved PCRE regex metacharacters.
**
*/
void ConfigParser::EscapeRegExp(std::wstring& str)
{
size_t start = 0;
while ((start = str.find_first_of(L"\\^$|()[{.+*?", start)) != std::wstring::npos)
{
str.insert(start, L"\\");
start += 2;
}
}
void ConfigParser::ResetMonitorVariables(MeterWindow* meterWindow)
{
// Set the SCREENAREA/WORKAREA variables

View File

@ -110,8 +110,6 @@ private:
bool GetSectionVariable(std::wstring& strVariable, std::wstring& strValue);
void EscapeRegExp(std::wstring& str);
static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const std::wstring& strVariable, const std::wstring& strValue);
static void SetVariable(std::unordered_map<std::wstring, std::wstring>& variables, const WCHAR* strVariable, const WCHAR* strValue);