From 7fd6f5b3ff10fbdb2088eee3da78614ecf1b74ad Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Thu, 28 Feb 2019 11:55:12 -0800 Subject: [PATCH] Fix search highlight in multi-whitespace bodies. The way the highlight was done could get screwed up if you had multiple whitespaces in a row. This particularly came up with messages with multiple newlines. --- .../securesms/util/SearchUtil.java | 29 ++++++++++++------- .../securesms/util/SearchUtilTest.java | 9 ++++++ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/src/org/thoughtcrime/securesms/util/SearchUtil.java b/src/org/thoughtcrime/securesms/util/SearchUtil.java index 0fe4b59abe..4125caacf3 100644 --- a/src/org/thoughtcrime/securesms/util/SearchUtil.java +++ b/src/org/thoughtcrime/securesms/util/SearchUtil.java @@ -61,25 +61,32 @@ public class SearchUtil { @NonNull String text, @NonNull String highlight) { + if (text.length() == 0) { + return Collections.emptyList(); + } + String normalizedText = text.toLowerCase(locale); String normalizedHighlight = highlight.toLowerCase(locale); List highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList(); - List textTokens = Stream.of(normalizedText.split("\\s")).filter(s -> s.trim().length() > 0).toList(); List> ranges = new LinkedList<>(); - int textListIndex = 0; - int textCharIndex = 0; + int lastHighlightEndIndex = 0; for (String highlightToken : highlightTokens) { - for (int i = textListIndex; i < textTokens.size(); i++) { - if (textTokens.get(i).startsWith(highlightToken)) { - textListIndex = i + 1; - ranges.add(new Pair<>(textCharIndex, textCharIndex + highlightToken.length())); - textCharIndex += textTokens.get(i).length() + 1; - break; - } - textCharIndex += textTokens.get(i).length() + 1; + int index; + + do { + index = normalizedText.indexOf(highlightToken, lastHighlightEndIndex); + lastHighlightEndIndex = index + highlightToken.length(); + } while (index > 0 && !Character.isWhitespace(normalizedText.charAt(index - 1))); + + if (index >= 0) { + ranges.add(new Pair<>(index, lastHighlightEndIndex)); + } + + if (index < 0 || lastHighlightEndIndex >= normalizedText.length()) { + break; } } diff --git a/test/unitTest/java/org/thoughtcrime/securesms/util/SearchUtilTest.java b/test/unitTest/java/org/thoughtcrime/securesms/util/SearchUtilTest.java index e70ff88073..fcca0bad43 100644 --- a/test/unitTest/java/org/thoughtcrime/securesms/util/SearchUtilTest.java +++ b/test/unitTest/java/org/thoughtcrime/securesms/util/SearchUtilTest.java @@ -23,6 +23,15 @@ public class SearchUtilTest { assertEquals(Arrays.asList(new Pair<>(0, 1)), result); } + @Test + public void getHighlightRanges_singleHighlightTokenWithNewLines() { + String text = "123\n\n\nabc"; + String highlight = "a"; + List> result = SearchUtil.getHighlightRanges(LOCALE, text, highlight); + + assertEquals(Arrays.asList(new Pair<>(6, 7)), result); + } + @Test public void getHighlightRanges_multipleHighlightTokens() { String text = "a bc";