Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods propagating missing for string functions #26631

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion base/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ max(::Any, ::Missing) = missing
# Rounding and related functions
for f in (:(ceil), :(floor), :(round), :(trunc))
@eval begin
($f)(::Missing, digits::Integer=0, base::Integer=0) = missing
($f)(::Missing, digits::Integer=0; base::Integer=0) = missing
($f)(::Type{>:Missing}, ::Missing) = missing
($f)(::Type{T}, ::Missing) where {T} =
throw(MissingException("cannot convert a missing value to type $T: use Union{$T, Missing} instead"))
Expand All @@ -113,6 +113,52 @@ for f in (:(ceil), :(floor), :(round), :(trunc))
end
end

# String functions
repeat(::Missing, ::Integer) = missing
repeat(::AbstractString, ::Missing) = missing
repeat(::Missing, ::Missing) = missing
^(::AbstractString, ::Missing) = missing

occursin(::Missing, ::AbstractString; offset::Integer=0) = missing
occursin(::Union{AbstractChar,AbstractString,Regex}, ::Missing; offset::Integer=0) = missing
occursin(::Missing, ::Missing; offset::Integer=0) = missing

replace(::Missing, ::Pair; count::Integer=0) = missing

chop(::Missing; head::Integer = 0, tail::Integer = 1) = missing

escape_string(::Missing) = missing
unescape_string(::Missing) = missing
escape_string(::Missing, ::AbstractString) = missing

for f in (:strip, :lstrip, :rstrip)
@eval begin
$f(::Missing) = missing
$f(::Missing, ::Chars) = missing
end
end

for f in (:startswith, :endswith)
@eval begin
$f(::Missing, ::Union{AbstractString,Chars}) = missing
$f(::AbstractString, ::Missing) = missing
$f(::Missing, ::Missing) = missing
end
end

for f in (:uppercase, :lowercase, :titlecase, :uppercasefirst, :lowercasefirst,
:isuppercase, :islowercase, :chomp, :textwidth)
@eval begin
$f(::Missing) = missing
end
end

parse(::Missing; base::Integer=0) = missing
parse(::Type{>:Missing}, ::Missing; base::Integer=0) = missing
parse(::Type{T}, ::Missing) where {T} =
throw(MissingException("cannot parse a missing value to type $T: use Union{$T, Missing} instead"))
parse(::Type{T}, x::Any; kwargs...) where {T>:Missing} = parse(nonmissingtype(T), x; kwargs...)

# to avoid ambiguity warnings
(^)(::Missing, ::Integer) = missing

Expand Down
1 change: 1 addition & 0 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ julia> thisind("αβγdef", 10)

julia> thisind("αβγdef", 20)
20
```
"""
thisind(s::AbstractString, i::Integer) = thisind(s, Int(i))

Expand Down
3 changes: 2 additions & 1 deletion base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Find the first occurrence of `pattern` in `string`. Equivalent to

# Examples
```jldoctest
julia> findfirst("z", "Hello to the world") # returns nothing, but not printed in the REPL
julia> findfirst("z", "Hello to the world") === nothing
true

julia> findfirst("Julia", "JuliaLang")
1:5
Expand Down
3 changes: 0 additions & 3 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ endswith(str::AbstractString, chars::Chars) = !isempty(str) && last(str) in char
startswith(a::String, b::String) = sizeof(a) ≥ sizeof(b) &&
ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, sizeof(b)) == 0

startswith(a::Vector{UInt8}, b::Vector{UInt8}) = length(a) ≥ length(b) &&
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Wouldn't this need a deprecation? Also unrelated to missing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, maybe. Though since it's not documented and it only works for Vector{UInt8}, is it really needed? I can add one, or move this to another PR, as you prefer.

ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, length(b)) == 0

# TODO: fast endswith

"""
Expand Down
54 changes: 53 additions & 1 deletion test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,65 @@ end
for f in rounding_functions
@test ismissing(f(missing))
@test ismissing(f(missing, 1))
@test ismissing(f(missing, 1, 1))
@test ismissing(f(missing, 1, base=10))
@test ismissing(f(Union{Int, Missing}, missing))
@test f(Union{Int, Missing}, 1.0) === 1
@test_throws MissingException f(Int, missing)
end
end

@testset "string functions" begin
@test repeat("a", missing) === missing
@test repeat(missing, 2) === missing
@test repeat(missing, missing) === missing
@test "a"^missing === missing
@test missing^2 === missing
@test missing^missing === missing

@test occursin(missing, "a") === missing
@test occursin("a", missing) === missing
@test occursin('a', missing) === missing
@test occursin(r"a", missing) === missing
@test occursin(missing, missing) === missing

@test replace(missing, 'a'=>'b') === missing
@test replace(missing, "a"=>"b") === missing
@test replace(missing, ['a','c']=>'b') === missing
@test replace(missing, 'a'=>uppercase) === missing
@test replace(missing, r"a"=>"b") === missing
@test replace(missing, 'a'=>'b', count=5) === missing

@test chop(missing) === missing
@test chop(missing, head=1, tail=1) === missing

@test escape_string(missing) === missing
@test unescape_string(missing) === missing
@test escape_string(missing, "a") === missing

for f in (strip, lstrip, rstrip)
@test f(missing) === missing
end

for f in (startswith, endswith)
@test f(missing, "a") === missing
@test f(missing, 'a') === missing
@test f(missing, ['a', 'b']) === missing
@test f("a", missing) === missing
@test f(missing, missing) === missing
end

for f in (uppercase, lowercase, titlecase, uppercasefirst, lowercasefirst,
isuppercase, islowercase, chomp, textwidth)
@test f(missing) === missing
end

@test parse(Union{Int, Missing}, missing) === missing
@test parse(Union{Int, Missing}, "1") === 1
@test parse(Union{Int, Missing}, missing, base=10) === missing
@test parse(Union{Int, Missing}, "1", base=10) === 1
@test_throws MissingException parse(Int, missing)
end

@testset "printing" begin
@test sprint(show, missing) == "missing"
@test sprint(show, missing, context=:compact => true) == "missing"
Expand Down