From 3acd3f93d41b83e6d43389dfb234069e90d098dc Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 30 Apr 2014 07:14:58 +0200 Subject: [PATCH] Iterate over the characters of a String like a sane person. --- .../net/pterodactylus/irc/util/MessageCleaner.java | 161 ++++++++++----------- 1 file changed, 74 insertions(+), 87 deletions(-) diff --git a/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java index 4b91e4a..160811e 100644 --- a/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java +++ b/src/main/java/net/pterodactylus/irc/util/MessageCleaner.java @@ -21,8 +21,6 @@ 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.io.IOException; -import java.io.StringReader; import java.util.EnumSet; import java.util.Set; @@ -71,101 +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 ((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 = 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 { + ++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(); -- 2.7.4