From 1759f0ef5b99bb8c4b97d9908461f30ff6d4031d Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 22 Apr 2021 01:21:44 +0200 Subject: [PATCH] improve performance of TextFormatter tokenization --- MLEM/Formatting/TextFormatter.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index b52bab1..8fcc7bc 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -76,9 +76,9 @@ namespace MLEM.Formatting { var firstCode = this.GetNextCode(s, 0, 0); if (firstCode != null) codes.Add(firstCode); + var index = 0; var rawIndex = 0; while (rawIndex < s.Length) { - var index = StripFormatting(font, s.Substring(0, rawIndex), tokens.SelectMany(t => t.AppliedCodes)).Length; var next = this.GetNextCode(s, rawIndex + 1); // if we've reached the end of the string if (next == null) { @@ -89,10 +89,12 @@ namespace MLEM.Formatting { // create a new token for the content up to the next code var ret = s.Substring(rawIndex, next.Match.Index - rawIndex); - tokens.Add(new Token(codes.ToArray(), index, rawIndex, StripFormatting(font, ret, codes), ret)); + var strippedRet = StripFormatting(font, ret, codes); + tokens.Add(new Token(codes.ToArray(), index, rawIndex, strippedRet, ret)); // move to the start of the next code rawIndex = next.Match.Index; + index += strippedRet.Length; // remove all codes that are incompatible with the next one and apply it codes.RemoveAll(c => c.EndsHere(next));