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 <headline@users.noreply.github.com> * Removed padding. Co-Authored-By: Headline <headline@users.noreply.github.com> * Removed more padding. Co-Authored-By: Headline <headline@users.noreply.github.com> Co-authored-by: Headline <michaelwflaherty@me.com>
This commit is contained in:
parent
2b6833f65d
commit
ded3867605
@ -39,10 +39,10 @@ RegEx::RegEx()
|
|||||||
{
|
{
|
||||||
mErrorOffset = 0;
|
mErrorOffset = 0;
|
||||||
mErrorCode = 0;
|
mErrorCode = 0;
|
||||||
mError = NULL;
|
mError = nullptr;
|
||||||
re = NULL;
|
re = nullptr;
|
||||||
mFree = true;
|
mFree = true;
|
||||||
subject = NULL;
|
subject = nullptr;
|
||||||
mMatchCount = 0;
|
mMatchCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,14 +50,14 @@ void RegEx::Clear ()
|
|||||||
{
|
{
|
||||||
mErrorOffset = 0;
|
mErrorOffset = 0;
|
||||||
mErrorCode = 0;
|
mErrorCode = 0;
|
||||||
mError = NULL;
|
mError = nullptr;
|
||||||
if (re)
|
if (re)
|
||||||
pcre_free(re);
|
pcre_free(re);
|
||||||
re = NULL;
|
re = nullptr;
|
||||||
mFree = true;
|
mFree = true;
|
||||||
if (subject)
|
if (subject)
|
||||||
delete [] subject;
|
delete [] subject;
|
||||||
subject = NULL;
|
subject = nullptr;
|
||||||
mMatchCount = 0;
|
mMatchCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,9 +82,9 @@ int RegEx::Compile(const char *pattern, int iFlags)
|
|||||||
if (!mFree)
|
if (!mFree)
|
||||||
Clear();
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -94,22 +94,19 @@ int RegEx::Compile(const char *pattern, int iFlags)
|
|||||||
return 1;
|
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;
|
int rc = 0;
|
||||||
|
|
||||||
if (mFree || re == NULL)
|
if (mFree || re == nullptr)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
this->ClearMatch();
|
this->ClearMatch();
|
||||||
|
|
||||||
//save str
|
//save str
|
||||||
subject = new char[strlen(str)+1];
|
subject = strdup(str);
|
||||||
strcpy(subject, str);
|
|
||||||
|
|
||||||
unsigned int len = strlen(subject);
|
rc = pcre_exec(re, nullptr, subject, strlen(subject), offset, 0, mMatches[0].mVector, MAX_CAPTURES);
|
||||||
|
|
||||||
rc = pcre_exec(re, NULL, subject, len, offset, 0, mMatches[0].mVector, MAX_CAPTURES);
|
|
||||||
|
|
||||||
if (rc < 0)
|
if (rc < 0)
|
||||||
{
|
{
|
||||||
@ -132,17 +129,16 @@ int RegEx::MatchAll(const char *str)
|
|||||||
{
|
{
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if (mFree || re == NULL)
|
if (mFree || re == nullptr)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
this->ClearMatch();
|
this->ClearMatch();
|
||||||
|
|
||||||
//save str
|
//save str
|
||||||
subject = new char[strlen(str) + 1];
|
subject = strdup(str);
|
||||||
strcpy(subject, str);
|
size_t len = strlen(subject);
|
||||||
|
|
||||||
unsigned int offset = 0;
|
size_t offset = 0;
|
||||||
unsigned int len = strlen(subject);
|
|
||||||
unsigned int matches = 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)
|
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
|
// Clears match results
|
||||||
mErrorOffset = 0;
|
mErrorOffset = 0;
|
||||||
mErrorCode = 0;
|
mErrorCode = 0;
|
||||||
mError = NULL;
|
mError = nullptr;
|
||||||
if (subject)
|
if (subject)
|
||||||
delete [] subject;
|
delete [] subject;
|
||||||
subject = NULL;
|
subject = nullptr;
|
||||||
mMatchCount = 0;
|
mMatchCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,7 +51,7 @@ public:
|
|||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
int Compile(const char *pattern, int iFlags);
|
int Compile(const char *pattern, int iFlags);
|
||||||
int Match(const char *str, unsigned int offset);
|
int Match(const char *const str, const size_t offset);
|
||||||
int MatchAll(const char *str);
|
int MatchAll(const char *str);
|
||||||
void ClearMatch();
|
void ClearMatch();
|
||||||
bool GetSubstring(int s, char buffer[], int max, int match);
|
bool GetSubstring(int s, char buffer[], int max, int match);
|
||||||
|
@ -114,35 +114,33 @@ static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
|
|||||||
sec.pOwner = NULL;
|
sec.pOwner = NULL;
|
||||||
sec.pIdentity = myself->GetIdentity();
|
sec.pIdentity = myself->GetIdentity();
|
||||||
|
|
||||||
unsigned int offset = 0;
|
|
||||||
|
|
||||||
if (params[0] >= 4)
|
|
||||||
{
|
|
||||||
offset = (unsigned int)params[4];
|
|
||||||
}
|
|
||||||
|
|
||||||
RegEx *x;
|
RegEx *x;
|
||||||
|
|
||||||
if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
|
if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
|
||||||
{
|
{
|
||||||
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
|
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!x)
|
if (!x)
|
||||||
{
|
{
|
||||||
pCtx->ThrowNativeError("Regex data not found\n");
|
pCtx->ThrowNativeError("Regex data not found\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t offset = 0;
|
||||||
|
if (params[0] >= 4)
|
||||||
|
{
|
||||||
|
offset = static_cast<size_t>(params[4]);
|
||||||
|
}
|
||||||
|
|
||||||
char *str;
|
char *str;
|
||||||
pCtx->LocalToString(params[2], &str);
|
pCtx->LocalToString(params[2], &str);
|
||||||
|
|
||||||
if(offset >= strlen(str))
|
size_t len = strlen(str);
|
||||||
return pCtx->ThrowNativeError("Invalid string index\n");
|
if (offset >= len)
|
||||||
|
{
|
||||||
|
return pCtx->ThrowNativeError("Offset greater or equal than string length\n");
|
||||||
|
}
|
||||||
|
|
||||||
int e = x->Match(str, offset);
|
int e = x->Match(str, offset);
|
||||||
|
|
||||||
if (e == -1)
|
if (e == -1)
|
||||||
{
|
{
|
||||||
/* there was a match error. move on. */
|
/* there was a match error. move on. */
|
||||||
|
@ -199,12 +199,13 @@ native Regex CompileRegex(const char[] pattern, int flags = 0, char[] error="",
|
|||||||
* @param regex Regex Handle from CompileRegex()
|
* @param regex Regex Handle from CompileRegex()
|
||||||
* @param str The string to check.
|
* @param str The string to check.
|
||||||
* @param ret Error code, if applicable.
|
* @param ret Error code, if applicable.
|
||||||
|
* @param offset Offset in the string to start searching from.
|
||||||
* @return Number of captures found or -1 on failure.
|
* @return Number of captures found or -1 on failure.
|
||||||
*
|
*
|
||||||
* @note Use the regex handle passed to this function to extract
|
* @note Use the regex handle passed to this function to extract
|
||||||
* matches with GetRegexSubString().
|
* matches with GetRegexSubString().
|
||||||
*/
|
*/
|
||||||
native int MatchRegex(Handle regex, const char[] str, RegexError &ret = REGEX_ERROR_NONE);
|
native int MatchRegex(Handle regex, const char[] str, RegexError &ret = REGEX_ERROR_NONE, int offset = 0);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a matched substring from a regex handle.
|
* Returns a matched substring from a regex handle.
|
||||||
|
Loading…
Reference in New Issue
Block a user