diff --git a/CliFx/Utils/Polyfills.Regex.cs b/CliFx/Utils/Polyfills.Regex.cs new file mode 100644 index 0000000..bd354eb --- /dev/null +++ b/CliFx/Utils/Polyfills.Regex.cs @@ -0,0 +1,12 @@ +// ReSharper disable CheckNamespace + +#if NETSTANDARD2_0 +using System.Linq; +using System.Text.RegularExpressions; + +internal static class RegexPolyfills +{ + public static Match[] ToArray(this MatchCollection matches) => + matches.Cast().ToArray(); +} +#endif \ No newline at end of file diff --git a/CliFx/Utils/Polyfills.Strings.cs b/CliFx/Utils/Polyfills.Strings.cs index b60cb88..5ba6b98 100644 --- a/CliFx/Utils/Polyfills.Strings.cs +++ b/CliFx/Utils/Polyfills.Strings.cs @@ -7,6 +7,6 @@ internal static class StringPolyfills str.Length > 0 && str[0] == c; public static bool EndsWith(this string str, char c) => - str.Length > 0 && str[str.Length - 1] == c; + str.Length > 0 && str[^1] == c; } #endif \ No newline at end of file diff --git a/CliFx/Utils/StackFrame.cs b/CliFx/Utils/StackFrame.cs index 18a193a..e97846b 100644 --- a/CliFx/Utils/StackFrame.cs +++ b/CliFx/Utils/StackFrame.cs @@ -52,33 +52,35 @@ internal partial class StackFrame private const string NotSpace = @"[^\x20\t]"; // Taken from https://github.com/atifaziz/StackTraceParser - private static readonly Regex Pattern = new(@" - ^ - " + Space + @"* - \w+ " + Space + @"+ - (? - (? " + NotSpace + @"+ ) \. - (? " + NotSpace + @"+? ) " + Space + @"* - (? \( ( " + Space + @"* \) - | (? .+?) " + Space + @"+ (? .+?) - (, " + Space + @"* (? .+?) " + Space + @"+ (? .+?) )* \) ) ) - ( " + Space + @"+ - ( # Microsoft .NET stack traces - \w+ " + Space + @"+ - (? ( [a-z] \: # Windows rooted path starting with a drive letter - | / ) # *nix rooted path starting with a forward-slash - .+? ) - \: \w+ " + Space + @"+ - (? [0-9]+ ) \p{P}? - | # Mono stack traces - \[0x[0-9a-f]+\] " + Space + @"+ \w+ " + Space + @"+ - <(? [^>]+ )> - :(? [0-9]+ ) - ) - )? - ) - \s* - $", + private static readonly Regex Pattern = new( + $$""" + ^ + {{Space}}* + \w+ {{Space}}+ + (? + (? {{NotSpace}}+ ) \. + (? {{NotSpace}}+? ) {{Space}}* + (? \( ( {{Space}}* \) + | (? .+?) {{Space}}+ (? .+?) + (, {{Space}}* (? .+?) {{Space}}+ (? .+?) )* \) ) ) + ( {{Space}}+ + ( # Microsoft .NET stack traces + \w+ {{Space}}+ + (? ( [a-z] \: # Windows rooted path starting with a drive letter + | / ) # Unix rooted path starting with a forward-slash + .+? ) + \: \w+ {{Space}}+ + (? [0-9]+ ) \p{P}? + | # Mono stack traces + \[0x[0-9a-f]+\] {{Space}}+ \w+ {{Space}}+ + <(? [^>]+ )> + :(? [0-9]+ ) + ) + )? + ) + \s* + $ + """, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.ExplicitCapture | @@ -89,7 +91,7 @@ internal partial class StackFrame public static IEnumerable ParseMany(string stackTrace) { - var matches = Pattern.Matches(stackTrace).Cast().ToArray(); + var matches = Pattern.Matches(stackTrace).ToArray(); if (matches.Length <= 0 || matches.Any(m => !m.Success)) {