Optimize ColorUtil.

* Use ChatColor.translateAlternateColorCode
* Use more (still too) complicated but faster method for color removal.
This commit is contained in:
asofold 2013-06-30 22:24:57 +02:00
parent 4085223594
commit 480264d0f1
2 changed files with 103 additions and 12 deletions

View File

@ -3,36 +3,66 @@ package fr.neatmonster.nocheatplus.utilities;
import org.bukkit.ChatColor;
/**
* Desperate :).
* More and less color.
* @author mc_dev
*
*/
public class ColorUtil {
private static final String allColorChars = "0123456789AaBbCcDdEeFfKkLlMmNnOoRr";
/**
* Removes the colors of a message.
* Removes the colors from a message (definitions with '&').
*
* @param text
* the text
* @return the string
* @return Either the object reference itself, or a copy with color definitions removed.
*/
public static String removeColors(String text) {
for (final ChatColor c : ChatColor.values())
text = text.replace("&" + c.getChar(), "");
return text;
public static String removeColors(final String text) {
if (text.length() <= 1) return text;
final char[] chars = text.toCharArray();
// First find a begin index at all.
// TODO: Consider removing the normal color char as well (!).
int srcIndex = 0; // SourceIndex
do{
if (chars[srcIndex] == '&' && allColorChars.indexOf(chars[srcIndex + 1]) > -1){
break;
}
srcIndex ++;
} while (srcIndex < chars.length - 1);
if (srcIndex >= chars.length - 1){
// Nothing found
return text;
}
// At least one color inside.
char[] newChars = new char[chars.length - 2];
int tgtIndex = 0; // TargetIndex.
do{
newChars[tgtIndex] = chars[tgtIndex];
tgtIndex++;
} while (tgtIndex < srcIndex);
for (srcIndex = srcIndex + 2; srcIndex < chars.length; srcIndex++){
if (chars[srcIndex] == '&' && srcIndex < chars.length -1 && allColorChars.indexOf(chars[srcIndex + 1]) > -1){
// Skip this one;
srcIndex ++;
}
else{
newChars[tgtIndex] = chars[srcIndex];
tgtIndex++;
}
}
return new String(newChars, 0, tgtIndex);
}
/**
* Replace colors of a message.
* Replace color definitions with '&' by ChatColor for the given text.
*
* @param text
* the text
* @return the string
*/
public static String replaceColors(String text) {
for (final ChatColor c : ChatColor.values())
text = text.replace("&" + c.getChar(), c.toString());
return text;
public static String replaceColors(final String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
}

View File

@ -0,0 +1,61 @@
package fr.neatmonster.nocheatplus.test;
import static org.junit.Assert.fail;
import java.util.LinkedList;
import java.util.List;
import org.bukkit.ChatColor;
import org.junit.Test;
import fr.neatmonster.nocheatplus.utilities.ColorUtil;
public class TestColorUtil {
@Test
public void testRemoveColor(){
List<String[]> items = new LinkedList<String[]>();
for (String[] item : new String[][]{
{"", ""},
{" ", " "},
{"&", "&"},
{"&&", "&&"},
{"o3rg7cbo'!§)=?%V823rg7c", "o3rg7cbo'!§)=?%V823rg7c"},
}){
items.add(item);
}
String[][] generic = new String[][]{
{"&/&/&/", ""},
{"&/&/", ""},
{" &/&/ ", " "},
{" &/&/", " "},
{"&/", ""},
{"&/ ", " "},
{" &/", " "},
{"123&/123", "123123"},
};
for (ChatColor color : ChatColor.values()){
char c = color.getChar();
for (String[] pattern : generic){
pattern[0] = pattern[0].replace('/', c);
}
}
int i = 0;
for (String[] item : items){
String input = item[0];
String expectedOutput = item[1];
String detail = "no details.";
String output = "(ERROR)";
try{
output = ColorUtil.removeColors(input);
}
catch(Throwable t){
detail = t.getClass().getSimpleName() + ": " + t.getMessage();
}
if (!expectedOutput.equals(output)){
fail("Wrong output at index " + i + " for input '" + input + "', expected '" + expectedOutput + "', but got instead: '" + output + "', details: " + detail );
}
i ++;
}
}
}