Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move DirectIndexString from Base #24

Merged
merged 2 commits into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 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 @@ -48,6 +49,12 @@ using Compat
import Base: lastidx
end

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

if VERSION >= v"0.5.0-"
immutable ASCIIString <: DirectIndexString
data::Vector{UInt8}
Expand Down
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