1 /*
2  * Copyright (c) 2018-2020 sel-project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  *
22  */
23 /**
24  * Copyright: Copyright (c) 2018-2020 sel-project
25  * License: MIT
26  * Authors: Kripth
27  * Source: $(HTTP github.com/sel-project/sel-util/format/sel/format.d, sel/format.d)
28  */
29 module sel.format;
30 
31 enum Format : string {
32 	
33 	black = "§0",
34 	darkBlue = "§1",
35 	darkGreen = "§2",
36 	darkAqua = "§3",
37 	darkRed = "§4",
38 	darkPurple = "§5",
39 	gold = "§6",
40 	gray = "§7",
41 	darkGray = "§8",
42 	blue = "§9",
43 	green = "§a",
44 	aqua = "§b",
45 	red = "§c",
46 	lightPurple = "§d",
47 	yellow = "§e",
48 	white = "§f",
49 	
50 	obfuscated = "§k",
51 	bold = "§l",
52 	strikethrough = "§m",
53 	underlined = "§n",
54 	italic = "§o",
55 	
56 	reset = "§r"
57 	
58 }
59 
60 /**
61  * Removes valid formatting codes from a string.
62  */
63 pure nothrow @safe string unformat(string message) {
64 	for(ptrdiff_t i=0; message.length>2 && i<message.length-2; i++) {
65 		if(message[i] == 194 && message[i+1] == 167) {
66 			char next = message[i+2];
67 			if(next >= '0' && next <= '9' || next >= 'a' && next <= 'f' || next >= 'k' && next <= 'o' || next == 'r') {
68 				message = message[0..i] ~ message[i+3..$];
69 				if(--i > 0) i -= 2;
70 			}
71 		}
72 	}
73 	return message;
74 }
75 
76 ///
77 unittest {
78 
79 	assert(unformat("§agreen") == "green");
80 	assert(unformat("res§ret") == "reset");
81 	assert(unformat("§xunsupported") == "§xunsupported");
82 
83 	// consecutive
84 	assert(unformat("§§rr") == "");
85 
86 }