From b0509d8bf15c538903dee2fe10cffd3cb3d26a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sat, 21 Oct 2017 23:25:42 +0200 Subject: [PATCH 1/2] move DirectIndexString from Base --- src/LegacyStrings.jl | 8 ++++++++ src/directindex.jl | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/directindex.jl diff --git a/src/LegacyStrings.jl b/src/LegacyStrings.jl index 1236cc3..9d5b16f 100644 --- a/src/LegacyStrings.jl +++ b/src/LegacyStrings.jl @@ -5,6 +5,7 @@ __precompile__(true) module LegacyStrings export + DirectIndexString, ByteString, ASCIIString, RepString, @@ -97,4 +98,11 @@ using Compat else include("rep.jl") end + + if isdefined(Base, :DirectIndexString) + using Base: DirectIndexString + else + include("directindex.jl") + end + end # module diff --git a/src/directindex.jl b/src/directindex.jl new file mode 100644 index 0000000..2d98de2 --- /dev/null +++ b/src/directindex.jl @@ -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 From 4d31f8b7e1037e827041c7a46c8ef4f654dbc89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bogumi=C5=82=20Kami=C5=84ski?= Date: Sun, 22 Oct 2017 00:37:12 +0200 Subject: [PATCH 2/2] move DirectIndexString inclusion code --- src/LegacyStrings.jl | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/LegacyStrings.jl b/src/LegacyStrings.jl index 9d5b16f..aa7fa55 100644 --- a/src/LegacyStrings.jl +++ b/src/LegacyStrings.jl @@ -49,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} @@ -98,11 +104,4 @@ using Compat else include("rep.jl") end - - if isdefined(Base, :DirectIndexString) - using Base: DirectIndexString - else - include("directindex.jl") - end - end # module