Skip to content

Commit c19101a

Browse files
bkaminsnalimilan
authored andcommitted
Move DirectIndexString from Base (#24)
1 parent 873a1b4 commit c19101a

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/LegacyStrings.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __precompile__(true)
55
module LegacyStrings
66

77
export
8+
DirectIndexString,
89
ByteString,
910
ASCIIString,
1011
RepString,
@@ -48,6 +49,12 @@ using Compat
4849
import Base: lastidx
4950
end
5051

52+
if isdefined(Base, :DirectIndexString)
53+
using Base: DirectIndexString
54+
else
55+
include("directindex.jl")
56+
end
57+
5158
if VERSION >= v"0.5.0-"
5259
immutable ASCIIString <: DirectIndexString
5360
data::Vector{UInt8}

src/directindex.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

Comments
 (0)