private static object MatchOrReplace(Context cx, Scriptable scope, Scriptable thisObj, object[] args, RegExpImpl reImpl, GlobData data, bool forceFlat)
{
NativeRegExp re;
string str = ScriptRuntime.ToString(thisObj);
data.str = str;
Scriptable topScope = ScriptableObject.GetTopLevelScope(scope);
if (args.Length == 0)
{
RECompiled compiled = NativeRegExp.CompileRE(cx, string.Empty, string.Empty, false);
re = new NativeRegExp(topScope, compiled);
}
else
{
if (args[0] is NativeRegExp)
{
re = (NativeRegExp)args[0];
}
else
{
string src = ScriptRuntime.ToString(args[0]);
string opt;
if (data.optarg < args.Length)
{
args[0] = src;
opt = ScriptRuntime.ToString(args[data.optarg]);
}
else
{
opt = null;
}
RECompiled compiled = NativeRegExp.CompileRE(cx, src, opt, forceFlat);
re = new NativeRegExp(topScope, compiled);
}
}
data.global = (re.GetFlags() & NativeRegExp.JSREG_GLOB) != 0;
int[] indexp = new int[] { 0 };
object result = null;
if (data.mode == RegExpProxyConstants.RA_SEARCH)
{
result = re.ExecuteRegExp(cx, scope, reImpl, str, indexp, NativeRegExp.TEST);
if (result != null && result.Equals(true))
{
result = Sharpen.Extensions.ValueOf(reImpl.leftContext.length);
}
else
{
result = Sharpen.Extensions.ValueOf(-1);
}
}
else
{
if (data.global)
{
re.lastIndex = 0;
for (int count = 0; indexp[0] <= str.Length; count++)
{
result = re.ExecuteRegExp(cx, scope, reImpl, str, indexp, NativeRegExp.TEST);
if (result == null || !result.Equals(true))
{
break;
}
if (data.mode == RegExpProxyConstants.RA_MATCH)
{
Match_glob(data, cx, scope, count, reImpl);
}
else
{
if (data.mode != RegExpProxyConstants.RA_REPLACE)
{
Kit.CodeBug();
}
SubString lastMatch = reImpl.lastMatch;
int leftIndex = data.leftIndex;
int leftlen = lastMatch.index - leftIndex;
data.leftIndex = lastMatch.index + lastMatch.length;
Replace_glob(data, cx, scope, reImpl, leftIndex, leftlen);
}
if (reImpl.lastMatch.length == 0)
{
if (indexp[0] == str.Length)
{
break;
}
indexp[0]++;
}
}
}
else
{
result = re.ExecuteRegExp(cx, scope, reImpl, str, indexp, ((data.mode == RegExpProxyConstants.RA_REPLACE) ? NativeRegExp.TEST : NativeRegExp.MATCH));
}
}
return result;
}