From 9b5871f4dc091b36a4aa3e33fb11fa390f39b0c5 Mon Sep 17 00:00:00 2001 From: Birunthan Mohanathas Date: Sat, 4 Jan 2014 19:01:21 +0200 Subject: [PATCH] Move EscapeRegExp into StringUtil --- Common/StringUtil.cpp | 13 +++++++++++++ Common/StringUtil.h | 2 ++ Common/StringUtil_Test.cpp | 7 +++++++ Library/ConfigParser.cpp | 18 ++---------------- Library/ConfigParser.h | 2 -- 5 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Common/StringUtil.cpp b/Common/StringUtil.cpp index d3093d3c..c573749c 100644 --- a/Common/StringUtil.cpp +++ b/Common/StringUtil.cpp @@ -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 diff --git a/Common/StringUtil.h b/Common/StringUtil.h index 87c76712..764063d0 100644 --- a/Common/StringUtil.h +++ b/Common/StringUtil.h @@ -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 diff --git a/Common/StringUtil_Test.cpp b/Common/StringUtil_Test.cpp index 5e7d23e9..835b839c 100644 --- a/Common/StringUtil_Test.cpp +++ b/Common/StringUtil_Test.cpp @@ -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 diff --git a/Library/ConfigParser.cpp b/Library/ConfigParser.cpp index 834a375f..961f62d5 100644 --- a/Library/ConfigParser.cpp +++ b/Library/ConfigParser.cpp @@ -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 diff --git a/Library/ConfigParser.h b/Library/ConfigParser.h index 27ecb41b..d8c6d54a 100644 --- a/Library/ConfigParser.h +++ b/Library/ConfigParser.h @@ -110,8 +110,6 @@ private: bool GetSectionVariable(std::wstring& strVariable, std::wstring& strValue); - void EscapeRegExp(std::wstring& str); - static void SetVariable(std::unordered_map& variables, const std::wstring& strVariable, const std::wstring& strValue); static void SetVariable(std::unordered_map& variables, const WCHAR* strVariable, const WCHAR* strValue);