Skip to content

Commit

Permalink
ncodeunits(c::Char): fast equivalent of ncodeunits(string(c))
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Sep 12, 2018
1 parent aef8fc5 commit cb4bc8e
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ Char
(::Type{T})(x::AbstractChar) where {T<:Union{Number,AbstractChar}} = T(codepoint(x))
(::Type{T})(x::T) where {T<:AbstractChar} = x

"""
ncodeunits(c::Char) -> Int
Return the number of code units required to encode a character as UTF-8.
This is the number of bytes which will be printed if the character is written
to an output stream, or `ncodeunits(string(c))` but computed efficiently.
"""
ncodeunits(c::Char) = max(1, 4 - (trailing_zeros(reinterpret(UInt32, c)) >> 3))

"""
codepoint(c::AbstractChar) -> UInt32
Expand Down

0 comments on commit cb4bc8e

Please sign in to comment.