softwaremill / ox

Safe direct style concurrency and resiliency for Scala on the JVM
https://ox.softwaremill.com
Apache License 2.0
320 stars 22 forks source link

Add text .encode .decode operators (UTF-8) #141

Closed kciesielski closed 2 weeks ago

kciesielski commented 2 weeks ago

Closes https://github.com/softwaremill/ox/issues/138

  1. Adds source.decodeStringUtf8 for Source[Chunk[Byte]], adapted from fs2
  2. Adds source.encodeUtf8 for Source[String]

Decoding other charsets can be added in the future. It would require a little more complex logic.

Additionally:

  1. Changes .lines to .linesUtf8 and adds .lines(charset)
  2. Changes Chunk's .asText to .asTextUtf8 - I'm not sure if we even want to keep these as public. A Chunk[Byte] may not be a complete set of bytes to decode to a String, and having such methods may encourage users to do source.map(_.asTextUtf8) instead of source.decodeUtf8, where only the second approach is fully safe, unless we are sure that chunks represent complete strings (like lines). These methods are useful internally though.

Some thoughts:

TODO: