Matches the specified text with the filter. The filter. The text. True if text has one or more matches, otherwise false.
(string filter, string text)
| 24 | /// <param name="text">The text.</param> |
| 25 | /// <returns>True if text has one or more matches, otherwise false.</returns> |
| 26 | public static bool Match(string filter, string text) |
| 27 | { |
| 28 | // Empty inputs |
| 29 | if (string.IsNullOrEmpty(filter) || string.IsNullOrEmpty(text)) |
| 30 | return false; |
| 31 | |
| 32 | // Full match |
| 33 | if (string.Equals(filter, text, StringComparison.CurrentCultureIgnoreCase)) |
| 34 | { |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | bool hasMatch = false; |
| 39 | |
| 40 | // Find matching sequences |
| 41 | // We do simple iteration over the characters |
| 42 | int textLength = text.Length; |
| 43 | int filterLength = filter.Length; |
| 44 | int searchEnd = textLength - filterLength; |
| 45 | for (int textPos = 0; textPos <= searchEnd; textPos++) |
| 46 | { |
| 47 | // Skip if the current text position doesn't match the filter start |
| 48 | if (char.ToLower(filter[0]) != char.ToLower(text[textPos])) |
| 49 | continue; |
| 50 | |
| 51 | int matchStartPos = -1; |
| 52 | int endPos = textPos + filterLength; |
| 53 | int filterPos = 0; |
| 54 | |
| 55 | for (int i = textPos; i < endPos; i++, filterPos++) |
| 56 | { |
| 57 | var filterChar = char.ToLower(filter[filterPos]); |
| 58 | var textChar = char.ToLower(text[i]); |
| 59 | |
| 60 | if (filterChar == textChar) |
| 61 | { |
| 62 | // Check if start the matching sequence |
| 63 | if (matchStartPos == -1) |
| 64 | { |
| 65 | matchStartPos = textPos; |
| 66 | } |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | // Check if stop matching sequence |
| 71 | if (matchStartPos != -1) |
| 72 | { |
| 73 | var length = textPos - matchStartPos; |
| 74 | if (length >= MinLength) |
| 75 | hasMatch = true; |
| 76 | textPos = matchStartPos + length; |
| 77 | matchStartPos = -1; |
| 78 | } |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // Check sequence on the end |
no test coverage detected