Skip to content

Commit

Permalink
Merge pull request runelite#4357 from Nightfirecat/fix-remove-tags
Browse files Browse the repository at this point in the history
text: Fix removeTags for isolated < and > chars
  • Loading branch information
Adam- authored Jul 16, 2018
2 parents d719f46 + 9cf09ee commit a16ff30
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
27 changes: 5 additions & 22 deletions runelite-client/src/main/java/net/runelite/client/util/Text.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018, Joshua Filby <[email protected]>
* Copyright (c) 2018, Jordan Atwood <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand All @@ -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`.
Expand All @@ -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("");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void removeTags()
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"));
}

Expand Down

0 comments on commit a16ff30

Please sign in to comment.