summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSergey Avseyev <sergey.avseyev@gmail.com>2015-05-26 00:29:58 +0300
committerSergey Avseyev <sergey.avseyev@gmail.com>2015-05-26 00:29:58 +0300
commit116347674c90873366f09c6464ec2c0f22f19b82 (patch)
tree271022cdb18c706fc263a755f7a15f4b6ebbec7c
parentf51643428359a12185b981e57117d7e8ecf2a776 (diff)
downloadNim-116347674c90873366f09c6464ec2c0f22f19b82.tar.gz
Fix peekLine() for streams
Motivation
----------
peekLine procs use defer to reset position in the stream, but it also
make them always return nil.

Modification
------------
Explicitly set result value in peekLine, and write missing unit test.

Result
------
Tests are green and bug is fixed
-rw-r--r--lib/pure/streams.nim8
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim
index f6c65466c..f51c979eb 100644
--- a/lib/pure/streams.nim
+++ b/lib/pure/streams.nim
@@ -242,7 +242,7 @@ proc peekLine*(s: Stream, line: var TaintedString): bool =
   ## otherwise. If ``false`` is returned `line` contains no new data.
   let pos = getPosition(s)
   defer: setPosition(s, pos)
-  readLine(s, line)
+  result = readLine(s, line)
 
 proc readLine*(s: Stream): TaintedString =
   ## Reads a line from a stream `s`. Note: This is not very efficient. Raises
@@ -263,7 +263,7 @@ proc peekLine*(s: Stream): TaintedString =
   ## `EIO` if an error occurred.
   let pos = getPosition(s)
   defer: setPosition(s, pos)
-  readLine(s)
+  result = readLine(s)
 
 type
   StringStream* = ref StringStreamObj ## a stream that encapsulates a string
@@ -458,3 +458,7 @@ when defined(testing):
   assert(ss.getPosition == 5) # did move
   assert(ss.peekLine() == "uick brown fox jumped over the lazy dog.")
   assert(ss.getPosition == 5) # haven't moved
+  var str = newString(100)
+  assert(ss.peekLine(str))
+  assert(str == "uick brown fox jumped over the lazy dog.")
+  assert(ss.getPosition == 5) # haven't moved