src/location.js
function LocationIndex(string) {
        // ensure newline termination
        if (string[string.length - 1] !== '\n') {
            string += '\n';
        }
        this._stringLength = string.length;
        /*
         * Each triple in _lineStartEndTriples consists of:
         * [0], the 0-based line index of the line the triple represents
         * [1], the 0-based code unit index (into the string) of the start of the line (inclusive)
         * [2], the 0-based code unit index (into the string) of the start of the next line (or the length of the string, if it is the last line)
         * A line starts with a non-newline character,
         * and always ends in a newline character, unless it is the very last line in the string.
         */
        this._lineStartEndTriples = [[0, 0]];
        var nextLineIndex = 1;
        var charIndex = 0;
        while (charIndex < string.length) {
            charIndex = string.indexOf('\n', charIndex);
            if (charIndex === -1) {
                /* istanbul ignore next */
                break;
            }
            charIndex++;// go past the newline
            this._lineStartEndTriples[this._lineStartEndTriples.length - 1].push(charIndex);
            this._lineStartEndTriples.push([nextLineIndex, charIndex]);

            nextLineIndex++;
        }
        this._lineStartEndTriples.pop();
    }
function LocationIndex(string) {
        // ensure newline termination
        if (string[string.length - 1] !== '\n') {
            string += '\n';
        }
        this._stringLength = string.length;
        /*
         * Each triple in _lineStartEndTriples consists of:
         * [0], the 0-based line index of the line the triple represents
         * [1], the 0-based code unit index (into the string) of the start of the line (inclusive)
         * [2], the 0-based code unit index (into the string) of the start of the next line (or the length of the string, if it is the last line)
         * A line starts with a non-newline character,
         * and always ends in a newline character, unless it is the very last line in the string.
         */
        this._lineStartEndTriples = [[0, 0]];
        var nextLineIndex = 1;
        var charIndex = 0;
        while (charIndex < string.length) {
            charIndex = string.indexOf('\n', charIndex);
            if (charIndex === -1) {
                /* istanbul ignore next */
                break;
            }
            charIndex++;// go past the newline
            this._lineStartEndTriples[this._lineStartEndTriples.length - 1].push(charIndex);
            this._lineStartEndTriples.push([nextLineIndex, charIndex]);

            nextLineIndex++;
        }
        this._lineStartEndTriples.pop();
    }
function LocationIndex ( string ) { // ensure newline termination if ( string [ string . length - 1 ] !== '\n' ) { string += '\n' ; } this . _stringLength = string . length ; /*
         * Each triple in _lineStartEndTriples consists of:
         * [0], the 0-based line index of the line the triple represents
         * [1], the 0-based code unit index (into the string) of the start of the line (inclusive)
         * [2], the 0-based code unit index (into the string) of the start of the next line (or the length of the string, if it is the last line)
         * A line starts with a non-newline character,
         * and always ends in a newline character, unless it is the very last line in the string.
         */ this . _lineStartEndTriples = [ [ 0 , 0 ] ] ; var nextLineIndex = 1 ; var charIndex = 0 ; while ( charIndex < string . length ) { charIndex = string . indexOf ( '\n' , charIndex ) ; if ( charIndex === - 1 ) { /* istanbul ignore next */ break ; } charIndex ++ ; // go past the newline this . _lineStartEndTriples [ this . _lineStartEndTriples . length - 1 ] . push ( charIndex ) ; this . _lineStartEndTriples . push ( [ nextLineIndex , charIndex ] ) ; nextLineIndex ++ ; } this . _lineStartEndTriples . pop ( ) ; }
Maps code unit indices into the string to line numbers and column numbers.
@param {string} string String to construct the index for
@class
Maps code unit indices into the string to line numbers and column numbers .