From 3379494a67aaf7db4e1d952255c581ba2592dee0 Mon Sep 17 00:00:00 2001 From: Broooklyn <54762282+Broooklyn@users.noreply.github.com> Date: Thu, 4 Jun 2020 22:27:51 -0700 Subject: [PATCH] util: Add removeFormattingTags text function --- .../client/plugins/emojis/EmojiPlugin.java | 30 ++----------------- .../java/net/runelite/client/util/Text.java | 27 +++++++++++++++++ .../net/runelite/client/util/TextTest.java | 14 +++++++++ 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/emojis/EmojiPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/emojis/EmojiPlugin.java index 06b97428bb..e63a97c3cc 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/emojis/EmojiPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/emojis/EmojiPlugin.java @@ -26,7 +26,6 @@ package net.runelite.client.plugins.emojis; import java.awt.image.BufferedImage; import java.util.Arrays; -import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Nullable; import javax.inject.Inject; @@ -46,6 +45,7 @@ import net.runelite.client.eventbus.Subscribe; import net.runelite.client.plugins.Plugin; import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.util.ImageUtil; +import net.runelite.client.util.Text; @PluginDescriptor( name = "Emojis", @@ -55,7 +55,6 @@ import net.runelite.client.util.ImageUtil; @Slf4j public class EmojiPlugin extends Plugin { - private static final Pattern TAG_REGEXP = Pattern.compile("<[^>]*>"); private static final Pattern WHITESPACE_REGEXP = Pattern.compile("[\\s\\u00A0]"); @Inject @@ -179,7 +178,7 @@ public class EmojiPlugin extends Plugin for (int i = 0; i < messageWords.length; i++) { // Remove tags except for and - final String trigger = removeTags(messageWords[i]); + final String trigger = Text.removeFormattingTags(messageWords[i]); final Emoji emoji = Emoji.getEmoji(trigger); if (emoji == null) @@ -201,29 +200,4 @@ public class EmojiPlugin extends Plugin return Strings.join(messageWords, " "); } - - /** - * Remove tags, except for <lt> and <gt> - * - * @return - */ - private static String removeTags(String str) - { - StringBuffer stringBuffer = new StringBuffer(); - Matcher matcher = TAG_REGEXP.matcher(str); - while (matcher.find()) - { - matcher.appendReplacement(stringBuffer, ""); - String match = matcher.group(0); - switch (match) - { - case "": - case "": - stringBuffer.append(match); - break; - } - } - matcher.appendTail(stringBuffer); - return stringBuffer.toString(); - } } diff --git a/runelite-client/src/main/java/net/runelite/client/util/Text.java b/runelite-client/src/main/java/net/runelite/client/util/Text.java index 6fc303a97e..b98b8dbe8e 100644 --- a/runelite-client/src/main/java/net/runelite/client/util/Text.java +++ b/runelite-client/src/main/java/net/runelite/client/util/Text.java @@ -30,6 +30,7 @@ import com.google.common.base.Joiner; import com.google.common.base.Splitter; import java.util.Collection; import java.util.List; +import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.text.WordUtils; import org.apache.commons.text.similarity.JaroWinklerDistance; @@ -83,6 +84,32 @@ public class Text return TAG_REGEXP.matcher(str).replaceAll(""); } + /** + * Remove tags from the given string, except for <lt> and <gt> + * + * @param str The string to remove formatting tags from. + * @return The given string with all formatting tags removed from it. + */ + public static String removeFormattingTags(String str) + { + StringBuffer stringBuffer = new StringBuffer(); + Matcher matcher = TAG_REGEXP.matcher(str); + while (matcher.find()) + { + matcher.appendReplacement(stringBuffer, ""); + String match = matcher.group(0); + switch (match) + { + case "": + case "": + stringBuffer.append(match); + break; + } + } + matcher.appendTail(stringBuffer); + return stringBuffer.toString(); + } + /** * In addition to removing all tags, replaces nbsp with space, trims string and lowercases it * @param str The string to standardize diff --git a/runelite-client/src/test/java/net/runelite/client/util/TextTest.java b/runelite-client/src/test/java/net/runelite/client/util/TextTest.java index af5815b0a3..f4aa704684 100644 --- a/runelite-client/src/test/java/net/runelite/client/util/TextTest.java +++ b/runelite-client/src/test/java/net/runelite/client/util/TextTest.java @@ -39,7 +39,21 @@ public class TextTest assertEquals("Not so much.", Text.removeTags("Not so much.")); assertEquals("Use Item -> Man", Text.removeTags("Use Item -> Man")); assertEquals("a < b", Text.removeTags("a < b")); + assertEquals("a b", Text.removeTags("a b")); assertEquals("Remove no tags", Text.removeTags("Remove no tags")); } + @Test + public void removeFormattingTags() + { + assertEquals("Test", Text.removeFormattingTags("Test")); + assertEquals("Test", Text.removeFormattingTags("Test")); + assertEquals("Zezima (level-126)", Text.removeFormattingTags("Zezima (level-126)")); + assertEquals("", Text.removeFormattingTags("")); + assertEquals("Not so much.", Text.removeFormattingTags("Not so much.")); + assertEquals("Use Item -> Man", Text.removeFormattingTags("Use Item -> Man")); + assertEquals("a < b", Text.removeFormattingTags("a < b")); + assertEquals("a b", Text.removeFormattingTags("a b")); + assertEquals("Remove no tags", Text.removeFormattingTags("Remove no tags")); + } }