-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Make chop
and chomp
return SubString
#18339
Conversation
I think @StefanKarpinski has a plan regarding |
LGTM (needs a rebase). |
Rebased. On another note, I noticed the doctest:
seems to use |
The return type should be tested for if you don't want it to regress accidentally. The |
I would just fix the doctest in this PR, @TotalVerb. |
LGTM as well. |
I made the doctest fix locally. Do I need to rebuild helpdb or something? @stevengj |
You change the docstring in the source, run |
OK, is this all right? Thanks for the help, @stevengj. |
LGTM. |
"March" | ||
|
||
julia> chop(a) | ||
"Marc" | ||
``` | ||
""" | ||
chop(s::AbstractString) = s[1:end-1] | ||
chop(s::AbstractString) = SubString(s, 1, endof(s)-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.
Do the doctests still pass with this change?
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.
The doctests don't pass, but I don't know if that's this change's fault. Failures seem unrelated to me.
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 looks to me like most of the failures are due to line numbers being not the same as expected?
https://gist.github.com/TotalVerb/d81e7fff617d315f51ecd019aac4f553
Bump. |
@@ -81,14 +80,21 @@ Remove a single trailing newline from a string. | |||
""" | |||
function chomp(s::AbstractString) | |||
i = endof(s) | |||
if (i < 1 || s[i] != '\n') return s end | |||
if (i < 1 || s[i] != '\n') return SubString(s, 1, i) end |
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 very important but I think that typically we use short circuiting ... && return ...
for this. But it was like this before the PR too.
This got lgtm from both Stefan and Steven so should be OK to merge? |
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.
News item?
Sorry, that should have been a comment. |
Sure, I'll write up a NEWS. |
This is unfortunate:
I suspect that the same reason that
split
returnsSubString
applies tochop
andchomp
also. The clear benefit of returning a view is performance and allocation avoidance; the disadvantages are that the underlying string cannot be freed, and accessing aSubString
is slightly slower. However, inchop
andchomp
's case, the disadvantage is incredibly slight—an extra byte or so that can't be freed—so I believe it would make sense to followsplit
here.Note that Perl, the only other language I know with
chop
andchomp
, has these as in-place operations—which is even faster than creating aSubString
, but not appropriate for Julia's conventionally immutableString
s.On another note, I wonder whether it might be worthwhile to make
String
intoString{T}
, so that a string's underlying data can be an array view—which would makeSubString
substantially faster.