Skip to content

Commit f5c5109

Browse files
committed
Rename endof->endindex and introduce beginindex. Fixes #23354
1 parent 8cb6949 commit f5c5109

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+276
-218
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ New language features
4343
* Values for `Enum`s can now be specified inside of a `begin` block when using the
4444
`@enum` macro ([#25424]).
4545

46+
* `a[begin]` can now be used to address the first element of an integer-indexed collection `a`.
47+
The index is computed by `beginindex(a)` ([#23554]).
48+
4649
Language changes
4750
----------------
4851

@@ -918,6 +921,8 @@ Deprecated or removed
918921

919922
* `findin(a, b)` has been deprecated in favor of `find(occursin(b), a)` ([#24673]).
920923

924+
* `endof(a)` has been renamed `endindex(a)` ([#23554]).
925+
921926
Command-line option changes
922927
---------------------------
923928

@@ -1074,6 +1079,7 @@ Command-line option changes
10741079
[#23323]: https://github.com/JuliaLang/julia/issues/23323
10751080
[#23341]: https://github.com/JuliaLang/julia/issues/23341
10761081
[#23342]: https://github.com/JuliaLang/julia/issues/23342
1082+
[#23354]: https://github.com/JuliaLang/julia/issues/23354
10771083
[#23366]: https://github.com/JuliaLang/julia/issues/23366
10781084
[#23373]: https://github.com/JuliaLang/julia/issues/23373
10791085
[#23404]: https://github.com/JuliaLang/julia/issues/23404

base/abstractarray.jl

+18-5
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ ndims(::Type{T}) where {T<:AbstractArray} = ndims(supertype(T))
146146
147147
Return the number of elements in the collection.
148148
149-
Use [`endof`](@ref) to get the last valid index of an indexable collection.
149+
Use [`endindex`](@ref) to get the last valid index of an indexable collection.
150150
151151
# Examples
152152
```jldoctest
@@ -165,17 +165,30 @@ _length(A::AbstractArray) = (@_inline_meta; prod(map(unsafe_length, axes(A)))) #
165165
_length(A) = (@_inline_meta; length(A))
166166

167167
"""
168-
endof(collection) -> Integer
168+
endindex(collection) -> Integer
169169
170170
Return the last index of the collection.
171171
172172
# Examples
173173
```jldoctest
174-
julia> endof([1,2,4])
174+
julia> endindex([1,2,4])
175175
3
176176
```
177177
"""
178-
endof(a::AbstractArray) = (@_inline_meta; last(linearindices(a)))
178+
endindex(a::AbstractArray) = (@_inline_meta; last(linearindices(a)))
179+
180+
"""
181+
beginindex(collection) -> Integer
182+
183+
Return the first index of the collection.
184+
185+
# Examples
186+
```jldoctest
187+
julia> beginindex([1,2,4])
188+
1
189+
```
190+
"""
191+
beginindex(a::AbstractArray) = (@_inline_meta; first(linearindices(a)))
179192

180193
first(a::AbstractArray) = a[first(eachindex(a))]
181194

@@ -204,7 +217,7 @@ end
204217
last(coll)
205218
206219
Get the last element of an ordered collection, if it can be computed in O(1) time. This is
207-
accomplished by calling [`endof`](@ref) to get the last index. Return the end
220+
accomplished by calling [`endindex`](@ref) to get the last index. Return the end
208221
point of an `AbstractRange` even if it is empty.
209222
210223
# Examples

base/array.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ julia> findnext(A, 3)
15121512
```
15131513
"""
15141514
function findnext(A, start::Integer)
1515-
l = endof(A)
1515+
l = endindex(A)
15161516
i = start
15171517
warned = false
15181518
while i <= l
@@ -1572,7 +1572,7 @@ julia> findnext(isodd, A, 2)
15721572
```
15731573
"""
15741574
function findnext(testf::Function, A, start::Integer)
1575-
l = endof(A)
1575+
l = endindex(A)
15761576
i = start
15771577
while i <= l
15781578
if testf(A[i])
@@ -1664,7 +1664,7 @@ julia> findlast(A)
16641664
0
16651665
```
16661666
"""
1667-
findlast(A) = findprev(A, endof(A))
1667+
findlast(A) = findprev(A, endindex(A))
16681668

16691669
"""
16701670
findprev(predicate::Function, A, i::Integer)
@@ -1715,7 +1715,7 @@ julia> findlast(x -> x > 5, A)
17151715
0
17161716
```
17171717
"""
1718-
findlast(testf::Function, A) = findprev(testf, A, endof(A))
1718+
findlast(testf::Function, A) = findprev(testf, A, endindex(A))
17191719

17201720
"""
17211721
find(f::Function, A)

base/char.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ size(c::Char,d) = convert(Int, d) < 1 ? throw(BoundsError()) : 1
7070
ndims(c::Char) = 0
7171
ndims(::Type{Char}) = 0
7272
length(c::Char) = 1
73-
endof(c::Char) = 1
73+
beginindex(c::Char) = 1
74+
endindex(c::Char) = 1
7475
getindex(c::Char) = c
7576
getindex(c::Char, i::Integer) = i == 1 ? c : throw(BoundsError())
7677
getindex(c::Char, I::Integer...) = all(x -> x == 1, I) ? c : throw(BoundsError())

base/deprecated.jl

+8-3
Original file line numberDiff line numberDiff line change
@@ -3871,9 +3871,9 @@ end
38713871
@deprecate rsearch(s::AbstractString, r::Regex) findlast(r, s)
38723872
@deprecate rsearch(s::AbstractString, c::Char, i::Integer) findprev(equalto(c), s, i)
38733873
@deprecate rsearch(s::AbstractString, c::Char) findlast(equalto(c), s)
3874-
@deprecate rsearch(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = endof(a)) findprev(equalto(b), a, i)
3875-
@deprecate rsearch(a::String, b::Union{Int8,UInt8}, i::Integer = endof(a)) findprev(equalto(Char(b)), a, i)
3876-
@deprecate rsearch(a::ByteArray, b::Char, i::Integer = endof(a)) findprev(equalto(UInt8(b)), a, i)
3874+
@deprecate rsearch(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = endindex(a)) findprev(equalto(b), a, i)
3875+
@deprecate rsearch(a::String, b::Union{Int8,UInt8}, i::Integer = endindex(a)) findprev(equalto(Char(b)), a, i)
3876+
@deprecate rsearch(a::ByteArray, b::Char, i::Integer = endindex(a)) findprev(equalto(UInt8(b)), a, i)
38773877

38783878
@deprecate searchindex(s::AbstractString, t::AbstractString) first(findfirst(t, s))
38793879
@deprecate searchindex(s::AbstractString, t::AbstractString, i::Integer) first(findnext(t, s, i))
@@ -3889,6 +3889,11 @@ end
38893889

38903890
@deprecate findin(a, b) find(occursin(b), a)
38913891

3892+
@deprecate endof(a) endindex(a)
3893+
function beginindex(a)
3894+
depwarn("if appropriate you should implement `beginindex` for type $(typeof(a)), which might just return 1", :beginof)
3895+
1
3896+
end
38923897

38933898
# END 0.7 deprecations
38943899
# BEGIN 1.0 deprecations

base/dict.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
1616

1717
lastidx != 0 && str[lastidx] in chars && (lastidx = prevind(str, lastidx))
1818
truncidx == 0 && (truncidx = lastidx)
19-
if lastidx < endof(str)
19+
if lastidx < endindex(str)
2020
return String(SubString(str, 1, truncidx) * truncmark)
2121
else
2222
return String(str)

base/essentials.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ function length(v::SimpleVector)
546546
@_gc_preserve_end t
547547
return l
548548
end
549-
endof(v::SimpleVector) = length(v)
549+
beginindex(v::SimpleVector) = 1
550+
endindex(v::SimpleVector) = length(v)
550551
start(v::SimpleVector) = 1
551552
next(v::SimpleVector,i) = (v[i],i+1)
552553
done(v::SimpleVector,i) = (length(v) < i)

base/exports.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -619,14 +619,15 @@ export
619619
allunique,
620620
any!,
621621
any,
622+
beginindex,
622623
collect,
623624
count,
624625
delete!,
625626
deleteat!,
626627
eltype,
627628
empty!,
628629
empty,
629-
endof,
630+
endindex,
630631
filter!,
631632
filter,
632633
foldl,

base/gmp.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,9 @@ hastypemax(::Type{BigInt}) = false
238238

239239
function tryparse_internal(::Type{BigInt}, s::AbstractString, startpos::Int, endpos::Int, base_::Integer, raise::Bool)
240240
# don't make a copy in the common case where we are parsing a whole String
241-
bstr = startpos == start(s) && endpos == endof(s) ? String(s) : String(SubString(s,startpos,endpos))
241+
bstr = startpos == start(s) && endpos == endindex(s) ? String(s) : String(SubString(s,startpos,endpos))
242242

243-
sgn, base, i = Base.parseint_preamble(true,Int(base_),bstr,start(bstr),endof(bstr))
243+
sgn, base, i = Base.parseint_preamble(true,Int(base_),bstr,start(bstr),endindex(bstr))
244244
if !(2 <= base <= 62)
245245
raise && throw(ArgumentError("invalid base: base must be 2 ≤ base ≤ 62, got $base"))
246246
return nothing

base/int.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,9 @@ end
539539
macro big_str(s)
540540
if '_' in s
541541
# remove _ in s[2:end-1]
542-
bf = IOBuffer(endof(s))
542+
bf = IOBuffer(endindex(s))
543543
print(bf, s[1])
544-
for c in SubString(s, 2, endof(s)-1)
544+
for c in SubString(s, 2, endindex(s)-1)
545545
c != '_' && print(bf, c)
546546
end
547547
print(bf, s[end])

base/io.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -648,14 +648,14 @@ function readuntil(s::IO, delim::T) where T
648648
return out
649649
end
650650

651-
# requires that indices for target are small ordered integers bounded by start and endof
651+
# requires that indices for target are small ordered integers bounded by start and endindex
652652
function readuntil_indexable(io::IO, target#=::Indexable{T}=#, out)
653653
T = eltype(target)
654654
first = start(target)
655655
if done(target, first)
656656
return
657657
end
658-
len = endof(target)
658+
len = endindex(target)
659659
local cache # will be lazy initialized when needed
660660
second = next(target, first)[2]
661661
max_pos = second

base/markdown/parse/parse.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ config(md::MD) = md.meta[:config]::Config
2323
Base.push!(md::MD, x) = push!(md.content, x)
2424
Base.getindex(md::MD, args...) = md.content[args...]
2525
Base.setindex!(md::MD, args...) = setindex!(md.content, args...)
26-
Base.endof(md::MD) = endof(md.content)
26+
Base.endindex(md::MD) = endindex(md.content)
27+
Base.beginindex(md::MD) = beginindex(md.content)
2728
Base.length(md::MD) = length(md.content)
2829
Base.isempty(md::MD) = isempty(md.content)
2930

base/namedtuple.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ length(t::NamedTuple) = nfields(t)
4848
start(t::NamedTuple) = 1
4949
done(t::NamedTuple, iter) = iter > nfields(t)
5050
next(t::NamedTuple, iter) = (getfield(t, iter), iter + 1)
51-
endof(t::NamedTuple) = nfields(t)
51+
beginindex(t::NamedTuple) = 1
52+
endindex(t::NamedTuple) = nfields(t)
5253
getindex(t::NamedTuple, i::Int) = getfield(t, i)
5354
getindex(t::NamedTuple, i::Symbol) = getfield(t, i)
5455
indexed_next(t::NamedTuple, i::Int, state) = (getfield(t, i), i+1)

base/number.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ eltype(::Type{T}) where {T<:Number} = T
5252
ndims(x::Number) = 0
5353
ndims(::Type{<:Number}) = 0
5454
length(x::Number) = 1
55-
endof(x::Number) = 1
55+
beginindex(x::Number) = 1
56+
endindex(x::Number) = 1
5657
IteratorSize(::Type{<:Number}) = HasShape()
5758
keys(::Number) = OneTo(1)
5859

base/pair.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ getindex(p::Pair,i::Int) = getfield(p,i)
5252
getindex(p::Pair,i::Real) = getfield(p, convert(Int, i))
5353
reverse(p::Pair{A,B}) where {A,B} = Pair{B,A}(p.second, p.first)
5454

55-
endof(p::Pair) = 2
55+
beginindex(p::Pair) = 1
56+
endindex(p::Pair) = 2
5657
length(p::Pair) = 2
5758
first(p::Pair) = p.first
5859
last(p::Pair) = p.second

base/parse.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,16 @@ Like [`parse`](@ref), but returns either a value of the requested type,
208208
or [`nothing`](@ref) if the string does not contain a valid number.
209209
"""
210210
tryparse(::Type{T}, s::AbstractString, base::Integer) where {T<:Integer} =
211-
tryparse_internal(T, s, start(s), endof(s), check_valid_base(base), false)
211+
tryparse_internal(T, s, start(s), endindex(s), check_valid_base(base), false)
212212
tryparse(::Type{T}, s::AbstractString) where {T<:Integer} =
213-
tryparse_internal(T, s, start(s), endof(s), 0, false)
213+
tryparse_internal(T, s, start(s), endindex(s), 0, false)
214214

215215
function parse(::Type{T}, s::AbstractString, base::Integer) where T<:Integer
216-
tryparse_internal(T, s, start(s), endof(s), check_valid_base(base), true)
216+
tryparse_internal(T, s, start(s), endindex(s), check_valid_base(base), true)
217217
end
218218

219219
function parse(::Type{T}, s::AbstractString) where T<:Integer
220-
tryparse_internal(T, s, start(s), endof(s), 0, true) # Zero means, "figure it out"
220+
tryparse_internal(T, s, start(s), endindex(s), 0, true) # Zero means, "figure it out"
221221
end
222222

223223
## string to float functions ##
@@ -331,7 +331,7 @@ tryparse_internal(T::Type{<:Complex}, s::AbstractString, i::Int, e::Int, raise::
331331

332332
# fallback methods for tryparse_internal
333333
tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int) where T<:Real =
334-
startpos == start(s) && endpos == endof(s) ? tryparse(T, s) : tryparse(T, SubString(s, startpos, endpos))
334+
startpos == start(s) && endpos == endindex(s) ? tryparse(T, s) : tryparse(T, SubString(s, startpos, endpos))
335335
function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int, raise::Bool) where T<:Real
336336
result = tryparse_internal(T, s, startpos, endpos)
337337
if raise && result === nothing
@@ -343,4 +343,4 @@ tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int, rais
343343
tryparse_internal(T, s, startpos, endpos, 10, raise)
344344

345345
parse(::Type{T}, s::AbstractString) where T<:Union{Real,Complex} =
346-
tryparse_internal(T, s, start(s), endof(s), true)
346+
tryparse_internal(T, s, start(s), endindex(s), true)

base/precompile.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -774,11 +774,11 @@ precompile(Tuple{Type{BoundsError}, Array{Expr, 1}, Base.UnitRange{Int64}})
774774
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@ncall")), Int64, Symbol, Symbol})
775775
precompile(Tuple{typeof(Base.getindex), Tuple{Symbol}, Base.UnitRange{Int64}})
776776
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@ncall")), Int64, Symbol, Symbol, Expr})
777-
precompile(Tuple{typeof(Base.endof), Tuple{Symbol, Expr}})
777+
precompile(Tuple{typeof(Base.endindex), Tuple{Symbol, Expr}})
778778
precompile(Tuple{typeof(Base.getindex), Tuple{Symbol, Expr}, Base.UnitRange{Int64}})
779779
precompile(Tuple{getfield(Base.Cartesian, Symbol("#@nloops")), Int64, Symbol, Expr, Expr})
780-
precompile(Tuple{typeof(Base.endof), Tuple{Expr}})
781-
precompile(Tuple{typeof(Base.endof), Tuple{Symbol, Symbol, Symbol}})
780+
precompile(Tuple{typeof(Base.endindex), Tuple{Expr}})
781+
precompile(Tuple{typeof(Base.endindex), Tuple{Symbol, Symbol, Symbol}})
782782
precompile(Tuple{typeof(Base.getindex), Tuple{Symbol, Symbol, Symbol}, Base.UnitRange{Int64}})
783783
precompile(Tuple{Type{Expr}, Symbol, Symbol, Symbol, Symbol, Symbol, Symbol})
784784
precompile(Tuple{typeof(Base.join), Base.GenericIOBuffer{Array{UInt8, 1}}, Tuple{String, String}, Char})
@@ -991,7 +991,7 @@ precompile(Tuple{typeof(Base.Markdown.terminline), Base.GenericIOBuffer{Array{UI
991991
precompile(Tuple{typeof(Base._search), Base.SubString{String}, String, Int64})
992992
precompile(Tuple{typeof(Base._split), Base.SubString{String}, String, Int64, Bool, Array{Base.SubString{String}, 1}})
993993
precompile(Tuple{getfield(Base.Markdown, Symbol("#kw##wrapped_lines")), Array{Any, 1}, typeof(Base.Markdown.wrapped_lines), Base.SubString{String}})
994-
precompile(Tuple{typeof(Base.endof), Array{AbstractString, 1}})
994+
precompile(Tuple{typeof(Base.endindex), Array{AbstractString, 1}})
995995
precompile(Tuple{typeof(Base.getindex), Array{AbstractString, 1}, Base.UnitRange{Int64}})
996996
precompile(Tuple{typeof(Base.throw_boundserror), Array{AbstractString, 1}, Tuple{Base.UnitRange{Int64}}})
997997
precompile(Tuple{typeof(Base.unsafe_copyto!), Array{AbstractString, 1}, Int64, Array{AbstractString, 1}, Int64, Int64})

base/printf.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function parse(s::AbstractString)
5454
j1 = j
5555
j = k
5656
end
57-
i > endof(s) || push!(list, s[i:end])
57+
i > endindex(s) || push!(list, s[i:end])
5858
# coalesce adjacent strings
5959
i = 1
6060
while i < length(list)

base/process.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -829,7 +829,7 @@ wait(x::ProcessChain) = for p in x.processes; wait(p); end
829829
show(io::IO, p::Process) = print(io, "Process(", p.cmd, ", ", process_status(p), ")")
830830

831831
# allow the elements of the Cmd to be accessed as an array or iterator
832-
for f in (:length, :endof, :start, :keys, :eltype, :first, :last)
832+
for f in (:length, :beginindex, :endindex, :start, :keys, :eltype, :first, :last)
833833
@eval $f(cmd::Cmd) = $f(cmd.exec)
834834
end
835835
for f in (:next, :done, :getindex)

base/random/generation.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,12 @@ Sampler(rng::AbstractRNG, str::AbstractString, n::Val{Inf}) = Sampler(rng, colle
416416
# when generating only one char from a string, the specialized method below
417417
# is usually more efficient
418418
Sampler(rng::AbstractRNG, str::AbstractString, ::Val{1}) =
419-
SamplerSimple(str, Sampler(rng, 1:_endof(str), Val(Inf)))
419+
SamplerSimple(str, Sampler(rng, 1:_endindex(str), Val(Inf)))
420420

421421
isvalid_unsafe(s::String, i) = !Base.is_valid_continuation(Base.@gc_preserve s unsafe_load(pointer(s), i))
422422
isvalid_unsafe(s::AbstractString, i) = isvalid(s, i)
423-
_endof(s::String) = sizeof(s)
424-
_endof(s::AbstractString) = endof(s)
423+
_endindex(s::String) = sizeof(s)
424+
_endindex(s::AbstractString) = endindex(s)
425425

426426
function rand(rng::AbstractRNG, sp::SamplerSimple{<:AbstractString,<:Sampler})::Char
427427
str = sp[]

base/reduce.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ end
126126
Like [`mapreduce`](@ref), but with guaranteed right associativity, as in [`foldr`](@ref).
127127
`v0` will be used exactly once.
128128
"""
129-
mapfoldr(f, op, v0, itr) = mapfoldr_impl(f, op, v0, itr, endof(itr))
129+
mapfoldr(f, op, v0, itr) = mapfoldr_impl(f, op, v0, itr, endindex(itr))
130130

131131
"""
132132
mapfoldr(f, op, itr)
@@ -137,7 +137,7 @@ Specifically, `mapfoldr(f, op, itr)` produces the same result as
137137
In general, this cannot be used with empty collections (see [`reduce(op, itr)`](@ref)).
138138
"""
139139
function mapfoldr(f, op, itr)
140-
i = endof(itr)
140+
i = endindex(itr)
141141
if isempty(itr)
142142
return Base.mapreduce_empty_iter(f, op, itr, IteratorEltype(itr))
143143
end

0 commit comments

Comments
 (0)