Skip to content

Commit

Permalink
add contains (argument flipped occursin) (#35132)
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed Apr 16, 2020
1 parent b0f3403 commit cc6e121
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ New library functions
* New function `bitreverse` for reversing the order of bits in a fixed-width integer ([#34791]).
* New function `bitrotate(x, k)` for rotating the bits in a fixed-width integer ([#33937]).
* One argument methods `startswith(x)` and `endswith(x)` have been added, returning partially-applied versions of the functions, similar to existing methods like `isequal(x)` ([#33193]).
* New function `contains(haystack, needle)` and its one argument partially applied form have been added, it acts like `occursin(needle, haystack)`([#35132]).

New library features
--------------------
Expand Down
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ export
zeros,

# search, find, match and related functions
contains,
eachmatch,
endswith,
findall,
Expand Down
2 changes: 2 additions & 0 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ true
julia> occursin(r"a.a", "abba")
false
```
See also: [`contains`](@ref).
"""
occursin(needle::Union{AbstractString,AbstractChar}, haystack::AbstractString) =
_searchindex(haystack, needle, firstindex(haystack)) != 0
Expand Down
38 changes: 38 additions & 0 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,33 @@ function endswith(a::Union{String, SubString{String}},
end
end

"""
contains(haystack::AbstractString, needle)
Return `true` if `haystack` contains `needle`.
This is the same as `occursin(needle, haystack)`, but is provided for consistency with
`startswith(haystack, needle)` and `endswith(haystack, needle)`.
# Examples
```jldoctest
julia> contains("JuliaLang is pretty cool!", "Julia")
true
julia> contains("JuliaLang is pretty cool!", 'a')
true
julia> contains("aba", r"a.a")
true
julia> contains("abba", r"a.a")
false
```
!!! compat "Julia 1.5"
The `contains` function requires at least Julia 1.5.
"""
contains(haystack::AbstractString, needle) = occursin(needle, haystack)

"""
endswith(suffix)
Expand Down Expand Up @@ -100,6 +127,17 @@ used to implement specialized methods.
"""
startswith(s) = Base.Fix2(startswith, s)

"""
contains(needle)
Create a function that checks whether its argument contains `needle`, i.e.
a function equivalent to `haystack -> contains(haystack, needle)`.
The returned function is of type `Base.Fix2{typeof(contains)}`, which can be
used to implement specialized methods.
"""
contains(needle) = Base.Fix2(contains, needle)

"""
chop(s::AbstractString; head::Integer = 0, tail::Integer = 1)
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Base.lstrip
Base.rstrip
Base.startswith
Base.endswith
Base.contains
Base.first(::AbstractString, ::Integer)
Base.last(::AbstractString, ::Integer)
Base.uppercase
Expand Down
7 changes: 7 additions & 0 deletions test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ end
@test occursin("o", "foo")
@test occursin('o', "foo")

# contains
@test contains("foo", "o")
@test contains("foo", 'o')
# contains in curried form
@test contains("o")("foo")
@test contains('o')("foo")

@test_throws ErrorException "ab" "abc"

# issue #15723
Expand Down

2 comments on commit cc6e121

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.