Rewrote some peeking procedures
Use `defer`s and `read...` procs that are already in place. Someone should check that the `defer`s are written correctly. I'm new to using them.
This commit is contained in:
parent
80b5e612c0
commit
d48bcb9873
1 changed files with 4 additions and 27 deletions
|
|
@ -240,22 +240,9 @@ proc peekLine*(s: Stream, line: var TaintedString): bool =
|
||||||
## ``CRLF``. The newline character(s) are not part of the returned string.
|
## ``CRLF``. The newline character(s) are not part of the returned string.
|
||||||
## Returns ``false`` if the end of the file has been reached, ``true``
|
## Returns ``false`` if the end of the file has been reached, ``true``
|
||||||
## otherwise. If ``false`` is returned `line` contains no new data.
|
## otherwise. If ``false`` is returned `line` contains no new data.
|
||||||
line.string.setLen(0)
|
|
||||||
let pos = getPosition(s)
|
let pos = getPosition(s)
|
||||||
while true:
|
defer: setPosition(s, pos)
|
||||||
var c = readChar(s)
|
readLine(s, line)
|
||||||
if c == '\c':
|
|
||||||
c = readChar(s)
|
|
||||||
break
|
|
||||||
elif c == '\L': break
|
|
||||||
elif c == '\0':
|
|
||||||
if line.len > 0: break
|
|
||||||
else:
|
|
||||||
setPosition(s, pos)
|
|
||||||
return false
|
|
||||||
line.string.add(c)
|
|
||||||
setPosition(s, pos)
|
|
||||||
result = true
|
|
||||||
|
|
||||||
proc readLine*(s: Stream): TaintedString =
|
proc readLine*(s: Stream): TaintedString =
|
||||||
## Reads a line from a stream `s`. Note: This is not very efficient. Raises
|
## Reads a line from a stream `s`. Note: This is not very efficient. Raises
|
||||||
|
|
@ -274,19 +261,9 @@ proc readLine*(s: Stream): TaintedString =
|
||||||
proc peekLine*(s: Stream): TaintedString =
|
proc peekLine*(s: Stream): TaintedString =
|
||||||
## Peeks a line from a stream `s`. Note: This is not very efficient. Raises
|
## Peeks a line from a stream `s`. Note: This is not very efficient. Raises
|
||||||
## `EIO` if an error occurred.
|
## `EIO` if an error occurred.
|
||||||
result = TaintedString""
|
|
||||||
let pos = getPosition(s)
|
let pos = getPosition(s)
|
||||||
while true:
|
defer: setPosition(s, pos)
|
||||||
var c = readChar(s)
|
readLine(s)
|
||||||
if c == '\c':
|
|
||||||
c = readChar(s)
|
|
||||||
setPosition(s, pos)
|
|
||||||
break
|
|
||||||
if c == '\L' or c == '\0':
|
|
||||||
setPosition(s, pos)
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
result.string.add(c)
|
|
||||||
|
|
||||||
type
|
type
|
||||||
StringStream* = ref StringStreamObj ## a stream that encapsulates a string
|
StringStream* = ref StringStreamObj ## a stream that encapsulates a string
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue