mirror of
https://github.com/Tyrrrz/DiscordChatExporter.git
synced 2026-03-16 20:02:42 +00:00
Refactor using c# 12 features
This commit is contained in:
@@ -2,15 +2,8 @@
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing;
|
||||
|
||||
internal class AggregateMatcher<T> : IMatcher<T>
|
||||
internal class AggregateMatcher<T>(IReadOnlyList<IMatcher<T>> matchers) : IMatcher<T>
|
||||
{
|
||||
private readonly IReadOnlyList<IMatcher<T>> _matchers;
|
||||
|
||||
public AggregateMatcher(IReadOnlyList<IMatcher<T>> matchers)
|
||||
{
|
||||
_matchers = matchers;
|
||||
}
|
||||
|
||||
public AggregateMatcher(params IMatcher<T>[] matchers)
|
||||
: this((IReadOnlyList<IMatcher<T>>)matchers) { }
|
||||
|
||||
@@ -19,7 +12,7 @@ internal class AggregateMatcher<T> : IMatcher<T>
|
||||
ParsedMatch<T>? earliestMatch = null;
|
||||
|
||||
// Try to match the input with each matcher and get the match with the lowest start index
|
||||
foreach (var matcher in _matchers)
|
||||
foreach (var matcher in matchers)
|
||||
{
|
||||
// Try to match
|
||||
var match = matcher.TryMatch(segment);
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing;
|
||||
|
||||
internal class ParsedMatch<T>
|
||||
internal class ParsedMatch<T>(StringSegment segment, T value)
|
||||
{
|
||||
public StringSegment Segment { get; }
|
||||
public StringSegment Segment { get; } = segment;
|
||||
|
||||
public T Value { get; }
|
||||
|
||||
public ParsedMatch(StringSegment segment, T value)
|
||||
{
|
||||
Segment = segment;
|
||||
Value = value;
|
||||
}
|
||||
public T Value { get; } = value;
|
||||
}
|
||||
|
||||
@@ -3,20 +3,11 @@ using System.Text.RegularExpressions;
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing;
|
||||
|
||||
internal class RegexMatcher<T> : IMatcher<T>
|
||||
internal class RegexMatcher<T>(Regex regex, Func<StringSegment, Match, T?> transform) : IMatcher<T>
|
||||
{
|
||||
private readonly Regex _regex;
|
||||
private readonly Func<StringSegment, Match, T?> _transform;
|
||||
|
||||
public RegexMatcher(Regex regex, Func<StringSegment, Match, T?> transform)
|
||||
{
|
||||
_regex = regex;
|
||||
_transform = transform;
|
||||
}
|
||||
|
||||
public ParsedMatch<T>? TryMatch(StringSegment segment)
|
||||
{
|
||||
var match = _regex.Match(segment.Source, segment.StartIndex, segment.Length);
|
||||
var match = regex.Match(segment.Source, segment.StartIndex, segment.Length);
|
||||
if (!match.Success)
|
||||
return null;
|
||||
|
||||
@@ -25,11 +16,11 @@ internal class RegexMatcher<T> : IMatcher<T>
|
||||
// Which is super weird because regex.Match(string, int) takes the whole input in context.
|
||||
// So in order to properly account for ^/$ regex tokens, we need to make sure that
|
||||
// the expression also matches on the bigger part of the input.
|
||||
if (!_regex.IsMatch(segment.Source[..segment.EndIndex], segment.StartIndex))
|
||||
if (!regex.IsMatch(segment.Source[..segment.EndIndex], segment.StartIndex))
|
||||
return null;
|
||||
|
||||
var segmentMatch = segment.Relocate(match);
|
||||
var value = _transform(segmentMatch, match);
|
||||
var value = transform(segmentMatch, match);
|
||||
|
||||
return value is not null ? new ParsedMatch<T>(segmentMatch, value) : null;
|
||||
}
|
||||
|
||||
@@ -2,37 +2,24 @@
|
||||
|
||||
namespace DiscordChatExporter.Core.Markdown.Parsing;
|
||||
|
||||
internal class StringMatcher<T> : IMatcher<T>
|
||||
internal class StringMatcher<T>(
|
||||
string needle,
|
||||
StringComparison comparison,
|
||||
Func<StringSegment, T?> transform
|
||||
) : IMatcher<T>
|
||||
{
|
||||
private readonly string _needle;
|
||||
private readonly StringComparison _comparison;
|
||||
private readonly Func<StringSegment, T?> _transform;
|
||||
|
||||
public StringMatcher(
|
||||
string needle,
|
||||
StringComparison comparison,
|
||||
Func<StringSegment, T?> transform
|
||||
)
|
||||
{
|
||||
_needle = needle;
|
||||
_comparison = comparison;
|
||||
_transform = transform;
|
||||
}
|
||||
|
||||
public StringMatcher(string needle, Func<StringSegment, T> transform)
|
||||
: this(needle, StringComparison.Ordinal, transform) { }
|
||||
|
||||
public ParsedMatch<T>? TryMatch(StringSegment segment)
|
||||
{
|
||||
var index = segment
|
||||
.Source
|
||||
.IndexOf(_needle, segment.StartIndex, segment.Length, _comparison);
|
||||
var index = segment.Source.IndexOf(needle, segment.StartIndex, segment.Length, comparison);
|
||||
|
||||
if (index < 0)
|
||||
return null;
|
||||
|
||||
var segmentMatch = segment.Relocate(index, _needle.Length);
|
||||
var value = _transform(segmentMatch);
|
||||
var segmentMatch = segment.Relocate(index, needle.Length);
|
||||
var value = transform(segmentMatch);
|
||||
|
||||
return value is not null ? new ParsedMatch<T>(segmentMatch, value) : null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user