mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
fix(std/bufio): Remove '\r' at the end of Windows lines (#8447)
Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
This commit is contained in:
parent
2d5c742cf6
commit
c10280214e
2 changed files with 21 additions and 1 deletions
|
@ -706,5 +706,13 @@ export async function* readStringDelim(
|
||||||
export async function* readLines(
|
export async function* readLines(
|
||||||
reader: Reader,
|
reader: Reader,
|
||||||
): AsyncIterableIterator<string> {
|
): AsyncIterableIterator<string> {
|
||||||
yield* readStringDelim(reader, "\n");
|
for await (let chunk of readStringDelim(reader, "\n")) {
|
||||||
|
// Finding a CR at the end of the line is evidence of a
|
||||||
|
// "\r\n" at the end of the line. The "\r" part should be
|
||||||
|
// removed too.
|
||||||
|
if (chunk.endsWith("\r")) {
|
||||||
|
chunk = chunk.slice(0, -1);
|
||||||
|
}
|
||||||
|
yield chunk;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -436,6 +436,10 @@ Deno.test("readStringDelimAndLines", async function (): Promise<void> {
|
||||||
assertEquals(chunks_, ["Hello World", "Hello World 2", "Hello World 3"]);
|
assertEquals(chunks_, ["Hello World", "Hello World 2", "Hello World 3"]);
|
||||||
|
|
||||||
const linesData = new Deno.Buffer(enc.encode("0\n1\n2\n3\n4\n5\n6\n7\n8\n9"));
|
const linesData = new Deno.Buffer(enc.encode("0\n1\n2\n3\n4\n5\n6\n7\n8\n9"));
|
||||||
|
// consider data with windows newlines too
|
||||||
|
const linesDataWindows = new Deno.Buffer(
|
||||||
|
enc.encode("0\r\n1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9"),
|
||||||
|
);
|
||||||
const lines_ = [];
|
const lines_ = [];
|
||||||
|
|
||||||
for await (const l of readLines(linesData)) {
|
for await (const l of readLines(linesData)) {
|
||||||
|
@ -444,6 +448,14 @@ Deno.test("readStringDelimAndLines", async function (): Promise<void> {
|
||||||
|
|
||||||
assertEquals(lines_.length, 10);
|
assertEquals(lines_.length, 10);
|
||||||
assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
|
assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
|
||||||
|
|
||||||
|
// Now test for "windows" lines
|
||||||
|
lines_.length = 0;
|
||||||
|
for await (const l of readLines(linesDataWindows)) {
|
||||||
|
lines_.push(l);
|
||||||
|
}
|
||||||
|
assertEquals(lines_.length, 10);
|
||||||
|
assertEquals(lines_, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test(
|
Deno.test(
|
||||||
|
|
Loading…
Reference in a new issue