Skip to content

Commit

Permalink
Deprecate getindex/checkbounds methods that depend on non Integer Rea…
Browse files Browse the repository at this point in the history
…l indices
  • Loading branch information
ScottPJones committed Sep 1, 2015
1 parent d62520f commit 67690c0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 0 additions & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ endof(c::Char) = 1
getindex(c::Char) = c
getindex(c::Char, i::Integer) = i == 1 ? c : throw(BoundsError())
getindex(c::Char, I::Integer...) = all(EqX(1), I) ? c : throw(BoundsError())
getindex(c::Char, I::Real...) = getindex(c, to_indexes(I...)...)
first(c::Char) = c
last(c::Char) = c
eltype(::Type{Char}) = Char
Expand Down
15 changes: 15 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,21 @@ end
to_indexes(I...)
end

@noinline function getindex(c::Char, I::Real...)
depwarn("getindex(c::Char, I::Real...) is deprecated, use getindex(c, map(Int, I)...) instead.", :getindex)
getindex(c, map(Int, I)...)
end

@noinline function getindex(s::AbstractString, x::Real)
depwarn("getindex(s::AbstractString, x::Real) is deprecated, use getindex(s, Int(x)) instead.", :getindex)
getindex(s, Int(x))
end

@noinline function checkbounds(s::AbstractString, i::Real)
depwarn("checkbounds(s::AbstractString, i::Real) is deprecated, use checkbounds(s, Int(i)) instead.", :checkbounds)
checkbounds(s, Int(i))
end

@noinline function float_isvalid{T<:Union{Float32,Float64}}(s::AbstractString, out::Array{T,1})
tf = tryparse(T, s)
isnull(tf) || (out[1] = get(tf))
Expand Down
4 changes: 2 additions & 2 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ start(s::AbstractString) = 1
done(s::AbstractString,i) = (i > endof(s))
getindex(s::AbstractString, i::Int) = next(s,i)[1]
getindex(s::AbstractString, i::Integer) = s[Int(i)]
getindex(s::AbstractString, x::Real) = s[to_index(x)]
getindex{T<:Integer}(s::AbstractString, r::UnitRange{T}) = s[Int(first(r)):Int(last(r))]
# TODO: handle other ranges with stride ±1 specially?
getindex(s::AbstractString, v::AbstractVector) =
Expand Down Expand Up @@ -150,9 +149,10 @@ function nextind(s::AbstractString, i::Integer)
end

checkbounds(s::AbstractString, i::Integer) = start(s) <= i <= endof(s) || throw(BoundsError(s, i))
checkbounds(s::AbstractString, i::Real) = checkbounds(s, to_index(i))
checkbounds{T<:Integer}(s::AbstractString, r::Range{T}) = isempty(r) || (minimum(r) >= start(s) && maximum(r) <= endof(s)) || throw(BoundsError(s, r))
# The following will end up using a deprecated checkbounds, when T is not Integer
checkbounds{T<:Real}(s::AbstractString, I::AbstractArray{T}) = all(i -> checkbounds(s, i), I)
checkbounds{T<:Integer}(s::AbstractString, I::AbstractArray{T}) = all(i -> checkbounds(s, i), I)

ind2chr(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end
chr2ind(s::DirectIndexString, i::Integer) = begin checkbounds(s,i); i end
Expand Down

0 comments on commit 67690c0

Please sign in to comment.