internal bool IsMatch(string str) { int num3; StringBuilder builder = new StringBuilder(str.Length); foreach (char ch in str) { builder.Append(this._characterNormalizer.Normalize(ch)); } str = builder.ToString(); PatternPositionsVisitor patternPositionsForCurrentStringPosition = new PatternPositionsVisitor(this._patternElements.Length); patternPositionsForCurrentStringPosition.Add(0); PatternPositionsVisitor patternPositionsForNextStringPosition = new PatternPositionsVisitor(this._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)) { this._patternElements[num2].ProcessStringCharacter(currentStringCharacter, num2, patternPositionsForCurrentStringPosition, patternPositionsForNextStringPosition); } PatternPositionsVisitor visitor3 = patternPositionsForCurrentStringPosition; patternPositionsForCurrentStringPosition = patternPositionsForNextStringPosition; patternPositionsForNextStringPosition = visitor3; } while (patternPositionsForCurrentStringPosition.MoveNext(out num3)) { this._patternElements[num3].ProcessEndOfString(num3, patternPositionsForCurrentStringPosition); } return(patternPositionsForCurrentStringPosition.ReachedEndOfPattern); }
public override void ProcessEndOfString( int currentPatternPosition, PatternPositionsVisitor patternPositionsForEndOfStringPosition) { // '*' : (patternPosition, endOfString) => (patternPosition + 1, endOfString) patternPositionsForEndOfStringPosition.Add(currentPatternPosition + 1); }
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; }
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); }
public override void ProcessStringCharacter( char currentStringCharacter, int currentPatternPosition, PatternPositionsVisitor patternPositionsForCurrentStringPosition, PatternPositionsVisitor patternPositionsForNextStringPosition) { // '?' : (patternPosition, stringPosition) => (patternPosition + 1, stringPosition + 1) patternPositionsForNextStringPosition.Add(currentPatternPosition + 1); }
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; }
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); }