on master help sign in
Reading from a file shouldn't strip \n
f8cb234Edwin Brady
libs/prelude/Prelude.idr+8−4
@@ -352,7 +352,13 @@ printLn x = putStrLn (show x)
352||| Read one line of input from stdin, without the trailing newline
352||| Read one line of input from stdin, without the trailing newline
353partial
353partial
354getLine : IO' ffi String
354getLine : IO' ffi String
355−getLine = prim_read
355+getLine = do x <- prim_read
356+ return (reverse (trimNL (reverse x)))
357+ where trimNL : String -> String
358+ trimNL str with (strM str)
359+ trimNL "" | StrNil = ""
360+ trimNL (strCons '\n' xs) | StrCons _ _ = xs
361+ trimNL (strCons x xs) | StrCons _ _ = strCons x xs
356
362
357||| Write a single character to stdout
363||| Write a single character to stdout
358partial
364partial
@@ -548,7 +554,5 @@ readFile fn = do h <- openFile fn Read
548 readFile' h contents =
554 readFile' h contents =
549 do x <- feof h
555 do x <- feof h
550 if not x then do l <- fread h
556 if not x then do l <- fread h
551− case contents of
552− "" => readFile' h l
553− _ => readFile' h (contents ++ "\n" ++ l)
557+ readFile' h (contents ++ l)
554 else return contents
558 else return contents
rts/idris_rts.c−2
@@ -509,8 +509,6 @@ VAL idris_readStr(VM* vm, FILE* h) {
509 size_t n = 0;
509 size_t n = 0;
510 ssize_t len;
510 ssize_t len;
511 len = getline(&buffer, &n, h);
511 len = getline(&buffer, &n, h);
512− strtok(buffer, "\n");
513−
514 if (len <= 0) {
512 if (len <= 0) {
515 ret = MKSTR(vm, "");
513 ret = MKSTR(vm, "");
516 } else {
514 } else {
test/dsl002/test014.idr+2−1
@@ -34,6 +34,7 @@ syntax rreadLine [h] = Use readLine h
34syntax reof [h] = Use eof h
34syntax reof [h] = Use eof h
35
35
36syntax rputStrLn [s] = Lift (putStrLn s)
36syntax rputStrLn [s] = Lift (putStrLn s)
37+syntax rputStr [s] = Lift (putStr s)
37
38
38syntax "if" "opened" [f] "then" [e] "else" [t] = Check f t e
39syntax "if" "opened" [f] "then" [e] "else" [t] = Check f t e
39
40
@@ -57,7 +58,7 @@ readH : String -> RES ()
57readH fn = res (do let x = open fn Reading
58readH fn = res (do let x = open fn Reading
58 if opened x then
59 if opened x then
59 do str <- rreadLine x
60 do str <- rreadLine x
60− rputStrLn str
61+ rputStr str
61 rclose x
62 rclose x
62 else rputStrLn "Error")
63 else rputStrLn "Error")
63
64
test/effects001/expected+1−1
@@ -1,4 +1,4 @@
1−["HELLO!!!", "WORLD!!!", ""]
1+["HELLO!!!\n", "WORLD!!!\n", ""]
23
23
315
315
4Answer: 99
4Answer: 99
test/io001/expected−1
@@ -13,5 +13,4 @@ World!
133
133
144
144
15Last line
15Last line
16−
17---
16---
test/io001/test004.idr+1−1
@@ -13,7 +13,7 @@ dumpFile fn = do { h <- openFile fn Read
13 x <- feof h
13 x <- feof h
14 return (not x) })
14 return (not x) })
15 (do { l <- fread h
15 (do { l <- fread h
16− putStrLn l })
16+ putStr l })
17 closeFile h }
17 closeFile h }
18
18
19main : IO ()
19main : IO ()