diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java index f3629054bc..76f51245ee 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/grandexchange/GrandExchangePlugin.java @@ -28,6 +28,7 @@ package net.runelite.client.plugins.grandexchange; +import com.google.common.annotations.VisibleForTesting; import com.google.common.primitives.Shorts; import com.google.common.reflect.TypeToken; import com.google.gson.Gson; @@ -182,7 +183,8 @@ public class GrandExchangePlugin extends Plugin /** * Logic from {@link org.apache.commons.text.similarity.FuzzyScore} */ - private static List findFuzzyIndices(String term, String query) + @VisibleForTesting + static List findFuzzyIndices(String term, String query) { List indices = new ArrayList<>(); @@ -201,7 +203,9 @@ public class GrandExchangePlugin extends Plugin { final char queryChar = queryLowerCase.charAt(queryIndex); - for (; termIndex < termLowerCase.length(); termIndex++) + boolean termCharacterMatchFound = false; + for (; termIndex < termLowerCase.length() + && !termCharacterMatchFound; termIndex++) { final char termChar = termLowerCase.charAt(termIndex); @@ -211,7 +215,7 @@ public class GrandExchangePlugin extends Plugin // we can leave the nested loop. Every character in the // query can match at most one character in the term. - break; + termCharacterMatchFound = true; } } } @@ -548,10 +552,11 @@ public class GrandExchangePlugin extends Plugin StringBuilder newItemName = new StringBuilder(itemName); for (int index : indices) { - if (wasFuzzySearch && (itemName.charAt(index) == ' ' || itemName.charAt(index) == '-')) + if (itemName.charAt(index) == ' ' || itemName.charAt(index) == '-') { continue; } + newItemName.insert(index + 1, ""); newItemName.insert(index, underlineTag); } diff --git a/runelite-client/src/test/java/net/runelite/client/plugins/grandexchange/GrandExchangePluginTest.java b/runelite-client/src/test/java/net/runelite/client/plugins/grandexchange/GrandExchangePluginTest.java new file mode 100644 index 0000000000..dc16d25dfc --- /dev/null +++ b/runelite-client/src/test/java/net/runelite/client/plugins/grandexchange/GrandExchangePluginTest.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2020, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.client.plugins.grandexchange; + +import java.util.Arrays; +import java.util.List; +import static net.runelite.client.plugins.grandexchange.GrandExchangePlugin.findFuzzyIndices; +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class GrandExchangePluginTest +{ + @Test + public void testFindFuzzyIndices() + { + List fuzzyIndices = findFuzzyIndices("Ancestral robe bottom", "obby"); + // robe bottom + assertEquals(Arrays.asList(11, 12, 15), fuzzyIndices); + } +} \ No newline at end of file