From 58ff9c16edaa33cc84f97ec3365f44e6631b7475 Mon Sep 17 00:00:00 2001 From: Marc Gurevitx Date: Wed, 10 Jul 2024 16:01:50 +0300 Subject: [PATCH] Fix for #156 --- .gitignore | 3 +++ MiniScript-cpp/src/ShellIntrinsics.cpp | 2 +- MiniScript-cpp/tests/testFileReadLines.ms | 25 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 MiniScript-cpp/tests/testFileReadLines.ms diff --git a/.gitignore b/.gitignore index 263764c..003bc2a 100644 --- a/.gitignore +++ b/.gitignore @@ -52,3 +52,6 @@ Debug miniscript-cpp.dir *.vcxproj* *.sln + +# Tests fallout +_*.txt diff --git a/MiniScript-cpp/src/ShellIntrinsics.cpp b/MiniScript-cpp/src/ShellIntrinsics.cpp index 780e1f2..3b1710b 100644 --- a/MiniScript-cpp/src/ShellIntrinsics.cpp +++ b/MiniScript-cpp/src/ShellIntrinsics.cpp @@ -727,7 +727,7 @@ static IntrinsicResult intrinsic_readLines(Context *context, IntrinsicResult par partialLine = ""; } list.Add(line); - if (buf[i] == '\n' && i+1 < bytesRead && buf[i+1] == '\r') i++; + if (buf[i] == '\r' && i+1 < bytesRead && buf[i+1] == '\n') i++; if (i+1 < bytesRead && buf[i+1] == 0) i++; lineStart = i + 1; } diff --git a/MiniScript-cpp/tests/testFileReadLines.ms b/MiniScript-cpp/tests/testFileReadLines.ms new file mode 100644 index 0000000..07d7d11 --- /dev/null +++ b/MiniScript-cpp/tests/testFileReadLines.ms @@ -0,0 +1,25 @@ +import "qa" + +testDir = "tests/" + +testFileReadLines = function + fn = file.child(testDir, "_cr.txt") + f = file.open(fn, "w") + f.write("a" + char(13) + "b" + char(13) + "c") + f.close + qa.assertEqual file.readLines("tests/_cr.txt"), ["a", "b", "c"] + + fn = file.child(testDir, "_lf.txt") + f = file.open(fn, "w") + f.write("a" + char(10) + "b" + char(10) + "c") + f.close + qa.assertEqual file.readLines("tests/_lf.txt"), ["a", "b", "c"] + + fn = file.child(testDir, "_crlf.txt") + f = file.open(fn, "w") + f.write("a" + char(13) + char(10) + "b" + char(13) + char(10) + "c") + f.close + qa.assertEqual file.readLines("tests/_crlf.txt"), ["a", "b", "c"] +end function + +if refEquals(locals, globals) then testFileReadLines