Пример #1
0
        /*
         * Analog of match_glob() in jsstr.c
         */
        private static void match_glob(GlobData mdata, Context cx, IScriptable scope, int count, RegExpImpl reImpl)
        {
            if (mdata.arrayobj == null)
            {
                IScriptable s = ScriptableObject.GetTopLevelScope(scope);
                mdata.arrayobj = ScriptRuntime.NewObject(cx, s, "Array", null);
            }
            SubString matchsub = reImpl.lastMatch;
            string    matchstr = matchsub.ToString();

            mdata.arrayobj.Put(count, mdata.arrayobj, matchstr);
        }
Пример #2
0
 /// <summary> Analog of REGEXP_PAREN_SUBSTRING in C jsregexp.h.
 /// Assumes zero-based; i.e., for $3, i==2
 /// </summary>
 internal virtual SubString getParenSubString(int i)
 {
     if (parens != null && i < parens.Length)
     {
         SubString parsub = parens [i];
         if (parsub != null)
         {
             return(parsub);
         }
     }
     return(SubString.EmptySubString);
 }
Пример #3
0
        /// <summary> Analog of do_replace in jsstr.c</summary>
        private static void do_replace(GlobData rdata, Context cx, RegExpImpl regExpImpl)
        {
            System.Text.StringBuilder charBuf = rdata.charBuf;
            int    cp = 0;
            string da = rdata.repstr;
            int    dp = rdata.dollar;

            if (dp != -1)
            {
                int [] skip = new int [1];
                do
                {
                    int len = dp - cp;
                    charBuf.Append(da.Substring(cp, (dp) - (cp)));
                    cp = dp;
                    SubString sub = interpretDollar(cx, regExpImpl, da, dp, skip);
                    if (sub != null)
                    {
                        len = sub.length;
                        if (len > 0)
                        {
                            charBuf.Append(sub.charArray, sub.index, len);
                        }
                        cp += skip [0];
                        dp += skip [0];
                    }
                    else
                    {
                        ++dp;
                    }
                    dp = da.IndexOf((char)'$', dp);
                }while (dp >= 0);
            }
            int daL = da.Length;

            if (daL > cp)
            {
                charBuf.Append(da.Substring(cp, (daL) - (cp)));
            }
        }
Пример #4
0
        /*
        * indexp is assumed to be an array of length 1
        */
        internal virtual object executeRegExp(Context cx, IScriptable scopeObj, RegExpImpl res, string str, int [] indexp, int matchType)
        {
            REGlobalData gData = new REGlobalData ();

            int start = indexp [0];
            char [] charArray = str.ToCharArray ();
            int end = charArray.Length;
            if (start > end)
                start = end;
            //
            // Call the recursive matcher to do the real work.
            //
            bool matches = matchRegExp (gData, re, charArray, start, end, res.multiline);
            if (!matches) {
                if (matchType != PREFIX)
                    return null;
                return Undefined.Value;
            }
            int index = gData.cp;
            int i = index;
            indexp [0] = i;
            int matchlen = i - (start + gData.skipped);
            int ep = index;
            index -= matchlen;
            object result;
            IScriptable obj;

            if (matchType == TEST) {
                /*
                * Testing for a match and updating cx.regExpImpl: don't allocate
                * an array object, do return true.
                */
                result = true;
                obj = null;
            }
            else {
                /*
                * The array returned on match has element 0 bound to the matched
                * string, elements 1 through re.parenCount bound to the paren
                * matches, an index property telling the length of the left context,
                * and an input property referring to the input string.
                */
                IScriptable scope = GetTopLevelScope (scopeObj);
                result = ScriptRuntime.NewObject (cx, scope, "Array", null);
                obj = (IScriptable)result;

                string matchstr = new string (charArray, index, matchlen);
                obj.Put (0, obj, matchstr);
            }

            if (re.parenCount == 0) {
                res.parens = null;
                res.lastParen = SubString.EmptySubString;
            }
            else {
                SubString parsub = null;
                int num;
                res.parens = new SubString [re.parenCount];
                for (num = 0; num < re.parenCount; num++) {
                    int cap_index = gData.parens_index (num);
                    string parstr;
                    if (cap_index != -1) {
                        int cap_length = gData.parens_length (num);
                        parsub = new SubString (charArray, cap_index, cap_length);
                        res.parens [num] = parsub;
                        if (matchType == TEST)
                            continue;
                        parstr = parsub.ToString ();
                        obj.Put (num + 1, obj, parstr);
                    }
                    else {
                        if (matchType != TEST)
                            obj.Put (num + 1, obj, Undefined.Value);
                    }
                }
                res.lastParen = parsub;
            }

            if (!(matchType == TEST)) {
                /*
                * Define the index and input properties last for better for/in loop
                * order (so they come after the elements).
                */
                obj.Put ("index", obj, (object)(start + gData.skipped));
                obj.Put ("input", obj, str);
            }

            if (res.lastMatch == null) {
                res.lastMatch = new SubString ();
                res.leftContext = new SubString ();
                res.rightContext = new SubString ();
            }
            res.lastMatch.charArray = charArray;
            res.lastMatch.index = index;
            res.lastMatch.length = matchlen;

            res.leftContext.charArray = charArray;
            if (cx.Version == Context.Versions.JS1_2) {
                /*
                * JS1.2 emulated Perl4.0.1.8 (patch level 36) for global regexps used
                * in scalar contexts, and unintentionally for the string.match "list"
                * psuedo-context.  On "hi there bye", the following would result:
                *
                * Language     while(/ /g){print("$`");}   s/ /$`/g
                * perl4.036    "hi", "there"               "hihitherehi therebye"
                * perl5        "hi", "hi there"            "hihitherehi therebye"
                * js1.2        "hi", "there"               "hihitheretherebye"
                *
                * Insofar as JS1.2 always defined $` as "left context from the last
                * match" for global regexps, it was more consistent than perl4.
                */
                res.leftContext.index = start;
                res.leftContext.length = gData.skipped;
            }
            else {
                /*
                * For JS1.3 and ECMAv2, emulate Perl5 exactly:
                *
                * js1.3        "hi", "hi there"            "hihitherehi therebye"
                */
                res.leftContext.index = 0;
                res.leftContext.length = start + gData.skipped;
            }

            res.rightContext.charArray = charArray;
            res.rightContext.index = ep;
            res.rightContext.length = end - ep;

            return result;
        }
Пример #5
0
        public virtual object Perform(Context cx, IScriptable scope, IScriptable thisObj, object [] args, RegExpActions actionType)
        {
            GlobData data = new GlobData();

            data.mode = actionType;

            switch ((RegExpActions)actionType)
            {
            case EcmaScript.NET.RegExpActions.Match: {
                object rval;
                data.optarg = 1;
                rval        = matchOrReplace(cx, scope, thisObj, args, this, data, false);
                return(data.arrayobj == null ? rval : data.arrayobj);
            }


            case EcmaScript.NET.RegExpActions.Search:
                data.optarg = 1;
                return(matchOrReplace(cx, scope, thisObj, args, this, data, false));


            case EcmaScript.NET.RegExpActions.Replace: {
                object    arg1   = args.Length < 2 ? Undefined.Value : args [1];
                string    repstr = null;
                IFunction lambda = null;
                if (arg1 is IFunction)
                {
                    lambda = (IFunction)arg1;
                }
                else
                {
                    repstr = ScriptConvert.ToString(arg1);
                }

                data.optarg    = 2;
                data.lambda    = lambda;
                data.repstr    = repstr;
                data.dollar    = repstr == null ? -1 : repstr.IndexOf((char)'$');
                data.charBuf   = null;
                data.leftIndex = 0;
                object    val = matchOrReplace(cx, scope, thisObj, args, this, data, true);
                SubString rc  = this.rightContext;

                if (data.charBuf == null)
                {
                    if (data.global || val == null || !val.Equals(true))
                    {
                        /* Didn't match even once. */
                        return(data.str);
                    }
                    SubString lc = this.leftContext;
                    replace_glob(data, cx, scope, this, lc.index, lc.length);
                }
                data.charBuf.Append(rc.charArray, rc.index, rc.length);
                return(data.charBuf.ToString());
            }


            default:
                throw Context.CodeBug();
            }
        }
Пример #6
0
        /*
         * Analog of replace_glob() in jsstr.c
         */
        private static void replace_glob(GlobData rdata, Context cx, IScriptable scope, RegExpImpl reImpl, int leftIndex, int leftlen)
        {
            int    replen;
            string lambdaStr;

            if (rdata.lambda != null)
            {
                // invoke lambda function with args lastMatch, $1, $2, ... $n,
                // leftContext.length, whole string.
                SubString [] parens     = reImpl.parens;
                int          parenCount = (parens == null) ? 0 : parens.Length;
                object []    args       = new object [parenCount + 3];
                args [0] = reImpl.lastMatch.ToString();
                for (int i = 0; i < parenCount; i++)
                {
                    SubString sub = parens [i];
                    if (sub != null)
                    {
                        args [i + 1] = sub.ToString();
                    }
                    else
                    {
                        args [i + 1] = Undefined.Value;
                    }
                }
                args [parenCount + 1] = (int)reImpl.leftContext.length;
                args [parenCount + 2] = rdata.str;
                // This is a hack to prevent expose of reImpl data to
                // JS function which can run new regexps modifing
                // regexp that are used later by the engine.
                // TODO: redesign is necessary
                if (reImpl != cx.RegExpProxy)
                {
                    Context.CodeBug();
                }
                RegExpImpl re2 = new RegExpImpl();
                re2.multiline  = reImpl.multiline;
                re2.input      = reImpl.input;
                cx.RegExpProxy = re2;
                try {
                    IScriptable parent = ScriptableObject.GetTopLevelScope(scope);
                    object      result = rdata.lambda.Call(cx, parent, parent, args);
                    lambdaStr = ScriptConvert.ToString(result);
                }
                finally {
                    cx.RegExpProxy = reImpl;
                }
                replen = lambdaStr.Length;
            }
            else
            {
                lambdaStr = null;
                replen    = rdata.repstr.Length;
                if (rdata.dollar >= 0)
                {
                    int [] skip = new int [1];
                    int    dp   = rdata.dollar;
                    do
                    {
                        SubString sub = interpretDollar(cx, reImpl, rdata.repstr, dp, skip);
                        if (sub != null)
                        {
                            replen += sub.length - skip [0];
                            dp     += skip [0];
                        }
                        else
                        {
                            ++dp;
                        }
                        dp = rdata.repstr.IndexOf((char)'$', dp);
                    }while (dp >= 0);
                }
            }

            int growth = leftlen + replen + reImpl.rightContext.length;

            System.Text.StringBuilder charBuf = rdata.charBuf;
            if (charBuf == null)
            {
                charBuf       = new System.Text.StringBuilder(growth);
                rdata.charBuf = charBuf;
            }
            else
            {
                charBuf.EnsureCapacity(rdata.charBuf.Length + growth);
            }

            charBuf.Append(reImpl.leftContext.charArray, leftIndex, leftlen);
            if (rdata.lambda != null)
            {
                charBuf.Append(lambdaStr);
            }
            else
            {
                do_replace(rdata, cx, reImpl);
            }
        }
Пример #7
0
        public virtual int FindSplit(Context cx, IScriptable scope, string target, string separator, IScriptable reObj, int [] ip, int [] matchlen, bool [] matched, string [] [] parensp)
        {
            int i      = ip [0];
            int length = target.Length;
            int result;

            Context.Versions version = cx.Version;
            BuiltinRegExp    re      = (BuiltinRegExp)reObj;

            while (true)
            {
                // imitating C label
                /* JS1.2 deviated from Perl by never matching at end of string. */
                int ipsave = ip [0]; // reuse ip to save object creation
                ip [0] = i;
                object ret = re.executeRegExp(cx, scope, this, target, ip, BuiltinRegExp.TEST);
                if (ret == null || !ret.Equals(true))
                {
                    // Mismatch: ensure our caller advances i past end of string.
                    ip [0]       = ipsave;
                    matchlen [0] = 1;
                    matched [0]  = false;
                    return(length);
                }
                i           = ip [0];
                ip [0]      = ipsave;
                matched [0] = true;

                SubString sep = this.lastMatch;
                matchlen [0] = sep.length;
                if (matchlen [0] == 0)
                {
                    /*
                     * Empty string match: never split on an empty
                     * match at the start of a find_split cycle.  Same
                     * rule as for an empty global match in
                     * match_or_replace.
                     */
                    if (i == ip [0])
                    {
                        /*
                         * "Bump-along" to avoid sticking at an empty
                         * match, but don't bump past end of string --
                         * our caller must do that by adding
                         * sep->length to our return value.
                         */
                        if (i == length)
                        {
                            if (version == Context.Versions.JS1_2)
                            {
                                matchlen [0] = 1;
                                result       = i;
                            }
                            else
                            {
                                result = -1;
                            }
                            break;
                        }
                        i++;

                        goto again; // imitating C goto
                    }
                }
                // PR_ASSERT((size_t)i >= sep->length);
                result = i - matchlen [0];
                break;


again:
                ;
            }
            int size = (parens == null) ? 0 : parens.Length;

            parensp [0] = new string [size];
            for (int num = 0; num < size; num++)
            {
                SubString parsub = getParenSubString(num);
                parensp [0] [num] = parsub.ToString();
            }
            return(result);
        }
Пример #8
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);
        }