Пример #1
0
 public virtual object Compile(Context cx, string source, string flags)
 {
     return(BuiltinRegExp.compileRE(source, flags, false));
 }
Пример #2
0
        /// <summary> Analog of C match_or_replace.</summary>
        private static object matchOrReplace(Context cx, IScriptable scope, IScriptable thisObj, object [] args, RegExpImpl reImpl, GlobData data, bool forceFlat)
        {
            BuiltinRegExp re;

            string str = ScriptConvert.ToString(thisObj);

            data.str = str;
            IScriptable topScope = ScriptableObject.GetTopLevelScope(scope);

            if (args.Length == 0)
            {
                object compiled = BuiltinRegExp.compileRE("", "", false);
                re = new BuiltinRegExp(topScope, compiled);
            }
            else if (args [0] is BuiltinRegExp)
            {
                re = (BuiltinRegExp)args [0];
            }
            else
            {
                string src = ScriptConvert.ToString(args [0]);
                string opt;
                if (data.optarg < args.Length)
                {
                    args [0] = src;
                    opt      = ScriptConvert.ToString(args [data.optarg]);
                }
                else
                {
                    opt = null;
                }
                object compiled = BuiltinRegExp.compileRE(src, opt, forceFlat);
                re = new BuiltinRegExp(topScope, compiled);
            }
            data.regexp = re;

            data.global = (re.Flags & BuiltinRegExp.JSREG_GLOB) != 0;
            int [] indexp = new int [] { 0 };
            object result = null;

            if (data.mode == EcmaScript.NET.RegExpActions.Search)
            {
                result = re.executeRegExp(cx, scope, reImpl, str, indexp, BuiltinRegExp.TEST);
                if (result != null && result.Equals(true))
                {
                    result = (int)reImpl.leftContext.length;
                }
                else
                {
                    result = -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, BuiltinRegExp.TEST);
                    if (result == null || !result.Equals(true))
                    {
                        break;
                    }
                    if (data.mode == EcmaScript.NET.RegExpActions.Match)
                    {
                        match_glob(data, cx, scope, count, reImpl);
                    }
                    else
                    {
                        if (data.mode != EcmaScript.NET.RegExpActions.Replace)
                        {
                            Context.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 == EcmaScript.NET.RegExpActions.Replace) ? BuiltinRegExp.TEST : BuiltinRegExp.MATCH));
            }

            return(result);
        }