From dfe015bf1fcebc10eee69a7acb8eafba7bdfde41 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Thu, 1 Mar 2018 13:08:40 -0800 Subject: [PATCH] Add isfound --- base/operators.jl | 18 +++++++++++++++++- base/regex.jl | 2 ++ doc/src/base/arrays.md | 1 + test/arrayops.jl | 7 +++++++ test/regex.jl | 3 +++ test/strings/search.jl | 7 ++++++- 6 files changed, 36 insertions(+), 2 deletions(-) diff --git a/base/operators.jl b/base/operators.jl index 1ec7d76bad003..cbf6fea014c89 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -874,4 +874,20 @@ julia> map(splat(+), zip(1:3,4:6)) 9 ``` """ -splat(f) = args->f(args...) \ No newline at end of file +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 diff --git a/base/regex.jl b/base/regex.jl index 6f75eee72e5f5..d61938bb3e162 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -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 diff --git a/doc/src/base/arrays.md b/doc/src/base/arrays.md index 402ebab2d4da8..9be41b809d3f0 100644 --- a/doc/src/base/arrays.md +++ b/doc/src/base/arrays.md @@ -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 diff --git a/test/arrayops.jl b/test/arrayops.jl index 570c1510592b3..9bb1b8a8653f6 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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 diff --git a/test/regex.jl b/test/regex.jl index 7db1e282907b2..0948786c3d62c 100644 --- a/test/regex.jl +++ b/test/regex.jl @@ -49,3 +49,6 @@ end # Proper unicode handling @test match(r"∀∀", "∀x∀∀∀").match == "∀∀" + +@test isfound(r"\d\+", "abc123") +@test !isfound(r"[^_]", "___") diff --git a/test/strings/search.jl b/test/strings/search.jl index b494d72c83ee4..f98e6ca42ea61 100644 --- a/test/strings/search.jl +++ b/test/strings/search.jl @@ -327,4 +327,9 @@ end @test @inferred findall(equalto('a'), "éa") == [3] @test @inferred findall(equalto('€'), "€€") == [1, 4] -@test @inferred isempty(findall(equalto('é'), "")) \ No newline at end of file +@test @inferred isempty(findall(equalto('é'), "")) + +@test isfound(equalto('x'), "xylophone") +@test isfound("hi", "hi, mom") +@test !isfound("elephant", "zoo") +@test !isfound(equalto('3'), "124")