-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add ZSink.splitLines #1384
Merged
Merged
Add ZSink.splitLines #1384
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ package zio.stream | |
import zio._ | ||
import zio.clock.Clock | ||
import zio.duration.Duration | ||
|
||
import scala.collection.mutable | ||
import scala.language.postfixOps | ||
|
||
/** | ||
|
@@ -1333,6 +1335,78 @@ object ZSink extends ZSinkPlatformSpecific { | |
} | ||
} | ||
|
||
/** | ||
* Splits strings on newlines. Handles both `\r\n` and `\n`. | ||
*/ | ||
final val splitLines: ZSink[Any, Nothing, String, String, Chunk[String]] = | ||
new SinkPure[Nothing, String, String, Chunk[String]] { | ||
type State = (Chunk[String], Option[String], Boolean) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On an urelated note, this is probably what the stepless sinks' code will look like after the refactoring. :) |
||
|
||
override val initialPure: Step[State, Nothing] = Step.more((Chunk.empty, None, false)) | ||
|
||
override def stepPure(s: State, a: String): Step[State, String] = { | ||
val accumulatedLines = s._1 | ||
val concat = s._2.getOrElse("") + a | ||
val wasSplitCRLF = s._3 | ||
|
||
if (concat.isEmpty) Step.more(s) | ||
else { | ||
val buf = mutable.ArrayBuffer[String]() | ||
|
||
var i = | ||
// If we had a split CRLF, we start reading from the last character of the | ||
// leftover (which was the '\r') | ||
if (wasSplitCRLF) s._2.map(_.length).getOrElse(1) - 1 | ||
// Otherwise we just skip over the entire previous leftover as it doesn't | ||
// contain a newline. | ||
else s._2.map(_.length).getOrElse(0) | ||
|
||
var sliceStart = 0 | ||
var splitCRLF = false | ||
|
||
while (i < concat.length) { | ||
if (concat(i) == '\n') { | ||
buf += concat.substring(sliceStart, i) | ||
i += 1 | ||
sliceStart = i | ||
} else if (concat(i) == '\r' && (i + 1 < concat.length) && (concat(i + 1) == '\n')) { | ||
buf += concat.substring(sliceStart, i) | ||
i += 2 | ||
sliceStart = i | ||
} else if (concat(i) == '\r' && (i == concat.length - 1)) { | ||
splitCRLF = true | ||
i += 1 | ||
} else { | ||
i += 1 | ||
} | ||
} | ||
|
||
if (buf.isEmpty) Step.more((accumulatedLines, Some(concat), splitCRLF)) | ||
else { | ||
val newLines = Chunk.fromArray(buf.toArray[String]) | ||
val leftover = concat.substring(sliceStart, concat.length) | ||
|
||
if (splitCRLF) Step.more((accumulatedLines ++ newLines, Some(leftover), splitCRLF)) | ||
else | ||
Step.done( | ||
(accumulatedLines ++ newLines, None, splitCRLF), | ||
if (leftover.nonEmpty) Chunk.single(leftover) else Chunk.empty | ||
) | ||
} | ||
} | ||
} | ||
|
||
override def extractPure(s: State): Either[Nothing, Chunk[String]] = | ||
Right(s._1 ++ s._2.map(Chunk.single(_)).getOrElse(Chunk.empty)) | ||
} | ||
|
||
/** | ||
* Merges chunks of strings and splits them on newlines. Handles both | ||
* `\r\n` and `\n`. | ||
*/ | ||
final val splitLinesChunk: ZSink[Any, Nothing, Chunk[String], Chunk[String], Chunk[String]] = | ||
splitLines.contramap[Chunk[String]](_.mkString).mapRemainder(Chunk.single) | ||
|
||
/** | ||
* Creates a single-value sink from a value. | ||
*/ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not to nitpick here, but I see this as a deviation from the previous sinks, which have all been defined with
def
. AFAIKval
creates objects eagerly, and they stay in memory for the duration of the program which references this code. Is that something desirable in this case? I may be completely off base here, I just want to hear the reasoning. Same for every otherval
, including the ones inSink
.