mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-17 17:32:50 +08:00
Replaces emoji regex with ReadOnlySpan implementation
The RegEx runtime perf was never anything noticeable - it was the startup time that was eating over a third of time during initialization. This shaves 200ms off the startup time.
This commit is contained in:
parent
6f16081f42
commit
254880e93a
@ -41,5 +41,56 @@ namespace Spectre.Console.Tests.Unit
|
|||||||
result.ShouldBe("Hello 🌍!");
|
result.ShouldBe("Hello 🌍!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public sealed class Parsing
|
||||||
|
{
|
||||||
|
[Theory]
|
||||||
|
[InlineData(":", ":")]
|
||||||
|
[InlineData("::", "::")]
|
||||||
|
[InlineData(":::", ":::")]
|
||||||
|
[InlineData("::::", "::::")]
|
||||||
|
[InlineData("::i:", "::i:")]
|
||||||
|
[InlineData(":i:i:", ":i:i:")]
|
||||||
|
[InlineData("::globe_showing_europe_africa::", ":🌍:")]
|
||||||
|
[InlineData(":globe_showing_europe_africa::globe_showing_europe_africa:", "🌍🌍")]
|
||||||
|
[InlineData("::globe_showing_europe_africa:::test:::globe_showing_europe_africa:::", ":🌍::test::🌍::")]
|
||||||
|
public void Can_Handle_Different_Combinations(string markup, string expected)
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes);
|
||||||
|
|
||||||
|
// When
|
||||||
|
console.Markup(markup);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
console.Output.ShouldBe(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Should_Leave_Single_Colons()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes);
|
||||||
|
|
||||||
|
// When
|
||||||
|
console.Markup("Hello :globe_showing_europe_africa:! Output: good");
|
||||||
|
|
||||||
|
// Then
|
||||||
|
console.Output.ShouldBe("Hello 🌍! Output: good");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Unknown_emojis_should_remain()
|
||||||
|
{
|
||||||
|
// Given
|
||||||
|
var console = new FakeAnsiConsole(ColorSystem.Standard, AnsiSupport.Yes);
|
||||||
|
|
||||||
|
// When
|
||||||
|
console.Markup("Hello :globe_showing_flat_earth:!");
|
||||||
|
|
||||||
|
// Then
|
||||||
|
console.Output.ShouldBe("Hello :globe_showing_flat_earth:!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.Design;
|
||||||
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace Spectre.Console
|
namespace Spectre.Console
|
||||||
@ -9,7 +11,6 @@ namespace Spectre.Console
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static partial class Emoji
|
public static partial class Emoji
|
||||||
{
|
{
|
||||||
private static readonly Regex _emojiCode = new Regex(@"(:(\S*?):)", RegexOptions.Compiled);
|
|
||||||
private static readonly Dictionary<string, string> _remappings;
|
private static readonly Dictionary<string, string> _remappings;
|
||||||
|
|
||||||
static Emoji()
|
static Emoji()
|
||||||
@ -40,6 +41,7 @@ namespace Spectre.Console
|
|||||||
_remappings[tag] = emoji;
|
_remappings[tag] = emoji;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if NETSTANDARD2_0
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Replaces emoji markup with corresponding unicode characters.
|
/// Replaces emoji markup with corresponding unicode characters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -47,24 +49,86 @@ namespace Spectre.Console
|
|||||||
/// <returns>A string with emoji codes replaced with actual emoji.</returns>
|
/// <returns>A string with emoji codes replaced with actual emoji.</returns>
|
||||||
public static string Replace(string value)
|
public static string Replace(string value)
|
||||||
{
|
{
|
||||||
static string ReplaceEmoji(Match match)
|
return Replace(value.AsSpan());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Replaces emoji markup with corresponding unicode characters.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value">A string with emojis codes, e.g. "Hello :smiley:!".</param>
|
||||||
|
/// <returns>A string with emoji codes replaced with actual emoji.</returns>
|
||||||
|
public static string Replace(ReadOnlySpan<char> value)
|
||||||
{
|
{
|
||||||
var key = match.Groups[2].Value;
|
var output = new StringBuilder();
|
||||||
|
var colonPos = value.IndexOf(':');
|
||||||
if (_remappings.Count > 0 && _remappings.TryGetValue(key, out var remappedEmoji))
|
if (colonPos == -1)
|
||||||
{
|
{
|
||||||
return remappedEmoji;
|
// No colons, no emoji. return what was passed in with no changes.
|
||||||
|
return value.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_emojis.TryGetValue(key, out var emoji))
|
while ((colonPos = value.IndexOf(':')) != -1)
|
||||||
{
|
{
|
||||||
return emoji;
|
// Append text up to colon
|
||||||
|
output.AppendSpan(value.Slice(0, colonPos));
|
||||||
|
|
||||||
|
// Set value equal to that colon and the rest of the string
|
||||||
|
value = value.Slice(colonPos);
|
||||||
|
|
||||||
|
// Find colon after that. if no colon, break out
|
||||||
|
var nextColonPos = value.IndexOf(':', 1);
|
||||||
|
if (nextColonPos == -1)
|
||||||
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return match.Value;
|
// Get the emoji text minus the colons
|
||||||
|
var emojiKey = value.Slice(1, nextColonPos - 1).ToString();
|
||||||
|
if (TryGetEmoji(emojiKey, out var emojiValue))
|
||||||
|
{
|
||||||
|
output.Append(emojiValue);
|
||||||
|
value = value.Slice(nextColonPos + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output.Append(':');
|
||||||
|
value = value.Slice(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return _emojiCode.Replace(value, ReplaceEmoji);
|
output.AppendSpan(value);
|
||||||
|
return output.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool TryGetEmoji(string emoji, out string value)
|
||||||
|
{
|
||||||
|
if (_remappings.TryGetValue(emoji, out var remappedEmojiValue))
|
||||||
|
{
|
||||||
|
value = remappedEmojiValue;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_emojis.TryGetValue(emoji, out var emojiValue))
|
||||||
|
{
|
||||||
|
value = emojiValue;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
value = string.Empty;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int IndexOf(this ReadOnlySpan<char> span, char value, int startIndex)
|
||||||
|
{
|
||||||
|
var indexInSlice = span.Slice(startIndex).IndexOf(value);
|
||||||
|
|
||||||
|
if (indexInSlice == -1)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return startIndex + indexInSlice;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,4 @@
|
|||||||
|
using System;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
@ -25,5 +26,16 @@ namespace Spectre.Console
|
|||||||
|
|
||||||
return builder.Append(value);
|
return builder.Append(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void AppendSpan(this StringBuilder builder, ReadOnlySpan<char> span)
|
||||||
|
{
|
||||||
|
// NetStandard 2 lacks the override for StringBuilder to add the span. We'll need to convert the span
|
||||||
|
// to a string for it, but for .NET 5.0 we'll use the override.
|
||||||
|
#if NETSTANDARD2_0
|
||||||
|
builder.Append(span.ToString());
|
||||||
|
#else
|
||||||
|
builder.Append(span);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Wcwidth" Version="0.2.0" />
|
<PackageReference Include="Wcwidth" Version="0.2.0" />
|
||||||
|
<PackageReference Include="System.Memory" Version="4.5.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user