Skip to content

Commit 999dde7

Browse files
authored
Unicode: assume foldable ccall in category_code (#54394)
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 ```
1 parent 5f7bfc0 commit 999dde7

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

base/strings/unicode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ function category_code(c::AbstractChar)
350350
end
351351

352352
function category_code(x::Integer)
353-
x 0x10ffff ? ccall(:utf8proc_category, Cint, (UInt32,), x) : Cint(30)
353+
x 0x10ffff ? (@assume_effects :foldable @ccall utf8proc_category(UInt32(x)::UInt32)::Cint) : Cint(30)
354354
end
355355

356356
# more human-readable representations of the category code

test/char.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,17 @@ end
361361
@test convert(ASCIIChar, 1) == Char(1)
362362
end
363363

364-
@testset "foldable isuppercase/islowercase" begin
364+
@testset "foldable functions" begin
365365
v = @inferred (() -> Val(isuppercase('C')))()
366366
@test v isa Val{true}
367367
v = @inferred (() -> Val(islowercase('C')))()
368368
@test v isa Val{false}
369369

370+
v = @inferred (() -> Val(isletter('C')))()
371+
@test v isa Val{true}
372+
v = @inferred (() -> Val(isnumeric('C')))()
373+
@test v isa Val{false}
374+
370375
struct MyChar <: AbstractChar
371376
x :: Char
372377
end
@@ -377,4 +382,9 @@ end
377382
@test v isa Val{true}
378383
v = @inferred (() -> Val(islowercase(MyChar('C'))))()
379384
@test v isa Val{false}
385+
386+
v = @inferred (() -> Val(isletter(MyChar('C'))))()
387+
@test v isa Val{true}
388+
v = @inferred (() -> Val(isnumeric(MyChar('C'))))()
389+
@test v isa Val{false}
380390
end

0 commit comments

Comments
 (0)