More simplification

This commit is contained in:
Tyrrrz
2022-12-13 03:24:23 +02:00
parent d0ad3bc45d
commit 3d9ad16117
3 changed files with 43 additions and 29 deletions

View File

@@ -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<Match>().ToArray();
}
#endif

View File

@@ -7,6 +7,6 @@ internal static class StringPolyfills
str.Length > 0 && str[0] == c; str.Length > 0 && str[0] == c;
public static bool EndsWith(this string str, char 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 #endif

View File

@@ -52,33 +52,35 @@ internal partial class StackFrame
private const string NotSpace = @"[^\x20\t]"; private const string NotSpace = @"[^\x20\t]";
// Taken from https://github.com/atifaziz/StackTraceParser // Taken from https://github.com/atifaziz/StackTraceParser
private static readonly Regex Pattern = new(@" private static readonly Regex Pattern = new(
^ $$"""
" + Space + @"* ^
\w+ " + Space + @"+ {{Space}}*
(?<frame> \w+ {{Space}}+
(?<type> " + NotSpace + @"+ ) \. (?<frame>
(?<method> " + NotSpace + @"+? ) " + Space + @"* (?<type> {{NotSpace}}+ ) \.
(?<params> \( ( " + Space + @"* \) (?<method> {{NotSpace}}+? ) {{Space}}*
| (?<pt> .+?) " + Space + @"+ (?<pn> .+?) (?<params> \( ( {{Space}}* \)
(, " + Space + @"* (?<pt> .+?) " + Space + @"+ (?<pn> .+?) )* \) ) ) | (?<pt> .+?) {{Space}}+ (?<pn> .+?)
( " + Space + @"+ (, {{Space}}* (?<pt> .+?) {{Space}}+ (?<pn> .+?) )* \) ) )
( # Microsoft .NET stack traces ( {{Space}}+
\w+ " + Space + @"+ ( # Microsoft .NET stack traces
(?<file> ( [a-z] \: # Windows rooted path starting with a drive letter \w+ {{Space}}+
| / ) # *nix rooted path starting with a forward-slash (?<file> ( [a-z] \: # Windows rooted path starting with a drive letter
.+? ) | / ) # Unix rooted path starting with a forward-slash
\: \w+ " + Space + @"+ .+? )
(?<line> [0-9]+ ) \p{P}? \: \w+ {{Space}}+
| # Mono stack traces (?<line> [0-9]+ ) \p{P}?
\[0x[0-9a-f]+\] " + Space + @"+ \w+ " + Space + @"+ | # Mono stack traces
<(?<file> [^>]+ )> \[0x[0-9a-f]+\] {{Space}}+ \w+ {{Space}}+
:(?<line> [0-9]+ ) <(?<file> [^>]+ )>
) :(?<line> [0-9]+ )
)? )
) )?
\s* )
$", \s*
$
""",
RegexOptions.IgnoreCase | RegexOptions.IgnoreCase |
RegexOptions.Multiline | RegexOptions.Multiline |
RegexOptions.ExplicitCapture | RegexOptions.ExplicitCapture |
@@ -89,7 +91,7 @@ internal partial class StackFrame
public static IEnumerable<StackFrame> ParseMany(string stackTrace) public static IEnumerable<StackFrame> ParseMany(string stackTrace)
{ {
var matches = Pattern.Matches(stackTrace).Cast<Match>().ToArray(); var matches = Pattern.Matches(stackTrace).ToArray();
if (matches.Length <= 0 || matches.Any(m => !m.Success)) if (matches.Length <= 0 || matches.Any(m => !m.Success))
{ {