(std unicode normalize)

Reference

Overview

Unicode normalization: NFC, NFD, NFKC and NFKD.

(import (std unicode normalize))

(def typed "caf\u{E9}")        ; é as one character
(def pasted "cafe\u{301}")     ; e and a combining accent
(= typed pasted)                        ; #f
(= (string-nfc typed) (string-nfc pasted))   ; #t

(string-nfkc "\u{FB01}le")     ; "file": the ligature fi is two letters
(string-nfkc "x\u{B2}")        ; "x2"

One text can be written with different code points. é is one code point, U+00E9, or two: e and U+0301 COMBINING ACUTE ACCENT. Normalization makes the forms the same, so that strings can be compared, hashed and stored. This is Unicode Standard Annex #15.

The four forms. - string-nfd: the canonical decomposition. é becomes e and U+0301, and marks are put in a fixed order. - string-nfc: the canonical decomposition, then composed again. é is one code point. Most text is in NFC already. Use it to store and compare text. - string-nfkd and string-nfkc: the same, after the compatibility decompositions too. fi becomes fi, ² becomes 2, and a full-width A becomes A. Some of the meaning is lost. Use them to search, or to compare identifiers, not to store what the user wrote.

Canonical equivalence. Two strings with the same NFD, which is when they have the same NFC, are canonically equivalent: they are the same text, and should look the same.

Stability. Unicode does not change the normal form of a string of assigned characters in a later version. A string normalized with this version stays normalized, so a stored NFC string or a hash of one need not be made again. Only a character that is unassigned here, and is assigned later, can normalize differently in a later version.

Tables. The tables are generated from the Unicode Character Database by tools/unicode/normalize.bjo, and do not use .NET or ICU. Every platform normalizes the same way. The version is normalize-unicode-version.

Case. Normalization does not fold case. To compare without case, compare (string-nfd (string-foldcase (string-nfd s))), with string-foldcase of (std unicode casefold): the canonical caseless match of the Unicode Standard. The fold can undo a normalization, so the normalization comes before and after it.

See also: string-nfc, string-nfkc, string-nfc?, normalize-unicode-version

Reference

Functions

Values

Functions

string-nfc

function ↑ Functions

(: string-nfc (-> string string))
(string-nfc s)

The NFC of a string: the canonical decomposition, composed again.

s
The string to normalize.
returns
The NFC of s. When s is in NFC, s itself, with no copy.
(string-nfc "cafe\u{301}")      ; "caf\u{E9}"
(string-nfc "\u{212B}")         ; "\u{C5}": ANGSTROM SIGN is Å
(string-nfc "\u{1100}\u{1161}") ; "\u{AC00}": the Hangul syllable 가

See also: string-nfd, string-nfkc, string-nfc?

string-nfd

function ↑ Functions

(: string-nfd (-> string string))
(string-nfd s)

The NFD of a string: the canonical decomposition.

s
The string to normalize.
returns
The NFD of s. When s is in NFD, s itself, with no copy.
(string-nfd "caf\u{E9}")        ; "cafe\u{301}"
(string-nfd "\u{AC00}")         ; "\u{1100}\u{1161}"

See also: string-nfc, string-nfkd, string-nfd?

string-nfkc

function ↑ Functions

(: string-nfkc (-> string string))
(string-nfkc s)

The NFKC of a string: the compatibility decomposition, composed again.

s
The string to normalize.
returns
The NFKC of s. When s is in NFKC, s itself, with no copy.
(string-nfkc "\u{FB01}")        ; "fi"
(string-nfkc "\u{FF21}")        ; "A": FULLWIDTH LATIN CAPITAL LETTER A
(string-nfkc "\u{2460}")        ; "1": CIRCLED DIGIT ONE

See also: string-nfc, string-nfkd, string-nfkc?

string-nfkd

function ↑ Functions

(: string-nfkd (-> string string))
(string-nfkd s)

The NFKD of a string: the compatibility decomposition.

s
The string to normalize.
returns
The NFKD of s. When s is in NFKD, s itself, with no copy.
(string-nfkd "\u{1E9B}\u{323}") ; "s\u{323}\u{307}"

See also: string-nfd, string-nfkc, string-nfkd?

string-nfc?

function ↑ Functions

(: string-nfc? (-> string bool))
(string-nfc? s)

Whether a string is in NFC.

s
The string.
returns
#t when (string-nfc s) is s.

Most strings are decided by the quick check of UAX #15, without a copy. A string with a character that may compose with the one before it is normalized, and compared.

See also: string-nfc

string-nfd?

function ↑ Functions

(: string-nfd? (-> string bool))
(string-nfd? s)

Whether a string is in NFD.

s
The string.
returns
#t when (string-nfd s) is s.

See also: string-nfd

string-nfkc?

function ↑ Functions

(: string-nfkc? (-> string bool))
(string-nfkc? s)

Whether a string is in NFKC.

s
The string.
returns
#t when (string-nfkc s) is s.

See also: string-nfkc

string-nfkd?

function ↑ Functions

(: string-nfkd? (-> string bool))
(string-nfkd? s)

Whether a string is in NFKD.

s
The string.
returns
#t when (string-nfkd s) is s.

See also: string-nfkd

Values

normalize-unicode-version

value ↑ Values

(: normalize-unicode-version string)

The version of the Unicode Character Database the tables are from.

See also: string-nfc