From db5c92d1f1f3abc043e1822a09cd454029bfa1b6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 27 Feb 2013 01:43:36 -0600 Subject: 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. --- src/main/scala/org/perl8/test/tap/Parser.scala | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src') 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) } } } -- cgit v1.2.3-54-g00ecf