Skip to content

Commit

Permalink
Unicode: assume foldable ccall in category_code (#54394)
Browse files Browse the repository at this point in the history
Following on from #54346, this
marks the `ccall` in `category_code` as foldable. This lets us compute
the results of several functions at compile time, such as:
```julia
julia> @code_typed (() -> isletter('C'))()
CodeInfo(
1 ─     return true
) => Bool

julia> @code_typed (() -> isnumeric('C'))()
CodeInfo(
1 ─     return false
) => Bool

julia> @code_typed (() -> ispunct('C'))()
CodeInfo(
1 ─     return false
) => Bool
```
  • Loading branch information
jishnub authored May 8, 2024
1 parent 5f7bfc0 commit 999dde7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion base/strings/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ function category_code(c::AbstractChar)
end

function category_code(x::Integer)
x 0x10ffff ? ccall(:utf8proc_category, Cint, (UInt32,), x) : Cint(30)
x 0x10ffff ? (@assume_effects :foldable @ccall utf8proc_category(UInt32(x)::UInt32)::Cint) : Cint(30)
end

# more human-readable representations of the category code
Expand Down
12 changes: 11 additions & 1 deletion test/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,17 @@ end
@test convert(ASCIIChar, 1) == Char(1)
end

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

v = @inferred (() -> Val(isletter('C')))()
@test v isa Val{true}
v = @inferred (() -> Val(isnumeric('C')))()
@test v isa Val{false}

struct MyChar <: AbstractChar
x :: Char
end
Expand All @@ -377,4 +382,9 @@ end
@test v isa Val{true}
v = @inferred (() -> Val(islowercase(MyChar('C'))))()
@test v isa Val{false}

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

0 comments on commit 999dde7

Please sign in to comment.