Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions src/Gemstone/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,6 @@

namespace Gemstone;

#region [ Enumerations ]

/// <summary>
/// Indicates the type of update.
/// </summary>
public enum UpdateType
{
/// <summary>
/// Update is informational.
/// </summary>
Information,
/// <summary>
/// Update is a warning.
/// </summary>
Warning,
/// <summary>
/// Update is an alarm.
/// </summary>
Alarm
}

#endregion

/// <summary>
/// Defines common global functions.
/// </summary>
Expand Down
70 changes: 70 additions & 0 deletions src/Gemstone/StringExtensions/StringMatcher/StringMatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Text.RegularExpressions;

namespace Gemstone.StringExtensions.StringMatcher;

/// <summary>
/// Represents a string matcher that can perform various types of string matching operations.
/// </summary>
public class StringMatcher
{
/// <summary>
/// Gets the <see cref="StringMatchingMode"/> used.
/// </summary>
public StringMatchingMode MatchMode { get; }

/// <summary>
/// Gets the string that is being matched.
/// </summary>
public string MatchText { get; }

/// <summary>
/// Gets a flag indicating whether the match is case sensitive.
/// </summary>
public bool CaseSensitive { get; }

private readonly Regex? m_matchRegex;

/// <summary>
/// Generates a new <see cref="StringMatcher"/> instance.
/// </summary>
/// <param name="mode"> Sets the <see cref="StringMatchingMode"/> used to match.</param>
/// <param name="value"> The string being matched. </param>
/// <param name="caseSensitive">A flag indicating if the match is case sensitive. </param>
public StringMatcher(StringMatchingMode mode, string value, bool caseSensitive = false)
{
MatchMode = mode;
MatchText = value;
CaseSensitive = caseSensitive;

RegexOptions options = (caseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase) | RegexOptions.Compiled;

m_matchRegex = MatchMode switch
{
StringMatchingMode.Regex => new Regex(MatchText, options),
StringMatchingMode.Contains => new Regex(Regex.Escape(MatchText), options),
_ => null
};
}

/// <summary>
/// Compares the specified <paramref name="value"/> against the match criteria.
/// </summary>
/// <param name="value">The string that is being matched.</param>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public bool IsMatch(string value)
{
StringComparison comparison = CaseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

return MatchMode switch
{
StringMatchingMode.Exact => value.Equals(MatchText, comparison),
StringMatchingMode.StartsWith => value.StartsWith(MatchText, comparison),
StringMatchingMode.EndsWith => value.EndsWith(MatchText, comparison),
StringMatchingMode.Contains => m_matchRegex?.IsMatch(value) ?? false,
StringMatchingMode.Regex => m_matchRegex?.IsMatch(value) ?? false,
_ => throw new ArgumentOutOfRangeException()
};
}
}
28 changes: 28 additions & 0 deletions src/Gemstone/StringExtensions/StringMatcher/StringMatchingMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Gemstone.StringExtensions.StringMatcher;

/// <summary>
/// Defines a set of string matching modes.
/// </summary>
public enum StringMatchingMode
{
/// <summary>
/// Exact string matching.
/// </summary>
Exact,
/// <summary>
/// String starts with matching.
/// </summary>
StartsWith,
/// <summary>
/// String ends with matching.
/// </summary>
EndsWith,
/// <summary>
/// String contains matching.
/// </summary>
Contains,
/// <summary>
/// Regular expression string matching.
/// </summary>
Regex
}
Loading