Package org.parboiled.buffers
Class MutableInputBuffer
- java.lang.Object
-
- org.parboiled.buffers.MutableInputBuffer
-
- All Implemented Interfaces:
InputBuffer
public class MutableInputBuffer extends java.lang.Object implements InputBuffer
An InputBuffer wrapping another InputBuffer and providing for the ability to insert (and undo) characters at certain index positions. Inserted chars do not appear in extracted text and have the same positions as the original chars at their indices. Note that this implementation is optimized for a rather small number of insertions and will perform badly with a large number of insertions.
-
-
Constructor Summary
Constructors Constructor Description MutableInputBuffer(InputBuffer buffer)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description charcharAt(int index)Returns the character at the given index.java.lang.Stringextract(int start, int end)Constructs a newStringfrom all character between the given indices.java.lang.Stringextract(IndexRange range)Constructs a newStringfrom all character covered by the given IndexRange.java.lang.StringextractLine(int lineNumber)Constructs a newStringcontaining all characters with the given line number except for the trailing newline.intgetLineCount()Returns the number of lines in the input buffer.intgetOriginalIndex(int index)Translates the given index from the scope of this InputBuffer to the scope of the original, underlying char array.PositiongetPosition(int index)Returns the line and column number of the character with the given index encapsulated in aPositionobject.voidinsertChar(int index, char c)voidreplaceInsertedChar(int index, char c)booleantest(int index, char[] characters)Determines whether the characters starting at the given index match the ones from the given array (in order).charundoCharInsertion(int index)
-
-
-
Constructor Detail
-
MutableInputBuffer
public MutableInputBuffer(InputBuffer buffer)
-
-
Method Detail
-
charAt
public char charAt(int index)
Description copied from interface:InputBufferReturns the character at the given index. If the index is invalid the method returnsChars.EOI.- Specified by:
charAtin interfaceInputBuffer- Parameters:
index- the index- Returns:
- the character at the given index or Chars.EOI.
-
test
public boolean test(int index, char[] characters)Description copied from interface:InputBufferDetermines whether the characters starting at the given index match the ones from the given array (in order).- Specified by:
testin interfaceInputBuffer- Parameters:
index- the index into the input buffer where to start the comparisoncharacters- the characters to test against the input buffer- Returns:
- true if matched
-
getPosition
public Position getPosition(int index)
Description copied from interface:InputBufferReturns the line and column number of the character with the given index encapsulated in aPositionobject. The very first character has the line number 1 and the column number 1.- Specified by:
getPositionin interfaceInputBuffer- Parameters:
index- the index of the character to get the line number of- Returns:
- the line number
-
getOriginalIndex
public int getOriginalIndex(int index)
Description copied from interface:InputBufferTranslates the given index from the scope of this InputBuffer to the scope of the original, underlying char array. TheDefaultInputBufferimplementation simply returns the given index, but other implementations like theIndentDedentInputBufferor theMutableInputBufferneed to "undo" all compressions and index shiftings performed internally in order to return the underlying index.- Specified by:
getOriginalIndexin interfaceInputBuffer- Parameters:
index- the index relative to this InputBuffer- Returns:
- the index relative to the underlying string or char array
-
extractLine
public java.lang.String extractLine(int lineNumber)
Description copied from interface:InputBufferConstructs a newStringcontaining all characters with the given line number except for the trailing newline.- Specified by:
extractLinein interfaceInputBuffer- Parameters:
lineNumber- the line number to get- Returns:
- the string
-
extract
public java.lang.String extract(int start, int end)Description copied from interface:InputBufferConstructs a newStringfrom all character between the given indices. Invalid indices are automatically adjusted to their respective boundary.- Specified by:
extractin interfaceInputBuffer- Parameters:
start- the start index (inclusively)end- the end index (exclusively)- Returns:
- a new String (non-interned)
-
extract
public java.lang.String extract(IndexRange range)
Description copied from interface:InputBufferConstructs a newStringfrom all character covered by the given IndexRange.- Specified by:
extractin interfaceInputBuffer- Parameters:
range- the IndexRange- Returns:
- a new String (non-interned)
-
getLineCount
public int getLineCount()
Description copied from interface:InputBufferReturns the number of lines in the input buffer.- Specified by:
getLineCountin interfaceInputBuffer- Returns:
- number of lines in the input buffer.
-
insertChar
public void insertChar(int index, char c)
-
undoCharInsertion
public char undoCharInsertion(int index)
-
replaceInsertedChar
public void replaceInsertedChar(int index, char c)
-
-