Exemple #1
0
        public override (bool success, int offset) IsMatch(string text, int startOffset = 0)
        {
            int            offset  = startOffset;
            bool           success = true;
            int            subOffset;
            PatternMatcher subPattern = _subPattern;

            if (subPattern != null)
            {
                while (success)
                {
                    (success, subOffset) = subPattern.IsMatch(text, offset);
                    offset += subOffset;
                }
            }
            return(true, offset - startOffset);
        }
        public override (bool success, int offset) IsMatch(string text, int startOffset = 0)
        {
            int            upperBounds = _upperBounds;
            int            offset      = startOffset;
            bool           subSuccess  = true;
            int            subOffset;
            PatternMatcher subPattern = _subPattern;
            int            matches    = 0;

            for (; matches < upperBounds && subSuccess; matches++)
            {
                (subSuccess, subOffset) = subPattern.IsMatch(text, offset);
                offset += subOffset;
            }
            bool success = subSuccess || matches >= _lowerBounds;

            return(success ? (true, offset - startOffset) : (false, 0));
        }
Exemple #3
0
        private PatternMatcher AsOrGroupPatternMatcher(string pattern)
        {
            List <PatternMatcher> matchers    = new List <PatternMatcher>();
            PatternMatcher        lastMatcher = null;
            bool terminate = false;

            while (Offset < pattern.Length && !terminate)
            {
                switch (pattern[Offset++])
                {
                case '\\':
                    lastMatcher = AsSpecialMatcher(pattern);
                    matchers.Add(lastMatcher);
                    break;

                case '.':
                    lastMatcher = new AnyPatternMatcher();
                    matchers.Add(lastMatcher);
                    break;

                case '(':
                    lastMatcher = AsGroupMatcher(pattern);
                    matchers.Add(lastMatcher);
                    break;

                case '[':
                    lastMatcher = AsCharBoundsMatcher(pattern);
                    matchers.Add(lastMatcher);
                    break;

                case '*':
                    lastMatcher = new WildcardPatternMatcher(lastMatcher);
                    matchers.SetLast(lastMatcher);
                    break;

                case '+':
                    lastMatcher = new OneOrMorePatternMatcher(lastMatcher);
                    matchers.SetLast(lastMatcher);
                    break;

                case '?':
                    lastMatcher = new OptionalPatternMatcher(lastMatcher);
                    matchers.SetLast(lastMatcher);
                    break;

                case '|':
                    terminate = true;
                    break;

                case ')':
                    Offset--;
                    terminate = true;
                    break;

                case '{':
                    lastMatcher = AsCountBoundsMatcher(pattern, lastMatcher);
                    matchers.SetLast(lastMatcher);
                    break;

                default:
                    Offset--;
                    lastMatcher = AsLiteralMatcher(pattern);
                    matchers.Add(lastMatcher);
                    break;
                }
            }
            return(matchers.Count == 1 ? matchers[0] : new GroupPatternMatcher(matchers));
        }