Skip to content
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

Merged
merged 4 commits into from
Oct 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,20 @@ startswith(a::Vector{UInt8}, b::Vector{UInt8}) =

# TODO: fast endswith


"""
chop(s::AbstractString)

Remove the last character from `s`.

```jldoctest
julia> a = string("March")
julia> a = "March"
"March"

julia> chop(a)
"Marc"
```
"""
chop(s::AbstractString) = s[1:end-1]
chop(s::AbstractString) = SubString(s, 1, endof(s)-1)
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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


"""
chomp(s::AbstractString)
Expand All @@ -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
Copy link
Member

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.

j = prevind(s,i)
if (j < 1 || s[j] != '\r') return s[1:i-1] end
return s[1:j-1]
if (j < 1 || s[j] != '\r') return SubString(s, 1, i-1) end
return SubString(s, 1, j-1)
end
function chomp(s::String)
i = endof(s)
if i < 1 || s.data[i] != 0x0a
SubString(s, 1, i)
elseif i < 2 || s.data[i-1] != 0x0d
SubString(s, 1, i-1)
else
SubString(s, 1, i-2)
end
end
chomp(s::String) =
(endof(s) < 1 || s.data[end] != 0x0a) ? s :
(endof(s) < 2 || s.data[end-1] != 0x0d) ? s[1:end-1] : s[1:end-2]

# NOTE: use with caution -- breaks the immutable string convention!
function chomp!(s::String)
Expand Down
2 changes: 1 addition & 1 deletion doc/stdlib/strings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@

.. doctest::

julia> a = string("March")
julia> a = "March"
"March"

julia> chop(a)
Expand Down
4 changes: 3 additions & 1 deletion test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ end

# chomp/chop
@test chomp("foo\n") == "foo"
@test chop("foob") == "foo"
@test chop("fooε") == "foo"
@test isa(chomp("foo"), SubString)
@test isa(chop("foo"), SubString)

# bytes2hex and hex2bytes
hex_str = "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"
Expand Down