Package org.jparsec
Class SourceLocator
java.lang.Object
org.jparsec.SourceLocator
Locates the line and column number of a 0-based index in the source.
This class internally keeps a cache of the indices of all the line break characters scanned so far, therefore repeated location lookup can be done in amortized log(n) time.
It is not multi-thread safe.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final char
The line break character.(package private) final IntList
The 0-based indices of the line break characters scanned so far.(package private) int
The 0-based index of the column of the next character to be scanned.(package private) int
The 0-based index of the next character to be scanned.private final CharSequence
private final int
The first column number.private final int
The first line number. -
Constructor Summary
ConstructorsConstructorDescriptionSourceLocator
(CharSequence source) Creates aSourceLocator
object.SourceLocator
(CharSequence source, int lineNumber, int columnNumber) Creates aSourceLocator
object. -
Method Summary
Modifier and TypeMethodDescription(package private) static int
binarySearch
(IntList ascendingInts, int value) Uses binary search to look up the index of the first element inascendingInts
that's greater than or equal tovalue
.private int
getLineBreakColumnIndex
(int lineIndex) Gets the 0-based column number of the line break character for line identified bylineIndex
.private Location
getLineBreakLocation
(int lineIndex) (package private) Location
locate
(int index) private Location
location
(int l, int c) (package private) Location
lookup
(int index) Looks up the location identified byind
using the cached indices of line break characters.(package private) Location
scanTo
(int index) Scans fromnextIndex
toind
and saves all indices of line break characters intolineBreakIndices
and adjusts the current column number as it goes.
-
Field Details
-
LINE_BREAK
private static final char LINE_BREAKThe line break character.- See Also:
-
source
-
lineBreakIndices
The 0-based indices of the line break characters scanned so far. -
startLineNumber
private final int startLineNumberThe first line number. -
startColumnNumber
private final int startColumnNumberThe first column number. -
nextIndex
int nextIndexThe 0-based index of the next character to be scanned. -
nextColumnIndex
int nextColumnIndexThe 0-based index of the column of the next character to be scanned.
-
-
Constructor Details
-
SourceLocator
SourceLocator(CharSequence source) Creates aSourceLocator
object.- Parameters:
source
- the source.
-
SourceLocator
SourceLocator(CharSequence source, int lineNumber, int columnNumber) Creates aSourceLocator
object.- Parameters:
source
- the source.lineNumber
- the starting line number.columnNumber
- the starting column number.
-
-
Method Details
-
locate
-
lookup
Looks up the location identified byind
using the cached indices of line break characters. This assumes that all line-break characters beforeind
are already scanned. -
scanTo
Scans fromnextIndex
toind
and saves all indices of line break characters intolineBreakIndices
and adjusts the current column number as it goes. The location of the character onind
is returned.After this method returns,
nextIndex
andnextColumnIndex
will point to the next character to be scanned or the EOF if the end of input is encountered. -
getLineBreakColumnIndex
private int getLineBreakColumnIndex(int lineIndex) Gets the 0-based column number of the line break character for line identified bylineIndex
. -
getLineBreakLocation
-
location
-
binarySearch
Uses binary search to look up the index of the first element inascendingInts
that's greater than or equal tovalue
. If all elements are smaller thanvalue
,ascendingInts.size()
is returned.
-