Package org.jparsec

Class SourceLocator


  • final class SourceLocator
    extends java.lang.Object
    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

      Fields 
      Modifier and Type Field Description
      private static char LINE_BREAK
      The line break character.
      (package private) IntList lineBreakIndices
      The 0-based indices of the line break characters scanned so far.
      (package private) int nextColumnIndex
      The 0-based index of the column of the next character to be scanned.
      (package private) int nextIndex
      The 0-based index of the next character to be scanned.
      private java.lang.CharSequence source  
      private int startColumnNumber
      The first column number.
      private int startLineNumber
      The first line number.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static int binarySearch​(IntList ascendingInts, int value)
      Uses binary search to look up the index of the first element in ascendingInts that's greater than or equal to value.
      private int getLineBreakColumnIndex​(int lineIndex)
      Gets the 0-based column number of the line break character for line identified by lineIndex.
      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 by ind using the cached indices of line break characters.
      (package private) Location scanTo​(int index)
      Scans from nextIndex to ind and saves all indices of line break characters into lineBreakIndices and adjusts the current column number as it goes.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LINE_BREAK

        private static final char LINE_BREAK
        The line break character.
        See Also:
        Constant Field Values
      • source

        private final java.lang.CharSequence source
      • lineBreakIndices

        final IntList lineBreakIndices
        The 0-based indices of the line break characters scanned so far.
      • startLineNumber

        private final int startLineNumber
        The first line number.
      • startColumnNumber

        private final int startColumnNumber
        The first column number.
      • nextIndex

        int nextIndex
        The 0-based index of the next character to be scanned.
      • nextColumnIndex

        int nextColumnIndex
        The 0-based index of the column of the next character to be scanned.
    • Constructor Detail

      • SourceLocator

        SourceLocator​(java.lang.CharSequence source)
        Creates a SourceLocator object.
        Parameters:
        source - the source.
      • SourceLocator

        SourceLocator​(java.lang.CharSequence source,
                      int lineNumber,
                      int columnNumber)
        Creates a SourceLocator object.
        Parameters:
        source - the source.
        lineNumber - the starting line number.
        columnNumber - the starting column number.
    • Method Detail

      • locate

        Location locate​(int index)
      • lookup

        Location lookup​(int index)
        Looks up the location identified by ind using the cached indices of line break characters. This assumes that all line-break characters before ind are already scanned.
      • scanTo

        Location scanTo​(int index)
        Scans from nextIndex to ind and saves all indices of line break characters into lineBreakIndices and adjusts the current column number as it goes. The location of the character on ind is returned.

        After this method returns, nextIndex and nextColumnIndex 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 by lineIndex.
      • getLineBreakLocation

        private Location getLineBreakLocation​(int lineIndex)
      • location

        private Location location​(int l,
                                  int c)
      • binarySearch

        static int binarySearch​(IntList ascendingInts,
                                int value)
        Uses binary search to look up the index of the first element in ascendingInts that's greater than or equal to value. If all elements are smaller than value, ascendingInts.size() is returned.