fixes #8961
This commit is contained in:
parent
d2208091fa
commit
4ab9953787
3 changed files with 14 additions and 5 deletions
|
|
@ -3127,8 +3127,8 @@ when not defined(JS): #and not defined(nimscript):
|
||||||
|
|
||||||
proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],
|
proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],
|
||||||
benign.}
|
benign.}
|
||||||
## reads a line of text from the file `f` into `line`. `line` must not be
|
## reads a line of text from the file `f` into `line`. May throw an IO
|
||||||
## ``nil``! May throw an IO exception.
|
## exception.
|
||||||
## A line of text may be delimited by ``LF`` or ``CRLF``. The newline
|
## A line of text may be delimited by ``LF`` or ``CRLF``. The newline
|
||||||
## character(s) are not part of the returned string. Returns ``false``
|
## character(s) are not part of the returned string. Returns ``false``
|
||||||
## if the end of the file has been reached, ``true`` otherwise. If
|
## if the end of the file has been reached, ``true`` otherwise. If
|
||||||
|
|
|
||||||
|
|
@ -161,7 +161,7 @@ proc readLine(f: File, line: var TaintedString): bool =
|
||||||
var 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 fgetsSuccess
|
return last > 1 or fgetsSuccess
|
||||||
# We have to distinguish between two possible cases:
|
# We have to distinguish between two possible cases:
|
||||||
# \0\l\0 => line ending in a null character.
|
# \0\l\0 => line ending in a null character.
|
||||||
# \0\l\l => last line without newline, null was put there by fgets.
|
# \0\l\l => last line without newline, null was put there by fgets.
|
||||||
|
|
@ -169,7 +169,7 @@ proc readLine(f: File, line: var TaintedString): bool =
|
||||||
if last < pos + sp - 1 and line.string[last+1] != '\0':
|
if last < pos + sp - 1 and line.string[last+1] != '\0':
|
||||||
dec last
|
dec last
|
||||||
line.string.setLen(last)
|
line.string.setLen(last)
|
||||||
return fgetsSuccess
|
return last > 0 or fgetsSuccess
|
||||||
else:
|
else:
|
||||||
# fgets will have inserted a null byte at the end of the string.
|
# fgets will have inserted a null byte at the end of the string.
|
||||||
dec sp
|
dec sp
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
discard """
|
discard """
|
||||||
output: '''9
|
output: '''9
|
||||||
b = false
|
b = true
|
||||||
123456789
|
123456789
|
||||||
Second readLine raised an exception
|
Second readLine raised an exception
|
||||||
123456789
|
123456789
|
||||||
|
1
|
||||||
|
2aaaaaaaa
|
||||||
|
3bbbbbbb
|
||||||
'''
|
'''
|
||||||
"""
|
"""
|
||||||
# bug #5349
|
# bug #5349
|
||||||
|
|
@ -38,3 +41,9 @@ echo line
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
removeFile(fn)
|
removeFile(fn)
|
||||||
|
|
||||||
|
# bug #8961
|
||||||
|
writeFile("test.txt", "1\C\L2aaaaaaaa\C\L3bbbbbbb")
|
||||||
|
|
||||||
|
for line in lines("test.txt"):
|
||||||
|
echo line
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue