summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIván Canales <kanales@users.noreply.github.com>2020-12-05 17:41:16 +0100
committerGitHub <noreply@github.com>2020-12-05 17:41:16 +0100
commitc10280214e5e15fb31b83368082916b9f25470f9 (patch)
treec0ca3b76a688c625629f23e8e77a48f1dbfbbef8
parent2d5c742cf608b2ce0c5a51fb80f34cfd8ffd2e83 (diff)
fix(std/bufio): Remove '\r' at the end of Windows lines (#8447)
Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com>
-rw-r--r--std/io/bufio.ts10
-rw-r--r--std/io/bufio_test.ts12
2 files changed, 21 insertions, 1 deletions
diff --git a/std/io/bufio.ts b/std/io/bufio.ts
index 954fc5eee..df3ecb002 100644
--- a/std/io/bufio.ts
+++ b/std/io/bufio.ts
@@ -706,5 +706,13 @@ export async function* readStringDelim(
export async function* readLines(
reader: Reader,
): 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;
+ }
}
diff --git a/std/io/bufio_test.ts b/std/io/bufio_test.ts
index 03f699d50..804d59e99 100644
--- a/std/io/bufio_test.ts
+++ b/std/io/bufio_test.ts
@@ -436,6 +436,10 @@ Deno.test("readStringDelimAndLines", async function (): Promise<void> {
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"));
+ // 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_ = [];
for await (const l of readLines(linesData)) {
@@ -444,6 +448,14 @@ Deno.test("readStringDelimAndLines", async function (): Promise<void> {
assertEquals(lines_.length, 10);
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(