mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-27 12:05:22 +00:00
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.
This commit is contained in:
parent
42e94d8f92
commit
7fd6f5b3ff
@ -61,25 +61,32 @@ public class SearchUtil {
|
|||||||
@NonNull String text,
|
@NonNull String text,
|
||||||
@NonNull String highlight)
|
@NonNull String highlight)
|
||||||
{
|
{
|
||||||
|
if (text.length() == 0) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
String normalizedText = text.toLowerCase(locale);
|
String normalizedText = text.toLowerCase(locale);
|
||||||
String normalizedHighlight = highlight.toLowerCase(locale);
|
String normalizedHighlight = highlight.toLowerCase(locale);
|
||||||
List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList();
|
List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList();
|
||||||
List<String> textTokens = Stream.of(normalizedText.split("\\s")).filter(s -> s.trim().length() > 0).toList();
|
|
||||||
|
|
||||||
List<Pair<Integer, Integer>> ranges = new LinkedList<>();
|
List<Pair<Integer, Integer>> ranges = new LinkedList<>();
|
||||||
|
|
||||||
int textListIndex = 0;
|
int lastHighlightEndIndex = 0;
|
||||||
int textCharIndex = 0;
|
|
||||||
|
|
||||||
for (String highlightToken : highlightTokens) {
|
for (String highlightToken : highlightTokens) {
|
||||||
for (int i = textListIndex; i < textTokens.size(); i++) {
|
int index;
|
||||||
if (textTokens.get(i).startsWith(highlightToken)) {
|
|
||||||
textListIndex = i + 1;
|
do {
|
||||||
ranges.add(new Pair<>(textCharIndex, textCharIndex + highlightToken.length()));
|
index = normalizedText.indexOf(highlightToken, lastHighlightEndIndex);
|
||||||
textCharIndex += textTokens.get(i).length() + 1;
|
lastHighlightEndIndex = index + highlightToken.length();
|
||||||
break;
|
} while (index > 0 && !Character.isWhitespace(normalizedText.charAt(index - 1)));
|
||||||
}
|
|
||||||
textCharIndex += textTokens.get(i).length() + 1;
|
if (index >= 0) {
|
||||||
|
ranges.add(new Pair<>(index, lastHighlightEndIndex));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index < 0 || lastHighlightEndIndex >= normalizedText.length()) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,15 @@ public class SearchUtilTest {
|
|||||||
assertEquals(Arrays.asList(new Pair<>(0, 1)), result);
|
assertEquals(Arrays.asList(new Pair<>(0, 1)), result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getHighlightRanges_singleHighlightTokenWithNewLines() {
|
||||||
|
String text = "123\n\n\nabc";
|
||||||
|
String highlight = "a";
|
||||||
|
List<Pair<Integer, Integer>> result = SearchUtil.getHighlightRanges(LOCALE, text, highlight);
|
||||||
|
|
||||||
|
assertEquals(Arrays.asList(new Pair<>(6, 7)), result);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getHighlightRanges_multipleHighlightTokens() {
|
public void getHighlightRanges_multipleHighlightTokens() {
|
||||||
String text = "a bc";
|
String text = "a bc";
|
||||||
|
Loading…
Reference in New Issue
Block a user