From bceacc47c4c02e615296ed5177e7fc0104b3f2d9 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Wed, 16 May 2012 18:12:48 -0500 Subject: [PATCH] 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. By: Travis Watkins --- .../src/main/java/org/bukkit/ChatColor.java | 22 ++++++++++--------- .../test/java/org/bukkit/ChatColorTest.java | 10 +++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/ChatColor.java b/paper-api/src/main/java/org/bukkit/ChatColor.java index 6b3f35fb36..fcaa04ef80 100644 --- a/paper-api/src/main/java/org/bukkit/ChatColor.java +++ b/paper-api/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; } } } diff --git a/paper-api/src/test/java/org/bukkit/ChatColorTest.java b/paper-api/src/test/java/org/bukkit/ChatColorTest.java index ed7000206c..80108a5d3a 100644 --- a/paper-api/src/test/java/org/bukkit/ChatColorTest.java +++ b/paper-api/src/test/java/org/bukkit/ChatColorTest.java @@ -70,4 +70,14 @@ public class ChatColorTest { String u = ChatColor.BLACK.toString() + ChatColor.DARK_BLUE + ChatColor.DARK_GREEN + ChatColor.DARK_AQUA + ChatColor.DARK_RED + ChatColor.DARK_PURPLE + ChatColor.GOLD + ChatColor.GRAY + ChatColor.DARK_GRAY + ChatColor.BLUE + ChatColor.GREEN + ChatColor.GREEN + ChatColor.AQUA + ChatColor.AQUA + ChatColor.RED + ChatColor.RED + ChatColor.LIGHT_PURPLE + ChatColor.LIGHT_PURPLE + ChatColor.YELLOW + ChatColor.YELLOW + ChatColor.WHITE + ChatColor.WHITE + ChatColor.MAGIC + ChatColor.MAGIC + " & more"; assertThat(t, is(u)); } + + @Test + public void getChatColors() { + String s = String.format("%c%ctest%c%ctest%c", ChatColor.COLOR_CHAR, ChatColor.RED.getChar(), ChatColor.COLOR_CHAR, ChatColor.ITALIC.getChar(), ChatColor.COLOR_CHAR); + String expected = ChatColor.RED.toString() + ChatColor.ITALIC; + assertThat(ChatColor.getLastColors(s), is(expected)); + + s = String.format("%c%ctest%c%ctest", ChatColor.COLOR_CHAR, ChatColor.RED.getChar(), ChatColor.COLOR_CHAR, ChatColor.BLUE.getChar()); + assertThat(ChatColor.getLastColors(s), is(ChatColor.BLUE.toString())); + } }