Skip to content

Commit

Permalink
Merge pull request #3357 from zainab-ali/write-utf8-improvement
Browse files Browse the repository at this point in the history
Preserve chunking of input to `writeUtf8Lines` with `intersperse`.
  • Loading branch information
mpilquist authored Dec 23, 2023
2 parents 2161daf + 9f9fc1e commit 97b9d23
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion io/shared/src/main/scala/fs2/io/file/Files.scala
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,14 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
* using the specified flags to open the file.
*/
def writeUtf8Lines(path: Path, flags: Flags): Pipe[F, String, Nothing] = in =>
in.flatMap(s => Stream[F, String](s, lineSeparator)).through(writeUtf8(path, flags))
in.pull.uncons
.flatMap {
case Some(_) =>
in.intersperse(lineSeparator).append(Stream[F, String](lineSeparator)).underlying
case None => Pull.done
}
.stream
.through(writeUtf8(path, flags))
}

private[fs2] trait FilesLowPriority { this: Files.type =>
Expand Down
14 changes: 14 additions & 0 deletions io/shared/src/test/scala/fs2/io/file/FilesSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,20 @@ class FilesSuite extends Fs2IoSuite with BaseFileSuite {
|bar
|""".stripMargin)
}

test("writeUtf8Lines - empty stream") {
Stream
.resource(tempFile)
.flatMap { path =>
Stream.empty
.covary[IO]
.through(Files[IO].writeUtf8Lines(path)) ++ Files[IO]
.readUtf8(path)
}
.compile
.foldMonoid
.assertEquals("")
}
}

group("tail") {
Expand Down

0 comments on commit 97b9d23

Please sign in to comment.