diff --git a/src/LegacyStrings.jl b/src/LegacyStrings.jl index 1236cc3..9d5b16f 100644 --- a/src/LegacyStrings.jl +++ b/src/LegacyStrings.jl @@ -5,6 +5,7 @@ __precompile__(true) module LegacyStrings export + DirectIndexString, ByteString, ASCIIString, RepString, @@ -97,4 +98,11 @@ using Compat else include("rep.jl") end + + if isdefined(Base, :DirectIndexString) + using Base: DirectIndexString + else + include("directindex.jl") + end + end # module diff --git a/src/directindex.jl b/src/directindex.jl new file mode 100644 index 0000000..2d98de2 --- /dev/null +++ b/src/directindex.jl @@ -0,0 +1,34 @@ +# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license + +abstract type DirectIndexString <: AbstractString end + +next(s::DirectIndexString, i::Int) = (s[i],i+1) + +length(s::DirectIndexString) = endof(s) + +isvalid(s::DirectIndexString, i::Integer) = (start(s) <= i <= endof(s)) + +prevind(s::DirectIndexString, i::Integer) = Int(i)-1 +nextind(s::DirectIndexString, i::Integer) = Int(i)+1 + +function prevind(s::DirectIndexString, i::Integer, nchar::Integer) + nchar > 0 || throw(ArgumentError("nchar must be greater than 0")) + Int(i)-nchar +end + +function nextind(s::DirectIndexString, i::Integer, nchar::Integer) + nchar > 0 || throw(ArgumentError("nchar must be greater than 0")) + Int(i)+nchar +end + +ind2chr(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end +chr2ind(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end + +length(s::SubString{<:DirectIndexString}) = endof(s) + +isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (start(s) <= i <= endof(s)) + +ind2chr(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end +chr2ind(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end + +reverseind(s::Union{DirectIndexString,SubString{DirectIndexString}}, i::Integer) = length(s) + 1 - i