Ejemplo n.º 1
0
 public override void ProcessEndOfString(
     int currentPatternPosition,
     PatternPositionsVisitor patternPositionsForEndOfStringPosition)
 {
     // '*' : (patternPosition, endOfString) => (patternPosition + 1, endOfString)
     patternPositionsForEndOfStringPosition.Add(currentPatternPosition + 1);
 }
Ejemplo n.º 2
0
 internal bool IsMatch (string str)
 {
     int num3;
     var builder = new StringBuilder(str.Length);
     foreach (char ch in str)
         builder.Append(_characterNormalizer.Normalize(ch));
     str = builder.ToString();
     var patternPositionsForCurrentStringPosition = new PatternPositionsVisitor(_patternElements.Length);
     patternPositionsForCurrentStringPosition.Add(0);
     var patternPositionsForNextStringPosition = new PatternPositionsVisitor(_patternElements.Length);
     for (int i = 0; i < str.Length; i++) {
         int num2;
         char currentStringCharacter = str[i];
         patternPositionsForCurrentStringPosition.StringPosition = i;
         patternPositionsForNextStringPosition.StringPosition = i + 1;
         while (patternPositionsForCurrentStringPosition.MoveNext(out num2)) {
             _patternElements[num2].ProcessStringCharacter(currentStringCharacter, num2,
                 patternPositionsForCurrentStringPosition, patternPositionsForNextStringPosition);
         }
         PatternPositionsVisitor visitor3 = patternPositionsForCurrentStringPosition;
         patternPositionsForCurrentStringPosition = patternPositionsForNextStringPosition;
         patternPositionsForNextStringPosition = visitor3;
     }
     while (patternPositionsForCurrentStringPosition.MoveNext(out num3))
         _patternElements[num3].ProcessEndOfString(num3, patternPositionsForCurrentStringPosition);
     return patternPositionsForCurrentStringPosition.IsEndOfPattern;
 }
Ejemplo n.º 3
0
        internal bool IsMatch(string str)
        {
            // - each state of NFA is represented by (patternPosition, stringPosition) tuple
            //     - state transitions are documented in
            //       ProcessStringCharacter and ProcessEndOfString methods
            // - the algorithm below tries to see if there is a path
            //   from (0, 0) to (lengthOfPattern, lengthOfString)
            //    - this is a regular graph traversal
            //    - there are O(1) edges per node (at most 2 edges)
            //      so the whole graph traversal takes O(number of nodes in the graph) =
            //      = O(lengthOfPattern * lengthOfString) time
            //    - for efficient remembering which states have already been visited,
            //      the traversal goes methodically from beginning to end of the string
            //      therefore requiring only O(lengthOfPattern) memory for remembering
            //      which states have been already visited
            //  - Wikipedia calls this algorithm the "NFA" algorithm at
            //    http://en.wikipedia.org/wiki/Regular_expression#Implementations_and_running_times

            var patternPositionsForCurrentStringPosition =
                new PatternPositionsVisitor(_patternElements.Length);

            patternPositionsForCurrentStringPosition.Add(0);

            var patternPositionsForNextStringPosition =
                new PatternPositionsVisitor(_patternElements.Length);

            for (int currentStringPosition = 0;
                 currentStringPosition < str.Length;
                 currentStringPosition++)
            {
                char currentStringCharacter = _characterNormalizer.Normalize(str[currentStringPosition]);
                patternPositionsForCurrentStringPosition.StringPosition = currentStringPosition;
                patternPositionsForNextStringPosition.StringPosition    = currentStringPosition + 1;

                while (patternPositionsForCurrentStringPosition.MoveNext(out int patternPosition))
                {
                    _patternElements[patternPosition].ProcessStringCharacter(
                        currentStringCharacter,
                        patternPosition,
                        patternPositionsForCurrentStringPosition,
                        patternPositionsForNextStringPosition);
                }

                // swap patternPositionsForCurrentStringPosition
                // with patternPositionsForNextStringPosition
                var tmp = patternPositionsForCurrentStringPosition;
                patternPositionsForCurrentStringPosition = patternPositionsForNextStringPosition;
                patternPositionsForNextStringPosition    = tmp;
            }

            while (patternPositionsForCurrentStringPosition.MoveNext(out int patternPosition2))
            {
                _patternElements[patternPosition2].ProcessEndOfString(
                    patternPosition2,
                    patternPositionsForCurrentStringPosition);
            }

            return(patternPositionsForCurrentStringPosition.ReachedEndOfPattern);
        }
Ejemplo n.º 4
0
 public override void ProcessStringCharacter(char currentStringCharacter, int currentPatternPosition,
                                             PatternPositionsVisitor patternPositionsForCurrentStringPosition, PatternPositionsVisitor patternPositionsForNextStringPosition)
 {
     if (_regex.IsMatch(new string(currentStringCharacter, 1)))
     {
         base.ProcessStringCharacter(currentStringCharacter, currentPatternPosition,
                                     patternPositionsForCurrentStringPosition, patternPositionsForNextStringPosition);
     }
 }
Ejemplo n.º 5
0
 public override void ProcessStringCharacter(
     char currentStringCharacter,
     int currentPatternPosition,
     PatternPositionsVisitor patternPositionsForCurrentStringPosition,
     PatternPositionsVisitor patternPositionsForNextStringPosition)
 {
     // '?' : (patternPosition, stringPosition) => (patternPosition + 1, stringPosition + 1)
     patternPositionsForNextStringPosition.Add(currentPatternPosition + 1);
 }
Ejemplo n.º 6
0
 public override void ProcessStringCharacter(char currentStringCharacter, int currentPatternPosition,
                                             PatternPositionsVisitor patternPositionsForCurrentStringPosition, PatternPositionsVisitor patternPositionsForNextStringPosition)
 {
     if (_literalCharacter == currentStringCharacter)
     {
         base.ProcessStringCharacter(currentStringCharacter, currentPatternPosition,
                                     patternPositionsForCurrentStringPosition, patternPositionsForNextStringPosition);
     }
 }
Ejemplo n.º 7
0
        internal bool IsMatch(string str)
        {
            int num3;
            var builder = new StringBuilder(str.Length);

            foreach (char ch in str)
            {
                builder.Append(_characterNormalizer.Normalize(ch));
            }
            str = builder.ToString();
            var patternPositionsForCurrentStringPosition = new PatternPositionsVisitor(_patternElements.Length);

            patternPositionsForCurrentStringPosition.Add(0);
            var patternPositionsForNextStringPosition = new PatternPositionsVisitor(_patternElements.Length);

            for (int i = 0; i < str.Length; i++)
            {
                int  num2;
                char currentStringCharacter = str[i];
                patternPositionsForCurrentStringPosition.StringPosition = i;
                patternPositionsForNextStringPosition.StringPosition    = i + 1;
                while (patternPositionsForCurrentStringPosition.MoveNext(out num2))
                {
                    _patternElements[num2].ProcessStringCharacter(currentStringCharacter, num2,
                                                                  patternPositionsForCurrentStringPosition, patternPositionsForNextStringPosition);
                }
                PatternPositionsVisitor visitor3 = patternPositionsForCurrentStringPosition;
                patternPositionsForCurrentStringPosition = patternPositionsForNextStringPosition;
                patternPositionsForNextStringPosition    = visitor3;
            }
            while (patternPositionsForCurrentStringPosition.MoveNext(out num3))
            {
                _patternElements[num3].ProcessEndOfString(num3, patternPositionsForCurrentStringPosition);
            }
            return(patternPositionsForCurrentStringPosition.IsEndOfPattern);
        }
Ejemplo n.º 8
0
        internal bool IsMatch(string str)
        {
            // - each state of NFA is represented by (patternPosition, stringPosition) tuple
            //     - state transitions are documented in
            //       ProcessStringCharacter and ProcessEndOfString methods
            // - the algorithm below tries to see if there is a path 
            //   from (0, 0) to (lengthOfPattern, lengthOfString)
            //    - this is a regular graph traversal
            //    - there are O(1) edges per node (at most 2 edges) 
            //      so the whole graph traversal takes O(number of nodes in the graph) =
            //      = O(lengthOfPattern * lengthOfString) time
            //    - for efficient remembering which states have already been visited, 
            //      the traversal goes methodically from beginning to end of the string
            //      therefore requiring only O(lengthOfPattern) memory for remembering
            //      which states have been already visited
            //  - Wikipedia calls this algorithm the "NFA" algorithm at
            //    http://en.wikipedia.org/wiki/Regular_expression#Implementations_and_running_times

            var patternPositionsForCurrentStringPosition =
                    new PatternPositionsVisitor(_patternElements.Length);
            patternPositionsForCurrentStringPosition.Add(0);

            var patternPositionsForNextStringPosition =
                    new PatternPositionsVisitor(_patternElements.Length);

            for (int currentStringPosition = 0;
                 currentStringPosition < str.Length;
                 currentStringPosition++)
            {
                char currentStringCharacter = _characterNormalizer.Normalize(str[currentStringPosition]);
                patternPositionsForCurrentStringPosition.StringPosition = currentStringPosition;
                patternPositionsForNextStringPosition.StringPosition = currentStringPosition + 1;

                int patternPosition;
                while (patternPositionsForCurrentStringPosition.MoveNext(out patternPosition))
                {
                    _patternElements[patternPosition].ProcessStringCharacter(
                        currentStringCharacter,
                        patternPosition,
                        patternPositionsForCurrentStringPosition,
                        patternPositionsForNextStringPosition);
                }

                // swap patternPositionsForCurrentStringPosition 
                // with patternPositionsForNextStringPosition
                var tmp = patternPositionsForCurrentStringPosition;
                patternPositionsForCurrentStringPosition = patternPositionsForNextStringPosition;
                patternPositionsForNextStringPosition = tmp;
            }

            int patternPosition2;
            while (patternPositionsForCurrentStringPosition.MoveNext(out patternPosition2))
            {
                _patternElements[patternPosition2].ProcessEndOfString(
                    patternPosition2,
                    patternPositionsForCurrentStringPosition);
            }

            return patternPositionsForCurrentStringPosition.ReachedEndOfPattern;
        }
Ejemplo n.º 9
0
 public override void ProcessEndOfString(
                 int currentPatternPosition,
                 PatternPositionsVisitor patternPositionsForEndOfStringPosition)
 {
     // '*' : (patternPosition, endOfString) => (patternPosition + 1, endOfString)
     patternPositionsForEndOfStringPosition.Add(currentPatternPosition + 1);
 }
Ejemplo n.º 10
0
            public override void ProcessStringCharacter(
                            char currentStringCharacter,
                            int currentPatternPosition,
                            PatternPositionsVisitor patternPositionsForCurrentStringPosition,
                            PatternPositionsVisitor patternPositionsForNextStringPosition)
            {
                // '*' : (patternPosition, stringPosition) => (patternPosition + 1, stringPosition)
                patternPositionsForCurrentStringPosition.Add(currentPatternPosition + 1);

                // '*' : (patternPosition, stringPosition) => (patternPosition, stringPosition + 1)
                patternPositionsForNextStringPosition.Add(currentPatternPosition);
            }
Ejemplo n.º 11
0
 public override void ProcessStringCharacter(
                 char currentStringCharacter,
                 int currentPatternPosition,
                 PatternPositionsVisitor patternPositionsForCurrentStringPosition,
                 PatternPositionsVisitor patternPositionsForNextStringPosition)
 {
     if (_regex.IsMatch(new string(currentStringCharacter, 1)))
     {
         base.ProcessStringCharacter(currentStringCharacter, currentPatternPosition,
                                     patternPositionsForCurrentStringPosition,
                                     patternPositionsForNextStringPosition);
     }
 }
Ejemplo n.º 12
0
 public override void ProcessStringCharacter(
                 char currentStringCharacter,
                 int currentPatternPosition,
                 PatternPositionsVisitor patternPositionsForCurrentStringPosition,
                 PatternPositionsVisitor patternPositionsForNextStringPosition)
 {
     if (_literalCharacter == currentStringCharacter)
     {
         base.ProcessStringCharacter(
                 currentStringCharacter,
                 currentPatternPosition,
                 patternPositionsForCurrentStringPosition,
                 patternPositionsForNextStringPosition);
     }
 }
Ejemplo n.º 13
0
 public abstract void ProcessStringCharacter(
                 char currentStringCharacter,
                 int currentPatternPosition,
                 PatternPositionsVisitor patternPositionsForCurrentStringPosition,
                 PatternPositionsVisitor patternPositionsForNextStringPosition);
Ejemplo n.º 14
0
 public override void ProcessEndOfString (int currentPatternPosition, PatternPositionsVisitor patternPositionsForEndOfStringPosition)
 {}
Ejemplo n.º 15
0
 public override void ProcessEndOfString(int currentPatternPosition, PatternPositionsVisitor patternPositionsForEndOfStringPosition)
 {
 }
Ejemplo n.º 16
0
 public abstract void ProcessStringCharacter(
     char currentStringCharacter,
     int currentPatternPosition,
     PatternPositionsVisitor patternPositionsForCurrentStringPosition,
     PatternPositionsVisitor patternPositionsForNextStringPosition);
Ejemplo n.º 17
0
 public abstract void ProcessEndOfString(
     int currentPatternPosition,
     PatternPositionsVisitor patternPositionsForEndOfStringPosition);
Ejemplo n.º 18
0
 public abstract void ProcessEndOfString(
                 int currentPatternPosition,
                 PatternPositionsVisitor patternPositionsForEndOfStringPosition);
Ejemplo n.º 19
0
 public override void ProcessEndOfString(
     int currentPatternPosition,
     PatternPositionsVisitor patternPositionsForEndOfStringPosition)
 {
     // '?' : (patternPosition, endOfString) => <no transitions out of this state - cannot move beyond end of string>
 }
Ejemplo n.º 20
0
 public override void ProcessEndOfString(
                 int currentPatternPosition,
                 PatternPositionsVisitor patternPositionsForEndOfStringPosition)
 {
     // '?' : (patternPosition, endOfString) => <no transitions out of this state - cannot move beyond end of string>
 }