Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added writeUtf8 and writeUtf8Lines #3167

Merged
merged 8 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions io/js/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ private[fs2] trait FilesCompanionPlatform {
override def isSameFile(path1: Path, path2: Path): F[Boolean] =
F.pure(path1.absolute == path2.absolute)

override val lineSeparator: String = facade.os.EOL()
TonioGela marked this conversation as resolved.
Show resolved Hide resolved

override def list(path: Path): Stream[F, Path] =
Stream
.bracket(F.fromPromise(F.delay(facade.fs.promises.opendir(path.toString))))(dir =>
Expand Down
4 changes: 4 additions & 0 deletions io/js/src/main/scala/fs2/io/internal/facade/os.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ package object os {
@JSImport("os", "networkInterfaces")
private[io] def networkInterfaces(): js.Dictionary[js.Array[NetworkInterfaceInfo]] = js.native

@js.native
@JSImport("os", "EOL")
private[io] def EOL(): String = js.native
TonioGela marked this conversation as resolved.
Show resolved Hide resolved

}

package os {
Expand Down
2 changes: 2 additions & 0 deletions io/jvm-native/src/main/scala/fs2/io/file/FilesPlatform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ private[file] trait FilesCompanionPlatform {
def isSameFile(path1: Path, path2: Path): F[Boolean] =
Sync[F].blocking(JFiles.isSameFile(path1.toNioPath, path2.toNioPath))

val lineSeparator: String = System.lineSeparator()
TonioGela marked this conversation as resolved.
Show resolved Hide resolved

def list(path: Path): Stream[F, Path] =
_runJavaCollectionResource[JStream[JPath]](
Sync[F].blocking(JFiles.list(path.toNioPath)),
Expand Down
5 changes: 4 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 @@ -240,6 +240,9 @@ sealed trait Files[F[_]] extends FilesPlatform[F] {
/** Returns true if the supplied paths reference the same file. */
def isSameFile(path1: Path, path2: Path): F[Boolean]

/** Returns the line separator for the specific OS */
val lineSeparator: String

/** Gets the contents of the specified directory. */
def list(path: Path): Stream[F, Path]

Expand Down Expand Up @@ -439,7 +442,7 @@ 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.intersperse("\n").through(writeUtf8(path, flags))
in.intersperse(lineSeparator).through(writeUtf8(path, flags))
Copy link
Member

@armanbilge armanbilge Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really feel like we should be appending a line-separator to each string, instead of interspersing.

A sequence of zero or more non- characters plus a terminating <newline> character.

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206

Copy link
Member Author

@TonioGela TonioGela Mar 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would in.flatMap(s => Stream(s, lineSeparator)) do?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly!

}

object Files extends FilesCompanionPlatform {
Expand Down