From 3840e601df8bb72c06249c65309a5c9c63bd81eb Mon Sep 17 00:00:00 2001 From: Chris Johnston Date: Mon, 3 Jun 2019 22:06:47 -0700 Subject: [PATCH] refactor to reuse a local function uses CheckWrappedInCode to check that there are no code blocks that surround the tag being parsed --- .../Entities/Messages/MessageHelper.cs | 79 ++++++------------- 1 file changed, 23 insertions(+), 56 deletions(-) diff --git a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs index 6517caeae..789b243b2 100644 --- a/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs +++ b/src/Discord.Net.Rest/Entities/Messages/MessageHelper.cs @@ -98,10 +98,15 @@ namespace Discord.Rest public static ImmutableArray ParseTags(string text, IMessageChannel channel, IGuild guild, IReadOnlyCollection userMentions) { var tags = ImmutableArray.CreateBuilder(); + int index = 0; + int codeBlockIndex = 0; + int closeIndex; // gets the next index of a codeblock stop/start int IndexOfCode(int startIndex) { + if (startIndex == -1) + return -1; // code block has precedence over inline code var codeIndex = text.IndexOf("```", startIndex); if (codeIndex != -1) @@ -111,43 +116,28 @@ namespace Discord.Rest return codeIndex + 1; return -1; } - - int index = 0; - int codeBlockIndex = 0; - while (true) + // checks if the tag being parsed is wrapped in code blocks + bool CheckWrappedCode() { - index = text.IndexOf('<', index); - if (index == -1) break; - int endIndex = text.IndexOf('>', index + 1); - if (endIndex == -1) break; - - int closeIndex = 0; - bool CheckCloseIndex() - => closeIndex != -1 && closeIndex < index; - - // check for code blocks before the start index, of either type + closeIndex = 0; codeBlockIndex = IndexOfCode(codeBlockIndex); - while (codeBlockIndex != -1) + // loop through all code blocks, before the starting index + while (codeBlockIndex != -1 && closeIndex != -1 && closeIndex < index) { - // code block opens before this tag closeIndex = IndexOfCode(codeBlockIndex); - // not closed at all - if (closeIndex == -1) - break; - // closed somewhere after the start index - if (closeIndex > index) - break; - // advance codeblock start index codeBlockIndex = IndexOfCode(closeIndex); } - // unclosed code block - if (closeIndex == -1) - break; - // closed somewhere after the start index - if (closeIndex > index) - break; - + // break if unclosed code block, or code block beyond starting index + return closeIndex == -1 || closeIndex > index; + } + while (true) + { + index = text.IndexOf('<', index); + if (index == -1) break; + int endIndex = text.IndexOf('>', index + 1); + if (endIndex == -1) break; + if (CheckWrappedCode()) break; string content = text.Substring(index, endIndex - index + 1); if (MentionUtils.TryParseUser(content, out ulong id)) @@ -191,36 +181,12 @@ namespace Discord.Rest index = 0; codeBlockIndex = 0; + closeIndex = 0; while (true) { - int closeIndex = 0; - bool CheckCloseIndex() - => closeIndex != -1 && closeIndex < index; - - // check for code blocks before the start index, of either type - codeBlockIndex = IndexOfCode(codeBlockIndex); - while (codeBlockIndex != -1) - { - // code block opens before this tag - closeIndex = IndexOfCode(codeBlockIndex); - // not closed at all - if (closeIndex == -1) - break; - // closed somewhere after the start index - if (closeIndex > index) - break; - // advance codeblock start index - codeBlockIndex = IndexOfCode(closeIndex); - } - // unclosed code block - if (closeIndex == -1) - break; - // closed somewhere after the start index - if (closeIndex > index) - break; - index = text.IndexOf("@everyone", index); if (index == -1) break; + if (CheckWrappedCode()) break; var tagIndex = FindIndex(tags, index); if (tagIndex.HasValue) tags.Insert(tagIndex.Value, new Tag(TagType.EveryoneMention, index, "@everyone".Length, 0, guild?.EveryoneRole)); @@ -232,6 +198,7 @@ namespace Discord.Rest { index = text.IndexOf("@here", index); if (index == -1) break; + if (CheckWrappedCode()) break; var tagIndex = FindIndex(tags, index); if (tagIndex.HasValue) tags.Insert(tagIndex.Value, new Tag(TagType.HereMention, index, "@here".Length, 0, guild?.EveryoneRole));