Unicode

Unicode.julia_chartransform — Function

  1. Unicode.julia_chartransform(c::Union{Char,Integer})

Map the Unicode character (Char) or codepoint (Integer) c to the corresponding “equivalent” character or codepoint, respectively, according to the custom equivalence used within the Julia parser (in addition to NFC normalization).

For example, 'µ' (U+00B5 micro) is treated as equivalent to 'μ' (U+03BC mu) by Julia’s parser, so julia_chartransform performs this transformation while leaving other characters unchanged:

  1. julia> Unicode.julia_chartransform('µ')
  2. 'μ': Unicode U+03BC (category Ll: Letter, lowercase)
  3. julia> Unicode.julia_chartransform('x')
  4. 'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

julia_chartransform is mainly useful for passing to the Unicode.normalize function in order to mimic the normalization used by the Julia parser:

  1. julia> s = "µö"
  2. "µö"
  3. julia> s2 = Unicode.normalize(s, compose=true, stable=true, chartransform=Unicode.julia_chartransform)
  4. "μö"
  5. julia> collect(s2)
  6. 2-element Vector{Char}:
  7. 'μ': Unicode U+03BC (category Ll: Letter, lowercase)
  8. 'ö': Unicode U+00F6 (category Ll: Letter, lowercase)
  9. julia> s2 == string(Meta.parse(s))
  10. true

Julia 1.8

This function was introduced in Julia 1.8.

Unicode.isassigned — Function

  1. Unicode.isassigned(c) -> Bool

Return true if the given char or integer is an assigned Unicode code point.

Examples

  1. julia> Unicode.isassigned(101)
  2. true
  3. julia> Unicode.isassigned('\x01')
  4. true

Unicode.isequal_normalized — Function

  1. isequal_normalized(s1::AbstractString, s2::AbstractString; casefold=false, stripmark=false, chartransform=identity)

Return whether s1 and s2 are canonically equivalent Unicode strings. If casefold=true, ignores case (performs Unicode case-folding); if stripmark=true, strips diacritical marks and other combining characters.

As with Unicode.normalize, you can also pass an arbitrary function via the chartransform keyword (mapping Integer codepoints to codepoints) to perform custom normalizations, such as Unicode.julia_chartransform.

Examples

For example, the string "noël" can be constructed in two canonically equivalent ways in Unicode, depending on whether "ë" is formed from a single codepoint U+00EB or from the ASCII character 'o' followed by the U+0308 combining-diaeresis character.

  1. julia> s1 = "noël"
  2. "noël"
  3. julia> s2 = "noël"
  4. "noël"
  5. julia> s1 == s2
  6. false
  7. julia> isequal_normalized(s1, s2)
  8. true
  9. julia> isequal_normalized(s1, "noel", stripmark=true)
  10. true
  11. julia> isequal_normalized(s1, "NOËL", casefold=true)
  12. true

Unicode.normalize — Function

  1. Unicode.normalize(s::AbstractString; keywords...)
  2. Unicode.normalize(s::AbstractString, normalform::Symbol)

Normalize the string s. By default, canonical composition (compose=true) is performed without ensuring Unicode versioning stability (compat=false), which produces the shortest possible equivalent string but may introduce composition characters not present in earlier Unicode versions.

Alternatively, one of the four “normal forms” of the Unicode standard can be specified: normalform can be :NFC, :NFD, :NFKC, or :NFKD. Normal forms C (canonical composition) and D (canonical decomposition) convert different visually identical representations of the same abstract string into a single canonical form, with form C being more compact. Normal forms KC and KD additionally canonicalize “compatibility equivalents”: they convert characters that are abstractly similar but visually distinct into a single canonical choice (e.g. they expand ligatures into the individual characters), with form KC being more compact.

Alternatively, finer control and additional transformations may be obtained by calling Unicode.normalize(s; keywords...), where any number of the following boolean keywords options (which all default to false except for compose) are specified:

  • compose=false: do not perform canonical composition
  • decompose=true: do canonical decomposition instead of canonical composition (compose=true is ignored if present)
  • compat=true: compatibility equivalents are canonicalized
  • casefold=true: perform Unicode case folding, e.g. for case-insensitive string comparison
  • newline2lf=true, newline2ls=true, or newline2ps=true: convert various newline sequences (LF, CRLF, CR, NEL) into a linefeed (LF), line-separation (LS), or paragraph-separation (PS) character, respectively
  • stripmark=true: strip diacritical marks (e.g. accents)
  • stripignore=true: strip Unicode’s “default ignorable” characters (e.g. the soft hyphen or the left-to-right marker)
  • stripcc=true: strip control characters; horizontal tabs and form feeds are converted to spaces; newlines are also converted to spaces unless a newline-conversion flag was specified
  • rejectna=true: throw an error if unassigned code points are found
  • stable=true: enforce Unicode versioning stability (never introduce characters missing from earlier Unicode versions)

You can also use the chartransform keyword (which defaults to identity) to pass an arbitrary function mapping Integer codepoints to codepoints, which is is called on each character in s as it is processed, in order to perform arbitrary additional normalizations. For example, by passing chartransform=Unicode.julia_chartransform, you can apply a few Julia-specific character normalizations that are performed by Julia when parsing identifiers (in addition to NFC normalization: compose=true, stable=true).

For example, NFKC corresponds to the options compose=true, compat=true, stable=true.

Examples

  1. julia> "é" == Unicode.normalize("é") #LHS: Unicode U+00e9, RHS: U+0065 & U+0301
  2. true
  3. julia> "μ" == Unicode.normalize("µ", compat=true) #LHS: Unicode U+03bc, RHS: Unicode U+00b5
  4. true
  5. julia> Unicode.normalize("JuLiA", casefold=true)
  6. "julia"
  7. julia> Unicode.normalize("JúLiA", stripmark=true)
  8. "JuLiA"

Julia 1.8

The chartransform keyword argument requires Julia 1.8.

Unicode.graphemes — Function

  1. graphemes(s::AbstractString) -> GraphemeIterator

Return an iterator over substrings of s that correspond to the extended graphemes in the string, as defined by Unicode UAX #29. (Roughly, these are what users would perceive as single characters, even though they may contain more than one codepoint; for example a letter combined with an accent mark is a single grapheme.)

  1. graphemes(s::AbstractString, m:n) -> SubString

Returns a SubString of s consisting of the m-th through n-th graphemes of the string s, where the second argument m:n is an integer-valued AbstractUnitRange.

Loosely speaking, this corresponds to the m:n-th user-perceived “characters” in the string. For example:

  1. julia> s = graphemes("exposé", 3:6)
  2. "posé"
  3. julia> collect(s)
  4. 5-element Vector{Char}:
  5. 'p': ASCII/Unicode U+0070 (category Ll: Letter, lowercase)
  6. 'o': ASCII/Unicode U+006F (category Ll: Letter, lowercase)
  7. 's': ASCII/Unicode U+0073 (category Ll: Letter, lowercase)
  8. 'e': ASCII/Unicode U+0065 (category Ll: Letter, lowercase)
  9. '́': Unicode U+0301 (category Mn: Mark, nonspacing)

This consists of the 3rd to 7th codepoints (Chars) in "exposé", because the grapheme "é" is actually two Unicode codepoints (an 'e' followed by an acute-accent combining character U+0301).

Because finding grapheme boundaries requires iteration over the string contents, the graphemes(s, m:n) function requires time proportional to the length of the string (number of codepoints) before the end of the substring.

Julia 1.9

The m:n argument of graphemes requires Julia 1.9.