diff --git a/base/strings/basic.jl b/base/strings/basic.jl index a523c67897a7e..2305ab422950f 100644 --- a/base/strings/basic.jl +++ b/base/strings/basic.jl @@ -34,8 +34,10 @@ getindex(s::AbstractString, i::Integer) = s[Int(i)] getindex(s::AbstractString, i::Colon) = s 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) = +getindex{T<:Integer}(s::AbstractString, v::AbstractVector{T}) = sprint(length(v), io->(for i in v; write(io,s[i]) end)) +getindex(s::AbstractString, v::AbstractVector{Bool}) = + throw(ArgumentError("logical indexing not supported for strings")) Symbol(s::AbstractString) = Symbol(String(s)) diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 9048ba7e2036a..e5d264c66b971 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -172,9 +172,9 @@ gstr = GenericString("12") @test convert(Array{Char,1}, gstr) ==['1';'2'] @test convert(Symbol, gstr)==Symbol("12") -@test getindex(gstr, Bool(1))=='1' -@test getindex(gstr,Bool(1):Bool(1))=="1" -@test getindex(gstr,AbstractVector([Bool(1):Bool(1);]))=="1" +@test gstr[1] == '1' +@test gstr[1:1] == "1" +@test gstr[[1]] == "1" @test done(eachindex("foobar"),7) @test eltype(Base.EachStringIndex) == Int @@ -187,9 +187,8 @@ gstr = GenericString("12") @test length(GenericString(""))==0 -@test getindex(gstr,AbstractVector([Bool(1):Bool(1);]))=="1" - -@test nextind(AbstractArray([Bool(1):Bool(1);]),1)==2 +@test nextind(1:1, 1) == 2 +@test nextind([1], 1) == 2 @test ind2chr(gstr,2)==2 @@ -461,3 +460,7 @@ Base.endof(x::CharStr) = endof(x.chars) # Case with Unicode characters @test cmp("\U1f596\U1f596", CharStr("\U1f596")) == 1 # Gives BoundsError with bug @test cmp(CharStr("\U1f596"), "\U1f596\U1f596") == -1 + +# issue #12495: check that logical indexing attempt raises ArgumentError +@test_throws ArgumentError "abc"[[true, false, true]] +@test_throws ArgumentError "abc"[BitArray([true, false, true])]