Skip to content

Commit

Permalink
move DirectIndexString from Base
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Oct 21, 2017
1 parent d30435d commit e14c580
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/LegacyStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ __precompile__(true)
module LegacyStrings

export
DirectIndexString,
ByteString,
ASCIIString,
RepString,
Expand Down Expand Up @@ -97,4 +98,11 @@ using Compat
else
include("rep.jl")
end

if isdefined(Base, :DirectIndexString)
using Base: RepString
else
include("directindex.jl")
end

end # module
34 changes: 34 additions & 0 deletions src/directindex.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e14c580

Please sign in to comment.