Normalization forms (composition and decomposition) <uninorm.h>
***************************************************************

This include file defines functions for transforming Unicode strings to one of the four normal forms, known as NFC, NFD, NKFC, NFKD.  These transformations involve decomposition and --- for NFC and NFKC --- composition of Unicode characters.

Decomposition of Unicode characters
===================================

The following enumerated values are the possible types of decomposition of a Unicode character.

 -- Constant: int UC_DECOMP_CANONICAL
     Denotes canonical decomposition.

 -- Constant: int UC_DECOMP_FONT
     UCD marker: <font>.  Denotes a font variant (e.g. a blackletter form).

 -- Constant: int UC_DECOMP_NOBREAK
     UCD marker: <noBreak>. Denotes a no-break version of a space or hyphen.

 -- Constant: int UC_DECOMP_INITIAL
     UCD marker: <initial>. Denotes an initial presentation form (Arabic).

 -- Constant: int UC_DECOMP_MEDIAL
     UCD marker: <medial>. Denotes a medial presentation form (Arabic).

 -- Constant: int UC_DECOMP_FINAL
     UCD marker: <final>. Denotes a final presentation form (Arabic).

 -- Constant: int UC_DECOMP_ISOLATED
     UCD marker: <isolated>. Denotes an isolated presentation form (Arabic).

 -- Constant: int UC_DECOMP_CIRCLE
     UCD marker: <circle>. Denotes an encircled form.

 -- Constant: int UC_DECOMP_SUPER
     UCD marker: <super>. Denotes a superscript form.

 -- Constant: int UC_DECOMP_SUB
     UCD marker: <sub>. Denotes a subscript form.

 -- Constant: int UC_DECOMP_VERTICAL
     UCD marker: <vertical>. Denotes a vertical layout presentation form.

 -- Constant: int UC_DECOMP_WIDE
     UCD marker: <wide>. Denotes a wide (or zenkaku) compatibility character.

 -- Constant: int UC_DECOMP_NARROW
     UCD marker: <narrow>. Denotes a narrow (or hankaku) compatibility character.

 -- Constant: int UC_DECOMP_SMALL
     UCD marker: <small>. Denotes a small variant form (CNS compatibility).

 -- Constant: int UC_DECOMP_SQUARE
     UCD marker: <square>. Denotes a CJK squared font variant.

 -- Constant: int UC_DECOMP_FRACTION
     UCD marker: <fraction>. Denotes a vulgar fraction form.

 -- Constant: int UC_DECOMP_COMPAT
     UCD marker: <compat>. Denotes an otherwise unspecified compatibility character.

The following constant denotes the maximum size of decomposition of a single Unicode character.

 -- Macro: unsigned int UC_DECOMPOSITION_MAX_LENGTH
     This macro expands to a constant that is the required size of buffer passed to the uc_decomposition and uc_canonical_decomposition functions.

The following functions decompose a Unicode character.

 -- Function: int uc_decomposition (ucs4_t UC, int *DECOMP_TAG, ucs4_t *DECOMPOSITION)
     Returns the character decomposition mapping of the Unicode character UC. DECOMPOSITION must point to an array of at least UC_DECOMPOSITION_MAX_LENGTH ucs_t elements.

     When a decomposition exists, DECOMPOSITION[0..N-1] and *DECOMP_TAG are filled and N is returned.  Otherwise -1 is returned.

 -- Function: int uc_canonical_decomposition (ucs4_t UC, ucs4_t *DECOMPOSITION)
     Returns the canonical character decomposition mapping of the Unicode character UC.  DECOMPOSITION must point to an array of at least UC_DECOMPOSITION_MAX_LENGTH ucs_t elements.

     When a decomposition exists, DECOMPOSITION[0..N-1] is filled and N is returned.  Otherwise -1 is returned.

Composition of Unicode characters
=================================

The following function composes a Unicode character from two Unicode characters.

 -- Function: ucs4_t uc_composition (ucs4_t UC1, ucs4_t UC2)
     Attempts to combine the Unicode characters UC1, UC2. UC1 is known to have canonical combining class 0.

     Returns the combination of UC1 and UC2, if it exists. Returns 0 otherwise.

     Not all decompositions can be recombined using this function.  See the Unicode file CompositionExclusions.txt for details.

Normalization of strings
========================

The Unicode standard defines four normalization forms for Unicode strings. The following type is used to denote a normalization form.

 -- Type: uninorm_t
     An object of type uninorm_t denotes a Unicode normalization form. This is a scalar type; its values can be compared with ==.

The following constants denote the four normalization forms.

 -- Macro: uninorm_t UNINORM_NFD
     Denotes Normalization form D: canonical decomposition.

 -- Macro: uninorm_t UNINORM_NFC
     Normalization form C: canonical decomposition, then canonical composition.

 -- Macro: uninorm_t UNINORM_NFKD
     Normalization form KD: compatibility decomposition.

 -- Macro: uninorm_t UNINORM_NFKC
     Normalization form KC: compatibility decomposition, then canonical composition.

The following functions operate on uninorm_t objects.

 -- Function: bool uninorm_is_compat_decomposing (uninorm_t NF)
     Tests whether the normalization form NF does compatibility decomposition.

 -- Function: bool uninorm_is_composing (uninorm_t NF)
     Tests whether the normalization form NF includes canonical composition.

 -- Function: uninorm_t uninorm_decomposing_form (uninorm_t NF)
     Returns the decomposing variant of the normalization form NF. This maps NFC,NFD -> NFD and NFKC,NFKD -> NFKD.

The following functions apply a Unicode normalization form to a Unicode string.

 -- Function: uint8_t * u8_normalize (uninorm_t NF, const uint8_t *S, size_t N, uint8_t *RESULTBUF, size_t *LENGTHP)
 -- Function: uint16_t * u16_normalize (uninorm_t NF, const uint16_t *S, size_t N, uint16_t *RESULTBUF, size_t *LENGTHP)
 -- Function: uint32_t * u32_normalize (uninorm_t NF, const uint32_t *S, size_t N, uint32_t *RESULTBUF, size_t *LENGTHP)
     Returns the specified normalization form of a string.

Normalizing comparisons
=======================

The following functions compare Unicode string, ignoring differences in normalization.

 -- Function: int u8_normcmp (const uint8_t *S1, size_t N1, const uint8_t *S2, size_t N2, uninorm_t NF, int *RESULTP)
 -- Function: int u16_normcmp (const uint16_t *S1, size_t N1, const uint16_t *S2, size_t N2, uninorm_t NF, int *RESULTP)
 -- Function: int u32_normcmp (const uint32_t *S1, size_t N1, const uint32_t *S2, size_t N2, uninorm_t NF, int *RESULTP)
     Compares S1 and S2, ignoring differences in normalization.

     NF must be either UNINORM_NFD or UNINORM_NFKD.

     If successful, sets *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and returns 0. Upon failure, returns -1 with errno set.

 -- Function: char * u8_normxfrm (const uint8_t *S, size_t N, uninorm_t NF, char *RESULTBUF, size_t *LENGTHP)
 -- Function: char * u16_normxfrm (const uint16_t *S, size_t N, uninorm_t NF, char *RESULTBUF, size_t *LENGTHP)
 -- Function: char * u32_normxfrm (const uint32_t *S, size_t N, uninorm_t NF, char *RESULTBUF, size_t *LENGTHP)
     Converts the string S of length N to a NUL-terminated byte sequence, in such a way that comparing u8_normxfrm (S1) and u8_normxfrm (S2) with the u8_cmp2 function is equivalent to comparing S1 and S2 with the u8_normcoll function.

     NF must be either UNINORM_NFC or UNINORM_NFKC.

 -- Function: int u8_normcoll (const uint8_t *S1, size_t N1, const uint8_t *S2, size_t N2, uninorm_t NF, int *RESULTP)
 -- Function: int u16_normcoll (const uint16_t *S1, size_t N1, const uint16_t *S2, size_t N2, uninorm_t NF, int *RESULTP)
 -- Function: int u32_normcoll (const uint32_t *S1, size_t N1, const uint32_t *S2, size_t N2, uninorm_t NF, int *RESULTP)
     Compares S1 and S2, ignoring differences in normalization, using the collation rules of the current locale.

     NF must be either UNINORM_NFC or UNINORM_NFKC.

     If successful, sets *RESULTP to -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2, and returns 0. Upon failure, returns -1 with errno set.

Normalization of streams of Unicode characters
==============================================

A ``stream of Unicode characters'' is essentially a function that accepts an ucs4_t argument repeatedly, optionally combined with a function that ``flushes'' the stream.

 -- Type: struct uninorm_filter
     This is the data type of a stream of Unicode characters that normalizes its input according to a given normalization form and passes the normalized character sequence to the encapsulated stream of Unicode characters.

 -- Function: struct uninorm_filter * uninorm_filter_create (uninorm_t NF, int (*STREAM_FUNC) (void *STREAM_DATA, ucs4_t UC), void *STREAM_DATA)
     Creates and returns a normalization filter for Unicode characters.

     The pair (STREAM_FUNC, STREAM_DATA) is the encapsulated stream. STREAM_FUNC (STREAM_DATA, UC) receives the Unicode character UC and returns 0 if successful, or -1 with errno set upon failure.

     Returns the new filter, or NULL with errno set upon failure.

 -- Function: int uninorm_filter_write (struct uninorm_filter *FILTER, ucs4_t UC)
     Stuffs a Unicode character into a normalizing filter. Returns 0 if successful, or -1 with errno set upon failure.

 -- Function: int uninorm_filter_flush (struct uninorm_filter *FILTER)
     Brings data buffered in the filter to its destination, the encapsulated stream.

     Returns 0 if successful, or -1 with errno set upon failure.

     Note! If after calling this function, additional characters are written into the filter, the resulting character sequence in the encapsulated stream will not necessarily be normalized.

 -- Function: int uninorm_filter_free (struct uninorm_filter *FILTER)
     Brings data buffered in the filter to its destination, the encapsulated stream, then closes and frees the filter.

     Returns 0 if successful, or -1 with errno set upon failure.