Permalink
Browse files

Fixed bug in text.lines that did not handle a CRLF that spanned chunks

  • Loading branch information...
mpilquist committed Jun 25, 2016
1 parent 689cf29 commit 66f291972195c04fbe0068f165ff9aa59559a8d5
Showing with 10 additions and 8 deletions.
  1. +1 −0 .gitignore
  2. +8 −8 core/src/main/scala/fs2/text.scala
  3. +1 −0 core/src/test/scala/fs2/TextSpec.scala
View
@@ -13,3 +13,4 @@ z_local.sbt
.DS_Store
.ensime
/.ensime_cache
.tags
@@ -102,11 +102,11 @@ object text {
(out, carry)
}
def extractLines(buffer: Vector[String], chunk: Chunk[String]): (Chunk[String], Vector[String]) = {
def extractLines(buffer: Vector[String], chunk: Chunk[String], pendingLineFeed: Boolean): (Chunk[String], Vector[String], Boolean) = {
@annotation.tailrec
def loop(remainingInput: Vector[String], buffer: Vector[String], output: Vector[String], pendingLineFeed: Boolean): (Chunk[String], Vector[String]) = {
def loop(remainingInput: Vector[String], buffer: Vector[String], output: Vector[String], pendingLineFeed: Boolean): (Chunk[String], Vector[String], Boolean) = {
if (remainingInput.isEmpty) {
Chunk.indexedSeq(output) -> buffer
(Chunk.indexedSeq(output), buffer, pendingLineFeed)
} else {
var next = remainingInput.head
if (pendingLineFeed) {
@@ -125,18 +125,18 @@ object text {
}
}
}
loop(chunk.toVector, buffer, Vector.empty, false)
loop(chunk.toVector, buffer, Vector.empty, pendingLineFeed)
}
def go(buffer: Vector[String]): Handle[F, String] => Pull[F, String, Unit] = {
def go(buffer: Vector[String], pendingLineFeed: Boolean): Handle[F, String] => Pull[F, String, Unit] = {
Pull.receiveOption[F,String,String,Unit] {
case Some(chunk #: h) =>
val (toOutput, newBuffer) = extractLines(buffer, chunk)
Pull.output(toOutput) >> go(newBuffer)(h)
val (toOutput, newBuffer, newPendingLineFeed) = extractLines(buffer, chunk, pendingLineFeed)
Pull.output(toOutput) >> go(newBuffer, newPendingLineFeed)(h)
case None if buffer.nonEmpty => Pull.output1(buffer.mkString)
case None => Pull.done
}(_)
}
_.pull(go(Vector.empty))
_.pull(go(Vector.empty, false))
}
}
@@ -180,6 +180,7 @@ class TextSpec extends Fs2Spec {
if (lines.toList.nonEmpty) {
val s = lines.intersperse("\r\n").toList.mkString.grouped(3).toList
Stream.emits(s).throughPure(text.lines).toList shouldBe lines.toList
Stream.emits(s).unchunk.throughPure(text.lines).toList shouldBe lines.toList
}
}
}

0 comments on commit 66f2919

Please sign in to comment.