Ejemplo n.º 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);
        }
Ejemplo n.º 2
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;
        }
Ejemplo n.º 3
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);
            }
        }
Ejemplo n.º 4
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);
        }