diff options
author | Travis Watkins <amaranth@ubuntu.com> | 2012-05-16 18:12:48 -0500 |
---|---|---|
committer | Travis Watkins <amaranth@ubuntu.com> | 2012-05-16 18:42:39 -0500 |
commit | f4bee983b04bad80b1c66ac45c46f1e571194486 (patch) | |
tree | 1475a56396e8f397ebb1c1e6485c6f841bb8ceed /src/main | |
parent | 5a7f09f1a7a77a9a9d6e9370a4efe35ee69bc844 (diff) | |
download | bukkit-f4bee983b04bad80b1c66ac45c46f1e571194486.tar bukkit-f4bee983b04bad80b1c66ac45c46f1e571194486.tar.gz bukkit-f4bee983b04bad80b1c66ac45c46f1e571194486.tar.lz bukkit-f4bee983b04bad80b1c66ac45c46f1e571194486.tar.xz bukkit-f4bee983b04bad80b1c66ac45c46f1e571194486.zip |
Optimize ChatColor.getLastColors.
ChatColor searches from the start to the end of a string for chat format
characters but this always has to search the entire string. By starting
from the end of the string and working backwards we can stop searching once
we find a color code or a reset code as any previous formatting is wiped
out by these.
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/org/bukkit/ChatColor.java | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java index 6b3f35fb..fcaa04ef 100644 --- a/src/main/java/org/bukkit/ChatColor.java +++ b/src/main/java/org/bukkit/ChatColor.java @@ -218,19 +218,21 @@ public enum ChatColor { */ public static String getLastColors(String input) { String result = ""; - int lastIndex = -1; int length = input.length(); - while ((lastIndex = input.indexOf(COLOR_CHAR, lastIndex + 1)) != -1) { - if (lastIndex < length - 1) { - char c = input.charAt(lastIndex + 1); - ChatColor col = getByChar(c); + // Search backwards from the end as it is faster + for (int index = length - 1; index > -1; index--) { + char section = input.charAt(index); + if (section == COLOR_CHAR && index < length - 1) { + char c = input.charAt(index + 1); + ChatColor color = getByChar(c); - if (col != null) { - if (col.isColor()) { - result = col.toString(); - } else if (col.isFormat()) { - result += col.toString(); + if (color != null) { + result = color.toString() + result; + + // Once we find a color or reset we can stop searching + if (color.isColor() || color.equals(RESET)) { + break; } } } |