public class LineOffsetConverter extends ManagedIntArray
This class works for strings with arbitrary line terminators.
The implementation works by storing all character offsets of the newline characters. For multi-character line endings (i.e. CR+LF on windows) the last offset is stored. For space/performance reasons, these offsets are stored in a bare array, which is managed by the base class.
Note: This class extends ManagedIntArray
although delegation
would be more elegant. The problem, however, is, that ManagedIntArray
does not support delegation. The idea of this class was to provide protected
access to its internals, which only works via inheritance. Actually, this is
not interface inheritance (ManagedIntArray
has no public method,
btw.) but implementation inheritance.
array, size
Constructor and Description |
---|
LineOffsetConverter(java.lang.String s)
Constructor
|
Modifier and Type | Method and Description |
---|---|
int |
getLine(int offset)
Returns the (one based) line for the character at the given (zero based)
offset.
|
int |
getLineCount()
Returns the number of lines of the input string.
|
int |
getOffset(int line)
Returns the (zero based) offset of the first character of the given line
(starting at 1).
|
boolean |
isValidLine(int line)
Returns whether the given line is valid for the converter.
|
addArrayElement, addArrayElements
public LineOffsetConverter(java.lang.String s)
public int getLineCount()
public int getOffset(int line)
java.lang.AssertionError
- if the line is not valid for the string.public boolean isValidLine(int line)
public int getLine(int offset)