Fix readLine handling of long lines.
This commit is contained in:
parent
370781b773
commit
7dd263f506
1 changed files with 10 additions and 1 deletions
|
|
@ -90,12 +90,21 @@ proc readLine(f: File, line: var TaintedString): bool =
|
||||||
let m = memchr(addr line.string[pos], '\l'.ord, space)
|
let m = memchr(addr line.string[pos], '\l'.ord, space)
|
||||||
if m != nil:
|
if m != nil:
|
||||||
# \l found: Could be our own or the one by fgets, in any case, we're done
|
# \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':
|
if last > 0 and line.string[last-1] == '\c':
|
||||||
line.string.setLen(last-1)
|
line.string.setLen(last-1)
|
||||||
return true
|
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)
|
line.string.setLen(last)
|
||||||
return true
|
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
|
# No \l found: Increase buffer and read more
|
||||||
inc pos, space
|
inc pos, space
|
||||||
space = 128 # read in 128 bytes at a time
|
space = 128 # read in 128 bytes at a time
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue