public String highlight()

in java/src/main/java/com/twitter/twittertext/HitHighlighter.java [40:82]


  public String highlight(String text, List<List<Integer>> hits) {
    if (hits == null || hits.isEmpty()) {
      return text;
    }

    final StringBuilder sb = new StringBuilder(text.length());
    final CharacterIterator iterator = new StringCharacterIterator(text);
    boolean isCounting = true;
    boolean tagOpened = false;
    int currentIndex = 0;
    char currentChar = iterator.first();

    while (currentChar != CharacterIterator.DONE) {
      // TODO: this is slow.
      for (List<Integer> startEnd : hits) {
        if (startEnd.get(0) == currentIndex) {
          sb.append(tag(false));
          tagOpened = true;
        } else if (startEnd.get(1) == currentIndex) {
          sb.append(tag(true));
          tagOpened = false;
        }
      }

      if (currentChar == '<') {
        isCounting = false;
      } else if (currentChar == '>' && !isCounting) {
        isCounting = true;
      }

      if (isCounting) {
        currentIndex++;
      }
      sb.append(currentChar);
      currentChar = iterator.next();
    }

    if (tagOpened) {
      sb.append(tag(true));
    }

    return sb.toString();
  }