Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2142d3

Browse files
committedJan 26, 2017
Restrict signature for getindex(::AbstractString, ::AbstractVector}
Only integer indices are supported by the generic interface, so better raise a MethodError for other element types rather than failing later. Ideally the Bool-specific method would not be needed, but it is required as long as Bool<:Integer.
1 parent 1303dfb commit a2142d3

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed
 

‎base/strings/basic.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ getindex(s::AbstractString, i::Integer) = s[Int(i)]
3434
getindex(s::AbstractString, i::Colon) = s
3535
getindex{T<:Integer}(s::AbstractString, r::UnitRange{T}) = s[Int(first(r)):Int(last(r))]
3636
# TODO: handle other ranges with stride ±1 specially?
37-
getindex(s::AbstractString, v::AbstractVector) =
37+
getindex{T<:Integer}(s::AbstractString, v::AbstractVector{T}) =
3838
sprint(length(v), io->(for i in v; write(io,s[i]) end))
39+
getindex(s::AbstractString, v::AbstractVector{Bool}) =
40+
throw(ArgumentError("logical indexing not supported for strings"))
3941

4042
Symbol(s::AbstractString) = Symbol(String(s))
4143

‎test/strings/basic.jl

+9-6
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ gstr = GenericString("12")
172172
@test convert(Array{Char,1}, gstr) ==['1';'2']
173173
@test convert(Symbol, gstr)==Symbol("12")
174174

175-
@test getindex(gstr, Bool(1))=='1'
176-
@test getindex(gstr,Bool(1):Bool(1))=="1"
177-
@test getindex(gstr,AbstractVector([Bool(1):Bool(1);]))=="1"
175+
@test gstr[1] == '1'
176+
@test gstr[1:1] == "1"
177+
@test gstr[[1]] == "1"
178178

179179
@test done(eachindex("foobar"),7)
180180
@test eltype(Base.EachStringIndex) == Int
@@ -187,9 +187,8 @@ gstr = GenericString("12")
187187

188188
@test length(GenericString(""))==0
189189

190-
@test getindex(gstr,AbstractVector([Bool(1):Bool(1);]))=="1"
191-
192-
@test nextind(AbstractArray([Bool(1):Bool(1);]),1)==2
190+
@test nextind(1:1, 1) == 2
191+
@test nextind([1], 1) == 2
193192

194193
@test ind2chr(gstr,2)==2
195194

@@ -461,3 +460,7 @@ Base.endof(x::CharStr) = endof(x.chars)
461460
# Case with Unicode characters
462461
@test cmp("\U1f596\U1f596", CharStr("\U1f596")) == 1 # Gives BoundsError with bug
463462
@test cmp(CharStr("\U1f596"), "\U1f596\U1f596") == -1
463+
464+
# issue #12495: logical indexing attempt raises ArgumentError
465+
@test_throws ArgumentError "abc"[[true, false, true]]
466+
@test_throws ArgumentError "abc"[BitArray([true, false, true])]

0 commit comments

Comments
 (0)
Please sign in to comment.