Add new regex natives. (#767)
Add new regex natives to get multiple/all matches.
This commit is contained in:
+59
-13
@@ -33,7 +33,6 @@
|
||||
|
||||
#include "pcre.h"
|
||||
#include "CRegEx.h"
|
||||
#include <sh_string.h>
|
||||
#include "extension.h"
|
||||
|
||||
RegEx::RegEx()
|
||||
@@ -43,7 +42,7 @@ RegEx::RegEx()
|
||||
re = NULL;
|
||||
mFree = true;
|
||||
subject = NULL;
|
||||
mSubStrings = 0;
|
||||
mMatchCount = 0;
|
||||
}
|
||||
|
||||
void RegEx::Clear ()
|
||||
@@ -57,7 +56,7 @@ void RegEx::Clear ()
|
||||
if (subject)
|
||||
delete [] subject;
|
||||
subject = NULL;
|
||||
mSubStrings = 0;
|
||||
mMatchCount = 0;
|
||||
}
|
||||
|
||||
RegEx::~RegEx()
|
||||
@@ -93,7 +92,7 @@ int RegEx::Compile(const char *pattern, int iFlags)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RegEx::Match(const char *str)
|
||||
int RegEx::Match(const char *str, unsigned int offset)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
@@ -106,7 +105,9 @@ int RegEx::Match(const char *str)
|
||||
subject = new char[strlen(str)+1];
|
||||
strcpy(subject, str);
|
||||
|
||||
rc = pcre_exec(re, NULL, subject, (int)strlen(subject), 0, 0, ovector, 30);
|
||||
unsigned int len = strlen(subject);
|
||||
|
||||
rc = pcre_exec(re, NULL, subject, len, offset, 0, mMatches[0].mVector, MAX_CAPTURES);
|
||||
|
||||
if (rc < 0)
|
||||
{
|
||||
@@ -119,10 +120,54 @@ int RegEx::Match(const char *str)
|
||||
}
|
||||
}
|
||||
|
||||
mSubStrings = rc;
|
||||
mMatches[0].mSubStringCount = rc;
|
||||
mMatchCount = 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int RegEx::MatchAll(const char *str)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (mFree || re == NULL)
|
||||
return -1;
|
||||
|
||||
this->ClearMatch();
|
||||
|
||||
//save str
|
||||
subject = new char[strlen(str) + 1];
|
||||
strcpy(subject, str);
|
||||
|
||||
unsigned int offset = 0;
|
||||
unsigned int len = strlen(subject);
|
||||
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)
|
||||
{
|
||||
offset = mMatches[matches].mVector[1];
|
||||
mMatches[matches].mSubStringCount = rc;
|
||||
|
||||
matches++;
|
||||
}
|
||||
|
||||
if (rc < PCRE_ERROR_NOMATCH || (rc == PCRE_ERROR_NOMATCH && matches == 0))
|
||||
{
|
||||
if (rc == PCRE_ERROR_NOMATCH)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
mErrorOffset = rc;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
mMatchCount = matches;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void RegEx::ClearMatch()
|
||||
{
|
||||
// Clears match results
|
||||
@@ -131,17 +176,18 @@ void RegEx::ClearMatch()
|
||||
if (subject)
|
||||
delete [] subject;
|
||||
subject = NULL;
|
||||
mSubStrings = 0;
|
||||
mMatchCount = 0;
|
||||
}
|
||||
|
||||
const char *RegEx::GetSubstring(int s, char buffer[], int max)
|
||||
bool RegEx::GetSubstring(int s, char buffer[], int max, int match)
|
||||
{
|
||||
int i = 0;
|
||||
if (s >= mSubStrings || s < 0)
|
||||
return NULL;
|
||||
|
||||
char *substr_a = subject + ovector[2*s];
|
||||
int substr_l = ovector[2*s+1] - ovector[2*s];
|
||||
if (s >= mMatches[match].mSubStringCount || s < 0)
|
||||
return false;
|
||||
|
||||
char *substr_a = subject + mMatches[match].mVector[2 * s];
|
||||
int substr_l = mMatches[match].mVector[2 * s + 1] - mMatches[match].mVector[2 * s];
|
||||
|
||||
for (i = 0; i<substr_l; i++)
|
||||
{
|
||||
@@ -152,6 +198,6 @@ const char *RegEx::GetSubstring(int s, char buffer[], int max)
|
||||
|
||||
buffer[i] = '\0';
|
||||
|
||||
return buffer;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user