Пример #1
0
        public static object match(object thisObj, VsaEngine engine, object regExp)
        {
            string       string_obj = Convert.ToString(thisObj);
            RegExpObject regex_obj  = Convert.ToRegExp(regExp);
            bool         global     = regex_obj.global;

            if (!global)
            {
                return(RegExpPrototype.exec(regex_obj, string_obj));
            }

            MatchCollection md        = regex_obj.regex.Matches(string_obj);
            uint            n         = (uint)md.Count;
            Match           lastMatch = md [(int)(n - 1)];

            regex_obj.lastIndex = lastMatch.Index + 1;
            RegExpConstructor.UpdateLastMatch(lastMatch, string_obj);

            ArrayObject result = new ArrayObject();

            result.length = n;
            for (uint i = 0; i < n; i++)
            {
                result.elems [i] = md [(int)i].Value;
            }

            return(result);
        }
Пример #2
0
        public static object exec(object thisObj, object input)
        {
            RegExpObject re  = Convert.ToRegExp(thisObj);
            string       str = null;

            if (input == null)
            {
                RegExpConstructor ctr = RegExpConstructor.Ctr;
                str = Convert.ToString(ctr.GetField("$_").GetValue("$_"));
            }
            else
            {
                str = Convert.ToString(input);
            }
            bool global    = re.global;
            int  lastIndex = global ? (int)((double)re.lastIndex) : 0;
            bool success   = lastIndex >= 0 && lastIndex <= str.Length;

            Match md = null;

            if (success)
            {
                md      = re.regex.Match(str, lastIndex);
                success = md.Success;
            }

            if (!success)
            {
                re.lastIndex = 0;
                return(DBNull.Value);
            }

            int index    = md.Index;
            int endIndex = index + md.Length;

            if (global)
            {
                re.lastIndex = endIndex;
            }
            RegExpConstructor.UpdateLastMatch(md, str);

            GroupCollection caps   = md.Groups;
            uint            len    = (uint)caps.Count;
            RegExpMatch     result = new RegExpMatch();

            result.AddField("index", index);
            result.AddField("input", input);
            result.length = len;
            for (uint j = 0; j < len; j++)
            {
                result.elems [j] = caps [(int)j].Value;
            }

            return(result);
        }
Пример #3
0
        public static RegExpObject compile(object thisObj, object source, object flags)
        {
            //
            // Note: We always compile RegExp internals so all this method is useful for is for
            // changing the properties of the otherwise immutable RegExp objects.
            //
            RegExpObject re       = Convert.ToRegExp(thisObj);
            string       flag_str = Convert.ToString(flags);

            re.Initialize(Convert.ToString(source),
                          flag_str.IndexOfAny(new char [] { 'i' }) > -1,
                          flag_str.IndexOfAny(new char [] { 'g' }) > -1,
                          flag_str.IndexOfAny(new char [] { 'm' }) > -1);
            return(re);
        }
Пример #4
0
        public static int search(object thisObj, VsaEngine engine, object regExp)
        {
            string       string_obj = Convert.ToString(thisObj);
            RegExpObject regex_obj  = Convert.ToRegExp(regExp);
            Match        md         = regex_obj.regex.Match(string_obj);

            /* Note: Microsoft's implementation updates the lastIndex property of regex_obj here, but
             * ECMA-262, 15.5.4.12, NOTE 1 explicitely says not to do so. We do the ECMA-262 behavior. */
            if (md.Success)
            {
                return(md.Index);
            }
            else
            {
                return(-1);
            }
        }