-
Notifications
You must be signed in to change notification settings - Fork 603
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
text.lines
enhancements
#2758
Merged
mpilquist
merged 12 commits into
typelevel:main
from
stephenjudkins:sdj/lines-enhancements
Dec 22, 2021
Merged
text.lines
enhancements
#2758
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a6c6b01
`text.lines` enhancements:
stephenjudkins a56fe21
Fix docs, change some param names
stephenjudkins 6e503a4
format
stephenjudkins e05d9b2
scalafmt
stephenjudkins 601e72c
scalafmt
stephenjudkins 9d46873
Fix for scala 2.12 build
stephenjudkins f6adcbe
Use custom exception for too-long line
stephenjudkins 960e1fc
Don't use deprecated method, for good measure.
stephenjudkins f54d4fd
Scala 2.12 fix, again
stephenjudkins 68a2a59
Test that existing CRLF-delimited text is split appropriately when `c…
stephenjudkins 774cb3b
Always split lines on '\r'-delimited input; simplify methods
stephenjudkins e8c89f2
unsure how that compiled
stephenjudkins 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 |
---|---|---|
|
@@ -316,14 +316,34 @@ object text { | |
def utf8EncodeC[F[_]]: Pipe[F, String, Chunk[Byte]] = | ||
utf8.encodeC | ||
|
||
/** Transforms a stream of `String` such that each emitted `String` is a line from the input | ||
* @param maxLineLength maximum size to accumulate a line to; throw an error if a line is larger | ||
* @param crsOnly separate lines that are delimited only by '\r' | ||
* @tparam F | ||
*/ | ||
def linesFor[F[_]: RaiseThrowable]( | ||
maxLineLength: Option[Int] = None, | ||
crsOnly: Boolean = false | ||
): Pipe[F, String, String] = | ||
linesImpl[F]( | ||
maxLineLength = maxLineLength.map((_, implicitly[RaiseThrowable[F]])), | ||
crsOnly = crsOnly | ||
) | ||
|
||
/** Transforms a stream of `String` such that each emitted `String` is a line from the input. */ | ||
def lines[F[_]]: Pipe[F, String, String] = { | ||
def lines[F[_]]: Pipe[F, String, String] = linesImpl[F]() | ||
|
||
private def linesImpl[F[_]]( | ||
maxLineLength: Option[(Int, RaiseThrowable[F])] = None, | ||
crsOnly: Boolean = false | ||
): Pipe[F, String, String] = { | ||
def fillBuffers( | ||
stringBuilder: StringBuilder, | ||
linesBuffer: ArrayBuffer[String], | ||
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) | ||
|
@@ -341,6 +361,9 @@ object text { | |
linesBuffer += stringBuilder.result() | ||
stringBuilder.clear() | ||
i += 1 | ||
case '\r' if crsOnly => | ||
linesBuffer += stringBuilder.result() | ||
stringBuilder.clear() | ||
case other => | ||
stringBuilder.append(other) | ||
} | ||
|
@@ -360,12 +383,25 @@ object text { | |
chunk.foreach { string => | ||
fillBuffers(stringBuilder, linesBuffer, string) | ||
} | ||
Pull.output(Chunk.buffer(linesBuffer)) >> go(stream, stringBuilder, first = false) | ||
|
||
maxLineLength match { | ||
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. There is |
||
case Some((max, raiseThrowable)) if stringBuilder.length > max => | ||
Pull.raiseError[F]( | ||
new LineTooLongException(stringBuilder.length, max) | ||
)(raiseThrowable) | ||
case _ => | ||
Pull.output(Chunk.indexedSeq(linesBuffer)) >> go(stream, stringBuilder, first = false) | ||
} | ||
} | ||
|
||
s => Stream.suspend(go(s, new StringBuilder(), first = true).stream) | ||
} | ||
|
||
class LineTooLongException(val length: Int, val max: Int) | ||
extends RuntimeException( | ||
s"Max line size is $max but $length chars have been accumulated" | ||
) | ||
|
||
/** Functions for working with base 64. */ | ||
object base64 { | ||
|
||
|
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.
I see, that
\r
can also be a line delimiter.So, should it be a regression? i.e. should the default behavior change from treating
\n
&\r\n
as a line delimiter to treating\r
,\r\n
,\n
as a line delimiter? I would expectlines
to handle this kind of ambiguity not with a flag, but always. In that case we should check that\r\n
doesn’t produce two lines.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.
My assumption is that other users wouldn't want the existing behavior to change. If I were the only consumer of this library I'd just have it always treat bare
\r
s as line separators.I don't know about the consensus-forming process is here. But I'd be happy to go with the flow and do whatever other stakeholders agree on here.