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; 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 } // 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 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); } 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 } // namespace StringUtil
#endif #endif

View File

@ -39,6 +39,13 @@ public:
Assert::AreEqual("\xd0\xa2\xc4\x94st", NarrowUTF8(L"\u0422\u0114st").c_str()); Assert::AreEqual("\xd0\xa2\xc4\x94st", NarrowUTF8(L"\u0422\u0114st").c_str());
Assert::AreEqual("\xd0\xa2", NarrowUTF8(L"\u0422\u0114st", 1).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 } // namespace StringUtil

View File

@ -296,7 +296,7 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s
if (valueType == ValueType::EscapeRegExp) if (valueType == ValueType::EscapeRegExp)
{ {
strValue = measure->GetStringValue(); strValue = measure->GetStringValue();
EscapeRegExp(strValue); StringUtil::EscapeRegExp(strValue);
return true; return true;
} }
@ -379,20 +379,6 @@ bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& s
return false; 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) void ConfigParser::ResetMonitorVariables(MeterWindow* meterWindow)
{ {
// Set the SCREENAREA/WORKAREA variables // Set the SCREENAREA/WORKAREA variables

View File

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