public void TestCtor()
        {
            Should.Throw<ArgumentNullException> (() => new StringMatchOperation (null), "a1");
            Should.Throw<ArgumentException> (() => new StringMatchOperation (String.Empty), "a2");

            var op = new StringMatchOperation ("foo");
            Assert.AreEqual ("foo", op.String, "a3");
        }
        public void TestStringProperty()
        {
            var op = new StringMatchOperation ("foo");
            Assert.AreEqual ("foo", op.String, "a1");

            Should.Throw<ArgumentNullException> (() => op.String = null, "a2");
            Should.Throw<ArgumentException> (() => op.String = String.Empty, "a3");

            op.String = "baz";
            Assert.AreEqual ("baz", op.String);
        }
        public void MatchNotAtStartShouldFail()
        {
            var op = new StringMatchOperation ("oobar");
            var data = new DataDictionary ();
            int end;

            bool m = op.IsMatch ("foobar", 0, out data, out end);

            Assert.IsFalse (m, "a1");
            Assert.IsNull (data, "a2");
            Assert.AreEqual (0, end, "a3");
        }
        public void MatchFullStringTest()
        {
            var op = new StringMatchOperation ("foobar");
            var data = new DataDictionary ();
            int end;

            bool m = op.IsMatch ("foobar", 0, out data, out end);

            Assert.IsTrue (m, "a1");
            Assert.IsNull (data, "a2");
            Assert.AreEqual (6, end, "a3");
        }
        public void MatchPartialStringTest()
        {
            var op = new StringMatchOperation ("foo");
            var data = new DataDictionary ();
            int end;

            bool m = op.IsMatch ("foobar", 0, data, out end);

            Assert.IsTrue (m, "a1");
            Assert.AreEqual (0, data.Count, "a2");
            Assert.AreEqual (3, end, "a3");

            op = new StringMatchOperation ("bar");
            m = op.IsMatch ("foobar", end, data, out end);
            Assert.IsTrue (m, "a4");
            Assert.AreEqual (0, data.Count, "a5");
            Assert.AreEqual (6, end, "a3");
        }
Exemple #6
0
        public bool IsMatch(string input, int start, out DataDictionary data, out int end)
        {
            end  = start;
            data = null;

            if (groups == null)
            {
                return(StringMatchOperation.IsMatchInternal(Pattern, input, start, out data, out end));
            }

            int input_pos   = start;
            int pattern_pos = 0;

            string data_str;

            foreach (Group g in groups)
            {
                // scan until start
                int g_start = start + g.Start;

                int len = g_start - pattern_pos;
                for (int i = start; i < len; i++)
                {
                    if (input [input_pos] != pattern [pattern_pos])
                    {
                        return(false);
                    }

                    input_pos++;
                    pattern_pos++;

                    if (input_pos > input.Length - 1)
                    {
                        return(false);
                    }
                }

                if (g.End == pattern.Length - 1)
                {
                    // slurp until end
                    data_str = input.Substring(input_pos);
                    if (data == null)
                    {
                        data = new DataDictionary();
                    }
                    data.Set(g.Name, data_str);

                    end = input.Length;
                    return(true);
                }

                int  input_start = input_pos;
                char end_marker  = pattern [g.End + 1];
                while (input [input_pos] != end_marker)
                {
                    input_pos++;
                    if (input_pos >= input.Length)
                    {
                        return(false);
                    }
                }

                data_str = input.Substring(input_start, input_pos - input_start);
                if (data == null)
                {
                    data = new DataDictionary();
                }
                data.Set(g.Name, data_str);

                input_pos++;
                pattern_pos = g.End + 2;
            }

            while (pattern_pos < pattern.Length)
            {
                if (pattern [pattern_pos] != input [input_pos])
                {
                    return(false);
                }
                pattern_pos++;
                input_pos++;

                if (input_pos > input.Length)
                {
                    return(false);
                }
            }

            end = input_pos;
            return(true);
        }