Add new regex natives. (#767)

Add new regex natives to get multiple/all matches.
This commit is contained in:
Ruben Gonzalez
2018-02-15 15:31:24 -05:00
committed by GitHub
parent f9faf9e48c
commit 5ac3390656
4 changed files with 277 additions and 37 deletions
+154 -9
View File
@@ -112,6 +112,13 @@ static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();
unsigned int offset = 0;
if (params[0] >= 4)
{
offset = (unsigned int)params[4];
}
RegEx *x;
if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
@@ -129,7 +136,10 @@ static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
char *str;
pCtx->LocalToString(params[2], &str);
int e = x->Match(str);
if(offset >= strlen(str))
return pCtx->ThrowNativeError("Invalid string index\n");
int e = x->Match(str, offset);
if (e == -1)
{
@@ -153,7 +163,60 @@ static cell_t MatchRegex(IPluginContext *pCtx, const cell_t *params)
}
else
{
return x->mSubStrings;
return x->mMatches[0].mSubStringCount;
}
}
static cell_t MatchRegexAll(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError err;
HandleSecurity sec;
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();
RegEx *x;
if ((err = g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
{
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
}
if (!x)
{
pCtx->ThrowNativeError("Regex data not found\n");
return 0;
}
char *str;
pCtx->LocalToString(params[2], &str);
int e = x->MatchAll(str);
if (e == -1)
{
/* there was a match error. move on. */
cell_t *res;
pCtx->LocalToPhysAddr(params[3], &res);
*res = x->mErrorOffset;
/* only clear the match results, since the regex object
may still be referenced later */
x->ClearMatch();
return -1;
}
else if (e == 0)
{
/* only clear the match results, since the regex object
may still be referenced later */
x->ClearMatch();
return 0;
}
else
{
return x->mMatchCount;
}
}
@@ -165,6 +228,8 @@ static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params)
sec.pOwner=NULL;
sec.pIdentity=myself->GetIdentity();
int match = 0;
RegEx *x;
if ((err=g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
@@ -178,17 +243,93 @@ static cell_t GetRegexSubString(IPluginContext *pCtx, const cell_t *params)
return 0;
}
static char buffer[4096];
const char *ret=x->GetSubstring(params[2], buffer, sizeof(buffer));
if(!ret)
if (params[0] >= 5)
{
return 0;
match = params[5];
}
pCtx->StringToLocalUTF8(params[3], params[4], ret, NULL);
if(match >= x->mMatchCount || match < 0)
return pCtx->ThrowNativeError("Invalid match index passed.\n");
return 1;
char *buffer;
pCtx->LocalToString(params[3], &buffer);
return x->GetSubstring(params[2], buffer, params[4], match);
}
static cell_t GetRegexMatchCount(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError err;
HandleSecurity sec;
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();
RegEx *x;
if ((err = g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
{
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
}
if (!x)
{
return pCtx->ThrowNativeError("Regex data not found\n");
}
return x->mMatchCount;
}
static cell_t GetRegexCaptureCount(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError err;
HandleSecurity sec;
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();
RegEx *x;
if ((err = g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
{
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
}
if (!x)
{
return pCtx->ThrowNativeError("Regex data not found\n");
}
if (params[2] >= x->mMatchCount || params[2] < 0)
return pCtx->ThrowNativeError("Invalid match index passed.\n");
return x->mMatches[params[2]].mSubStringCount;
}
static cell_t GetRegexOffset(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError err;
HandleSecurity sec;
sec.pOwner = NULL;
sec.pIdentity = myself->GetIdentity();
RegEx *x;
if ((err = g_pHandleSys->ReadHandle(hndl, g_RegexHandle, &sec, (void **)&x)) != HandleError_None)
{
return pCtx->ThrowNativeError("Invalid regex handle %x (error %d)", hndl, err);
}
if (!x)
{
return pCtx->ThrowNativeError("Regex data not found\n");
}
if (params[2] >= x->mMatchCount || params[2] < 0)
return pCtx->ThrowNativeError("Invalid match index passed.\n");
return x->mMatches[params[2]].mVector[1];
}
void RegexHandler::OnHandleDestroy(HandleType_t type, void *object)
@@ -209,5 +350,9 @@ const sp_nativeinfo_t regex_natives[] =
{"Regex.GetSubString", GetRegexSubString},
{"Regex.Match", MatchRegex},
{"Regex.Regex", CompileRegex},
{"Regex.MatchAll", MatchRegexAll},
{"Regex.MatchCount", GetRegexMatchCount},
{"Regex.CaptureCount", GetRegexCaptureCount},
{"Regex.MatchOffset", GetRegexOffset},
{NULL, NULL},
};