Inheritance: JsObject
Exemplo n.º 1
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray A     = Global.ArrayClass.New();
            string  input = parameters[0].ToString();

            A["input"] = Global.StringClass.New(input);

            int             i       = 0;
            MatchCollection matches = Regex.Matches(input, regexp.Pattern, regexp.Options);

            if (matches.Count > 0)
            {
                if (regexp.IsGlobal)
                {
                    foreach (Match m in matches)
                    {
                        A[Global.NumberClass.New(i++)] = Global.StringClass.New(m.Value);
                    }
                }
                else
                {
                    foreach (Group g in matches[0].Groups)
                    {
                        A[Global.NumberClass.New(i++)] = Global.StringClass.New(g.Value);
                    }
                }

                A["index"] = Global.NumberClass.New(matches[0].Index);
            }

            return(A);
        }
Exemplo n.º 2
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray jsArray = this.Global.ArrayClass.New();
            string  input   = parameters[0].ToString();

            jsArray["input"] = (JsInstance)this.Global.StringClass.New(input);
            int             num             = 0;
            MatchCollection matchCollection = Regex.Matches(input, regexp.Pattern, regexp.Options);

            if (matchCollection.Count > 0)
            {
                if (regexp.IsGlobal)
                {
                    foreach (Match match in matchCollection)
                    {
                        jsArray[(JsInstance)this.Global.NumberClass.New((double)num++)] = (JsInstance)this.Global.StringClass.New(match.Value);
                    }
                }
                else
                {
                    foreach (Group group in matchCollection[0].Groups)
                    {
                        jsArray[(JsInstance)this.Global.NumberClass.New((double)num++)] = (JsInstance)this.Global.StringClass.New(group.Value);
                    }
                }
                jsArray["index"] = (JsInstance)this.Global.NumberClass.New((double)matchCollection[0].Index);
            }
            return((JsInstance)jsArray);
        }
Exemplo n.º 3
0
 public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
 {
     string S = parameters[0].ToString();
     int length = S.Length;
     int lastIndex = (int)regexp["lastIndex"].ToNumber();
     int i = lastIndex;
     if (regexp["global"].ToBoolean())
         i = 0;
     if (i < 0 || i > length)
     {
         lastIndex = 0;
         return JsNull.Instance;
     }
     Match r = ((Regex)regexp.Value).Match(S, i);
     if (!r.Success)
     {
         lastIndex = 0;
         return JsNull.Instance;
     }
     int e = r.Index + r.Length;
     if (regexp["global"].ToBoolean())
         lastIndex = e;
     int n = r.Groups.Count;
     JsArray result = Global.ArrayClass.New();
     result["index"] = Global.NumberClass.New(r.Index);
     result["input"] = Global.StringClass.New(S);
     result["length"] = Global.NumberClass.New(n + 1);
     result[Global.NumberClass.New(0)] = Global.StringClass.New(r.Value);
     for (i = 1; i > 0 && i < n; i++)
     {
         result[Global.NumberClass.New(i)] = Global.StringClass.New(r.Groups[i].Value);
     }
     return result;
 }
Exemplo n.º 4
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray A = Global.ArrayClass.New();
            string input = parameters[0].ToString();
            A["input"] = Global.StringClass.New(input);

            int i = 0;
            var lastIndex = regexp.IsGlobal ? regexp["lastIndex"].ToNumber() : 0;
            MatchCollection matches = Regex.Matches(input.Substring((int)lastIndex), regexp.Pattern, regexp.Options);
            if (matches.Count > 0) {
                // A[Global.NumberClass.New(i++)] = Global.StringClass.New(matches[0].Value);
                A["index"] = Global.NumberClass.New(matches[0].Index);

                if(regexp.IsGlobal)
                {
                    regexp["lastIndex"] = Global.NumberClass.New(lastIndex + matches[0].Index + matches[0].Value.Length);
                }

                foreach (Group g in matches[0].Groups) {
                    A[Global.NumberClass.New(i++)] = Global.StringClass.New(g.Value);
                }

                return A;
            }
            else
            {
                return JsNull.Instance;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 15.5.4.10
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance MatchFunc(JsDictionaryObject target, JsInstance[] parameters)
        {
            JsRegExp regexp = parameters[0].Class == JsInstance.CLASS_STRING
                ? Global.RegExpClass.New(parameters[0].ToString(), false, false, false)
                : (JsRegExp)parameters[0];

            if (!regexp.IsGlobal)
            {
                return(Global.RegExpClass.ExecImpl(regexp, new JsInstance[] { target }));
            }
            else
            {
                var result  = Global.ArrayClass.New();
                var matches = Regex.Matches(target.ToString(), regexp.Pattern, regexp.Options);
                if (matches.Count > 0)
                {
                    var i = 0;
                    foreach (Match match in matches)
                    {
                        result[Global.NumberClass.New(i++)] = Global.StringClass.New(match.Value);
                    }

                    return(result);
                }
                else
                {
                    return(JsNull.Instance);
                }
            }
        }
Exemplo n.º 6
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray A = Global.ArrayClass.New();
            string input = parameters[0].ToString();
            A["input"] = Global.StringClass.New(input);

            int i = 0;
            MatchCollection matches = Regex.Matches(input, regexp.Pattern, regexp.Options);
            if (matches.Count > 0) {
                if (regexp.IsGlobal) {
                    foreach (Match m in matches) {
                        A[Global.NumberClass.New(i++)] = Global.StringClass.New(m.Value);
                    }
                }
                else {
                    foreach (Group g in matches[0].Groups) {
                        A[Global.NumberClass.New(i++)] = Global.StringClass.New(g.Value);
                    }
                }

                A["index"] = Global.NumberClass.New(matches[0].Index);
            }

            return A;
        }
Exemplo n.º 7
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            JsArray A     = Global.ArrayClass.New();
            string  input = parameters[0].ToString();

            A["input"] = Global.StringClass.New(input);

            int             i         = 0;
            var             lastIndex = regexp.IsGlobal ? regexp["lastIndex"].ToNumber() : 0;
            MatchCollection matches   = Regex.Matches(input.Substring((int)lastIndex), regexp.Pattern, regexp.Options);

            if (matches.Count > 0)
            {
                // A[Global.NumberClass.New(i++)] = Global.StringClass.New(matches[0].Value);
                A["index"] = Global.NumberClass.New(matches[0].Index);

                if (regexp.IsGlobal)
                {
                    regexp["lastIndex"] = Global.NumberClass.New(lastIndex + matches[0].Index + matches[0].Value.Length);
                }

                foreach (Group g in matches[0].Groups)
                {
                    A[Global.NumberClass.New(i++)] = Global.StringClass.New(g.Value);
                }

                return(A);
            }
            else
            {
                return(JsNull.Instance);
            }
        }
        /// <summary>
        /// 15.5.4.10
        /// </summary>
        /// <param name="target"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public JsInstance MatchFunc(JsDictionaryObject target, JsInstance[] parameters)
        {
            JsRegExp regexp = parameters[0].Class == JsInstance.CLASS_STRING
                ? Global.RegExpClass.New(parameters[0].ToString(), false, false, false)
                : (JsRegExp)parameters[0];

            return(Global.RegExpClass.ExecImpl(regexp, new JsInstance[] { target }));
        }
        public JsRegExp New(string pattern, bool g, bool i, bool m) {
            var ret = new JsRegExp(pattern, g, i, m, PrototypeProperty);
            ret["source"] = Global.StringClass.New(pattern);
            ret["lastIndex"] = Global.NumberClass.New(0);
            ret["global"] = Global.BooleanClass.New(g);

            return ret;
        }
Exemplo n.º 10
0
        public JsRegExp New(string pattern, bool g, bool i, bool m)
        {
            JsRegExp jsRegExp = new JsRegExp(pattern, g, i, m, this.PrototypeProperty);

            jsRegExp["source"]    = (JsInstance)this.Global.StringClass.New(pattern);
            jsRegExp["lastIndex"] = (JsInstance)this.Global.NumberClass.New(0.0);
            jsRegExp["global"]    = (JsInstance)this.Global.BooleanClass.New(g);
            return(jsRegExp);
        }
Exemplo n.º 11
0
        public JsRegExp New(string pattern, bool g, bool i, bool m)
        {
            var ret = new JsRegExp(pattern, g, i, m, PrototypeProperty);

            ret["source"]    = Global.StringClass.New(pattern);
            ret["lastIndex"] = Global.NumberClass.New(0);
            ret["global"]    = Global.BooleanClass.New(g);

            return(ret);
        }
Exemplo n.º 12
0
        public JsInstance ExecImpl(JsRegExp regexp, JsInstance[] parameters)
        {
            string S         = parameters[0].ToString();
            int    length    = S.Length;
            int    lastIndex = (int)regexp["lastIndex"].ToNumber();
            int    i         = lastIndex;

            if (regexp["global"].ToBoolean())
            {
                i = 0;
            }
            if (i < 0 || i > length)
            {
                lastIndex = 0;
                return(JsNull.Instance);
            }
            Match r = ((Regex)regexp.Value).Match(S, i);

            if (!r.Success)
            {
                lastIndex = 0;
                return(JsNull.Instance);
            }
            int e = r.Index + r.Length;

            if (regexp["global"].ToBoolean())
            {
                lastIndex = e;
            }
            int     n      = r.Groups.Count;
            JsArray result = Global.ArrayClass.New();

            result["index"]  = Global.NumberClass.New(r.Index);
            result["input"]  = Global.StringClass.New(S);
            result["length"] = Global.NumberClass.New(n + 1);
            result[Global.NumberClass.New(0)] = Global.StringClass.New(r.Value);
            for (i = 1; i > 0 && i < n; i++)
            {
                result[Global.NumberClass.New(i)] = Global.StringClass.New(r.Groups[i].Value);
            }
            return(result);
        }
Exemplo n.º 13
0
 public JsInstance TestImpl(JsRegExp regex, JsInstance[] parameters)
 {
     return(new JsBoolean(ExecImpl(regex, parameters) != JsNull.Instance));
 }
Exemplo n.º 14
0
 public JsInstance GetLastIndex(JsRegExp regex, JsInstance[] parameters)
 {
     return(regex["lastIndex"]);
 }
Exemplo n.º 15
0
 public JsInstance GetLastIndex(JsRegExp regex, JsInstance[] parameters)
 {
     return regex["lastIndex"];
 }
Exemplo n.º 16
0
 public JsInstance TestImpl(JsRegExp regex, JsInstance[] parameters)
 {
     return Global.BooleanClass.New(ExecImpl(regex, parameters) != JsNull.Instance);
 }
Exemplo n.º 17
0
        public JsInstance TestImpl(JsRegExp regex, JsInstance[] parameters)
        {
            var array = ExecImpl(regex, parameters) as JsArray;

            return(Global.BooleanClass.New(array != null && array.Length > 0));
        }
Exemplo n.º 18
0
 public JsInstance TestImpl(JsRegExp regex, JsInstance[] parameters)
 {
     var array = ExecImpl(regex, parameters) as JsArray;
     return Global.BooleanClass.New(array != null && array.Length > 0);
 }
Exemplo n.º 19
0
        public JsInstance ReplaceImpl(JsDictionaryObject target, JsInstance[] parameters)
        {
            if (parameters.Length == 0)
            {
                return((JsInstance)this.Global.StringClass.New(target.ToString()));
            }
            JsInstance parameter  = parameters[0];
            JsInstance jsInstance = (JsInstance)JsUndefined.Instance;

            if (parameters.Length > 1)
            {
                jsInstance = parameters[1];
            }
            string     source   = target.ToString();
            JsFunction function = jsInstance as JsFunction;

            if (parameter.Class == "RegExp")
            {
                int      count   = ((JsRegExp)parameters[0]).IsGlobal ? int.MaxValue : 1;
                JsRegExp regexp  = (JsRegExp)parameters[0];
                int      startat = regexp.IsGlobal ? 0 : Math.Max(0, (int)regexp["lastIndex"].ToNumber() - 1);
                if (regexp.IsGlobal)
                {
                    regexp["lastIndex"] = (JsInstance)this.Global.NumberClass.New(0.0);
                }
                if (jsInstance is JsFunction)
                {
                    return((JsInstance)this.Global.StringClass.New(((JsRegExp)parameters[0]).Regex.Replace(source, (MatchEvaluator)(m =>
                    {
                        List <JsInstance> jsInstanceList = new List <JsInstance>();
                        if (!regexp.IsGlobal)
                        {
                            regexp["lastIndex"] = (JsInstance)this.Global.NumberClass.New((double)(m.Index + 1));
                        }
                        jsInstanceList.Add((JsInstance)this.Global.StringClass.New(m.Value));
                        for (int index = 1; index < m.Groups.Count; ++index)
                        {
                            if (m.Groups[index].Success)
                            {
                                jsInstanceList.Add((JsInstance)this.Global.StringClass.New(m.Groups[index].Value));
                            }
                            else
                            {
                                jsInstanceList.Add((JsInstance)JsUndefined.Instance);
                            }
                        }
                        jsInstanceList.Add((JsInstance)this.Global.NumberClass.New((double)m.Index));
                        jsInstanceList.Add((JsInstance)this.Global.StringClass.New(source));
                        this.Global.Visitor.ExecuteFunction(function, (JsDictionaryObject)null, jsInstanceList.ToArray());
                        return this.Global.Visitor.Returned.ToString();
                    }), count, startat)));
                }
                string str = parameters[1].ToString();
                return((JsInstance)this.Global.StringClass.New(((JsRegExp)parameters[0]).Regex.Replace(target.ToString(), (MatchEvaluator)(m =>
                {
                    if (!regexp.IsGlobal)
                    {
                        regexp["lastIndex"] = (JsInstance)this.Global.NumberClass.New((double)(m.Index + 1));
                    }
                    string after = source.Substring(Math.Min(source.Length - 1, m.Index + m.Length));
                    return JsStringConstructor.EvaluateReplacePattern(m.Value, source.Substring(0, m.Index), after, str, m.Groups);
                }), count, startat)));
            }
            string matched = parameter.ToString();
            int    length  = source.IndexOf(matched);

            if (length == -1)
            {
                return((JsInstance)this.Global.StringClass.New(source));
            }
            if (jsInstance is JsFunction)
            {
                this.Global.Visitor.ExecuteFunction(function, (JsDictionaryObject)null, new List <JsInstance>()
                {
                    (JsInstance)this.Global.StringClass.New(matched),
                    (JsInstance)this.Global.NumberClass.New((double)length),
                    (JsInstance)this.Global.StringClass.New(source)
                }.ToArray());
                JsInstance result = this.Global.Visitor.Result;
                return((JsInstance)this.Global.StringClass.New(source.Substring(0, length) + result.ToString() + source.Substring(length + matched.Length)));
            }
            string before         = source.Substring(0, length);
            string after1         = source.Substring(length + matched.Length);
            string replacePattern = JsStringConstructor.EvaluateReplacePattern(matched, before, after1, jsInstance.ToString(), (GroupCollection)null);

            return((JsInstance)this.Global.StringClass.New(before + replacePattern + after1));
        }
Exemplo n.º 20
0
 public JsInstance TestImpl(JsRegExp regex, JsInstance[] parameters)
 {
     return(Global.BooleanClass.New(ExecImpl(regex, parameters) != JsNull.Instance));
 }
Exemplo n.º 21
0
 public JsInstance TestImpl(JsRegExp regex, JsInstance[] parameters)
 {
     return new JsBoolean(ExecImpl(regex, parameters) != JsNull.Instance);
 }