Skip to content

Commit

Permalink
Add isfound
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Mar 1, 2018
1 parent 4c75937 commit dfe015b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
18 changes: 17 additions & 1 deletion base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,20 @@ julia> map(splat(+), zip(1:3,4:6))
9
```
"""
splat(f) = args->f(args...)
splat(f) = args->f(args...)

"""
isfound(needle, haystack)
Determine whether `needle` is found within the collection `haystack`.
# Examples
```jldoctest
julia> isfound("wor", "Hello, world!")
true
julia> isfound(r"\d\+", "No numbers here")
false
```
"""
isfound(needle, haystack) = findfirst(needle, haystack) !== nothing
2 changes: 2 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ findnext(r::Regex, s::AbstractString, idx::Integer) = throw(ArgumentError(
))
findfirst(r::Regex, s::AbstractString) = findnext(r,s,firstindex(s))

isfound(r::Regex, s::AbstractString) = contains(s, r)

struct SubstitutionString{T<:AbstractString} <: AbstractString
string::T
end
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Base.findnext(::Any, ::Integer)
Base.findnext(::Function, ::Any, ::Integer)
Base.findprev(::Any, ::Integer)
Base.findprev(::Function, ::Any, ::Integer)
Base.isfound
Base.permutedims
Base.permutedims!
Base.PermutedDimsArray
Expand Down
7 changes: 7 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,13 @@ end
@test findprev(iseven, A, CartesianIndex(2, 1)) === nothing
end

@testset "isfound" begin
@test isfound(isodd, [1, 2, 3])
@test isfound(equalto(0x01), [0x00, 0x01, 0x02])
@test !isfound(x->x > 4, [1, 2, 3])
@test !isfound(isnan, [0.0, 4.5, 2.2])
end

@testset "findmin findmax argmin argmax" begin
@test argmax([10,12,9,11]) == 2
@test argmin([10,12,9,11]) == 3
Expand Down
3 changes: 3 additions & 0 deletions test/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ end

# Proper unicode handling
@test match(r"∀∀", "∀x∀∀∀").match == "∀∀"

@test isfound(r"\d\+", "abc123")
@test !isfound(r"[^_]", "___")
7 changes: 6 additions & 1 deletion test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,9 @@ end

@test @inferred findall(equalto('a'), "éa") == [3]
@test @inferred findall(equalto(''), "€€") == [1, 4]
@test @inferred isempty(findall(equalto('é'), ""))
@test @inferred isempty(findall(equalto('é'), ""))

@test isfound(equalto('x'), "xylophone")
@test isfound("hi", "hi, mom")
@test !isfound("elephant", "zoo")
@test !isfound(equalto('3'), "124")

0 comments on commit dfe015b

Please sign in to comment.