@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); }
static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) { if (text.length() == 0) { return Collections.emptyList(); } String normalizedText = text.toLowerCase(locale); String normalizedHighlight = highlight.toLowerCase(locale); List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList(); List<Pair<Integer, Integer>> ranges = new LinkedList<>(); int lastHighlightEndIndex = 0; for (String highlightToken : highlightTokens) { 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; } } if (ranges.size() != highlightTokens.size()) { return Collections.emptyList(); } return ranges; }
SearchUtil { static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) { if (text.length() == 0) { return Collections.emptyList(); } String normalizedText = text.toLowerCase(locale); String normalizedHighlight = highlight.toLowerCase(locale); List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList(); List<Pair<Integer, Integer>> ranges = new LinkedList<>(); int lastHighlightEndIndex = 0; for (String highlightToken : highlightTokens) { 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; } } if (ranges.size() != highlightTokens.size()) { return Collections.emptyList(); } return ranges; } }
SearchUtil { static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) { if (text.length() == 0) { return Collections.emptyList(); } String normalizedText = text.toLowerCase(locale); String normalizedHighlight = highlight.toLowerCase(locale); List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList(); List<Pair<Integer, Integer>> ranges = new LinkedList<>(); int lastHighlightEndIndex = 0; for (String highlightToken : highlightTokens) { 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; } } if (ranges.size() != highlightTokens.size()) { return Collections.emptyList(); } return ranges; }  }
SearchUtil { static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) { if (text.length() == 0) { return Collections.emptyList(); } String normalizedText = text.toLowerCase(locale); String normalizedHighlight = highlight.toLowerCase(locale); List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList(); List<Pair<Integer, Integer>> ranges = new LinkedList<>(); int lastHighlightEndIndex = 0; for (String highlightToken : highlightTokens) { 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; } } if (ranges.size() != highlightTokens.size()) { return Collections.emptyList(); } return ranges; }  static Spannable getHighlightedSpan(@NonNull Locale locale,
                                             @NonNull StyleFactory styleFactory,
                                             @Nullable String text,
                                             @Nullable String highlight); static Spannable getHighlightedSpan(@NonNull Locale locale,
                                             @NonNull StyleFactory styleFactory,
                                             @Nullable Spannable text,
                                             @Nullable String highlight); }
SearchUtil { static List<Pair<Integer, Integer>> getHighlightRanges(@NonNull Locale locale, @NonNull String text, @NonNull String highlight) { if (text.length() == 0) { return Collections.emptyList(); } String normalizedText = text.toLowerCase(locale); String normalizedHighlight = highlight.toLowerCase(locale); List<String> highlightTokens = Stream.of(normalizedHighlight.split("\\s")).filter(s -> s.trim().length() > 0).toList(); List<Pair<Integer, Integer>> ranges = new LinkedList<>(); int lastHighlightEndIndex = 0; for (String highlightToken : highlightTokens) { 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; } } if (ranges.size() != highlightTokens.size()) { return Collections.emptyList(); } return ranges; }  static Spannable getHighlightedSpan(@NonNull Locale locale,
                                             @NonNull StyleFactory styleFactory,
                                             @Nullable String text,
                                             @Nullable String highlight); static Spannable getHighlightedSpan(@NonNull Locale locale,
                                             @NonNull StyleFactory styleFactory,
                                             @Nullable Spannable text,
                                             @Nullable String highlight);  }