function offsetToLineNum(textOrLines, offset) {
    if (Array.isArray(textOrLines)) {
        var lines = textOrLines,
            total = 0,
            line;
        for (line = 0; line < lines.length; line++) {
            if (total < offset) {
                // add 1 per line since /n were removed by splitting, but they needed to
                // contribute to the total offset count
                total += lines[line].length + 1;
            } else if (total === offset) {
                return line;
            } else {
                return line - 1;
            }
        }

        // if offset is NOT over the total then offset is in the last line
        if (offset <= total) {
            return line - 1;
        } else {
            return undefined;
        }
    } else {
        return textOrLines.substr(0, offset).split("\n").length - 1;
    }
}
function offsetToLineNum(textOrLines, offset) {
    if (Array.isArray(textOrLines)) {
        var lines = textOrLines,
            total = 0,
            line;
        for (line = 0; line < lines.length; line++) {
            if (total < offset) {
                // add 1 per line since /n were removed by splitting, but they needed to
                // contribute to the total offset count
                total += lines[line].length + 1;
            } else if (total === offset) {
                return line;
            } else {
                return line - 1;
            }
        }

        // if offset is NOT over the total then offset is in the last line
        if (offset <= total) {
            return line - 1;
        } else {
            return undefined;
        }
    } else {
        return textOrLines.substr(0, offset).split("\n").length - 1;
    }
}
function offsetToLineNum ( textOrLines , offset ) { if ( Array . isArray ( textOrLines ) ) { var lines = textOrLines , total = 0 , line ; for ( line = 0 ; line < lines . length ; line ++ ) { if ( total < offset ) { // add 1 per line since /n were removed by splitting, but they needed to // contribute to the total offset count total += lines [ line ] . length + 1 ; } else if ( total === offset ) { return line ; } else { return line - 1 ; } } // if offset is NOT over the total then offset is in the last line if ( offset <= total ) { return line - 1 ; } else { return undefined ; } } else { return textOrLines . substr ( 0 , offset ) . split ( "\n" ) . length - 1 ; } }
Copied from StringUtils.js
Returns a line number corresponding to an offset in some text. The text can
be specified as a single string or as an array of strings that correspond to
the lines of the string.

Specify the text in lines when repeatedly calling the function on the same
text in a loop. Use getLines() to divide the text into lines, then repeatedly call
this function to compute a line number from the offset.

@param {string | Array.<string>} textOrLines - string or array of lines from which
to compute the line number from the offset
@param {number} offset
@return {number} line number
Copied from StringUtils . js Returns a line number corresponding to an offset in some text . The text can be specified as a single string or as an array of strings that correspond to the lines of the string .
offsetToLineNum - offset to the last line of the file