mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 04:53:29 +01:00
Added TextFormatter.StripAllFormatting
This commit is contained in:
parent
0766220a8e
commit
f1740b7b32
3 changed files with 26 additions and 5 deletions
|
@ -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
|
||||
|
|
|
@ -214,18 +214,32 @@ namespace MLEM.Formatting {
|
|||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strips all formatting codes from the given string, causing a string without any formatting codes to be returned.
|
||||
/// Note that, if a <see cref="TokenizedString"/> has already been created using <see cref="Tokenize"/>, it is more efficient to use <see cref="TokenizedString.String"/> or <see cref="TokenizedString.DisplayString"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The string to strip formatting codes from.</param>
|
||||
/// <returns>The stripped string.</returns>
|
||||
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<Code> 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
|
||||
}
|
||||
|
|
|
@ -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 <b>test string</b></b><b></b> <i>with a lot of</i>content</b><i></b></i> and an <k> invalid code</b> as well<ü>.");
|
||||
Assert.AreEqual("This is a test string with a lot ofcontent and an <k> invalid code as well<ü>.", stripped);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestConsistency() {
|
||||
void CompareSizes(string s) {
|
||||
|
|
Loading…
Reference in a new issue