-
Notifications
You must be signed in to change notification settings - Fork 601
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 fs2.text.linesWithEndings #2418
Open
fizzy33
wants to merge
1
commit into
typelevel:main
Choose a base branch
from
fizzy33:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,65 @@ object text { | |
def utf8EncodeC[F[_]]: Pipe[F, String, Chunk[Byte]] = | ||
encodeC(utf8Charset) | ||
|
||
sealed abstract class LineEnding(val value: String) | ||
object LineEnding { | ||
// case object CR extends LineEnding("\r") looking at the existing code this won't be produced so leaving it out for now | ||
case object LF extends LineEnding("\n") | ||
case object CRLF extends LineEnding("\r\n") | ||
case object EOF extends LineEnding("") | ||
} | ||
|
||
/** Transforms a stream of `String` such that each emitted `String` is a line from the input. */ | ||
def linesWithEndings[F[_]]: Pipe[F, String, (String,LineEnding)] = { | ||
import LineEnding._ | ||
def fillBuffers( | ||
stringBuilder: StringBuilder, | ||
linesBuffer: ArrayBuffer[(String,LineEnding)], | ||
string: String | ||
): Unit = { | ||
val l = stringBuilder.length | ||
var i = | ||
if (l > 0 && stringBuilder(l - 1) == '\r' && string.nonEmpty && string(0) == '\n') { | ||
stringBuilder.deleteCharAt(l - 1) | ||
linesBuffer += (stringBuilder.result() -> CRLF) | ||
stringBuilder.clear() | ||
1 | ||
} else 0 | ||
|
||
while (i < string.size) { | ||
string(i) match { | ||
case '\n' => | ||
linesBuffer += (stringBuilder.result() -> LF) | ||
stringBuilder.clear() | ||
case '\r' if i + 1 < string.size && string(i + 1) == '\n' => | ||
linesBuffer += (stringBuilder.result() -> CRLF) | ||
stringBuilder.clear() | ||
i += 1 | ||
case other => | ||
stringBuilder.append(other) | ||
} | ||
i += 1 | ||
} | ||
} | ||
|
||
def go( | ||
stream: Stream[F, String], | ||
stringBuilder: StringBuilder, | ||
first: Boolean | ||
): Pull[F, (String, LineEnding), Unit] = | ||
stream.pull.uncons.flatMap { | ||
case None => if (first) Pull.done else Pull.output1(stringBuilder.result() -> LineEnding.EOF) | ||
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. If the EOF remains, I think we should replace |
||
case Some((chunk, stream)) => | ||
val linesBuffer = ArrayBuffer.empty[(String,LineEnding)] | ||
chunk.foreach { string => | ||
fillBuffers(stringBuilder, linesBuffer, string) | ||
} | ||
Pull.output(Chunk.buffer(linesBuffer)) >> go(stream, stringBuilder, first = false) | ||
} | ||
|
||
s => Stream.suspend(go(s, new StringBuilder(), first = true).stream) | ||
} | ||
|
||
/** Transforms a stream of `String` such that each emitted `String` is a line from the input. */ | ||
def lines[F[_]]: Pipe[F, String, String] = { | ||
def fillBuffers( | ||
|
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
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.
EOF
is technically not aLineEnding
, but I can't come up with the appropriate name for the group(LF
,CRLF
,EOF
)..lines
operator.