on master help sign in
Merge pull request #2143 from soimort/getLine-inconsistency

Fix getLine inconsistency

commitf6414691de03f2ea5b89c2b54cc2284c83987fc7 authorEdwin Brady <edwin.brady@gmail.com> dateTue 14 Apr 2015, 20:10 (12 years ago) parent222612ac9a05f4
6 files changed+12−7
libs/prelude/Prelude.idr+4−2 rts/idris_rts.c+5−2 test/dsl002/expected−1 test/effects001/expected+1−1 test/io001/expected+1 test/io001/test004.idr+1−1
libs/prelude/Prelude.idr+4−2
@@ -346,7 +346,7 @@ partial
346346printLn : Show a => a -> IO' ffi ()
347347printLn x = putStrLn (show x)
348348
349−||| Read one line of input from stdin
349+||| Read one line of input from stdin, without the trailing newline
350350partial
351351getLine : IO' ffi String
352352getLine = prim_read
@@ -545,5 +545,7 @@ readFile fn = do h <- openFile fn Read
545545 readFile' h contents =
546546 do x <- feof h
547547 if not x then do l <- fread h
548− readFile' h (contents ++ l)
548+ case contents of
549+ "" => readFile' h l
550+ _ => readFile' h (contents ++ "\n" ++ l)
549551 else return contents
rts/idris_rts.c+5−2
@@ -504,17 +504,20 @@ VAL idris_strlen(VM* vm, VAL l) {
504504}
505505
506506VAL idris_readStr(VM* vm, FILE* h) {
507+ VAL ret;
507508 char *buffer = NULL;
508509 size_t n = 0;
509510 ssize_t len;
510511 len = getline(&buffer, &n, h);
512+ strtok(buffer, "\n");
511513
512514 if (len <= 0) {
513− return MKSTR(vm, "");
515+ ret = MKSTR(vm, "");
514516 } else {
515− return MKSTR(vm, buffer);
517+ ret = MKSTR(vm, buffer);
516518 }
517519 free(buffer);
520+ return ret;
518521}
519522
520523VAL idris_strHead(VM* vm, VAL str) {
test/dsl002/expected−1
@@ -1,2 +1 @@
11foo
2−
test/effects001/expected+1−1
@@ -1,4 +1,4 @@
1−["HELLO!!!\n", "WORLD!!!\n", ""]
1+["HELLO!!!", "WORLD!!!", ""]
223
3315
44Answer: 99
test/io001/expected+1
@@ -13,4 +13,5 @@ World!
13133
14144
1515Last line
16+
1617---
test/io001/test004.idr+1−1
@@ -13,7 +13,7 @@ dumpFile fn = do { h <- openFile fn Read
1313 x <- feof h
1414 return (not x) })
1515 (do { l <- fread h
16− putStr l })
16+ putStrLn l })
1717 closeFile h }
1818
1919main : IO ()