public void IntersectANegatedSetTest()
        {
            // gives just the consonants
            //[a-z&&[^aeiou]]

            var target = CharacterClass.Intersect(
                new CharacterClass('a', 'z'),
                CharacterClass.Negate(
                    CharacterClass.Union(
                        new CharacterClass('a'),
                        new CharacterClass('e'),
                        new CharacterClass('i'),
                        new CharacterClass('o'),
                        new CharacterClass('u')
                        )
                    )
                );

            var vowels     = "aeiou";
            var consonants = "bcdfghjklmnpqrstvwxyz";

            foreach (var character in vowels)
            {
                Assert.AreEqual(false, target.Contains(character));
            }

            foreach (var character in consonants)
            {
                Assert.AreEqual(true, target.Contains(character));
            }
        }
Exemplo n.º 2
0
        private CharacterClass MultiCharacterClass()
        {
            bool negative = false;

            // are we dealing with a negated character class?
            if (Peek() == '^')
            {
                negative = true;
                Consume('^');
            }

            var output = new CharacterClass();

            // keep going until we find the last closing bracket
            while (More() && Peek() != ']')
            {
                // parse a single charclass out
                var single = SingleCharacterClass();

                // union them together
                output = CharacterClass.Union(output, single);

                // have we got a intersection or substraction?
                while (More() && PeekIsAny('&', '-'))
                {
                    switch (Peek())
                    {
                    case '&':
                        // intersection
                        Consume('&');
                        Consume('&');
                        var and = IntersectionWithClass();
                        output = CharacterClass.Intersect(output, and);
                        break;

                    case '-':
                        // subtraction
                        Consume('-');
                        var subtract = MultiCharacterClass();
                        output = CharacterClass.Subtract(output, subtract);
                        break;
                    }
                }
            }

            if (negative)
            {
                output = CharacterClass.Negate(output);
            }

            return(output);
        }
        public void IntersectionRangeContainsTest(char c, bool expected)
        {
            var target = CharacterClass.Intersect(new CharacterClass('a', 'f'),
                                                  CharacterClass.Union(
                                                      new CharacterClass('a'),
                                                      new CharacterClass('e'),
                                                      new CharacterClass('i'),
                                                      new CharacterClass('o'),
                                                      new CharacterClass('u')
                                                      ));

            Assert.AreEqual(expected, target.Contains(c));
        }
        public void MultipleIntersectionsRangeContainsTest()
        {
            //[0-9&&[0-6&&[4-9]]] is the same as [4-6]

            var target = CharacterClass.Intersect(new CharacterClass('0', '9'),
                                                  CharacterClass.Intersect(new CharacterClass('0', '6'), new CharacterClass('4', '9')));

            var included = "456";
            var excluded = "123789";

            foreach (var character in included)
            {
                Assert.AreEqual(true, target.Contains(character));
            }

            foreach (var character in excluded)
            {
                Assert.AreEqual(false, target.Contains(character));
            }
        }
        public void CharacterClassToStringTest()
        {
            // gives just the consonants
            //[a-z&&[^aeiuo]]

            var target = CharacterClass.Intersect(
                new CharacterClass('a', 'z'),
                CharacterClass.Negate(
                    CharacterClass.Union(
                        new CharacterClass('a'),
                        new CharacterClass('e'),
                        new CharacterClass('i'),
                        new CharacterClass('o'),
                        new CharacterClass('u')
                        )
                    )
                );

            Assert.AreEqual("[a-z&&[^aeiou]]", target.ToString());
        }