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

WIP: Julia interface for custom Unicode normalization #30275

Closed
wants to merge 4 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
140 changes: 115 additions & 25 deletions base/strings/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function utf8proc_map(str::String, options::Integer)
nwords = ccall(:utf8proc_decompose, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint),
str, sizeof(str), C_NULL, 0, options)
nwords < 0 && utf8proc_error(nwords)
buffer = Base.StringVector(nwords*4)
buffer = Base.StringVector(nwords*sizeof(Int32))
nwords = ccall(:utf8proc_decompose, Int, (Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint),
str, sizeof(str), buffer, nwords, options)
nwords < 0 && utf8proc_error(nwords)
Expand All @@ -150,22 +150,40 @@ end

utf8proc_map(s::AbstractString, flags::Integer) = utf8proc_map(String(s), flags)

# Documented in Unicode module
function normalize(
s::AbstractString;
stable::Bool=false,
compat::Bool=false,
compose::Bool=true,
decompose::Bool=false,
stripignore::Bool=false,
rejectna::Bool=false,
newline2ls::Bool=false,
newline2ps::Bool=false,
newline2lf::Bool=false,
stripcc::Bool=false,
casefold::Bool=false,
lump::Bool=false,
stripmark::Bool=false,

custom_func_wrapper(codepoint::Int32, custom_func::Any) = Int32(custom_func(codepoint))

function utf8proc_map(str::String, options::Integer, custom_func)
ccustom_func = @cfunction(custom_func_wrapper, Int32, (Int32, Any))

nwords = ccall(:utf8proc_decompose_custom, Int,
(Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint, Ptr{Cvoid}, Any),
str, sizeof(str), C_NULL, 0, options, ccustom_func, custom_func)
nwords < 0 && utf8proc_error(nwords)
buffer = Base.StringVector(nwords*sizeof(Int32))
nwords = ccall(:utf8proc_decompose_custom, Int,
(Ptr{UInt8}, Int, Ptr{UInt8}, Int, Cint, Ptr{Cvoid}, Any),
str, sizeof(str), buffer, nwords, options, ccustom_func, custom_func)
nwords < 0 && utf8proc_error(nwords)
nbytes = ccall(:utf8proc_reencode, Int, (Ptr{UInt8}, Int, Cint), buffer, nwords, options)
nbytes < 0 && utf8proc_error(nbytes)
return String(resize!(buffer, nbytes))
end

function _compute_options(
stable::Bool,
compat::Bool,
compose::Bool,
decompose::Bool,
stripignore::Bool,
rejectna::Bool,
newline2ls::Bool,
newline2ps::Bool,
newline2lf::Bool,
stripcc::Bool,
casefold::Bool,
lump::Bool,
stripmark::Bool,
)
flags = 0
stable && (flags = flags | UTF8PROC_STABLE)
Expand All @@ -188,19 +206,91 @@ function normalize(
casefold && (flags = flags | UTF8PROC_CASEFOLD)
lump && (flags = flags | UTF8PROC_LUMP)
stripmark && (flags = flags | UTF8PROC_STRIPMARK)
flags
end

_compute_options(nf::Symbol) =
nf == :NFC ? (UTF8PROC_STABLE | UTF8PROC_COMPOSE) :
nf == :NFD ? (UTF8PROC_STABLE | UTF8PROC_DECOMPOSE) :
nf == :NFKC ? (UTF8PROC_STABLE | UTF8PROC_COMPOSE | UTF8PROC_COMPAT) :
nf == :NFKD ? (UTF8PROC_STABLE | UTF8PROC_DECOMPOSE | UTF8PROC_COMPAT) :
throw(ArgumentError(":$nf is not one of :NFC, :NFD, :NFKC, :NFKD"))

# Documented in Unicode module
function normalize(
s::AbstractString;
stable::Bool=false,
compat::Bool=false,
compose::Bool=true,
decompose::Bool=false,
stripignore::Bool=false,
rejectna::Bool=false,
newline2ls::Bool=false,
newline2ps::Bool=false,
newline2lf::Bool=false,
stripcc::Bool=false,
casefold::Bool=false,
lump::Bool=false,
stripmark::Bool=false,
)
flags = _compute_options(
stable,
compat,
compose,
decompose,
stripignore,
rejectna,
newline2ls,
newline2ps,
newline2lf,
stripcc,
casefold,
lump,
stripmark,
)
utf8proc_map(s, flags)
end

function normalize(s::AbstractString, nf::Symbol)
utf8proc_map(s, nf == :NFC ? (UTF8PROC_STABLE | UTF8PROC_COMPOSE) :
nf == :NFD ? (UTF8PROC_STABLE | UTF8PROC_DECOMPOSE) :
nf == :NFKC ? (UTF8PROC_STABLE | UTF8PROC_COMPOSE
| UTF8PROC_COMPAT) :
nf == :NFKD ? (UTF8PROC_STABLE | UTF8PROC_DECOMPOSE
| UTF8PROC_COMPAT) :
throw(ArgumentError(":$nf is not one of :NFC, :NFD, :NFKC, :NFKD")))
normalize(s::AbstractString, nf::Symbol) = utf8proc_map(s, _compute_options(nf))

function normalize(
s::AbstractString,
custom_func;
stable::Bool=false,
compat::Bool=false,
compose::Bool=true,
decompose::Bool=false,
stripignore::Bool=false,
rejectna::Bool=false,
newline2ls::Bool=false,
newline2ps::Bool=false,
newline2lf::Bool=false,
stripcc::Bool=false,
casefold::Bool=false,
lump::Bool=false,
stripmark::Bool=false,
)
flags = _compute_options(
stable,
compat,
compose,
decompose,
stripignore,
rejectna,
newline2ls,
newline2ps,
newline2lf,
stripcc,
casefold,
lump,
stripmark,
)
utf8proc_map(s, flags, custom_func)
end

normalize(s::AbstractString, nf::Symbol, custom_func) =
utf8proc_map(s, _compute_options(nf), custom_func)

############################################################################

## character column width function ##
Expand Down
12 changes: 12 additions & 0 deletions stdlib/Unicode/src/Unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ julia> Unicode.normalize("JuLiA", casefold=true)
julia> Unicode.normalize("JúLiA", stripmark=true)
"JuLiA"
```

You can also passing a `custom_func` to the `normalize` to do custom normalization
by calling `Unicode.normalize(s::AbstractString, nf::Symbol, custom_func)` or
`Unicode.normalize(s::AbstractString, custom_func, custom_data)`
where custom function should be in `custom_func(codepoint)::Cint` and return a new codepoint.

"""
function normalize end
normalize(s::AbstractString, nf::Symbol) = Base.Unicode.normalize(s, nf)
normalize(s::AbstractString; kwargs...) = Base.Unicode.normalize(s; kwargs...)
normalize(s::AbstractString, nf::Symbol, custom_func) =
Base.Unicode.normalize(s, nf, custom_func)
normalize(s::AbstractString, custom_func; kwargs...) =
Base.Unicode.normalize(s, custom_func; kwargs...)



"""
Unicode.isassigned(c) -> Bool
Expand Down
9 changes: 9 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -951,3 +951,12 @@ let x = SubString("ab", 1, 1)
@test y === x
chop("ab") === chop.(["ab"])[1]
end

# custom unicode normalize function
let d = Dict{Cint, Cint}(Cint('a')=>Cint('\u6666'))
function mycustom(uc)::Cint
@show d
get(d, uc, Cint('\u2721'))
end
@test Unicode.normalize("abcd",:NFKC, mycustom) == "\u6666\u2721\u2721\u2721"
end