Class CodeBuffer

java.lang.Object
com.google.common.css.compiler.passes.CodeBuffer

public class CodeBuffer extends Object
Class to write/delete characters used by CodePrinter subclasses to print out code. It has finalized core public APIs to endure correct update of character index and line index while operating on the buffer.

This class can be extended and with helper methods if necessary.

See Also:
  • Constructor Details

    • CodeBuffer

      public CodeBuffer()
  • Method Details

    • getOutput

      public final String getOutput()
      Returns buffer as String.
    • getCurrentLength

      public final int getCurrentLength()
      Returns the current length of the buffer.
    • getLastChar

      public final char getLastChar()
      Returns the last character in the buffer.
    • getNextCharIndex

      public final int getNextCharIndex()
      Returns nextCharIndex.
    • getNextLineIndex

      public final int getNextLineIndex()
      Returns nextLineIndex.
    • getLastCharIndex

      public final int getLastCharIndex()
      Returns lastCharIndex.
    • getLastLineIndex

      public final int getLastLineIndex()
      Returns the index of the line which contains the last character at lastCharIndex. It is always the same or 1 index behind nextLineIndex.
    • append

      public final CodeBuffer append(@Nullable String str)
      Appends str to the buffer. The string is safe to contain newlines. nextCharIndex and nextLineIndex will be updated accordingly.

      Prefer to use append(char c) API to append characters for efficiency.

    • append

      public final CodeBuffer append(char c)
      Appends c to the buffer and update nextCharIndex.
    • append

      public final CodeBuffer append(Object o)
      Appends the toString representation of o to the buffer.
    • startNewLine

      public final CodeBuffer startNewLine()
      Appends a newline to buffer, resetting nextCharIndex and incrementing nextLineIndex.
    • deleteLastChar

      public final CodeBuffer deleteLastChar()
      Deletes the last character in the buffer and updates the nextCharIndex and nextLineIndex.
    • deleteLastChars

      public final CodeBuffer deleteLastChars(int n)
      Deletes last n characters in the buffer.
    • reset

      public final CodeBuffer reset()
      Clears the contents of the buffer and resets nextCharIndex and nextLineIndex.
    • deleteLastCharIfCharIs

      public void deleteLastCharIfCharIs(char ch)
      Deletes the last character from the string builder if the character is as given.

      Subclasses can modify this method in order to delete more in cases where they've added extra delimiters.

      Parameters:
      ch - the character to delete
    • deleteEndingIfEndingIs

      public void deleteEndingIfEndingIs(String s)
      Deletes the end of the buffer if it exactly equals s.