diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d380e92..26ccfbe 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,6 +4,7 @@ image: mcr.microsoft.com/dotnet/sdk:10.0 variables: TZ: Europe/Berlin LANG: de + GIT_DEPTH: 0 stages: - build diff --git a/CHANGELOG.md b/CHANGELOG.md index fc33a81..f7c777b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `DomainComparer` ordering alphabetically from TLD to sub-domain - `IPAddressComparer` able to compare IPv4 and IPv6 - `VersionStringComparer` to compare version strings (SemVer) +- `ContainsAny()` and `ContainsAll()` for strings - `ASPNETCORE_APPL_PROXY` environment variable can be used on proxy configuration - Support for .NET 10.0 LTS diff --git a/src/AMWD.Common/Extensions/StringExtensions.cs b/src/AMWD.Common/Extensions/StringExtensions.cs index 4952ce4..77720c2 100644 --- a/src/AMWD.Common/Extensions/StringExtensions.cs +++ b/src/AMWD.Common/Extensions/StringExtensions.cs @@ -253,6 +253,96 @@ namespace System public static StringBuilder AppendLine(this StringBuilder sb, string value, string newLine) => sb.Append(value).Append(newLine); + /// + /// Returns a value indicating whether any of the specified strings occurs within this string. + /// + /// The string to search within. Cannot be null. + /// One of the strings to seek. + /// + /// if any of the parameters occur within this string, or if any of the is the empty string (""); + /// otherwise, . + /// + public static bool ContainsAny(this string str, params string[] values) + => ContainsAny(str, StringComparison.CurrentCulture, values); + + /// + /// Returns a value indicating whether any of the specified strings occurs within this string, using the specified comparison rules. + /// + /// The string to search within. Cannot be null. + /// One of the enumeration values that specifies the rules for the comparison. + /// One of the strings to seek. + /// + /// if any of the parameters occur within this string, or if any of the is the empty string (""); + /// otherwise, . + /// + public static bool ContainsAny(this string str, StringComparison comparisonType, params string[] values) + { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(str, nameof(str)); + ArgumentNullException.ThrowIfNull(values, nameof(values)); +#else + if (str == null) + throw new ArgumentNullException(nameof(str)); + + if (values == null) + throw new ArgumentNullException(nameof(values)); +#endif + + string[] compareValues = values.Distinct().ToArray(); + foreach (string value in compareValues) + { + if (str.Contains(value, comparisonType)) + return true; + } + + return false; + } + + /// + /// Returns a value indicating whether all of the specified strings occurs within this string. + /// + /// The string to search within. Cannot be null. + /// All the strings to seek. + /// + /// if all of the parameters occur within this string, or if all of the are the empty string (""); + /// otherwise, . + /// + public static bool ContainsAll(this string str, params string[] values) + => ContainsAll(str, StringComparison.CurrentCulture, values); + + /// + /// Returns a value indicating whether all of the specified strings occurs within this string, using the specified comparison rules. + /// + /// The string to search within. Cannot be null. + /// One of the enumeration values that specifies the rules for the comparison. + /// All the strings to seek. + /// + /// if all of the parameters occur within this string, or if all of the are the empty string (""); + /// otherwise, . + /// + public static bool ContainsAll(this string str, StringComparison comparisonType, params string[] values) + { +#if NET8_0_OR_GREATER + ArgumentNullException.ThrowIfNull(str, nameof(str)); + ArgumentNullException.ThrowIfNull(values, nameof(values)); +#else + if (str == null) + throw new ArgumentNullException(nameof(str)); + + if (values == null) + throw new ArgumentNullException(nameof(values)); +#endif + + string[] compareValues = values.Distinct().ToArray(); + foreach (string value in compareValues) + { + if (!str.Contains(value, comparisonType)) + return false; + } + + return true; + } + #if NET8_0_OR_GREATER [GeneratedRegex(EmailRegex, RegexOptions.Compiled)] private static partial Regex ValidEmailRegex();