/**
  * Shifts indices by 1 since the Twitter REST Api does not count them correctly for our language
  * runtime.
  *
  * @param entities The entities that need to be adjusted
  * @param indices The indices in the string where there are supplementary chars
  */
 static void adjustEntitiesWithOffsets(
     List<? extends FormattedUrlEntity> entities, List<Integer> indices) {
   if (entities == null || indices == null) return;
   for (FormattedUrlEntity entity : entities) {
     // find all indices <= start and update offsets by that much
     final int start = entity.start;
     int offset = 0;
     for (Integer index : indices) {
       if (index - offset <= start) {
         offset += 1;
       } else {
         break;
       }
     }
     entity.start = entity.start + offset;
     entity.end = entity.end + offset;
   }
 }
  /**
   * Since the unescaping of html causes for example &amp; to turn into & we need to adjust the
   * entity indices after that by 4 characters. This loops through the entities and adjusts them as
   * necessary.
   *
   * @param entities The entities that need to be adjusted
   * @param indices The indices of where there were escaped html chars that we unescaped
   */
  static void adjustIndicesForEscapedChars(
      List<? extends FormattedUrlEntity> entities, List<int[]> indices) {
    if (entities == null || indices == null || indices.isEmpty()) {
      return;
    }
    final int size = indices.size();
    int m = 0; // marker
    int diff = 0; // accumulated difference
    int inDiff; // end difference for escapes in range
    int len; // escaped length
    int start; // escaped start
    int end; // escaped end
    int i; // reusable index
    int[] index;
    // For each of the entities, update the start and end indices
    // Note: tweet entities are sorted.

    for (FormattedUrlEntity entity : entities) {
      inDiff = 0;
      // Go through the escaped entities' indices
      for (i = m; i < size; i++) {
        index = indices.get(i);
        start = index[0];
        end = index[1];
        // len is actually (end - start + 1) - 1
        len = end - start;
        if (end < entity.start) {
          // bump position of the next marker
          diff += len;
          m++;
        } else if (end < entity.end) {
          inDiff += len;
        }
      }
      // Once we've accumulated diffs, calc the offset
      entity.start = entity.start - diff;
      entity.end = entity.end - (diff + inDiff);
    }
  }