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); }
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())); } }
/// <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}"); } }
/// <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); } }
/// <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); } }
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); } }
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); } }
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)); } } }
/// <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); }
public ContextContainsKeyMatcher(string keyName, StringMatchType matchType) { KeyName = keyName; MatchType = matchType; }
public HostAddressMatcher(string expectedHostAddress, StringMatchType hostAddressMatchType) { ExpectedHostAddress = expectedHostAddress; HostAddressMatchType = hostAddressMatchType; }
internal static extern int AddString(IntPtr filterHandle, uint propertyId, StringMatchType match, string value);
internal static extern int ContactsFilterAddStr(IntPtr filterHandle, uint propertyId, StringMatchType match, string matchValue);
public StringMatcher(string match, string matchTo, StringMatchType matchType) { Match = match; MatchTo = matchTo; MatchType = matchType; }
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; }
public StringFilter(StringMatchType matchType, string pattern, bool ignoreCase = false) { this.matchType = matchType; this.pattern = pattern; this.ignoreCase = ignoreCase; }