X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Firc%2Futil%2FMessageCleaner.java;h=160811ec651179f59425f62ccc280dc26a2e4813;hb=f96ea38946952e2a06ccc0d64a92e346b8761f85;hp=6d0e3a21ec1130027cdb98c71bc3daed839b4792;hpb=b138564a95720fa1238256b219acb4c28019dae9;p=xudocci.git diff --git a/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java index 6d0e3a2..160811e 100644 --- a/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java +++ b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java @@ -17,8 +17,10 @@ package net.pterodactylus.irc.util; -import java.io.IOException; -import java.io.StringReader; +import static net.pterodactylus.irc.util.MessageCleaner.Attributes.italics; +import static net.pterodactylus.irc.util.MessageCleaner.Attributes.reverse; +import static net.pterodactylus.irc.util.MessageCleaner.Attributes.underline; + import java.util.EnumSet; import java.util.Set; @@ -34,7 +36,10 @@ public class MessageCleaner { bold, color, - clear + clear, + reverse, + underline, + italics } @@ -64,92 +69,90 @@ public class MessageCleaner { public String clean(String line, Set attributes) { StringBuilder clean = new StringBuilder(line.length()); - StringReader reader = new StringReader(line); - - try { - int inColorCode = 0; - while (true) { - int r = reader.read(); - if (r == -1) { - break; - } - char c = (char) r; - if ((c == 2) && (attributes.contains(Attributes.bold))) { - continue; - } - if ((c == 3) && (attributes.contains(Attributes.color))) { - inColorCode = 1; - continue; - } - if ((c == 15) && (attributes.contains(Attributes.clear))) { - continue; - } - if (inColorCode > 0) { - if (inColorCode == 1) { - if ((c < '0') || (c > '9')) { - inColorCode = 0; - } else { - if (c == '9') { - inColorCode = 8; - } else { - ++inColorCode; - } - continue; - } - } else if (inColorCode == 2) { - if (c == ',') { - inColorCode = 4; - continue; - } - if ((c < '0') || (c > '5')) { - inColorCode = 0; - } else { - ++inColorCode; - continue; - } - } else if (inColorCode == 3) { - if (c == ',') { - ++inColorCode; - continue; - } else { - inColorCode = 0; - } - } else if (inColorCode == 4) { + int inColorCode = 0; + for (char c : line.toCharArray()) { + if ((c == 2) && (attributes.contains(Attributes.bold))) { + continue; + } + if ((c == 3) && (attributes.contains(Attributes.color))) { + inColorCode = 1; + continue; + } + if ((c == 15) && (attributes.contains(Attributes.clear))) { + continue; + } + if ((c == 22) && attributes.contains(reverse)) { + continue; + } + if ((c == 29) && attributes.contains(italics)) { + continue; + } + if ((c == 31) && attributes.contains(underline)) { + continue; + } + if (inColorCode > 0) { + if (inColorCode == 1) { + if ((c < '0') || (c > '9')) { + inColorCode = 0; + } else { if (c == '9') { - inColorCode = 9; - continue; - } else if ((c < '0') || (c > '9')) { - inColorCode = 0; + inColorCode = 8; } else { ++inColorCode; - continue; } - } else if (inColorCode == 5) { + continue; + } + } else if (inColorCode == 2) { + if (c == ',') { + inColorCode = 4; + continue; + } + if ((c < '0') || (c > '5')) { inColorCode = 0; - if ((c >= '0') && (c <= '5')) { - continue; - } - } else if (inColorCode == 8) { - if (c == '9') { - inColorCode = 3; - continue; - } else if (c == ',') { - inColorCode = 4; - continue; - } else { - inColorCode = 0; - } - } else if (inColorCode == 9) { + } else { + ++inColorCode; + continue; + } + } else if (inColorCode == 3) { + if (c == ',') { + ++inColorCode; + continue; + } else { inColorCode = 0; - if (c == '9') { - continue; - } + } + } else if (inColorCode == 4) { + if (c == '9') { + inColorCode = 9; + continue; + } else if ((c < '0') || (c > '9')) { + inColorCode = 0; + } else { + ++inColorCode; + continue; + } + } else if (inColorCode == 5) { + inColorCode = 0; + if ((c >= '0') && (c <= '5')) { + continue; + } + } else if (inColorCode == 8) { + if (c == '9') { + inColorCode = 3; + continue; + } else if (c == ',') { + inColorCode = 4; + continue; + } else { + inColorCode = 0; + } + } else { + inColorCode = 0; + if (c == '9') { + continue; } } - clean.append(c); } - } catch (IOException ioe1) { - /* StringReader will never throw. */ + clean.append(c); } return clean.toString();