diff options
author | Jesse Luehrs <doy@tozt.net> | 2013-02-27 01:43:36 -0600 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2013-02-27 01:43:36 -0600 |
commit | db5c92d1f1f3abc043e1822a09cd454029bfa1b6 (patch) | |
tree | 6afa817870ea9ff8df2cbff92cb416cbc0c2a9d1 /src | |
parent | a3c7e06e53b5a0305c9951ef678dfb1c2cc69de4 (diff) | |
download | scala-test-more-db5c92d1f1f3abc043e1822a09cd454029bfa1b6.tar.gz scala-test-more-db5c92d1f1f3abc043e1822a09cd454029bfa1b6.zip |
use iterators instead of streams
streams buffer the next character (to support Stream.Empty), which means
that if there is exactly one line available, it will read that line and
then block waiting for the next character before the result stream can
be returned. this means that we don't get results from the current line
until the next line is ready. iterators don't have that problem, since
they don't have a separate Empty type, they just have a hasNext method.
Diffstat (limited to 'src')
-rw-r--r-- | src/main/scala/org/perl8/test/tap/Parser.scala | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/src/main/scala/org/perl8/test/tap/Parser.scala b/src/main/scala/org/perl8/test/tap/Parser.scala index ce39343..676d507 100644 --- a/src/main/scala/org/perl8/test/tap/Parser.scala +++ b/src/main/scala/org/perl8/test/tap/Parser.scala @@ -141,11 +141,11 @@ class Parser private ( } private class LineReader ( - in: Stream[Char], + in: Iterator[Char], lineNum: Int ) extends Reader[Line] { def this (in: InputStream) = - this(Source.fromInputStream(in).toStream, 1) + this(Source.fromInputStream(in), 1) def atEnd: Boolean = nextLine.isEmpty @@ -162,27 +162,27 @@ class Parser private ( private def nextLine: Option[Line] = state._1 - private def remainingStream: Stream[Char] = + private def remainingStream: Iterator[Char] = state._2 - private lazy val state: (Option[Line], Stream[Char]) = + private lazy val state: (Option[Line], Iterator[Char]) = readNextLine(in) @tailrec private def readNextLine ( - stream: Stream[Char] - ): (Option[Line], Stream[Char]) = { - stream match { - case Stream.Empty => (None, in) - case s => { - val (line, rest) = s.span(_ != '\n') match { - case (l, r) => (parseLine(l.mkString), r.tail) - } - line match { - case _: CommentLine => readNextLine(rest) - case other => (Some(other), rest) - } + stream: Iterator[Char] + ): (Option[Line], Iterator[Char]) = { + if (stream.hasNext) { + val (line, rest) = stream.span(_ != '\n') match { + case (l, r) => (parseLine(l.mkString), r.drop(1)) } + line match { + case _: CommentLine => readNextLine(rest) + case other => (Some(other), rest) + } + } + else { + (None, in) } } } |