ReversedStrings

provides a fast lazy reverse AbstractString interface implementation.

julia> using ReversedStrings

julia> using BenchmarkTools

julia> @btime reverse("JuliaCon")
  28.694 ns (1 allocation: 32 bytes)
"noCailuJ"

julia> @btime reversed("JuliaCon")
  3.167 ns (0 allocations: 0 bytes)
"noCailuJ"

julia> @btime reverse(reverse("JuliaCon"))
  58.530 ns (2 allocations: 64 bytes)
"JuliaCon"

julia> @btime reversed(reversed("JuliaCon"))
  3.734 ns (0 allocations: 0 bytes)
"JuliaCon"

The package is used in CombinedParsers.jl for lookbehind parsers.

ReversedStrings.reverse_indexFunction
reverse_index(x::ReversedString,i)

Return corresponding index in unreversed String x.lastindex-i+1.

source
reverse_index(x::AbstractString,i)

Return original index i.

source
Base.iterateFunction
Base.iterate(x::ReversedString[, state])

Iterate lazy reversed string.

source
Base.getindexFunction
Base.getindex(x::ReversedString,is::UnitRange{<:Integer})

ReversedString of getindex on representation.

Note

getindex is creating a new String representation. See SubString

source
Base.getindex(x::ReversedString,i::Integer)

getindex(x.representation,reverse_index(x,i)).

source

References:

  • https://discourse.julialang.org/t/what-is-the-interface-of-abstractstring/8937