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

Optimize fs2.text.{char2string,string2char} #3278

Merged
merged 1 commit into from
Aug 28, 2023
Merged
Changes from all commits
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
7 changes: 5 additions & 2 deletions core/shared/src/main/scala/fs2/text.scala
Original file line number Diff line number Diff line change
Expand Up @@ -563,10 +563,13 @@ object text {

/** Transforms a stream of `String` to a stream of `Char`. */
def string2char[F[_]]: Pipe[F, String, Char] =
_.map(s => Chunk.charBuffer(CharBuffer.wrap(s))).flatMap(Stream.chunk)
_.flatMap(s => Stream.chunk(Chunk.charBuffer(CharBuffer.wrap(s))))

/** Transforms a stream of `Char` to a stream of `String`. */
def char2string[F[_]]: Pipe[F, Char, String] = _.chunks.map(_.mkString_(""))
def char2string[F[_]]: Pipe[F, Char, String] = _.chunks.map { chunk =>
val Chunk.ArraySlice(chars, offset, length) = chunk.toArraySlice
new String(chars, offset, length)
Copy link

@filipwiech filipwiech Aug 15, 2023

Choose a reason for hiding this comment

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

Would it be a good idea to use the UTF-8 charset instead of relying on the default one, like we do in a few other places where we create a new String (private val utf8Charset = Charset.forName("UTF-8"))? 🙂

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, not sure. Perhaps you are mixing up the ctor that takes an Array[Byte] with the one we are using here, which takes an Array[Char]?

Choose a reason for hiding this comment

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

Ah, that's it, you're right, sorry for the confusion. 😅

}

class LineTooLongException(val length: Int, val max: Int)
extends RuntimeException(
Expand Down