From f1740b7b322e701113805f9eed847b6effc7c290 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 19 May 2023 19:30:45 +0200 Subject: [PATCH] Added TextFormatter.StripAllFormatting --- CHANGELOG.md | 1 + MLEM/Formatting/TextFormatter.cs | 24 +++++++++++++++++++----- Tests/FontTests.cs | 6 ++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0a785a..5d1c38f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Additions - Added a simple outline formatting code - Added the ability to add inverse modifiers to a Keybind - Added GenericInput collections AllKeys, AllMouseButtons, AllButtons and AllInputs +- Added TextFormatter.StripAllFormatting Improvements - Increased TextFormatter macro recursion limit to 64 diff --git a/MLEM/Formatting/TextFormatter.cs b/MLEM/Formatting/TextFormatter.cs index 6132efa..f0789a7 100644 --- a/MLEM/Formatting/TextFormatter.cs +++ b/MLEM/Formatting/TextFormatter.cs @@ -214,18 +214,32 @@ namespace MLEM.Formatting { return ret; } + /// + /// Strips all formatting codes from the given string, causing a string without any formatting codes to be returned. + /// Note that, if a has already been created using , it is more efficient to use or . + /// + /// The string to strip formatting codes from. + /// The stripped string. + public string StripAllFormatting(string s) { + foreach (var regex in this.Codes.Keys) + s = regex.Replace(s, string.Empty); + return s; + } + private Code GetNextCode(string s, int index, int maxIndex = int.MaxValue) { - var (c, m, r) = this.Codes - .Select(kv => (c: kv.Value, m: kv.Key.Match(s, index), r: kv.Key)) - .Where(kv => kv.m.Success && kv.m.Index <= maxIndex) - .OrderBy(kv => kv.m.Index) + var (constructor, match, regex) = this.Codes + .Select(kv => (Constructor: kv.Value, Match: kv.Key.Match(s, index), Regex: kv.Key)) + .Where(kv => kv.Match.Success && kv.Match.Index <= maxIndex) + .OrderBy(kv => kv.Match.Index) .FirstOrDefault(); - return c?.Invoke(this, m, r); + return constructor?.Invoke(this, match, regex); } private static string StripFormatting(GenericFont font, string s, IEnumerable codes) { foreach (var code in codes) { #pragma warning disable CS0618 + // this can be combined with StripAllFormatting (which was added after GetReplacementString was deprecated) once GetReplacementString is removed + // (just make this method accept a set of regular expressions, and then call it with all code keys in StripAllFormatting, and the applied codes' regexes in Tokenize) s = code.Regex.Replace(s, code.GetReplacementString(font)); #pragma warning restore CS0618 } diff --git a/Tests/FontTests.cs b/Tests/FontTests.cs index 7511d03..46de61b 100644 --- a/Tests/FontTests.cs +++ b/Tests/FontTests.cs @@ -125,6 +125,12 @@ public class FontTests { Assert.AreEqual(ret.AllCodes.Length, 12); } + [Test] + public void TestStripping() { + var stripped = this.formatter.StripAllFormatting("This is a test string with a lot ofcontent and an invalid code as well<ü>."); + Assert.AreEqual("This is a test string with a lot ofcontent and an invalid code as well<ü>.", stripped); + } + [Test] public void TestConsistency() { void CompareSizes(string s) {