Skip to content

Commit

Permalink
improve performance for reverse(::String) (JuliaLang#29610)
Browse files Browse the repository at this point in the history
* improve performance for reverse

* further perf improvement
  • Loading branch information
KristofferC authored Oct 16, 2018
1 parent b72fc4c commit 41c10af
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ julia> join(reverse(collect(graphemes("ax̂e")))) # reverses graphemes
```
"""
function reverse(s::Union{String,SubString{String}})::String
sprint(sizehint=sizeof(s)) do io
i, j = firstindex(s), lastindex(s)
while i j
c, j = s[j], prevind(s, j)
write(io, c)
end
# Read characters forwards from `s` and write backwards to `out`
out = _string_n(sizeof(s))
offs = sizeof(s) + 1
for c in s
offs -= ncodeunits(c)
__unsafe_string!(out, c, offs)
end
return out
end

string(a::String) = String(a)
Expand Down

0 comments on commit 41c10af

Please sign in to comment.