monaco-editor-core
Version:
A browser based code editor
43 lines (42 loc) • 1.56 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export function countEOL(text) {
let eolCount = 0;
let firstLineLength = 0;
let lastLineStart = 0;
let eol = 0 /* StringEOL.Unknown */;
for (let i = 0, len = text.length; i < len; i++) {
const chr = text.charCodeAt(i);
if (chr === 13 /* CharCode.CarriageReturn */) {
if (eolCount === 0) {
firstLineLength = i;
}
eolCount++;
if (i + 1 < len && text.charCodeAt(i + 1) === 10 /* CharCode.LineFeed */) {
// \r\n... case
eol |= 2 /* StringEOL.CRLF */;
i++; // skip \n
}
else {
// \r... case
eol |= 3 /* StringEOL.Invalid */;
}
lastLineStart = i + 1;
}
else if (chr === 10 /* CharCode.LineFeed */) {
// \n... case
eol |= 1 /* StringEOL.LF */;
if (eolCount === 0) {
firstLineLength = i;
}
eolCount++;
lastLineStart = i + 1;
}
}
if (eolCount === 0) {
firstLineLength = text.length;
}
return [eolCount, firstLineLength, text.length - lastLineStart, eol];
}