diff options
author | apense <apense@users.noreply.github.com> | 2015-05-21 17:51:41 -0400 |
---|---|---|
committer | apense <apense@users.noreply.github.com> | 2015-05-21 17:51:41 -0400 |
commit | d48bcb9873e426ece794d517290f262f6345f3b2 (patch) | |
tree | 692fb9636d5511b6be46eabe5f0f58d427830abd /lib | |
parent | 80b5e612c0f92f2bae6e148317a682deb86821f0 (diff) | |
download | Nim-d48bcb9873e426ece794d517290f262f6345f3b2.tar.gz |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/streams.nim | 31 |
1 files changed, 4 insertions, 27 deletions
diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index ce33a9625..9054031e0 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -240,22 +240,9 @@ proc peekLine*(s: Stream, line: var TaintedString): bool = ## ``CRLF``. The newline character(s) are not part of the returned string. ## Returns ``false`` if the end of the file has been reached, ``true`` ## otherwise. If ``false`` is returned `line` contains no new data. - line.string.setLen(0) let pos = getPosition(s) - while true: - var c = readChar(s) - 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 + defer: setPosition(s, pos) + readLine(s, line) proc readLine*(s: Stream): TaintedString = ## 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 = ## Peeks a line from a stream `s`. Note: This is not very efficient. Raises ## `EIO` if an error occurred. - result = TaintedString"" let pos = getPosition(s) - while true: - var c = readChar(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) + defer: setPosition(s, pos) + readLine(s) type StringStream* = ref StringStreamObj ## a stream that encapsulates a string |