Skip to content

Commit

Permalink
Use ReadOnlySpan<char> in MakeBracketed.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Jul 3, 2024
1 parent 31b9e9b commit 5d2b032
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
2 changes: 2 additions & 0 deletions TASVideos.WikiEngine/Builtins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public static partial class Builtins
{
private static KeyValuePair<string, string> Attr(string name, string value) => new(name, value);

private static KeyValuePair<string, string> Attr(string name, ReadOnlySpan<char> value) => Attr(name, value.ToString());
}
37 changes: 19 additions & 18 deletions TASVideos.WikiEngine/MakeBracketed.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using TASVideos.Common;
using TASVideos.Extensions;
using TASVideos.WikiEngine.AST;

namespace TASVideos.WikiEngine;
Expand All @@ -12,7 +13,7 @@ public static partial class Builtins
/// <summary>
/// Turns text inside [square brackets] into the appropriate thing, usually module or link. Does not handle [if:].
/// </summary>
public static IEnumerable<INode> MakeBracketed(string text, StringIndices range)
public static IEnumerable<INode> MakeBracketed(ReadOnlySpan<char> text, StringIndices range)
{
if (text.StartsWith("if:"))
{
Expand All @@ -32,7 +33,7 @@ public static IEnumerable<INode> MakeBracketed(string text, StringIndices range)
return MakeModuleInternal(range, "UserGetWikiName");
}

Match match;
RegexMatchShim match;
if ((match = Footnote.Match(text)).Success)
{
return MakeFootnote(range, match.Groups[1].Value);
Expand All @@ -51,12 +52,12 @@ public static IEnumerable<INode> MakeBracketed(string text, StringIndices range)
return MakeLinkOrImage(range, text);
}

private static Module[] MakeModuleInternal(StringIndices range, string module)
private static Module[] MakeModuleInternal(StringIndices range, ReadOnlySpan<char> module)
{
return [new Module(range, module)];
return [new Module(range, module.ToString())];
}

private static IEnumerable<INode> MakeFootnote(StringIndices range, string n)
private static IEnumerable<INode> MakeFootnote(StringIndices range, ReadOnlySpan<char> n)
{
return
[
Expand All @@ -67,7 +68,7 @@ private static IEnumerable<INode> MakeFootnote(StringIndices range, string n)
];
}

private static Element[] MakeFootnoteLink(StringIndices range, string n)
private static Element[] MakeFootnoteLink(StringIndices range, ReadOnlySpan<char> n)
{
return
[
Expand All @@ -88,24 +89,24 @@ private static Element[] MakeFootnoteLink(StringIndices range, string n)
// You can always make a wikilink by starting with "[=", and that will accept a wide range of characters
// This regex is just for things that we'll make implicit wiki links out of; contents of brackets that don't match any other known pattern
private static readonly Regex ImplicitWikiLink = ImplicitWikiLinkRegex();
private static bool IsLink(string text)
private static bool IsLink(ReadOnlySpan<char> text)
{
return LinkPrefixes.Any(text.StartsWith);
return text.StartsWithAny(LinkPrefixes);
}

private static bool IsImage(string text)
private static bool IsImage(ReadOnlySpan<char> text)
{
return IsLink(text) && ImageSuffixes.Any(text.EndsWith);
return IsLink(text) && text.EndsWithAny(ImageSuffixes);
}

private static string NormalizeImageUrl(string text)
private static ReadOnlySpan<char> NormalizeImageUrl(ReadOnlySpan<char> text)
{
return text[0] == '='
? text[1] is '/' ? text[1..] : $"/{text[1..]}"
: text;
}

private static string NormalizeUrl(string text)
private static ReadOnlySpan<char> NormalizeUrl(ReadOnlySpan<char> text)
{
if (text[0] == '=')
{
Expand All @@ -125,12 +126,12 @@ private static string NormalizeUrl(string text)
return text;
}

public static string NormalizeInternalLink(string input)
public static string NormalizeInternalLink(ReadOnlySpan<char> input)
{
var iAnchorSeparator = input.IndexOf('#');
var pathAndQuery = iAnchorSeparator < 0 ? input : input[..iAnchorSeparator];
var anchor = iAnchorSeparator < 0 ? "" : input[(iAnchorSeparator + 1)..];
var ss = pathAndQuery.TrimEnd('/').Split('/');
var ss = pathAndQuery.TrimEnd('/').ToString().Split('/');

int skip = -1;
if (ss.Length >= 4 && ss[1].Equals("users", StringComparison.OrdinalIgnoreCase) && ss[2].Equals("profile", StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -173,7 +174,7 @@ public static string NormalizeInternalLink(string input)
return anchor.Length is 0 ? newText : $"{newText}#{anchor}";
}

private static string DisplayTextForUrl(string text)
private static ReadOnlySpan<char> DisplayTextForUrl(ReadOnlySpan<char> text)
{
// If users don't like this, they should use links with explicit display text
if (text.StartsWith("user:"))
Expand All @@ -185,9 +186,9 @@ private static string DisplayTextForUrl(string text)
return text;
}

private static IEnumerable<INode> MakeLinkOrImage(StringIndices range, string text)
private static IEnumerable<INode> MakeLinkOrImage(StringIndices range, ReadOnlySpan<char> text)
{
var pp = text.Split('|');
var pp = text.ToString().Split('|');
if (pp.Length >= 2 && IsLink(pp[0]) && IsImage(pp[1]))
{
return [MakeLink(range, pp[0], MakeImage(range, pp.Skip(1), out _))];
Expand Down Expand Up @@ -232,7 +233,7 @@ private static IEnumerable<INode> MakeLinkOrImage(StringIndices range, string te
return [new Text(range, $"[{text}]")];
}

internal static INode MakeLink(StringIndices range, string text, INode child)
internal static INode MakeLink(StringIndices range, ReadOnlySpan<char> text, INode child)
{
var href = NormalizeUrl(text);
var attrs = new List<KeyValuePair<string, string>>
Expand Down
6 changes: 6 additions & 0 deletions TASVideos.WikiEngine/NodeImplementations/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public int CharEnd
set => _charRange.End = value;
}

public Text(StringIndices range, ReadOnlySpan<char> content)
: this(range, content.ToString()) { }

public Text(int charStart, string content)
: this((charStart, default), content) { }

public Text(int charStart, ReadOnlySpan<char> content)
: this(charStart, content.ToString()) { }

public Task WriteHtmlAsync(TextWriter w, WriterContext ctx)
{
foreach (var c in Content)
Expand Down

0 comments on commit 5d2b032

Please sign in to comment.