diff --git a/source/Nuke.Utilities/Collections/Enumerable.ToEmptyIfNull.cs b/source/Nuke.Utilities/Collections/Enumerable.ToEmptyIfNull.cs new file mode 100644 index 000000000..131d204a0 --- /dev/null +++ b/source/Nuke.Utilities/Collections/Enumerable.ToEmptyIfNull.cs @@ -0,0 +1,25 @@ +// Copyright 2024 Maintainers of NUKE. +// Distributed under the MIT License. +// https://github.com/nuke-build/nuke/blob/master/LICENSE + +using System.Collections.Generic; + +namespace Nuke.Common.Utilities.Collections; + +public static partial class EnumerableExtensions +{ + public static IEnumerable ToEmptyIfNull(this IEnumerable enumerable) + { + return enumerable ?? []; + } + + public static T[] ToEmptyIfNull(this T[] array) + { + return array ?? []; + } + + public static IList ToEmptyIfNull(this IList list) + { + return list ?? []; + } +} diff --git a/source/Nuke.Utilities/Text/String.Emptiness.cs b/source/Nuke.Utilities/Text/String.Emptiness.cs index d25cfca3c..f48cfe61c 100644 --- a/source/Nuke.Utilities/Text/String.Emptiness.cs +++ b/source/Nuke.Utilities/Text/String.Emptiness.cs @@ -19,7 +19,7 @@ public static bool IsNullOrEmpty(this string str) } /// - /// Indicates whether a specified string is null, empty, or consists only of white-space characters. + /// Indicates whether a specified string is null, empty, or only white-space. /// [Pure] [ContractAnnotation("null => halt")] @@ -27,4 +27,24 @@ public static bool IsNullOrWhiteSpace(this string str) { return string.IsNullOrWhiteSpace(str); } + + /// + /// Returns null if the specified string is empty. + /// + [Pure] + [ContractAnnotation("null => null")] + public static string ToNullIfEmpty(this string str) + { + return str.IsNullOrEmpty() ? null : str; + } + + /// + /// Returns null if the specified string is empty or only white-space. + /// + [Pure] + [ContractAnnotation("null => null")] + public static string ToNullIfWhiteSpace(this string str) + { + return str.IsNullOrWhiteSpace() ? null : str; + } } diff --git a/source/Nuke.Utilities/Text/String.Split.cs b/source/Nuke.Utilities/Text/String.Split.cs index 602e64121..3c1f72758 100644 --- a/source/Nuke.Utilities/Text/String.Split.cs +++ b/source/Nuke.Utilities/Text/String.Split.cs @@ -64,9 +64,18 @@ public static IEnumerable SplitCamelHumps(this string str, params string /// Splits a given string by new-lines with empty entries preserved. /// [Pure] - public static string[] SplitLineBreaks(this string str) + public static string[] SplitLineBreaks(this string str, StringSplitOptions options = StringSplitOptions.None) { - return str.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); + return str.Split(new[] { "\r\n", "\n" }, options); + } + + /// + /// Splits a given string by paragraphs (double new-line) with empty entries preserved. + /// + [Pure] + public static string[] SplitParagraphs(this string str, StringSplitOptions options = StringSplitOptions.None) + { + return str.Split(new[] { "\r\n\r\n", "\n\n" }, options); } /// diff --git a/source/Nuke.Utilities/Text/String.StartsEndsContains.cs b/source/Nuke.Utilities/Text/String.StartsEndsContains.cs index 31f2799f0..495fb676d 100644 --- a/source/Nuke.Utilities/Text/String.StartsEndsContains.cs +++ b/source/Nuke.Utilities/Text/String.StartsEndsContains.cs @@ -20,6 +20,15 @@ public static bool ContainsOrdinalIgnoreCase(this string str, string other) return str.IndexOf(other, StringComparison.OrdinalIgnoreCase) >= 0; } + /// + /// Indicates whether a collection of strings contains any other string under comparison. + /// + [Pure] + public static bool ContainsAnyOrdinalIgnoreCase(this IEnumerable str, params string[] others) + { + return others.Any(x => str.Contains(x, StringComparer.OrdinalIgnoreCase)); + } + /// /// Indicates whether a string equals another string under comparison. ///