1 module pixelperfectengine.concrete.types.inputfilter;
2 
3 import pixelperfectengine.graphics.text : Text;
4 import pixelperfectengine.system.etc : removeUnallowedSymbols, removeUnallowedDups;
5 
6 import std.algorithm.searching : count;
7 
8 /**
9  * Defines the basic layout for an input filter.
10  */
11 public abstract class InputFilter {
12 	
13 	/**
14 	 * Uses this input filter on the provided text.
15 	 */
16 	public abstract void use(ref dstring input) @safe;
17 }
18 /**
19  * Implements an integer input filter.
20  */
21 public class IntegerFilter(bool AllowNegative = true) : InputFilter {
22 	static immutable dstring symbolList = "0123456789";
23 	static immutable dstring symbolListWMinus = "-0123456789";
24 	///Creates an instance of this kind of filter
25 	public this () @safe {
26 	}
27 	public override void use(ref dstring input) @safe {
28 		static if (AllowNegative) {
29 			input = removeUnallowedSymbols(input[0..1], symbolListWMinus) ~ removeUnallowedSymbols(input[1..$], symbolList);
30 		} else {
31 			input = removeUnallowedSymbols(input, symbolList);
32 		}
33 	}
34 }
35 /**
36  * Implements an decimal input filter.
37  */
38 public class DecimalFilter(bool AllowNegative = true) : InputFilter {
39 	static immutable dstring symbolList = "0123456789.";
40 	static immutable dstring symbolListWMinus = "-0123456789.";
41 	///Creates an instance of this kind of filter
42 	public this () @safe {
43 	}
44 	public override void use(ref dstring input) @safe {
45 		static if (AllowNegative) {
46 			input = removeUnallowedSymbols(input[0..1], symbolListWMinus) ~ removeUnallowedSymbols(input[1..$], symbolList);
47 		} else {
48 			input = removeUnallowedSymbols(input, symbolList);
49 		}
50 		input = removeUnallowedDups(input, ".");
51 	}
52 }
53 public class HexadecimalFilter : InputFilter {
54 	///Creates an instance of this kind of filter
55 	public this () @safe {
56 	}
57 	public override void use(ref dstring input) @safe {
58 		dstring symbolList = "0123456789ABCDEFabcdef";
59 		input = removeUnallowedSymbols(input, symbolList);
60 	}
61 }
62 public class ASCIITextFilter : InputFilter {
63 	static dstring SYMBOL_LIST;
64 	shared static this() {
65 		for (dchar c = 0x20 ; c <= 0x7F ; c++) {
66 			SYMBOL_LIST ~= c;
67 		}
68 	}
69 	public this () @safe {
70 	}
71 	public override void use(ref dstring input) @safe {
72 		input = removeUnallowedSymbols(input, SYMBOL_LIST);
73 	}
74 }