Beispiel #1
0
        public static bool IsMatch(this string str, StringMatchType matchType, string matchPattern, bool ignoreCase = false)
        {
            if (str == null)
            {
                return(false);
            }

            var comparisonType = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.CurrentCulture;

            switch (matchType)
            {
            case StringMatchType.Equals:
                return(string.Equals(str, matchPattern, comparisonType));

            case StringMatchType.Contains:
                return(str.Contains(matchPattern, comparisonType));

            case StringMatchType.StartsWith:
                return(str.StartsWith(matchPattern, comparisonType));

            case StringMatchType.EndsWith:
                return(str.EndsWith(matchPattern, comparisonType));

            case StringMatchType.Wildcard:
                return(Regex.Match(str, matchPattern.WildcardToRegex(), ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None).Success);

            case StringMatchType.Regex:
                return(Regex.Match(str, matchPattern, ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None).Success);
            }

            return(false);
        }
Beispiel #2
0
        protected bool MatchString(string sLeft, string sRight, StringMatchType matchType = StringMatchType.Exact)
        {
            var Left  = sLeft.ToLowerInvariant();
            var Right = sRight.ToLowerInvariant();

            // dump matching logic
            switch (matchType)
            {
            case StringMatchType.Exact:
                return(Left == Right);

            case StringMatchType.Contains:
                return(Left.Contains(Right));

            case StringMatchType.StartsWith:
                return(Left.StartsWith(Right));

            case StringMatchType.UriHostandPortMatch:
            {
                var LeftUri = new Uri(Left);
                if (sRight.Contains(":"))
                {
                    return(string.Concat(LeftUri.Host, ":", LeftUri.Port) == Right);
                }
                else
                {
                    return(LeftUri.Host == Right);
                }
            }

            case StringMatchType.UriHostNameMatch:
            {
                var LeftUri = new Uri(Left);
                return(LeftUri.Host == Right);
            }

            case StringMatchType.UriHostPortMatch:
            {
                var LeftUri = new Uri(Left);
                return(LeftUri.Port.ToString() == Right);
            }


            // for RegEx we don't lower the variable
            case StringMatchType.RegExpression:
                return((new Regex(sRight)).Match(sLeft).Success);

            default:
                throw new InvalidOperationException(string.Format("string matcher encountered unknown string match type:{0}", matchType.ToString()));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StringMatchRule"/> struct.
        /// </summary>
        /// <param name="pattern">
        /// The pattern.
        /// </param>
        /// <param name="type">
        /// The type that determines how to apply the pattern.
        /// </param>
        public StringMatchRule(string pattern, StringMatchType type)
        {
            this.pattern = pattern ?? string.Empty;

            switch (type)
            {
            case StringMatchType.Exact:
            case StringMatchType.Prefix:
            case StringMatchType.Suffix:
                this.type = type;
                break;

            default:
                throw new InvalidOperationException(
                          $"Unknown match type {type}");
            }
        }
Beispiel #4
0
        /// <summary>
        /// Adds a condition for a string type property.
        /// </summary>
        /// <param name="logicalOperator">The operator type.</param>
        /// <param name="propertyId">The property ID to add a condition.</param>
        /// <param name="matchType">The match flag.</param>
        /// <param name="matchValue">The match value.</param>
        /// <feature>http://tizen.org/feature/contact</feature>
        /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
        /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
        /// <since_tizen> 4 </since_tizen>
        public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue)
        {
            int error = Interop.Filter.ContactsFilterAddOperator(_filterHandle, logicalOperator);

            if ((int)ContactsError.None != error)
            {
                Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
                throw ContactsErrorFactory.CheckAndCreateException(error);
            }

            error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue);
            if ((int)ContactsError.None != error)
            {
                Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
                throw ContactsErrorFactory.CheckAndCreateException(error);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Adds a condition for the string type.
        /// </summary>
        /// <since_tizen> 4 </since_tizen>
        /// <feature>http://tizen.org/feature/calendar</feature>
        /// <param name="logicalOperator">The operator type.</param>
        /// <param name="propertyId">The property ID to add a condition.</param>
        /// <param name="matchType">The match flag.</param>
        /// <param name="matchValue">The match value.</param>
        /// <exception cref="NotSupportedException">Thrown when the feature is not supported.</exception>
        /// <exception cref="ArgumentException">Thrown when one of the arguments provided to a method is not valid.</exception>
        public void AddCondition(LogicalOperator logicalOperator, uint propertyId, StringMatchType matchType, string matchValue)
        {
            int error = Interop.Filter.AddOperator(_filterHandle, logicalOperator);

            if (CalendarError.None != (CalendarError)error)
            {
                Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
                throw CalendarErrorFactory.GetException(error);
            }

            error = Interop.Filter.AddString(_filterHandle, propertyId, matchType, matchValue);
            if (CalendarError.None != (CalendarError)error)
            {
                Log.Error(Globals.LogTag, "AddCondition Failed with error " + error);
                throw CalendarErrorFactory.GetException(error);
            }
        }
Beispiel #6
0
        public ContactsFilter(string viewUri, uint propertyId, StringMatchType matchType, string matchValue)
        {
            int error = Interop.Filter.ContactsFilterCreate(viewUri, out _filterHandle);

            if ((int)ContactsError.None != error)
            {
                Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
                throw ContactsErrorFactory.CheckAndCreateException(error);
            }

            error = Interop.Filter.ContactsFilterAddStr(_filterHandle, propertyId, matchType, matchValue);
            if ((int)ContactsError.None != error)
            {
                Interop.Filter.ContactsFilterDestroy(_filterHandle);
                Log.Error(Globals.LogTag, "ContactsFilter Failed with error " + error);
                throw ContactsErrorFactory.CheckAndCreateException(error);
            }
        }
Beispiel #7
0
        public CalendarFilter(string viewUri, uint propertyId, StringMatchType matchType, string matchValue)
        {
            int error = 0;

            error = Interop.Filter.Create(viewUri, out _filterHandle);
            if (CalendarError.None != (CalendarError)error)
            {
                Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
                throw CalendarErrorFactory.GetException(error);
            }

            error = Interop.Filter.AddString(_filterHandle, propertyId, matchType, matchValue);
            if (CalendarError.None != (CalendarError)error)
            {
                Interop.Filter.Destroy(_filterHandle);
                Log.Error(Globals.LogTag, "CalendarFilter Failed with error " + error);
                throw CalendarErrorFactory.GetException(error);
            }
        }
Beispiel #8
0
        public static bool IsMatch(this string text, string pattern, StringMatchType matchType, bool ignoreCase)
        {
            StringComparison comparison = StringComparison.Ordinal;

            if (ignoreCase)
            {
                comparison = StringComparison.OrdinalIgnoreCase;
            }
            switch (matchType)
            {
            default:
            case StringMatchType.Equals:
                return(string.Equals(text, pattern, comparison));

            case StringMatchType.StartWith:
                return(text.StartsWith(pattern, comparison));

            case StringMatchType.EndWith:
                return(text.EndsWith(pattern, comparison));

            case StringMatchType.Contains:
                return(text.IndexOf(pattern, comparison) >= 0);

            case StringMatchType.RegexMatch:
                RegexOptions options = RegexOptions.None;
                if (ignoreCase)
                {
                    options = RegexOptions.IgnoreCase;
                }
                return(Regex.IsMatch(text, pattern, options));

            case StringMatchType.WildCharMatch:
                StringComparison sc = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
                if (!pattern.StartsWith("*"))
                {
                    int stop = pattern.IndexOf('*');
                    if (!text.StartsWith(pattern.Substring(0, stop), sc))
                    {
                        return(false);
                    }
                }

                if (!pattern.EndsWith("*"))
                {
                    int start = pattern.LastIndexOf('*') + 1;
                    if (!text.EndsWith(pattern.Substring(start, pattern.Length - start), sc))
                    {
                        return(false);
                    }
                }

                string regex = pattern.Replace(@".", @"\.").Replace(@"*", @".*");
                if (ignoreCase)
                {
                    return(Regex.IsMatch(text, regex, RegexOptions.IgnoreCase));
                }
                else
                {
                    return(Regex.IsMatch(text, regex));
                }
            }
        }
Beispiel #9
0
        /// <summary>
        /// Count the number of instances of a string in the specified file.
        /// Each line in the file is matched with the specified string and match type; if a match is made, the count is incremented.
        /// </summary>
        /// <param name="filePath">Path of file to evaluate.</param>
        /// <param name="match">String to count.</param>
        /// <param name="matchType">Method with which to match the string. Default is StringMatchType.StartsWith.</param>
        /// <returns>Number of string instances found. Negative value means an exception was raised.</returns>
        private int GetNumberOfStringInstancesInFile(string filePath, string match, StringMatchType matchType)
        {
            int count = 0;

            try
            {
                StreamReader file = new StreamReader(filePath);
                string       curLine;

                while ((curLine = file.ReadLine()) != null)
                {
                    switch (matchType)
                    {
                    case StringMatchType.StartsWith:
                        if (curLine.StartsWith(match))
                        {
                            count++;
                        }
                        break;

                    case StringMatchType.EndsWith:
                        if (curLine.EndsWith(match))
                        {
                            count++;
                        }
                        break;

                    case StringMatchType.Contains:
                        if (curLine.Contains(match))
                        {
                            count++;
                        }
                        break;

                    default:
                        if (curLine.StartsWith(match))
                        {
                            count++;
                        }
                        break;
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                var msg = string.Format("FileNotFoundException: File not found at path: {0}. Reason: {1}", filePath, ex.Message);
                Trace.WriteLine(msg);
                MessageBox.Show(this, "File not found at the specified path.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(-1);
            }
            catch (DirectoryNotFoundException ex)
            {
                var msg = string.Format("DirectoryNotFoundException: Directory not found at path: {0}. Reason: {1}", filePath, ex.Message);
                Trace.WriteLine(msg);
                MessageBox.Show(this, "Directory not found at the specified path.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(-2);
            }
            catch (IOException ex)
            {
                var msg = string.Format("IOException: Failed to read file at path: {0}. Reason: {1}", filePath, ex.Message);
                Trace.WriteLine(msg);
                MessageBox.Show(this, "Failed to read file at path. Reason: \n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(-3);
            }
            catch (ArgumentNullException ex)
            {
                Trace.WriteLine(ex.Message);
                throw;
            }

            return(count);
        }
Beispiel #10
0
 public ContextContainsKeyMatcher(string keyName, StringMatchType matchType)
 {
     KeyName   = keyName;
     MatchType = matchType;
 }
Beispiel #11
0
 public HostAddressMatcher(string expectedHostAddress, StringMatchType hostAddressMatchType)
 {
     ExpectedHostAddress  = expectedHostAddress;
     HostAddressMatchType = hostAddressMatchType;
 }
Beispiel #12
0
 internal static extern int AddString(IntPtr filterHandle, uint propertyId, StringMatchType match, string value);
Beispiel #13
0
 internal static extern int ContactsFilterAddStr(IntPtr filterHandle, uint propertyId, StringMatchType match, string matchValue);
Beispiel #14
0
 public StringMatcher(string match, string matchTo, StringMatchType matchType)
 {
     Match     = match;
     MatchTo   = matchTo;
     MatchType = matchType;
 }
Beispiel #15
0
 public StringOption(string v, StringMatchType t)
 {
     Value     = v;
     MatchType = t;
 }
 public ContextValueStringMatcher(string keyName, string expectedValue, StringMatchType matchType) : base(keyName, StringMatchType.Exact)
 {
     KeyName        = keyName;
     ExpectedValue  = expectedValue;
     ValueMatchType = matchType;
 }
Beispiel #17
0
 public StringFilter(StringMatchType matchType, string pattern, bool ignoreCase = false)
 {
     this.matchType  = matchType;
     this.pattern    = pattern;
     this.ignoreCase = ignoreCase;
 }