|
| 1 | +# This file includes code that was formerly a part of Julia. License is MIT: http://julialang.org/license |
| 2 | + |
| 3 | +abstract type DirectIndexString <: AbstractString end |
| 4 | + |
| 5 | +next(s::DirectIndexString, i::Int) = (s[i],i+1) |
| 6 | + |
| 7 | +length(s::DirectIndexString) = endof(s) |
| 8 | + |
| 9 | +isvalid(s::DirectIndexString, i::Integer) = (start(s) <= i <= endof(s)) |
| 10 | + |
| 11 | +prevind(s::DirectIndexString, i::Integer) = Int(i)-1 |
| 12 | +nextind(s::DirectIndexString, i::Integer) = Int(i)+1 |
| 13 | + |
| 14 | +function prevind(s::DirectIndexString, i::Integer, nchar::Integer) |
| 15 | + nchar > 0 || throw(ArgumentError("nchar must be greater than 0")) |
| 16 | + Int(i)-nchar |
| 17 | +end |
| 18 | + |
| 19 | +function nextind(s::DirectIndexString, i::Integer, nchar::Integer) |
| 20 | + nchar > 0 || throw(ArgumentError("nchar must be greater than 0")) |
| 21 | + Int(i)+nchar |
| 22 | +end |
| 23 | + |
| 24 | +ind2chr(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end |
| 25 | +chr2ind(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end |
| 26 | + |
| 27 | +length(s::SubString{<:DirectIndexString}) = endof(s) |
| 28 | + |
| 29 | +isvalid(s::SubString{<:DirectIndexString}, i::Integer) = (start(s) <= i <= endof(s)) |
| 30 | + |
| 31 | +ind2chr(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end |
| 32 | +chr2ind(s::SubString{<:DirectIndexString}, i::Integer) = begin checkbounds(s,i); i end |
| 33 | + |
| 34 | +reverseind(s::Union{DirectIndexString,SubString{DirectIndexString}}, i::Integer) = length(s) + 1 - i |
0 commit comments