Skip to content

Commit bf424d7

Browse files
committed
Change sentinel in find(first|next|prev|last) to nothing
1 parent 64c3fbd commit bf424d7

37 files changed

+203
-206
lines changed

base/array.jl

+16-20
Original file line numberDiff line numberDiff line change
@@ -1495,7 +1495,7 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
14951495
"""
14961496
findnext(A, i::Integer)
14971497
1498-
Find the next linear index >= `i` of a `true` element of `A`, or `0` if not found.
1498+
Find the next linear index >= `i` of a `true` element of `A`, or `nothing` if not found.
14991499
15001500
# Examples
15011501
```jldoctest
@@ -1508,7 +1508,6 @@ julia> findnext(A, 1)
15081508
2
15091509
15101510
julia> findnext(A, 3)
1511-
0
15121511
```
15131512
"""
15141513
function findnext(A, start::Integer)
@@ -1526,14 +1525,14 @@ function findnext(A, start::Integer)
15261525
end
15271526
i = nextind(A, i)
15281527
end
1529-
return 0
1528+
return nothing
15301529
end
15311530

15321531
"""
15331532
findfirst(A)
15341533
15351534
Return the linear index of the first `true` value in `A`.
1536-
Return `0` if no such value is found.
1535+
Return `nothing` if no such value is found.
15371536
To search for other kinds of values, pass a predicate as the first argument.
15381537
15391538
# Examples
@@ -1547,15 +1546,15 @@ julia> findfirst(A)
15471546
2
15481547
15491548
julia> findfirst(falses(3))
1550-
0
15511549
```
15521550
"""
15531551
findfirst(A) = findnext(A, 1)
15541552

15551553
"""
15561554
findnext(predicate::Function, A, i::Integer)
15571555
1558-
Find the next linear index >= `i` of an element of `A` for which `predicate` returns `true`, or `0` if not found.
1556+
Find the next linear index >= `i` of an element of `A` for which `predicate` returns `true`,
1557+
or `nothing` if not found.
15591558
15601559
# Examples
15611560
```jldoctest
@@ -1568,7 +1567,6 @@ julia> findnext(isodd, A, 1)
15681567
1
15691568
15701569
julia> findnext(isodd, A, 2)
1571-
0
15721570
```
15731571
"""
15741572
function findnext(testf::Function, A, start::Integer)
@@ -1580,14 +1578,14 @@ function findnext(testf::Function, A, start::Integer)
15801578
end
15811579
i = nextind(A, i)
15821580
end
1583-
return 0
1581+
return nothing
15841582
end
15851583

15861584
"""
15871585
findfirst(predicate::Function, A)
15881586
15891587
Return the linear index of the first element of `A` for which `predicate` returns `true`.
1590-
Return `0` if there is no such element.
1588+
Return `nothing` if there is no such element.
15911589
15921590
# Examples
15931591
```jldoctest
@@ -1600,7 +1598,6 @@ julia> findfirst(iseven, A)
16001598
2
16011599
16021600
julia> findfirst(x -> x>10, A)
1603-
0
16041601
16051602
julia> findfirst(equalto(4), A)
16061603
3
@@ -1611,7 +1608,7 @@ findfirst(testf::Function, A) = findnext(testf, A, 1)
16111608
"""
16121609
findprev(A, i::Integer)
16131610
1614-
Find the previous linear index <= `i` of a `true` element of `A`, or `0` if not found.
1611+
Find the previous linear index <= `i` of a `true` element of `A`, or `nothing` if not found.
16151612
16161613
# Examples
16171614
```jldoctest
@@ -1624,7 +1621,6 @@ julia> findprev(A,2)
16241621
2
16251622
16261623
julia> findprev(A,1)
1627-
0
16281624
```
16291625
"""
16301626
function findprev(A, start::Integer)
@@ -1639,14 +1635,14 @@ function findprev(A, start::Integer)
16391635
a != 0 && return i
16401636
i = prevind(A, i)
16411637
end
1642-
return 0
1638+
return nothing
16431639
end
16441640

16451641
"""
16461642
findlast(A)
16471643
16481644
Return the linear index of the last `true` value in `A`.
1649-
Return `0` if there is no `true` value in `A`.
1645+
Return `nothing` if there is no `true` value in `A`.
16501646
16511647
# Examples
16521648
```jldoctest
@@ -1661,7 +1657,6 @@ julia> findlast(A)
16611657
julia> A = falses(2,2);
16621658
16631659
julia> findlast(A)
1664-
0
16651660
```
16661661
"""
16671662
findlast(A) = findprev(A, endof(A))
@@ -1670,7 +1665,7 @@ findlast(A) = findprev(A, endof(A))
16701665
findprev(predicate::Function, A, i::Integer)
16711666
16721667
Find the previous linear index <= `i` of an element of `A` for which `predicate` returns `true`, or
1673-
`0` if not found.
1668+
`nothing` if not found.
16741669
16751670
# Examples
16761671
```jldoctest
@@ -1680,7 +1675,6 @@ julia> A = [4 6; 1 2]
16801675
1 2
16811676
16821677
julia> findprev(isodd, A, 1)
1683-
0
16841678
16851679
julia> findprev(isodd, A, 3)
16861680
2
@@ -1692,14 +1686,14 @@ function findprev(testf::Function, A, start::Integer)
16921686
testf(A[i]) && return i
16931687
i = prevind(A, i)
16941688
end
1695-
return 0
1689+
return nothing
16961690
end
16971691

16981692
"""
16991693
findlast(predicate::Function, A)
17001694
17011695
Return the linear index of the last element of `A` for which `predicate` returns `true`.
1702-
Return `0` if there is no such element.
1696+
Return `nothing` if there is no such element.
17031697
17041698
# Examples
17051699
```jldoctest
@@ -1712,11 +1706,13 @@ julia> findlast(isodd, A)
17121706
2
17131707
17141708
julia> findlast(x -> x > 5, A)
1715-
0
17161709
```
17171710
"""
17181711
findlast(testf::Function, A) = findprev(testf, A, endof(A))
17191712

1713+
zero_sentinel(i) = i === nothing ? 0 : i
1714+
nothing_sentinel(i) = i == 0 ? nothing : i
1715+
17201716
"""
17211717
find(f::Function, A)
17221718

base/bitarray.jl

+15-15
Original file line numberDiff line numberDiff line change
@@ -1459,13 +1459,13 @@ function unsafe_bitfindnext(Bc::Vector{UInt64}, start::Integer)
14591459
end
14601460
end
14611461
end
1462-
return 0
1462+
return nothing
14631463
end
14641464

14651465
# returns the index of the next non-zero element, or 0 if all zeros
14661466
function findnext(B::BitArray, start::Integer)
14671467
start > 0 || throw(BoundsError(B, start))
1468-
start > length(B) && return 0
1468+
start > length(B) && return nothing
14691469
unsafe_bitfindnext(B.chunks, start)
14701470
end
14711471

@@ -1474,11 +1474,11 @@ end
14741474
# aux function: same as findnext(~B, start), but performed without temporaries
14751475
function findnextnot(B::BitArray, start::Integer)
14761476
start > 0 || throw(BoundsError(B, start))
1477-
start > length(B) && return 0
1477+
start > length(B) && return nothing
14781478

14791479
Bc = B.chunks
14801480
l = length(Bc)
1481-
l == 0 && return 0
1481+
l == 0 && return nothing
14821482

14831483
chunk_start = _div64(start-1)+1
14841484
within_chunk_start = _mod64(start-1)
@@ -1499,7 +1499,7 @@ function findnextnot(B::BitArray, start::Integer)
14991499
elseif Bc[l] | mask != _msk_end(B)
15001500
return (l-1) << 6 + trailing_ones(Bc[l] | mask) + 1
15011501
end
1502-
return 0
1502+
return nothing
15031503
end
15041504
findfirstnot(B::BitArray) = findnextnot(B,1)
15051505

@@ -1508,7 +1508,7 @@ function findnext(pred::EqualTo, B::BitArray, start::Integer)
15081508
v = pred.x
15091509
v == false && return findnextnot(B, start)
15101510
v == true && return findnext(B, start)
1511-
return 0
1511+
return nothing
15121512
end
15131513
#findfirst(B::BitArray, v) = findnext(B, 1, v) ## defined in array.jl
15141514

@@ -1520,9 +1520,9 @@ function findnext(testf::Function, B::BitArray, start::Integer)
15201520
f0 && !f1 && return findnextnot(B, start)
15211521

15221522
start > 0 || throw(BoundsError(B, start))
1523-
start > length(B) && return 0
1523+
start > length(B) && return nothing
15241524
f0 && f1 && return Int(start)
1525-
return 0 # last case: !f0 && !f1
1525+
return nothing # last case: !f0 && !f1
15261526
end
15271527
#findfirst(testf::Function, B::BitArray) = findnext(testf, B, 1) ## defined in array.jl
15281528

@@ -1541,18 +1541,18 @@ function unsafe_bitfindprev(Bc::Vector{UInt64}, start::Integer)
15411541
end
15421542
end
15431543
end
1544-
return 0
1544+
return nothing
15451545
end
15461546

15471547
# returns the index of the previous non-zero element, or 0 if all zeros
15481548
function findprev(B::BitArray, start::Integer)
1549-
start > 0 || return 0
1549+
start > 0 || return nothing
15501550
start > length(B) && throw(BoundsError(B, start))
15511551
unsafe_bitfindprev(B.chunks, start)
15521552
end
15531553

15541554
function findprevnot(B::BitArray, start::Integer)
1555-
start > 0 || return 0
1555+
start > 0 || return nothing
15561556
start > length(B) && throw(BoundsError(B, start))
15571557

15581558
Bc = B.chunks
@@ -1571,7 +1571,7 @@ function findprevnot(B::BitArray, start::Integer)
15711571
end
15721572
end
15731573
end
1574-
return 0
1574+
return nothing
15751575
end
15761576
findlastnot(B::BitArray) = findprevnot(B, length(B))
15771577

@@ -1580,7 +1580,7 @@ function findprev(pred::EqualTo, B::BitArray, start::Integer)
15801580
v = pred.x
15811581
v == false && return findprevnot(B, start)
15821582
v == true && return findprev(B, start)
1583-
return 0
1583+
return nothing
15841584
end
15851585
#findlast(B::BitArray, v) = findprev(B, 1, v) ## defined in array.jl
15861586

@@ -1591,10 +1591,10 @@ function findprev(testf::Function, B::BitArray, start::Integer)
15911591
!f0 && f1 && return findprev(B, start)
15921592
f0 && !f1 && return findprevnot(B, start)
15931593

1594-
start > 0 || return 0
1594+
start > 0 || return nothing
15951595
start > length(B) && throw(BoundsError(B, start))
15961596
f0 && f1 && return Int(start)
1597-
return 0 # last case: !f0 && !f1
1597+
return nothing # last case: !f0 && !f1
15981598
end
15991599
#findlast(testf::Function, B::BitArray) = findprev(testf, B, 1) ## defined in array.jl
16001600

base/bitset.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ function _bits_findnext(b::Bits, start::Int)
6868
# start is 0-based
6969
# @assert start >= 0
7070
_div64(start) + 1 > length(b) && return -1
71-
unsafe_bitfindnext(b, start+1) - 1
71+
zero_sentinel(unsafe_bitfindnext(b, start+1)) - 1
7272
end
7373

7474
function _bits_findprev(b::Bits, start::Int)
7575
# start is 0-based
7676
# @assert start <= 64 * length(b) - 1
7777
start >= 0 || return -1
78-
unsafe_bitfindprev(b, start+1) - 1
78+
zero_sentinel(unsafe_bitfindprev(b, start+1)) - 1
7979
end
8080

8181
# An internal function for setting the inclusion bit for a given integer

base/client.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function display_error(io::IO, er, bt)
149149
print_with_color(Base.error_color(), io, "ERROR: "; bold = true)
150150
# remove REPL-related frames from interactive printing
151151
eval_ind = findlast(addr->Base.REPL.ip_matches_func(addr, :eval), bt)
152-
if eval_ind != 0
152+
if eval_ind !== nothing
153153
bt = bt[1:eval_ind-1]
154154
end
155155
showerror(IOContext(io, :limit => true), er, bt)

base/combinatorics.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function permute!!(a, p::AbstractVector{<:Integer})
7575
count = 0
7676
start = 0
7777
while count < length(a)
78-
ptr = start = findnext(!iszero, p, start+1)
78+
ptr = start = findnext(!iszero, p, start+1)::Int
7979
temp = a[start]
8080
next = p[start]
8181
count += 1
@@ -125,7 +125,7 @@ function invpermute!!(a, p::AbstractVector{<:Integer})
125125
count = 0
126126
start = 0
127127
while count < length(a)
128-
start = findnext(!iszero, p, start+1)
128+
start = findnext(!iszero, p, start+1)::Int
129129
temp = a[start]
130130
next = p[start]
131131
count += 1

base/event.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function ensure_rescheduled(othertask::Task)
231231
# also need to return it to the runnable state
232232
# before throwing an error
233233
i = findfirst(t->t===ct, Workqueue)
234-
i == 0 || deleteat!(Workqueue, i)
234+
i === nothing || deleteat!(Workqueue, i)
235235
ct.state = :runnable
236236
end
237237
nothing

base/file.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ function tempname(temppath::AbstractString,uunique::UInt32)
271271
tempp = cwstring(temppath)
272272
tname = Vector{UInt16}(uninitialized, 32767)
273273
uunique = ccall(:GetTempFileNameW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32,Ptr{UInt16}), tempp,temp_prefix,uunique,tname)
274-
lentname = findfirst(iszero,tname)-1
274+
lentname = zero_sentinel(findfirst(iszero,tname))-1
275275
if uunique == 0 || lentname <= 0
276276
error("GetTempFileName failed: $(Libc.FormatMessage())")
277277
end

base/filesystem.jl

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import Base:
4343
nb_available, position, read, read!, readavailable, seek, seekend, show,
4444
skip, stat, unsafe_read, unsafe_write, transcode, uv_error, uvhandle,
4545
uvtype, write
46+
using Base: zero_sentinel
4647

4748
if Sys.iswindows()
4849
import Base: cwstring

base/inference.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ function builtin_tfunction(@nospecialize(f), argtypes::Array{Any,1},
17941794
tf = t_ifunc[iidx]
17951795
else
17961796
fidx = findfirst(x->x===f, t_ffunc_key)
1797-
if fidx == 0
1797+
if fidx === nothing
17981798
# unknown/unhandled builtin function
17991799
return Any
18001800
end
@@ -5118,7 +5118,7 @@ function statement_cost(ex::Expr, line::Int, src::CodeInfo, mod::Module, params:
51185118
return plus_saturate(argcost, isknowntype(ex.typ) ? 4 : params.inline_nonleaf_penalty)
51195119
end
51205120
fidx = findfirst(x->x===f, t_ffunc_key)
5121-
if fidx == 0
5121+
if fidx === nothing
51225122
# unknown/unhandled builtin or anonymous function
51235123
# Use the generic cost of a direct function call
51245124
return plus_saturate(argcost, 20)

base/iobuffer.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, nb_availa
429429
function findfirst(delim::EqualTo{UInt8}, buf::IOBuffer)
430430
p = pointer(buf.data, buf.ptr)
431431
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
432-
nb::Int = (q == C_NULL ? 0 : q-p+1)
433-
return nb
432+
q == C_NULL && return nothing
433+
return Int(q-p+1)
434434
end
435435

436436
function findfirst(delim::EqualTo{UInt8}, buf::GenericIOBuffer)
@@ -441,7 +441,7 @@ function findfirst(delim::EqualTo{UInt8}, buf::GenericIOBuffer)
441441
return i - buf.ptr + 1
442442
end
443443
end
444-
return 0
444+
return nothing
445445
end
446446

447447
function readuntil(io::GenericIOBuffer, delim::UInt8)

0 commit comments

Comments
 (0)