Make StringPart readonly struct

This commit is contained in:
Alexey Golub
2019-12-07 20:16:53 +02:00
parent 2a223599f9
commit 964317dac0
5 changed files with 27 additions and 29 deletions

View File

@@ -1,20 +1,10 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Markdown.Internal
{
internal static class Extensions
{
public static StringPart Shrink(this StringPart stringPart, int newStartIndex, int newLength) =>
new StringPart(stringPart.Target, newStartIndex, newLength);
public static StringPart Shrink(this StringPart stringPart, int newStartIndex) =>
stringPart.Shrink(newStartIndex, stringPart.EndIndex - newStartIndex);
public static StringPart Shrink(this StringPart stringPart, Capture capture) =>
stringPart.Shrink(capture.Index, capture.Length);
public static IEnumerable<ParsedMatch<T>> MatchAll<T>(this IMatcher<T> matcher, StringPart stringPart,
Func<StringPart, T> fallbackTransform)
{
@@ -23,7 +13,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
while (currentIndex < stringPart.EndIndex)
{
// Find a match within this segment
var match = matcher.Match(stringPart.Shrink(currentIndex, stringPart.EndIndex - currentIndex));
var match = matcher.Match(stringPart.Slice(currentIndex, stringPart.EndIndex - currentIndex));
// If there's no match - break
if (match == null)
@@ -32,7 +22,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
// If this match doesn't start immediately at current index - transform and yield fallback first
if (match.StringPart.StartIndex > currentIndex)
{
var fallbackPart = stringPart.Shrink(currentIndex, match.StringPart.StartIndex - currentIndex);
var fallbackPart = stringPart.Slice(currentIndex, match.StringPart.StartIndex - currentIndex);
yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart));
}
@@ -46,7 +36,7 @@ namespace DiscordChatExporter.Core.Markdown.Internal
// If EOL wasn't reached - transform and yield remaining part as fallback
if (currentIndex < stringPart.EndIndex)
{
var fallbackPart = stringPart.Shrink(currentIndex);
var fallbackPart = stringPart.Slice(currentIndex);
yield return new ParsedMatch<T>(fallbackPart, fallbackTransform(fallbackPart));
}
}

View File

@@ -33,8 +33,8 @@ namespace DiscordChatExporter.Core.Markdown.Internal
if (!_regex.IsMatch(stringPart.Target.Substring(0, stringPart.EndIndex), stringPart.StartIndex))
return null;
var stringPartShrunk = stringPart.Shrink(match.Index, match.Length);
return new ParsedMatch<T>(stringPartShrunk, _transform(stringPartShrunk, match));
var stringPartMatch = stringPart.Slice(match.Index, match.Length);
return new ParsedMatch<T>(stringPartMatch, _transform(stringPartMatch, match));
}
}
}

View File

@@ -26,8 +26,8 @@ namespace DiscordChatExporter.Core.Markdown.Internal
if (index >= 0)
{
var stringPartShrunk = stringPart.Shrink(index, _needle.Length);
return new ParsedMatch<T>(stringPartShrunk, _transform(stringPartShrunk));
var stringPartMatch = stringPart.Slice(index, _needle.Length);
return new ParsedMatch<T>(stringPartMatch, _transform(stringPartMatch));
}
return null;

View File

@@ -1,6 +1,8 @@
namespace DiscordChatExporter.Core.Markdown.Internal
using System.Text.RegularExpressions;
namespace DiscordChatExporter.Core.Markdown.Internal
{
internal class StringPart
internal readonly struct StringPart
{
public string Target { get; }
@@ -23,6 +25,12 @@
{
}
public StringPart Slice(int newStartIndex, int newLength) => new StringPart(Target, newStartIndex, newLength);
public StringPart Slice(int newStartIndex) => Slice(newStartIndex, EndIndex - newStartIndex);
public StringPart Slice(Capture capture) => Slice(capture.Index, capture.Length);
public override string ToString() => Target.Substring(StartIndex, Length);
}
}