Skip to content

Commit

Permalink
make checkbounds(string, ...) return nothing or error
Browse files Browse the repository at this point in the history
actually fix #24840, partly (badly) fixed by #24999
  • Loading branch information
StefanKarpinski committed Dec 15, 2017
1 parent 5a2ef5f commit 485dad7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
6 changes: 3 additions & 3 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ checkbounds(::Type{Bool}, s::AbstractString, i::Integer) =
checkbounds(::Type{Bool}, s::AbstractString, r::AbstractRange{<:Integer}) =
isempty(r) || (1 minimum(r) && maximum(r) ncodeunits(s))
checkbounds(::Type{Bool}, s::AbstractString, I::AbstractArray{<:Real}) =
all(i -> checkbounds(s, i), I)
all(i -> checkbounds(Bool, s, i), I)
checkbounds(::Type{Bool}, s::AbstractString, I::AbstractArray{<:Integer}) =
all(i -> checkbounds(s, i), I)
all(i -> checkbounds(Bool, s, i), I)
checkbounds(s::AbstractString, I::Union{Integer,AbstractArray}) =
checkbounds(Bool, s, I) || throw(BoundsError(s, I))
checkbounds(Bool, s, I) ? nothing : throw(BoundsError(s, I))

## construction, conversion, promotion ##

Expand Down
19 changes: 16 additions & 3 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,22 @@ end
@test_throws BoundsError checkbounds("hello", 4:6)
@test_throws BoundsError checkbounds("hello", [0:3;])
@test_throws BoundsError checkbounds("hello", [4:6;])
@test checkbounds("hello", 2)
@test checkbounds("hello", 1:5)
@test checkbounds("hello", [1:5;])
@test checkbounds("hello", 1) === nothing
@test checkbounds("hello", 5) === nothing
@test checkbounds("hello", 1:3) === nothing
@test checkbounds("hello", 3:5) === nothing
@test checkbounds("hello", [1:3;]) === nothing
@test checkbounds("hello", [3:5;]) === nothing
@test checkbounds(Bool, "hello", 0) === false
@test checkbounds(Bool, "hello", 1) === true
@test checkbounds(Bool, "hello", 5) === true
@test checkbounds(Bool, "hello", 6) === false
@test checkbounds(Bool, "hello", 0:5) === false
@test checkbounds(Bool, "hello", 1:6) === false
@test checkbounds(Bool, "hello", 1:5) === true
@test checkbounds(Bool, "hello", [0:5;]) === false
@test checkbounds(Bool, "hello", [1:6;]) === false
@test checkbounds(Bool, "hello", [1:5;]) === true
end

@testset "issue #15624 (indexing with out of bounds empty range)" begin
Expand Down

0 comments on commit 485dad7

Please sign in to comment.