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

Assume :foldable in isuppercase/islowercase for Char #54346

Merged
merged 4 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 18 additions & 3 deletions base/strings/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module Unicode

import Base: show, ==, hash, string, Symbol, isless, length, eltype,
convert, isvalid, ismalformed, isoverlong, iterate,
AnnotatedString, AnnotatedChar, annotated_chartransform
AnnotatedString, AnnotatedChar, annotated_chartransform,
@assume_effects

# whether codepoints are valid Unicode scalar values, i.e. 0-0xd7ff, 0xe000-0x10ffff

Expand Down Expand Up @@ -385,7 +386,14 @@ julia> islowercase('❤')
false
```
"""
islowercase(c::AbstractChar) = ismalformed(c) ? false : Bool(ccall(:utf8proc_islower, Cint, (UInt32,), UInt32(c)))
function islowercase(c::AbstractChar)
if ismalformed(c)
false
else
ui = UInt32(c)::UInt32 # type-assertion to ensure that we may assume :foldable
@assume_effects :foldable Bool(ccall(:utf8proc_islower, Cint, (UInt32,), ui))
end
jishnub marked this conversation as resolved.
Show resolved Hide resolved
end
jishnub marked this conversation as resolved.
Show resolved Hide resolved

# true for Unicode upper and mixed case

Expand All @@ -409,7 +417,14 @@ julia> isuppercase('❤')
false
```
"""
isuppercase(c::AbstractChar) = ismalformed(c) ? false : Bool(ccall(:utf8proc_isupper, Cint, (UInt32,), UInt32(c)))
function isuppercase(c::AbstractChar)
if ismalformed(c)
false
else
ui = UInt32(c)::UInt32 # type-assertion to ensure that we may assume :foldable
@assume_effects :foldable Bool(ccall(:utf8proc_isupper, Cint, (UInt32,), ui))
end
end
jishnub marked this conversation as resolved.
Show resolved Hide resolved

"""
iscased(c::AbstractChar) -> Bool
Expand Down
18 changes: 18 additions & 0 deletions test/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,21 @@ end
@test Base.IteratorSize(Char) == Base.HasShape{0}()
@test convert(ASCIIChar, 1) == Char(1)
end

@testset "foldable isuppercase/islowercase" begin
v = @inferred (() -> Val(isuppercase('C')))()
@test v isa Val{true}
v = @inferred (() -> Val(islowercase('C')))()
@test v isa Val{false}

struct MyChar <: AbstractChar
x :: Char
end
Base.codepoint(m::MyChar) = codepoint(m.x)
MyChar(x::UInt32) = MyChar(Char(x))

v = @inferred (() -> Val(isuppercase(MyChar('C'))))()
@test v isa Val{true}
v = @inferred (() -> Val(islowercase(MyChar('C'))))()
@test v isa Val{false}
end