-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use stringOf from REPL to avoid trimming #11562
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
Conversation
With scala 2 published locally
|
Regressed with Scala 2 updated replStringOf.
Obviously the tests will break anyway with updated replStringOf. I thoughtlessly committed those test changes. ...because I didn't have that latest commit that truncates the replStringOf. That test started failing with an off-by-one in the length test. ...because the newline is trimmed before |
ScalaRunTime.replStringOf adds extra newlines. Try not to trim more than was added. New API will not include the newlines.
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 just made the comment below; I hope I'm not completely off, so take it for what it is (a question from an person who does not know the underlying code base...)
if len == 0 || res.charAt(len-1) != '\n' then | ||
res | ||
else if len == 1 || res.charAt(0) != '\n' then | ||
res.substring(0, len-1) |
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.
This code seems to assume that there always is a newline at the end of res? Why not check explicitly if also the last character is a newline to be cut, without just assuming that its there (to be more robust and not living on assumptions...)? Or am I missing something?
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.
L73 checks last, L75 checks first char. I'll look again when awake.
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.
Yes, my bad. I was not seeing all the cases in my head. It seems to work as far as I can tell.
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.
So the logic is:
if
non-zero or no trailing \n then
leave it as is
else if
only one char or no leading \n then
cut last (because we now know that there is a trailing \n)
else
cut both first and last (because we now know that there must be both leading and trailing \n)
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.
Somehow I think this is easier to grok: (but maybe it's just me)
def trimNewlinesAtStartOrEnd(res: String) =
val len = res.length
if len == 0 || res == "\n" then ""
else
val from = if res.charAt(0) == '\n' then 1 else 0
val until = if res.charAt(len - 1) == '\n' then len - 1 else len
res.substring(from, until)
Simpler to just take the special cases first IMHO.
But take whatever version you like best :)
I just had to test it :)
scala> trimNewlinesAtStartOrEnd("")
val res0: String = ""
scala> trimNewlinesAtStartOrEnd("\n")
val res1: String = ""
scala> trimNewlinesAtStartOrEnd("\n\n")
val res2: String = ""
scala> trimNewlinesAtStartOrEnd("\nhello\n")
val res3: String = hello
scala> trimNewlinesAtStartOrEnd("\n\n\n")
val res4: String = "
"
scala> trimNewlinesAtStartOrEnd("\nhello")
val res5: String = hello
scala> trimNewlinesAtStartOrEnd("hello\n")
val res6: String = hello
scala> trimNewlinesAtStartOrEnd("hello")
val res7: String = hello
scala> trimNewlinesAtStartOrEnd("hello\nhello")
val res8: String = hello
hello
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.
(BTW: Why does replStringOf (sometimes?) add extra newlines in the first place?)
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.
It appends always for Scala 2 REPL; other question is why doesn't Scala 3 use stringOf
. I suppose it prepends on multiline so the lines are all left-adjusted.
My time box expired on this tweak, so I didn't add a special test; the repl tests break easily. I agree those lines tax the short-term memory of the reviewer; if the Scala 2 PR is merged, they will be unused code.
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.
BTW stringOf
is the original method name, not an internal helper; replStringOf
was split out a decade ago out of fastidiousness. So my better option here was just to use stringOf
directly. I'll take a few minutes to try that out, after I knock off of my day job.
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.
Ok thanks for info. LGTM.
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.
LGTM.
I'll push a commit that uses Without the final null check in
The Scala 2 test case makes me think it was something @tpolecat complained about. |
Using |
My favorite Swedish action movie is "The Fastidious and the Furious." |
ScalaRunTime.replStringOf adds extra newlines. Try not to
trim more than was added.
New API will not include the newlines.
Fixes #11558