perf: don't escape markdown with regex

This commit is contained in:
Oliver Booth 2023-08-27 00:19:02 +01:00
parent 70708e3eef
commit 64fd5d45bf
Signed by: oliverbooth
GPG Key ID: B89D139977693FED
1 changed files with 1 additions and 11 deletions

View File

@ -2,7 +2,6 @@ using System.Diagnostics.CodeAnalysis;
using System.Reactive.Linq; using System.Reactive.Linq;
using System.Reactive.Subjects; using System.Reactive.Subjects;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using Cysharp.Text; using Cysharp.Text;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
@ -19,8 +18,6 @@ namespace VPLink.Services;
internal sealed partial class DiscordMessageService : BackgroundService, IDiscordMessageService internal sealed partial class DiscordMessageService : BackgroundService, IDiscordMessageService
{ {
private static readonly Encoding Utf8Encoding = new UTF8Encoding(false, false); private static readonly Encoding Utf8Encoding = new UTF8Encoding(false, false);
private static readonly Regex UnescapeRegex = GetUnescapeRegex();
private static readonly Regex EscapeRegex = GetEscapeRegex();
private readonly ILogger<DiscordMessageService> _logger; private readonly ILogger<DiscordMessageService> _logger;
private readonly IConfigurationService _configurationService; private readonly IConfigurationService _configurationService;
@ -103,8 +100,7 @@ internal sealed partial class DiscordMessageService : BackgroundService, IDiscor
return Task.CompletedTask; return Task.CompletedTask;
string displayName = author.Nickname ?? author.GlobalName ?? author.Username; string displayName = author.Nickname ?? author.GlobalName ?? author.Username;
string unescaped = UnescapeRegex.Replace(message.Content, "$1"); string content = message.Content;
string content = EscapeRegex.Replace(unescaped, "\\$1");
IReadOnlyCollection<IAttachment> attachments = message.Attachments; IReadOnlyCollection<IAttachment> attachments = message.Attachments;
if (attachments.Count > 0) if (attachments.Count > 0)
@ -154,10 +150,4 @@ internal sealed partial class DiscordMessageService : BackgroundService, IDiscor
channel = null; channel = null;
return false; return false;
} }
[GeneratedRegex(@"\\(\*|_|`|~|\\)", RegexOptions.Compiled)]
private static partial Regex GetUnescapeRegex();
[GeneratedRegex(@"(\*|_|`|~|\\)", RegexOptions.Compiled)]
private static partial Regex GetEscapeRegex();
} }