Skip to content

Commit

Permalink
Make chop and chomp return SubString
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Sep 2, 2016
1 parent 40c0fc6 commit d97475d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@ startswith(a::Vector{UInt8}, b::Vector{UInt8}) =

# TODO: fast endswith

chop(s::AbstractString) = s[1:end-1]
chop(s::AbstractString) = SubString(s, 1, endof(s)-1)

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
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

0 comments on commit d97475d

Please sign in to comment.