Skip to content

Commit 7f98bef

Browse files
committed
Reconcile carriageReturn handling
1 parent b07dc87 commit 7f98bef

File tree

1 file changed

+43
-10
lines changed

1 file changed

+43
-10
lines changed

src/compiler/utilities.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3964,13 +3964,29 @@ namespace ts {
39643964

39653965
function appendRawSmall(text: string) {
39663966
const len = text.length;
3967-
for (let pos = 0; pos < len; pos++) {
3967+
let pos = 0;
3968+
while (pos < len) {
39683969
const ch = text.charCodeAt(pos);
39693970
appendCharCode(ch);
3970-
// Ignore carriageReturn, since we mark the following lineFeed as the newline anyway
3971-
if (isLineBreak(ch) && ch !== CharacterCodes.carriageReturn) {
3972-
++lineCount;
3973-
linePos = totalChars;
3971+
pos++;
3972+
switch (ch) {
3973+
case CharacterCodes.carriageReturn:
3974+
const nextChar = text.charCodeAt(pos);
3975+
if (nextChar === CharacterCodes.lineFeed) {
3976+
appendCharCode(nextChar);
3977+
pos++;
3978+
}
3979+
// falls through
3980+
case CharacterCodes.lineFeed:
3981+
++lineCount;
3982+
linePos = totalChars;
3983+
break;
3984+
default:
3985+
if (ch > CharacterCodes.maxAsciiCharacter && isLineBreak(ch)) {
3986+
++lineCount;
3987+
linePos = totalChars;
3988+
}
3989+
break;
39743990
}
39753991
}
39763992

@@ -3981,14 +3997,31 @@ namespace ts {
39813997
flushBuffer();
39823998

39833999
const len = text.length;
3984-
for (let pos = 0; pos < len; pos++) {
4000+
let pos = 0;
4001+
while (pos < len) {
39854002
const ch = text.charCodeAt(pos);
39864003
++totalChars;
39874004
lastChar = ch;
3988-
// Ignore carriageReturn, since we mark the following lineFeed as the newline anyway
3989-
if (isLineBreak(ch) && ch !== CharacterCodes.carriageReturn) {
3990-
++lineCount;
3991-
linePos = totalChars;
4005+
pos++;
4006+
switch (ch) {
4007+
case CharacterCodes.carriageReturn:
4008+
const nextChar = text.charCodeAt(pos);
4009+
if (nextChar === CharacterCodes.lineFeed) {
4010+
++totalChars;
4011+
lastChar = nextChar;
4012+
pos++;
4013+
}
4014+
// falls through
4015+
case CharacterCodes.lineFeed:
4016+
++lineCount;
4017+
linePos = totalChars;
4018+
break;
4019+
default:
4020+
if (ch > CharacterCodes.maxAsciiCharacter && isLineBreak(ch)) {
4021+
++lineCount;
4022+
linePos = totalChars;
4023+
}
4024+
break;
39924025
}
39934026
}
39944027

0 commit comments

Comments
 (0)