Example #1
0
        //*
        // * Parses a regular expression character set. This method handles
        // * the contents of the '[...]' construct in a regular expression.
        // *
        // * @return the element representing this character set
        // *
        // * @throws RegExpException if an error was encountered in the
        // * pattern string
        //

        private Element ParseCharSet()
        {
            CharacterSetElement charset = default(CharacterSetElement);
            Element             elem    = default(Element);
            bool repeat = true;
            char start  = '\0';
            char end    = '\0';

            if (PeekChar(0) == Convert.ToInt32('^'))
            {
                ReadChar('^');
                charset = new CharacterSetElement(true);
            }
            else
            {
                charset = new CharacterSetElement(false);
            }

            while (PeekChar(0) > 0 && repeat)
            {
                start = Convert.ToChar(PeekChar(0));
                switch (start)
                {
                case ']':
                    repeat = false;
                    break;

                case '\\':
                    elem = ParseEscapeChar();
                    if (elem is StringElement)
                    {
                        charset.AddCharacters((StringElement)elem);
                    }
                    else
                    {
                        charset.AddCharacterSet((CharacterSetElement)elem);
                    }

                    break;

                default:
                    ReadChar(start);
                    if (PeekChar(0) == Convert.ToInt32('-') && PeekChar(1) > 0 && PeekChar(1) != Convert.ToInt32(']'))
                    {
                        ReadChar('-');
                        end = ReadChar();
                        charset.AddRange(FixChar(start), FixChar(end));
                    }
                    else
                    {
                        charset.AddCharacter(FixChar(start));
                    }

                    break;
                }
            }

            return(charset);
        }
        //*
        // * Checks if the specified character is present in the user-
        // * defined set. This method does not consider the inverted
        // * flag.
        // *
        // * @param value the character to check
        // *
        // * @return true if the character is present, or
        // * false otherwise
        //

        private bool InUserSet(char value)
        {
            object obj            = null;
            char   c              = '\0';
            Range  r              = default(Range);
            CharacterSetElement e = default(CharacterSetElement);

            for (int i = 0; i <= contents.Count - 1; i++)
            {
                obj = contents[i];
                if (obj is char)
                {
                    c = (char)obj;
                    if (c == value)
                    {
                        return(true);
                    }
                }
                else if (obj is Range)
                {
                    r = (Range)obj;
                    if (r.Inside(value))
                    {
                        return(true);
                    }
                }
                else if (obj is CharacterSetElement)
                {
                    e = (CharacterSetElement)obj;
                    if (e.InSet(value))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        //*
        // * Adds a character subset to this character set.
        // *
        // * @param elem the character set to add
        //

        public void AddCharacterSet(CharacterSetElement elem)
        {
            contents.Add(elem);
        }