14 public static IEnumerable<string> DoubleQuoteIfNeeded(
this IEnumerable<object>
self)
15 =>
self?.Select(s => s.ToString().DoubleQuoteIfNeeded()) ?? Enumerable.Empty<
string>();
24 public static Func<string, string?>
Parse(
27 RegexOptions options = RegexOptions.None,
28 bool forceNullOnEmpty =
false,
29 bool forceNullOnWhitespce =
false
30 ) =>
Parse(input,
new Regex(pattern, options), forceNullOnEmpty, forceNullOnWhitespce);
39 public static Func<string, string?>
Parse(
42 bool forceNullOnEmpty =
false,
43 bool forceNullOnWhitespce =
false
45 if (input ==
null)
return i =>
null;
47 var groups = pattern.Matches(input)?.FirstOrDefault()?.Groups;
48 return i => forceNullOnEmpty || forceNullOnWhitespce
49 ? groups?[i]?.Value.Else(ignoreWhitespace: forceNullOnWhitespce)
56 public static string AsSingleLine(
this string input,
string replaceWith =
" ")
57 => input.Replace(Environment.NewLine, replaceWith).Replace(
"\n", replaceWith);
64 public static string GlobToRegex(
this string glob) => Regex.Escape(glob)
70 .ReplaceRegex(
@"(?<PRE>[^\*]|^)\*(?<POST>[^\*])", m =>
$@"{m.Groups["PRE
"]}([^\/]*){m.Groups["POST
"]}")
72 .ReplaceRegex(
@"(?<PRE>[^\*]|^)\*$", m =>
$@"{m.Groups["PRE
"]}([^\/]*)")
74 .ReplaceRegex(
@"^\*\*", m =>
@"^(.*)")
76 .ReplaceRegex(
@"(?:\\\\|\\\/)\*\*", m =>
@"[\\\/]?(.*)");
82 =>
string.IsNullOrWhiteSpace(
self) ?
"" :
self + other;
88 =>
string.IsNullOrWhiteSpace(
self) ?
"" : other +
self;
101 public static string?
Else(
this string?
self,
string? def =
null,
bool ignoreWhitespace =
true)
102 => (
string.IsNullOrWhiteSpace(
self) && ignoreWhitespace) ||
string.IsNullOrEmpty(
self)
static Func< string, string?> Parse(this string? input, Regex pattern, bool forceNullOnEmpty=false, bool forceNullOnWhitespce=false)
Shorthand for one-liner regex parsing from named captures. Allows to use precompiled pattern.
static string PrependNonEmpty(this string? self, string other)
Convenience, prepend a piece of string to an input string only if input string is non-null and non-em...
static Func< string, string?> Parse(this string? input, string pattern, RegexOptions options=RegexOptions.None, bool forceNullOnEmpty=false, bool forceNullOnWhitespce=false)
Shorthand for one-liner regex parsing from named captures.
static ? string Else(this string? self, string? def=null, bool ignoreWhitespace=true)
Defer to a default value if string is null or empty or whitespace only.
static string AppendNonEmpty(this string? self, string other)
Convenience, append a piece of string to an input string only if input string is non-null and non-emp...
static string AsSingleLine(this string input, string replaceWith=" ")
Simple convenience function for replacing new lines with other string (space by default)
static string GlobToRegex(this string glob)
Converts a glob expression into a Regex expression with captures.