regex: add/document missing offset param (#1175)
* Regex Little Changes * Prevented to triple and double call `strlen`. * More informative message on `if (offset >= len)`. * Add missing parametr in navite `MatchRegex`. * Regex Little Changes v2 * Using `strdup` instead `strcpy`. * Replaced NULL to nullptr. * Removed note about MatchOffset. Co-Authored-By: Headline <[email protected]> * Removed padding. Co-Authored-By: Headline <[email protected]> * Removed more padding. Co-Authored-By: Headline <[email protected]> Co-authored-by: Headline <[email protected]>
This commit is contained in:
co-authored by
Headline
Headline
parent
2b6833f65d
commit
ded3867605
+18
-22
@@ -39,10 +39,10 @@ RegEx::RegEx()
|
||||
{
|
||||
mErrorOffset = 0;
|
||||
mErrorCode = 0;
|
||||
mError = NULL;
|
||||
re = NULL;
|
||||
mError = nullptr;
|
||||
re = nullptr;
|
||||
mFree = true;
|
||||
subject = NULL;
|
||||
subject = nullptr;
|
||||
mMatchCount = 0;
|
||||
}
|
||||
|
||||
@@ -50,14 +50,14 @@ void RegEx::Clear ()
|
||||
{
|
||||
mErrorOffset = 0;
|
||||
mErrorCode = 0;
|
||||
mError = NULL;
|
||||
mError = nullptr;
|
||||
if (re)
|
||||
pcre_free(re);
|
||||
re = NULL;
|
||||
re = nullptr;
|
||||
mFree = true;
|
||||
if (subject)
|
||||
delete [] subject;
|
||||
subject = NULL;
|
||||
subject = nullptr;
|
||||
mMatchCount = 0;
|
||||
}
|
||||
|
||||
@@ -82,9 +82,9 @@ int RegEx::Compile(const char *pattern, int iFlags)
|
||||
if (!mFree)
|
||||
Clear();
|
||||
|
||||
re = pcre_compile2(pattern, iFlags, &mErrorCode, &mError, &mErrorOffset, NULL);
|
||||
re = pcre_compile2(pattern, iFlags, &mErrorCode, &mError, &mErrorOffset, nullptr);
|
||||
|
||||
if (re == NULL)
|
||||
if (re == nullptr)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -94,22 +94,19 @@ int RegEx::Compile(const char *pattern, int iFlags)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RegEx::Match(const char *str, unsigned int offset)
|
||||
int RegEx::Match(const char *const str, const size_t offset)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (mFree || re == NULL)
|
||||
if (mFree || re == nullptr)
|
||||
return -1;
|
||||
|
||||
this->ClearMatch();
|
||||
|
||||
//save str
|
||||
subject = new char[strlen(str)+1];
|
||||
strcpy(subject, str);
|
||||
subject = strdup(str);
|
||||
|
||||
unsigned int len = strlen(subject);
|
||||
|
||||
rc = pcre_exec(re, NULL, subject, len, offset, 0, mMatches[0].mVector, MAX_CAPTURES);
|
||||
rc = pcre_exec(re, nullptr, subject, strlen(subject), offset, 0, mMatches[0].mVector, MAX_CAPTURES);
|
||||
|
||||
if (rc < 0)
|
||||
{
|
||||
@@ -132,17 +129,16 @@ int RegEx::MatchAll(const char *str)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (mFree || re == NULL)
|
||||
if (mFree || re == nullptr)
|
||||
return -1;
|
||||
|
||||
this->ClearMatch();
|
||||
|
||||
//save str
|
||||
subject = new char[strlen(str) + 1];
|
||||
strcpy(subject, str);
|
||||
subject = strdup(str);
|
||||
size_t len = strlen(subject);
|
||||
|
||||
unsigned int offset = 0;
|
||||
unsigned int len = strlen(subject);
|
||||
size_t offset = 0;
|
||||
unsigned int matches = 0;
|
||||
|
||||
while (matches < MAX_MATCHES && offset < len && (rc = pcre_exec(re, 0, subject, len, offset, 0, mMatches[matches].mVector, MAX_CAPTURES)) >= 0)
|
||||
@@ -175,10 +171,10 @@ void RegEx::ClearMatch()
|
||||
// Clears match results
|
||||
mErrorOffset = 0;
|
||||
mErrorCode = 0;
|
||||
mError = NULL;
|
||||
mError = nullptr;
|
||||
if (subject)
|
||||
delete [] subject;
|
||||
subject = NULL;
|
||||
subject = nullptr;
|
||||
mMatchCount = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user