Reading from a file shouldn't strip \n

We need to get exactly what's in the file or strange things might happen
and assumptions get broken.
Change getLine to strip newlines itself rather than have the RTS do it,
for consistency with the REPL behaviour.
This commit is contained in:
Edwin Brady 2015-05-16 14:03:13 +01:00
parent dcdfd8565e
commit f8cb234577
6 changed files with 12 additions and 10 deletions

View File

@ -352,7 +352,13 @@ printLn x = putStrLn (show x)
||| Read one line of input from stdin, without the trailing newline
partial
getLine : IO' ffi String
getLine = prim_read
getLine = do x <- prim_read
return (reverse (trimNL (reverse x)))
where trimNL : String -> String
trimNL str with (strM str)
trimNL "" | StrNil = ""
trimNL (strCons '\n' xs) | StrCons _ _ = xs
trimNL (strCons x xs) | StrCons _ _ = strCons x xs
||| Write a single character to stdout
partial
@ -548,7 +554,5 @@ readFile fn = do h <- openFile fn Read
readFile' h contents =
do x <- feof h
if not x then do l <- fread h
case contents of
"" => readFile' h l
_ => readFile' h (contents ++ "\n" ++ l)
readFile' h (contents ++ l)
else return contents

View File

@ -509,8 +509,6 @@ VAL idris_readStr(VM* vm, FILE* h) {
size_t n = 0;
ssize_t len;
len = getline(&buffer, &n, h);
strtok(buffer, "\n");
if (len <= 0) {
ret = MKSTR(vm, "");
} else {

View File

@ -34,6 +34,7 @@ syntax rreadLine [h] = Use readLine h
syntax reof [h] = Use eof h
syntax rputStrLn [s] = Lift (putStrLn s)
syntax rputStr [s] = Lift (putStr s)
syntax "if" "opened" [f] "then" [e] "else" [t] = Check f t e
@ -57,7 +58,7 @@ readH : String -> RES ()
readH fn = res (do let x = open fn Reading
if opened x then
do str <- rreadLine x
rputStrLn str
rputStr str
rclose x
else rputStrLn "Error")

View File

@ -1,4 +1,4 @@
["HELLO!!!", "WORLD!!!", ""]
["HELLO!!!\n", "WORLD!!!\n", ""]
3
15
Answer: 99

View File

@ -13,5 +13,4 @@ World!
3
4
Last line
---

View File

@ -13,7 +13,7 @@ dumpFile fn = do { h <- openFile fn Read
x <- feof h
return (not x) })
(do { l <- fread h
putStrLn l })
putStr l })
closeFile h }
main : IO ()