mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
Throw an exception when text formatter macros resolve recursively too many times
This commit is contained in:
parent
84e2544dc8
commit
c9c9e566b1
3 changed files with 12 additions and 0 deletions
|
@ -20,6 +20,7 @@ Improvements
|
||||||
- Exposed Camera's RoundPosition
|
- Exposed Camera's RoundPosition
|
||||||
- Exposed the epsilon value used by Camera
|
- Exposed the epsilon value used by Camera
|
||||||
- Added Padding.Empty
|
- Added Padding.Empty
|
||||||
|
- Throw an exception when text formatter macros resolve recursively too many times
|
||||||
|
|
||||||
### MLEM.Ui
|
### MLEM.Ui
|
||||||
Additions
|
Additions
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -114,6 +115,7 @@ namespace MLEM.Formatting {
|
||||||
/// <returns>The final, recursively resolved string</returns>
|
/// <returns>The final, recursively resolved string</returns>
|
||||||
public string ResolveMacros(string s) {
|
public string ResolveMacros(string s) {
|
||||||
// resolve macros that resolve into macros
|
// resolve macros that resolve into macros
|
||||||
|
var rec = 0;
|
||||||
bool matched;
|
bool matched;
|
||||||
do {
|
do {
|
||||||
matched = false;
|
matched = false;
|
||||||
|
@ -124,6 +126,9 @@ namespace MLEM.Formatting {
|
||||||
return macro.Value(this, m, macro.Key);
|
return macro.Value(this, m, macro.Key);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
rec++;
|
||||||
|
if (rec >= 16)
|
||||||
|
throw new ArithmeticException($"A string resolved macros recursively too many times. Does it contain any conflicting macros?\n{s}");
|
||||||
} while (matched);
|
} while (matched);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
|
@ -107,6 +108,11 @@ namespace Tests {
|
||||||
const string strg = "This text uses a bunch of non-breaking~spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <testmacro> text</c>.";
|
const string strg = "This text uses a bunch of non-breaking~spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <testmacro> text</c>.";
|
||||||
const string goal = "This text uses a bunch of non-breaking\u00A0spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <c Blue>blue text</c>.";
|
const string goal = "This text uses a bunch of non-breaking\u00A0spaces to see if macros work. Additionally, it uses a macro that resolves into a bunch of other macros and then, at the end, into <c Blue>blue text</c>.";
|
||||||
Assert.AreEqual(this.formatter.ResolveMacros(strg), goal);
|
Assert.AreEqual(this.formatter.ResolveMacros(strg), goal);
|
||||||
|
|
||||||
|
// test recursive macros
|
||||||
|
this.formatter.Macros.Add(new Regex("<rec1>"), (f, m, r) => "<rec2>");
|
||||||
|
this.formatter.Macros.Add(new Regex("<rec2>"), (f, m, r) => "<rec1>");
|
||||||
|
Assert.Throws<ArithmeticException>(() => this.formatter.ResolveMacros("Test <rec1> string"));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
|
|
Loading…
Reference in a new issue