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.ReversedString — TypeReversedString{V}<:AbstractStringLazy reversed V struct holding representation and caching lastindex.
ReversedStrings.reversed — Functionreversed(x::AbstractString)A lazy implementation of Base.reverse for Strings.
ReversedStrings.reverse_index — Functionreverse_index(x::ReversedString,i)Return corresponding index in unreversed String x.lastindex-i+1.
reverse_index(x::AbstractString,i)Return original index i.
Base.firstindex — FunctionBase.firstindex(x::ReversedString)1
Base.lastindex — FunctionBase.lastindex(x::ReversedString)cached x.lastindex.
Base.length — FunctionBase.length(x::ReversedString)length(x.representation)
Base.ncodeunits — FunctionBase.ncodeunits(x::ReversedString)ncodeunits(x.representation)
Base.thisind — FunctionBase.thisind(x::ReversedString,i::Integer)reverse_index(thisind(x.representation,reverse_index(x,i))).
Base.prevind — FunctionBase.prevind(x::ReversedString,i[,n=1])reverse_index(prevind(x.representation,reverse_index(x,i))[,n]).
Base.nextind — FunctionBase.nextind(x::ReversedString,i[,n=1])reverse_index(nextind(x.representation,reverse_index(x,i))[,n]).
Base.iterate — FunctionBase.iterate(x::ReversedString[, state])Iterate lazy reversed string.
Base.getindex — FunctionBase.getindex(x::ReversedString,is::UnitRange{<:Integer})ReversedString of getindex on representation.
getindex is creating a new String representation. See SubString
Base.getindex(x::ReversedString,i::Integer)getindex(x.representation,reverse_index(x,i)).
Base.codeunit — FunctionBase.ncodeunits(x::ReversedString)codeunit(x.representation,reverse_index(x,i)).
Base.isvalid — FunctionBase.isvalid(x::ReversedString,i::Int)isvalid(x.representation,reverse_index(x,i)).
Base.SubString — TypeBase.SubString(x::ReversedString,start::Int,stop::Int)Create a reversed SubString using reverse_index.
References:
- https://discourse.julialang.org/t/what-is-the-interface-of-abstractstring/8937