This commit is contained in:
Birunthan Mohanathas 2013-11-10 19:41:21 +02:00
parent cd658fe2b1
commit 4e936e5365
2 changed files with 100 additions and 114 deletions

View File

@ -225,9 +225,12 @@ const WCHAR* Measure::CheckSubstitute(const WCHAR* buffer)
{ {
static std::wstring str; static std::wstring str;
if (!m_Substitute.empty()) if (m_Substitute.empty())
{ {
if (!m_RegExpSubstitute) // Plain Substitutions only return buffer;
}
if (!m_RegExpSubstitute)
{ {
str = buffer; str = buffer;
@ -244,28 +247,23 @@ const WCHAR* Measure::CheckSubstitute(const WCHAR* buffer)
} }
} }
} }
else // Contains a RegEx else
{ {
std::string utf8str = StringUtil::NarrowUTF8(buffer); std::string utf8str = StringUtil::NarrowUTF8(buffer);
int* ovector = new int[OVECCOUNT]; int ovector[300];
for (size_t i = 0, isize = m_Substitute.size(); i < isize; i += 2) for (size_t i = 0, isize = m_Substitute.size(); i < isize; i += 2)
{ {
pcre* re;
const char* error; const char* error;
int erroffset; int errorOffset;
int rc;
int flags = PCRE_UTF8;
int offset = 0; int offset = 0;
pcre* re = pcre_compile(
re = pcre_compile( StringUtil::NarrowUTF8(m_Substitute[i]).c_str(),
StringUtil::NarrowUTF8(m_Substitute[i]).c_str(), // the pattern PCRE_UTF8,
flags, // default options &error,
&error, // for error message &errorOffset,
&erroffset, // for error offset nullptr); // Use default character tables.
nullptr); // use default character tables if (!re)
if (re == nullptr)
{ {
MakePlainSubstitute(str, i); MakePlainSubstitute(str, i);
LogNoticeF(this, L"Substitute: %S", error); LogNoticeF(this, L"Substitute: %S", error);
@ -274,73 +272,61 @@ const WCHAR* Measure::CheckSubstitute(const WCHAR* buffer)
{ {
do do
{ {
rc = pcre_exec( const int rc = pcre_exec(
re, // the compiled pattern re,
nullptr, // no extra data - we didn't study the pattern nullptr, // No extra data - we didn't study the pattern
utf8str.c_str(), // the subject string utf8str.c_str(), // The subject string
utf8str.length(), // the length of the subject utf8str.length(), // The length of the subject
offset, // start at offset 0 in the subject offset,
0, // default options 0,
ovector, // output vector for substring information ovector,
OVECCOUNT); // number of elements in the output vector _countof(ovector));
if (rc <= 0) if (rc <= 0)
{ {
break; break;
} }
else
{
std::string result = StringUtil::NarrowUTF8(m_Substitute[i + 1]); std::string result = StringUtil::NarrowUTF8(m_Substitute[i + 1]);
if (rc > 1) if (rc > 1)
{ {
for (int j = rc - 1 ; j >= 0 ; --j) for (int j = rc - 1 ; j >= 0 ; --j)
{ {
size_t new_start = ovector[2 * j]; size_t newStart = ovector[2 * j];
size_t in_length = ovector[2 * j + 1] - ovector[2 * j]; size_t inLength = ovector[2 * j + 1] - ovector[2 * j];
char tmpName[64]; char tmpName[64];
size_t cutLength = _snprintf_s(tmpName, _TRUNCATE, "\\%i", j);;
size_t cut_length = _snprintf_s(tmpName, _TRUNCATE, "\\%i", j);;
size_t start = 0, pos; size_t start = 0, pos;
do do
{ {
pos = result.find(tmpName, start, cut_length); pos = result.find(tmpName, start, cutLength);
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
result.replace(pos, cut_length, utf8str, new_start, in_length); result.replace(pos, cutLength, utf8str, newStart, inLength);
start = pos + in_length; start = pos + inLength;
} }
} }
while (pos != std::string::npos); while (pos != std::string::npos);
} }
} }
size_t start = ovector[0]; const size_t start = ovector[0];
size_t length = ovector[1] - ovector[0]; const size_t length = ovector[1] - ovector[0];
utf8str.replace(start, length, result); utf8str.replace(start, length, result);
offset = start + result.length(); offset = start + result.length();
} }
}
while (true); while (true);
// Release memory used for the compiled pattern
pcre_free(re); pcre_free(re);
} }
} }
delete [] ovector;
str = StringUtil::WidenUTF8(utf8str); str = StringUtil::WidenUTF8(utf8str);
} }
return str.c_str(); return str.c_str();
} }
else
{
return buffer;
}
}
/* /*
** Reads the buffer for "Name":"Value"-pairs separated with comma and ** Reads the buffer for "Name":"Value"-pairs separated with comma and

View File

@ -21,8 +21,8 @@
#include "MeasureCalc.h" #include "MeasureCalc.h"
#include "Rainmeter.h" #include "Rainmeter.h"
#define DEFAULT_LOWER_BOUND 0 const int DEFAULT_LOWER_BOUND = 0;
#define DEFAULT_UPPER_BOUND 100 const int DEFAULT_UPPER_BOUND = 100;
/* /*
** The constructor ** The constructor