diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2015-09-03 12:36:15 +0200 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2015-09-03 12:36:15 +0200 |
commit | 5a9d2d8e75ae32595f2f12549efc5d1c27fb7a91 (patch) | |
tree | 75c2fc1b448c747ea64f949eb3a64e1cf21828cb | |
parent | 6fc01d1501fdda38477734ac713e52ba3852f56a (diff) | |
parent | 7dd263f506453a849b585d586b27606235123277 (diff) | |
download | Nim-5a9d2d8e75ae32595f2f12549efc5d1c27fb7a91.tar.gz |
Merge pull request #3266 from rbehrends/fix-readline
Fix readLine handling of long lines.
-rw-r--r-- | lib/system/sysio.nim | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 265f92fa2..3d0b2aa8a 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -90,12 +90,21 @@ proc readLine(f: File, line: var TaintedString): bool = let m = memchr(addr line.string[pos], '\l'.ord, space) if m != nil: # \l found: Could be our own or the one by fgets, in any case, we're done - let last = cast[ByteAddress](m) - cast[ByteAddress](addr line.string[0]) + var last = cast[ByteAddress](m) - cast[ByteAddress](addr line.string[0]) if last > 0 and line.string[last-1] == '\c': line.string.setLen(last-1) return true + # We have to distinguish between two possible cases: + # \0\l\0 => line ending in a null character. + # \0\l\l => last line without newline, null was put there by fgets. + elif last > 0 and line.string[last-1] == '\0': + if last < pos + space - 1 and line.string[last+1] != '\0': + dec last line.string.setLen(last) return true + else: + # fgets will have inserted a null byte at the end of the string. + dec space # No \l found: Increase buffer and read more inc pos, space space = 128 # read in 128 bytes at a time |