Merge pull request #4357 from Nightfirecat/fix-remove-tags

text: Fix removeTags for isolated < and > chars
This commit is contained in:
Adam
2018-07-16 19:35:16 -04:00
committed by GitHub
2 changed files with 7 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018, Joshua Filby <joshua@filby.me>
* Copyright (c) 2018, Jordan Atwood <jordan.atwood423@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -24,11 +25,14 @@
*/
package net.runelite.client.util;
import java.util.regex.Pattern;
/**
* A set of utilities to use when dealing with text.
*/
public class Text
{
private static final Pattern TAG_REGEXP = Pattern.compile("<[^>]*>");
/**
* Removes all tags from the given `str`.
@@ -38,28 +42,7 @@ public class Text
*/
public static String removeTags(String str)
{
StringBuilder builder = new StringBuilder(str.length());
boolean inTag = false;
for (int i = 0; i < str.length(); i++)
{
char currentChar = str.charAt(i);
if (currentChar == '<')
{
inTag = true;
}
else if (currentChar == '>')
{
inTag = false;
}
else if (!inTag)
{
builder.append(currentChar);
}
}
return builder.toString();
return TAG_REGEXP.matcher(str).replaceAll("");
}
}

View File

@@ -37,6 +37,8 @@ public class TextTest
assertEquals("Zezima (level-126)", Text.removeTags("<col=ffffff><img=2>Zezima<col=00ffff> (level-126)"));
assertEquals("", Text.removeTags("<colrandomtext test>"));
assertEquals("Not so much.", Text.removeTags("<col=FFFFFF This is a very special message.</col>Not so much."));
assertEquals("Use Item -> Man", Text.removeTags("Use Item -> Man"));
assertEquals("a < b", Text.removeTags("a < b"));
assertEquals("Remove no tags", Text.removeTags("Remove no tags"));
}