Пример #1
0
  /**
   * entabLine: process one line, replacing blanks with tabs.
   *
   * @param line - the string to be processed
   */
  public String entabLine(String line) {
    int N = line.length(), outCol = 0;
    StringBuffer sb = new StringBuffer();
    char ch;
    int consumedSpaces = 0;

    for (int inCol = 0; inCol < N; inCol++) {
      ch = line.charAt(inCol);
      // If we get a space, consume it, don't output it.
      // If this takes us to a tab stop, output a tab character.
      if (ch == ' ') {
        Debug.println("space", "Got space at " + inCol);
        if (!tabs.isTabStop(inCol)) {
          consumedSpaces++;
        } else {
          Debug.println("tab", "Got a Tab Stop " + inCol);
          sb.append('\t');
          outCol += consumedSpaces;
          consumedSpaces = 0;
        }
        continue;
      }

      // We're at a non-space; if we're just past a tab stop, we need
      // to put the "leftover" spaces back out, since we consumed
      // them above.
      while (inCol - 1 > outCol) {
        Debug.println("pad", "Padding space at " + inCol);
        sb.append(' ');
        outCol++;
      }

      // Now we have a plain character to output.
      sb.append(ch);
      outCol++;
    }
    // If line ended with trailing (or only!) spaces, preserve them.
    for (int i = 0; i < consumedSpaces; i++) {
      Debug.println("trail", "Padding space at end # " + i);
      sb.append(' ');
    }
    return sb.toString();
  }