Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate getindex/checkbounds methods for non Integer Real indices for Chars and Strings #12905

Merged
merged 1 commit into from Sep 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion base/char.jl
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
4 changes: 4 additions & 0 deletions base/deprecated.jl
Expand Up @@ -439,6 +439,10 @@ end
to_indexes(I...)
end

@deprecate getindex(c::Char, I::Real...) getindex(c, map(Int, I)...)
@deprecate getindex(s::AbstractString, x::Real) getindex(s, Int(x))
@deprecate checkbounds(s::AbstractString, i::Real) checkbounds(s, Int(i))

@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
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