/
directindex.jl
36 lines (24 loc) · 1.4 KB
/
directindex.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) = (firstindex(s) <= i <= lastindex(s))
prevind(s::DirectIndexString, i::Int) = i-1
nextind(s::DirectIndexString, i::Int) = i+1
prevind(s::DirectIndexString, i::Integer) = prevind(s, i)
nextind(s::DirectIndexString, i::Integer) = nextind(s, i)
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}) = lastindex(s)
isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (firstindex(s) <= i <= ncodeunits(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