예제 #1
0
        public void CamelCaseReturnsPositivePriority()
        {
            var func = StringMatch.GetShowStringDelegates("FB");

            Assert.IsTrue(func("ThisIsMyFooBar.txt").MatchPriority > 0);
        }
 public void SetUp()
 {
     mValidateCalled = false;
     mSut            = new StringMatch(Validate);
 }
예제 #3
0
 internal StringMatch MatchString(string value, Dictionary<string, byte> keys)
 {
     value = value.Trim();
     StringMatch match = new StringMatch();
     match.original = value;
     foreach (var opcode in keys)
     {
         int valueIndex = 0;
         bool requiredWhitespaceMet = false;
         bool matchFound = true;
         match.isLiteral = false;
         for (int i = 0; i < opcode.Key.Length && valueIndex < value.Length; i++)
         {
             match.match = opcode.Key;
             match.value = opcode.Value;
             if (opcode.Key[i] == '_') // Required whitespace
             {
                 if (value[valueIndex] == ' ' || value[valueIndex] == '\t')
                 {
                     requiredWhitespaceMet = true;
                     i--;
                     valueIndex++;
                 }
                 else
                 {
                     if (!requiredWhitespaceMet)
                     {
                         matchFound = false;
                         break;
                     }
                     requiredWhitespaceMet = false;
                 }
             }
             else if (opcode.Key[i] == '.') // Optional whitespace
             {
                 if (value[valueIndex] == ' ' || value[valueIndex] == '\t')
                 {
                     i--;
                     valueIndex++;
                 }
             }
             else if (opcode.Key[i] == '%') // value, like A or POP
             {
                 i++;
                 char valID = opcode.Key[i];
                 int valueStart = valueIndex;
                 if (i == opcode.Key.Length - 1)
                 {
                     valueIndex = value.Length;
                 }
                 else
                 {
                     int delimiter = value.IndexOf(',', valueIndex);
                     if (delimiter == -1)
                     {
                         matchFound = false;
                         break;
                     }
                     else
                         valueIndex = delimiter;
                 }
                 if (valID == 'a')
                     match.valueA = value.Substring(valueStart, valueIndex - valueStart);
                 else
                     match.valueB = value.Substring(valueStart, valueIndex - valueStart);
             }
             else if (opcode.Key[i] == '$') // Literal
             {
                 i++;
                 char valID = opcode.Key[i];
                 int valueStart = valueIndex;
                 if (i == opcode.Key.Length - 1)
                     valueIndex = value.Length;
                 else
                 {
                     int delimiter = value.SafeIndexOf(',', valueIndex);
                     if (delimiter == -1 && opcode.Value != 0x1E)
                         delimiter = value.SafeIndexOfParenthesis('+', valueIndex);
                     if (delimiter == -1)
                         delimiter = value.SafeIndexOf(']', valueIndex);
                     if (delimiter == -1)
                     {
                         matchFound = false;
                         break;
                     }
                     else
                         valueIndex = delimiter;
                 }
                 match.isLiteral = true;
                 match.literal = value.Substring(valueStart, valueIndex - valueStart);
             }
             else if (opcode.Key[i] == '&') // Negative literal
             {
                 i++;
                 char valID = opcode.Key[i];
                 int valueStart = valueIndex;
                 if (i == opcode.Key.Length - 1)
                     valueIndex = value.Length;
                 else
                 {
                     int delimiter = value.SafeIndexOf(',', valueIndex);
                     if (delimiter == -1 && opcode.Value != 0x1E)
                         delimiter = value.SafeIndexOfParenthesis('-', valueIndex);
                     if (delimiter == -1)
                         delimiter = value.SafeIndexOf(']', valueIndex);
                     if (delimiter == -1)
                     {
                         matchFound = false;
                         break;
                     }
                     else
                         valueIndex = delimiter;
                 }
                 match.isLiteral = true;
                 match.literal = "-(" + value.Substring(valueStart, valueIndex - valueStart) + ")";
             }
             else
             {
                 if (value.ToUpper()[valueIndex] != opcode.Key.ToUpper()[i])
                 {
                     matchFound = false;
                     break;
                 }
                 valueIndex++;
             }
         }
         if (matchFound && valueIndex == value.Length)
             return match;
     }
     return null;
 }
예제 #4
0
        public void RegexCountMatchOnce()
        {
            var func = StringMatch.GetShowStringDelegates("foo");

            Assert.IsTrue(func("FooBar.txt").MatchPriority == func("FooFoo.txt").MatchPriority);
        }
예제 #5
0
        public List<StringMatch> FindTokens(String input)
        {
            List<StringMatch> tokens = new List<StringMatch>();
            BaseVertex current = start;

            String currentToken = "";
            int line = 1;

            for (int x = 0; x < input.Length; x++)
            {
                char c = input[x];
                bool foundNext = false;
                foreach (Edge edge in current.Connections)
                {
                    if (edge.Condition == c.ToString() && edge.Connection.Name != "Empty")
                    {
                        foundNext = true;
                        current = edge.Connection;
                        currentToken += c;
                        break;
                    }
                }
                if (!foundNext || x == input.Length-1)
                {
                    if (current.Accepting)
                    {
                        StringMatch match = new StringMatch(currentToken, "", line);
                        tokens.Add(match);
                        if(x < input.Length-1)
                            x--;
                    }
                    else
                    {
                        if (c == '\n')
                            line++;
                    }
                    //invalid input
                    currentToken = "";
                    current = start;
                }
            }

            return tokens;
        }
예제 #6
0
    public void AddConditionFromParts(
        Pattern pattern,
        ParsedModRewriteInput input,
        Flags flags)
    {
        if (_conditions == null)
        {
            _conditions = new List <Condition>();
        }

        var orNext = flags.HasFlag(FlagType.Or);

        UrlMatch match;

        switch (input.ConditionType)
        {
        case ConditionType.Regex:
            Debug.Assert(input.Operand != null);
            if (flags.HasFlag(FlagType.NoCase))
            {
                match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled | RegexOptions.IgnoreCase, _regexTimeout), input.Invert);
            }
            else
            {
                match = new RegexMatch(new Regex(input.Operand, RegexOptions.CultureInvariant | RegexOptions.Compiled, _regexTimeout), input.Invert);
            }
            break;

        case ConditionType.IntComp:
            Debug.Assert(input.Operand != null);
            switch (input.OperationType)
            {
            case OperationType.Equal:
                match = new IntegerMatch(input.Operand, IntegerOperationType.Equal);
                break;

            case OperationType.Greater:
                match = new IntegerMatch(input.Operand, IntegerOperationType.Greater);
                break;

            case OperationType.GreaterEqual:
                match = new IntegerMatch(input.Operand, IntegerOperationType.GreaterEqual);
                break;

            case OperationType.Less:
                match = new IntegerMatch(input.Operand, IntegerOperationType.Less);
                break;

            case OperationType.LessEqual:
                match = new IntegerMatch(input.Operand, IntegerOperationType.LessEqual);
                break;

            case OperationType.NotEqual:
                match = new IntegerMatch(input.Operand, IntegerOperationType.NotEqual);
                break;

            default:
                throw new ArgumentException("Invalid operation for integer comparison.");
            }
            break;

        case ConditionType.StringComp:
            Debug.Assert(input.Operand != null);
            switch (input.OperationType)
            {
            case OperationType.Equal:
                match = new StringMatch(input.Operand, StringOperationType.Equal, input.Invert);
                break;

            case OperationType.Greater:
                match = new StringMatch(input.Operand, StringOperationType.Greater, input.Invert);
                break;

            case OperationType.GreaterEqual:
                match = new StringMatch(input.Operand, StringOperationType.GreaterEqual, input.Invert);
                break;

            case OperationType.Less:
                match = new StringMatch(input.Operand, StringOperationType.Less, input.Invert);
                break;

            case OperationType.LessEqual:
                match = new StringMatch(input.Operand, StringOperationType.LessEqual, input.Invert);
                break;

            default:
                throw new ArgumentException("Invalid operation for string comparison.");
            }
            break;

        default:
            switch (input.OperationType)
            {
            case OperationType.Directory:
                match = new IsDirectoryMatch(input.Invert);
                break;

            case OperationType.RegularFile:
                match = new IsFileMatch(input.Invert);
                break;

            case OperationType.ExistingFile:
                match = new IsFileMatch(input.Invert);
                break;

            case OperationType.SymbolicLink:
                // TODO see if FileAttributes.ReparsePoint works for this?
                throw new NotImplementedException("Symbolic links are not supported because " +
                                                  "of cross platform implementation");

            case OperationType.Size:
                match = new FileSizeMatch(input.Invert);
                break;

            case OperationType.ExistingUrl:
                throw new NotSupportedException("Existing Url lookups not supported because it requires a subrequest");

            case OperationType.Executable:
                throw new NotSupportedException("Executable Property is not supported because Windows " +
                                                "requires a pinvoke to get this property");

            default:
                throw new ArgumentException("Invalid operation for property comparison");
            }
            break;
        }

        var condition = new Condition(pattern, match, orNext);

        _conditions.Add(condition);
    }
예제 #7
0
        /// <summary>
        /// Compares the start of a string against all other fragment endings. Alters the stringMatch object if a match has a greater length
        /// </summary>
        static private void CheckEndOfStrings(int fragmentIndex, ref List <string> fragments, ref StringMatch stringMatch)
        {
            var fragment = fragments[fragmentIndex];

            for (var ss = fragment.Length - 1; ss > 1; ss--)
            {
                var expression = fragment.Substring(0, ss);
                if (expression.Length < stringMatch.MatchLength)
                {
                    return;                     //exit early if we've already found the largest possible match
                }
                for (var i = 0; i < fragments.Count; i++)
                {
                    if (i != fragmentIndex && !(i == stringMatch.StartStringIndex && fragmentIndex == stringMatch.EndStringIndex)) // don't compare to self or a previous match
                    {
                        var isMatch = Regex.IsMatch(fragments[i], expression + "$");                                               // does the substring match the end of another string?
                        if (isMatch)
                        {
                            Console.WriteLine("Found a match in \"" + expression + "\"");
                            if (expression.Length > stringMatch.MatchLength)                             // is a match the largest match we've seen on this run?
                            {
                                Console.WriteLine("Match is largest seen in this pass with a length of " + expression.Length + ".\n");
                                stringMatch.StartStringIndex = i;
                                stringMatch.EndStringIndex   = fragmentIndex;
                                stringMatch.SubtringIndex    = fragments[i].IndexOf(expression);
                                stringMatch.MatchLength      = expression.Length;
                            }
                            else
                            {
                                Console.WriteLine("Other matches are larger. Ignoring match this pass.\n");
                            }
                        }
                    }
                }
            }
        }
예제 #8
0
        /// <summary>
        /// Compares the end of a fragment against all other fragment beginnings. Alters the stringMatch object if a match has the greatest length
        /// </summary>
        static private void CheckStartOfStrings(int fragmentIndex, ref List <string> fragments, ref StringMatch stringMatch)
        {
            var fragment = fragments[fragmentIndex];

            for (var ss = 1; ss < fragment.Length - 1; ss++)             // start at 1 index because a full match should have already been caught in MergeCompleteMatch()
            {
                var expression = fragment.Substring(ss);
                if (expression.Length < stringMatch.MatchLength)
                {
                    return;                     //exit early if we've already found the largest possible match
                }
                for (var i = 0; i < fragments.Count - 1; i++)
                {
                    if (i != fragmentIndex && !(i == stringMatch.EndStringIndex && stringMatch.StartStringIndex == fragmentIndex)) // don't compare to self
                    {
                        var isMatch = Regex.IsMatch(fragments[i], @"^" + expression);                                              // does the substring match the start of another string?
                        if (isMatch)
                        {
                            Console.WriteLine("Found a match in \"" + expression + "\"");
                            if (expression.Length > stringMatch.MatchLength)                             // is a match the largest match we've seen on this run?
                            {
                                Console.WriteLine("Match is largest seen in this pass with a length of " + expression.Length + ".\n");
                                stringMatch.StartStringIndex = fragmentIndex;
                                stringMatch.EndStringIndex   = i;
                                stringMatch.SubtringIndex    = ss;
                                stringMatch.MatchLength      = expression.Length;
                            }
                            else
                            {
                                Console.WriteLine("Other matches are larger. Ignoring match this pass.\n");
                            }
                        }
                    }
                }
            }
        }
 public void SetUp()
 {
     mValidateCalled = false;
     mSut            = new StringMatch(new ReadOnlyCollection <IProperty>(new List <IProperty>()), Validate);
 }
예제 #10
0
 public ActionResult Search(string Algorithm, string Keyword)
 {
     if (!String.IsNullOrEmpty(Keyword) && !String.IsNullOrEmpty(Algorithm))
     {
         var          feed    = new FeedReader();
         var          news    = feed.RetrieveFeed("http://www.antaranews.com/rss/terkini");
         ArrayList    result  = new ArrayList();
         HtmlDocument htmlDoc = new HtmlDocument();
         using (var article = new HttpClient())
         {
             foreach (var i in news)
             {
                 var response = article.GetAsync((i as FeedItem).Uri).Result;
                 if (response.IsSuccessStatusCode)
                 {
                     var content = response.Content;
                     htmlDoc.LoadHtml(content.ReadAsStringAsync().Result);
                     HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//div[@id='content_news']");
                     if (nodes != null)
                     {
                         var         node       = nodes.First();
                         StringMatch testSearch = new StringMatch(node.InnerText, Keyword);
                         if (Algorithm.Equals("KMP"))
                         {
                             //search with KMP
                             //if found, replace i.Content to string that contain keyword, then add to result
                             String searchResult = testSearch.KMPsearch();
                             if (!String.IsNullOrEmpty(searchResult))
                             {
                                 i.Content = searchResult;
                                 result.Add(i as FeedItem);
                             }
                         }
                         else if (Algorithm.Equals("Booyer-Moore"))
                         {
                             //search with Booyer-Moore
                             //if found, replace i.summary to string that contain keyword, then add to result
                             String searchResult = testSearch.BoyerMooreSearch();
                             if (!String.IsNullOrEmpty(searchResult))
                             {
                                 i.Content = searchResult;
                                 result.Add(i as FeedItem);
                             }
                         }
                         else if (Algorithm.Equals("Regex"))
                         {
                             //search with Regex
                             //if found, replace i.summary to string that contain keyword, then add to result
                             String searchResult = testSearch.RegexSearch();
                             if (!String.IsNullOrEmpty(searchResult))
                             {
                                 i.Content = searchResult;
                                 result.Add(i as FeedItem);
                             }
                         }
                     }
                 }
             }
         }
         ViewBag.items = result;
     }
     return(View());
 }
예제 #11
0
 public void StringMatchTest_abc_axc_result_0()
 {
     Assert.AreEqual(0, StringMatch.ShowAnswer("abc", "axc"));
 }
예제 #12
0
 public void StringMatchTest_xxcaazz_xxbaaz_result_3()
 {
     Assert.AreEqual(3, StringMatch.ShowAnswer("xxcaazz", "xxbaaz"));
 }
예제 #13
0
        public static bool WaitForValue(JediOmniDevice device, string elementSelector, string propertyName, OmniPropertyType propertyType, string expectedPropertyValue, StringMatch match, StringComparison comparison, TimeSpan waitTimeForElement, TimeSpan waitTimeForProperty)
        {
            DateTime endTime             = DateTime.Now + waitTimeForElement;
            string   actualPropertyValue = null;
            bool     elementExists       = false;

            do
            {
                if (device.ControlPanel.CheckState(elementSelector, OmniElementState.Exists))
                {
                    if (!elementExists)
                    {
                        endTime       = DateTime.Now + waitTimeForProperty;
                        elementExists = true;
                    }

                    actualPropertyValue = device.ControlPanel.GetValue(elementSelector, propertyName, propertyType);

                    if (StringMatcher.IsMatch(expectedPropertyValue, actualPropertyValue, match, comparison))
                    {
                        return(true);
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }while (DateTime.Now < endTime);

            return(false);
        }