Elementary Unicode string functions <unistr.h>
**********************************************

This include file declares elementary functions for Unicode strings.  It is essentially the equivalent of what <string.h> is for C strings.

Elementary string checks
========================

The following function is available to verify the integrity of a Unicode string.

 -- Function: const uint8_t * u8_check (const uint8_t *S, size_t N)
 -- Function: const uint16_t * u16_check (const uint16_t *S, size_t N)
 -- Function: const uint32_t * u32_check (const uint32_t *S, size_t N)
     This function checks whether a Unicode string is well-formed. It returns NULL if valid, or a pointer to the first invalid unit otherwise.

Elementary string conversions
=============================

The following functions perform conversions between the different forms of Unicode strings.

 -- Function: uint16_t * u8_to_u16 (const uint8_t *S, size_t N, uint16_t *RESULTBUF, size_t *LENGTHP)
     Converts an UTF-8 string to an UTF-16 string.

     The RESULTBUF and LENGTHP arguments are as described in chapter Conventions.

 -- Function: uint32_t * u8_to_u32 (const uint8_t *S, size_t N, uint32_t *RESULTBUF, size_t *LENGTHP)
     Converts an UTF-8 string to an UTF-32 string.

     The RESULTBUF and LENGTHP arguments are as described in chapter Conventions.

 -- Function: uint8_t * u16_to_u8 (const uint16_t *S, size_t N, uint8_t *RESULTBUF, size_t *LENGTHP)
     Converts an UTF-16 string to an UTF-8 string.

     The RESULTBUF and LENGTHP arguments are as described in chapter Conventions.

 -- Function: uint32_t * u16_to_u32 (const uint16_t *S, size_t N, uint32_t *RESULTBUF, size_t *LENGTHP)
     Converts an UTF-16 string to an UTF-32 string.

     The RESULTBUF and LENGTHP arguments are as described in chapter Conventions.

 -- Function: uint8_t * u32_to_u8 (const uint32_t *S, size_t N, uint8_t *RESULTBUF, size_t *LENGTHP)
     Converts an UTF-32 string to an UTF-8 string.

     The RESULTBUF and LENGTHP arguments are as described in chapter Conventions.

 -- Function: uint16_t * u32_to_u16 (const uint32_t *S, size_t N, uint16_t *RESULTBUF, size_t *LENGTHP)
     Converts an UTF-32 string to an UTF-16 string.

     The RESULTBUF and LENGTHP arguments are as described in chapter Conventions.

Elementary string functions
===========================

Iterating over a Unicode string
-------------------------------

The following functions inspect and return details about the first character in a Unicode string.

 -- Function: int u8_mblen (const uint8_t *S, size_t N)
 -- Function: int u16_mblen (const uint16_t *S, size_t N)
 -- Function: int u32_mblen (const uint32_t *S, size_t N)
     Returns the length (number of units) of the first character in S, which is no longer than N.  Returns 0 if it is the NUL character.  Returns -1 upon failure.

     This function is similar to mblen, except that it operates on a Unicode string and that S must not be NULL.

 -- Function: int u8_mbtouc (ucs4_t *PUC, const uint8_t *S, size_t N)
 -- Function: int u16_mbtouc (ucs4_t *PUC, const uint16_t *S, size_t N)
 -- Function: int u32_mbtouc (ucs4_t *PUC, const uint32_t *S, size_t N)
     Returns the length (number of units) of the first character in S, putting its ucs4_t representation in *PUC.  Upon failure, *PUC is set to 0xfffd, and an appropriate number of units is returned.

     The number of available units, N, must be > 0.

     This function fails if an invalid sequence of units is encountered at the beginning of S, or if additional units (after the N provided units) would be needed to form a character.

     This function is similar to mbtowc, except that it operates on a Unicode string, PUC and S must not be NULL, N must be > 0, and the NUL character is not treated specially.

 -- Function: int u8_mbtouc_unsafe (ucs4_t *PUC, const uint8_t *S, size_t N)
 -- Function: int u16_mbtouc_unsafe (ucs4_t *PUC, const uint16_t *S, size_t N)
 -- Function: int u32_mbtouc_unsafe (ucs4_t *PUC, const uint32_t *S, size_t N)
     This function is identical to u8_mbtouc/u16_mbtouc/u32_mbtouc. Earlier versions of this function performed fewer range-checks on the sequence of units.

 -- Function: int u8_mbtoucr (ucs4_t *PUC, const uint8_t *S, size_t N)
 -- Function: int u16_mbtoucr (ucs4_t *PUC, const uint16_t *S, size_t N)
 -- Function: int u32_mbtoucr (ucs4_t *PUC, const uint32_t *S, size_t N)
     Returns the length (number of units) of the first character in S, putting its ucs4_t representation in *PUC.  Upon failure, *PUC is set to 0xfffd, and -1 is returned for an invalid sequence of units, -2 is returned for an incomplete sequence of units.

     The number of available units, N, must be > 0.

     This function is similar to u8_mbtouc, except that the return value gives more details about the failure, similar to mbrtowc.

Creating Unicode strings one character at a time
------------------------------------------------

The following function stores a Unicode character as a Unicode string in memory.

 -- Function: int u8_uctomb (uint8_t *S, ucs4_t UC, int N)
 -- Function: int u16_uctomb (uint16_t *S, ucs4_t UC, int N)
 -- Function: int u32_uctomb (uint32_t *S, ucs4_t UC, int N)
     Puts the multibyte character represented by UC in S, returning its length.  Returns -1 upon failure, -2 if the number of available units, N, is too small.  The latter case cannot occur if N >= 6/2/1, respectively.

     This function is similar to wctomb, except that it operates on a Unicode strings, S must not be NULL, and the argument N must be specified.

Copying Unicode strings
-----------------------

The following functions copy Unicode strings in memory.

 -- Function: uint8_t * u8_cpy (uint8_t *DEST, const uint8_t *SRC, size_t N)
 -- Function: uint16_t * u16_cpy (uint16_t *DEST, const uint16_t *SRC, size_t N)
 -- Function: uint32_t * u32_cpy (uint32_t *DEST, const uint32_t *SRC, size_t N)
     Copies N units from SRC to DEST.

     This function is similar to memcpy, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_move (uint8_t *DEST, const uint8_t *SRC, size_t N)
 -- Function: uint16_t * u16_move (uint16_t *DEST, const uint16_t *SRC, size_t N)
 -- Function: uint32_t * u32_move (uint32_t *DEST, const uint32_t *SRC, size_t N)
     Copies N units from SRC to DEST, guaranteeing correct behavior for overlapping memory areas.

     This function is similar to memmove, except that it operates on Unicode strings.

The following function fills a Unicode string.

 -- Function: uint8_t * u8_set (uint8_t *S, ucs4_t UC, size_t N)
 -- Function: uint16_t * u16_set (uint16_t *S, ucs4_t UC, size_t N)
 -- Function: uint32_t * u32_set (uint32_t *S, ucs4_t UC, size_t N)
     Sets the first N characters of S to UC.  UC should be a character that occupies only 1 unit.

     This function is similar to memset, except that it operates on Unicode strings.

Comparing Unicode strings
-------------------------

The following function compares two Unicode strings of the same length.

 -- Function: int u8_cmp (const uint8_t *S1, const uint8_t *S2, size_t N)
 -- Function: int u16_cmp (const uint16_t *S1, const uint16_t *S2, size_t N)
 -- Function: int u32_cmp (const uint32_t *S1, const uint32_t *S2, size_t N)
     Compares S1 and S2, each of length N, lexicographically. Returns a negative value if S1 compares smaller than S2, a positive value if S1 compares larger than S2, or 0 if they compare equal.

     This function is similar to memcmp, except that it operates on Unicode strings.

The following function compares two Unicode strings of possibly different lengths.

 -- Function: int u8_cmp2 (const uint8_t *S1, size_t N1, const uint8_t *S2, size_t N2)
 -- Function: int u16_cmp2 (const uint16_t *S1, size_t N1, const uint16_t *S2, size_t N2)
 -- Function: int u32_cmp2 (const uint32_t *S1, size_t N1, const uint32_t *S2, size_t N2)
     Compares S1 and S2, lexicographically. Returns a negative value if S1 compares smaller than S2, a positive value if S1 compares larger than S2, or 0 if they compare equal.

     This function is similar to the gnulib function memcmp2, except that it operates on Unicode strings.

Searching for a character in a Unicode string
---------------------------------------------

The following function searches for a given Unicode character.

 -- Function: uint8_t * u8_chr (const uint8_t *S, size_t N, ucs4_t UC)
 -- Function: uint16_t * u16_chr (const uint16_t *S, size_t N, ucs4_t UC)
 -- Function: uint32_t * u32_chr (const uint32_t *S, size_t N, ucs4_t UC)
     Searches the string at S for UC.  Returns a pointer to the first occurrence of UC in S, or NULL if UC does not occur in S.

     This function is similar to memchr, except that it operates on Unicode strings.

Counting the characters in a Unicode string
-------------------------------------------

The following function counts the number of Unicode characters.

 -- Function: size_t u8_mbsnlen (const uint8_t *S, size_t N)
 -- Function: size_t u16_mbsnlen (const uint16_t *S, size_t N)
 -- Function: size_t u32_mbsnlen (const uint32_t *S, size_t N)
     Counts and returns the number of Unicode characters in the N units from S.

     This function is similar to the gnulib function mbsnlen, except that it operates on Unicode strings.

Elementary string functions with memory allocation
==================================================

The following function copies a Unicode string.

 -- Function: uint8_t * u8_cpy_alloc (const uint8_t *S, size_t N)
 -- Function: uint16_t * u16_cpy_alloc (const uint16_t *S, size_t N)
 -- Function: uint32_t * u32_cpy_alloc (const uint32_t *S, size_t N)
     Makes a freshly allocated copy of S, of length N.

Elementary string functions on NUL terminated strings
=====================================================

Iterating over a NUL terminated Unicode string
----------------------------------------------

The following functions inspect and return details about the first character in a Unicode string.

 -- Function: int u8_strmblen (const uint8_t *S)
 -- Function: int u16_strmblen (const uint16_t *S)
 -- Function: int u32_strmblen (const uint32_t *S)
     Returns the length (number of units) of the first character in S. Returns 0 if it is the NUL character.  Returns -1 upon failure.

 -- Function: int u8_strmbtouc (ucs4_t *PUC, const uint8_t *S)
 -- Function: int u16_strmbtouc (ucs4_t *PUC, const uint16_t *S)
 -- Function: int u32_strmbtouc (ucs4_t *PUC, const uint32_t *S)
     Returns the length (number of units) of the first character in S, putting its ucs4_t representation in *PUC.  Returns 0 if it is the NUL character.  Returns -1 upon failure.

 -- Function: const uint8_t * u8_next (ucs4_t *PUC, const uint8_t *S)
 -- Function: const uint16_t * u16_next (ucs4_t *PUC, const uint16_t *S)
 -- Function: const uint32_t * u32_next (ucs4_t *PUC, const uint32_t *S)
     Forward iteration step.  Advances the pointer past the next character, or returns NULL if the end of the string has been reached.  Puts the character's ucs4_t representation in *PUC.

The following function inspects and returns details about the previous character in a Unicode string.

 -- Function: const uint8_t * u8_prev (ucs4_t *PUC, const uint8_t *S, const uint8_t *START)
 -- Function: const uint16_t * u16_prev (ucs4_t *PUC, const uint16_t *S, const uint16_t *START)
 -- Function: const uint32_t * u32_prev (ucs4_t *PUC, const uint32_t *S, const uint32_t *START)
     Backward iteration step.  Advances the pointer to point to the previous character (the one that ends at S), or returns NULL if the beginning of the string (specified by START) had been reached. Puts the character's ucs4_t representation in *PUC. Note that this function works only on well-formed Unicode strings.

Length of a NUL terminated Unicode string
-----------------------------------------

The following functions determine the length of a Unicode string.

 -- Function: size_t u8_strlen (const uint8_t *S)
 -- Function: size_t u16_strlen (const uint16_t *S)
 -- Function: size_t u32_strlen (const uint32_t *S)
     Returns the number of units in S.

     This function is similar to strlen and wcslen, except that it operates on Unicode strings.

 -- Function: size_t u8_strnlen (const uint8_t *S, size_t MAXLEN)
 -- Function: size_t u16_strnlen (const uint16_t *S, size_t MAXLEN)
 -- Function: size_t u32_strnlen (const uint32_t *S, size_t MAXLEN)
     Returns the number of units in S, but at most MAXLEN.

     This function is similar to strnlen and wcsnlen, except that it operates on Unicode strings.

Copying a NUL terminated Unicode string
---------------------------------------

The following functions copy portions of Unicode strings in memory.

 -- Function: uint8_t * u8_strcpy (uint8_t *DEST, const uint8_t *SRC)
 -- Function: uint16_t * u16_strcpy (uint16_t *DEST, const uint16_t *SRC)
 -- Function: uint32_t * u32_strcpy (uint32_t *DEST, const uint32_t *SRC)
     Copies SRC to DEST.

     This function is similar to strcpy and wcscpy, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_stpcpy (uint8_t *DEST, const uint8_t *SRC)
 -- Function: uint16_t * u16_stpcpy (uint16_t *DEST, const uint16_t *SRC)
 -- Function: uint32_t * u32_stpcpy (uint32_t *DEST, const uint32_t *SRC)
     Copies SRC to DEST, returning the address of the terminating NUL in DEST.

     This function is similar to stpcpy, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_strncpy (uint8_t *DEST, const uint8_t *SRC, size_t N)
 -- Function: uint16_t * u16_strncpy (uint16_t *DEST, const uint16_t *SRC, size_t N)
 -- Function: uint32_t * u32_strncpy (uint32_t *DEST, const uint32_t *SRC, size_t N)
     Copies no more than N units of SRC to DEST.

     This function is similar to strncpy and wcsncpy, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_stpncpy (uint8_t *DEST, const uint8_t *SRC, size_t N)
 -- Function: uint16_t * u16_stpncpy (uint16_t *DEST, const uint16_t *SRC, size_t N)
 -- Function: uint32_t * u32_stpncpy (uint32_t *DEST, const uint32_t *SRC, size_t N)
     Copies no more than N units of SRC to DEST.  Returns a pointer past the last non-NUL unit written into DEST.  In other words, if the units written into DEST include a NUL, the return value is the address of the first such NUL unit, otherwise it is DEST + N.

     This function is similar to stpncpy, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_strcat (uint8_t *DEST, const uint8_t *SRC)
 -- Function: uint16_t * u16_strcat (uint16_t *DEST, const uint16_t *SRC)
 -- Function: uint32_t * u32_strcat (uint32_t *DEST, const uint32_t *SRC)
     Appends SRC onto DEST.

     This function is similar to strcat and wcscat, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_strncat (uint8_t *DEST, const uint8_t *SRC, size_t N)
 -- Function: uint16_t * u16_strncat (uint16_t *DEST, const uint16_t *SRC, size_t N)
 -- Function: uint32_t * u32_strncat (uint32_t *DEST, const uint32_t *SRC, size_t N)
     Appends no more than N units of SRC onto DEST.

     This function is similar to strncat and wcsncat, except that it operates on Unicode strings.

Comparing NUL terminated Unicode strings
----------------------------------------

The following functions compare two Unicode strings.

 -- Function: int u8_strcmp (const uint8_t *S1, const uint8_t *S2)
 -- Function: int u16_strcmp (const uint16_t *S1, const uint16_t *S2)
 -- Function: int u32_strcmp (const uint32_t *S1, const uint32_t *S2)
     Compares S1 and S2, lexicographically. Returns a negative value if S1 compares smaller than S2, a positive value if S1 compares larger than S2, or 0 if they compare equal.

     This function is similar to strcmp and wcscmp, except that it operates on Unicode strings.

 -- Function: int u8_strcoll (const uint8_t *S1, const uint8_t *S2)
 -- Function: int u16_strcoll (const uint16_t *S1, const uint16_t *S2)
 -- Function: int u32_strcoll (const uint32_t *S1, const uint32_t *S2)
     Compares S1 and S2 using the collation rules of the current locale. Returns -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.  Upon failure, sets errno and returns any value.

     This function is similar to strcoll and wcscoll, except that it operates on Unicode strings.

     Note that this function may consider different canonical normalizations of the same string as having a large distance.  It is therefore better to use the function u8_normcoll instead of this one; see uninorm.h.

 -- Function: int u8_strncmp (const uint8_t *S1, const uint8_t *S2, size_t N)
 -- Function: int u16_strncmp (const uint16_t *S1, const uint16_t *S2, size_t N)
 -- Function: int u32_strncmp (const uint32_t *S1, const uint32_t *S2, size_t N)
     Compares no more than N units of S1 and S2.

     This function is similar to strncmp and wcsncmp, except that it operates on Unicode strings.

Duplicating a NUL terminated Unicode string
-------------------------------------------

The following function allocates a duplicate of a Unicode string.

 -- Function: uint8_t * u8_strdup (const uint8_t *S)
 -- Function: uint16_t * u16_strdup (const uint16_t *S)
 -- Function: uint32_t * u32_strdup (const uint32_t *S)
     Duplicates S, returning an identical malloc'd string.

     This function is similar to strdup and wcsdup, except that it operates on Unicode strings.

Searching for a character in a NUL terminated Unicode string
------------------------------------------------------------

The following functions search for a given Unicode character.

 -- Function: uint8_t * u8_strchr (const uint8_t *STR, ucs4_t UC)
 -- Function: uint16_t * u16_strchr (const uint16_t *STR, ucs4_t UC)
 -- Function: uint32_t * u32_strchr (const uint32_t *STR, ucs4_t UC)
     Finds the first occurrence of UC in STR.

     This function is similar to strchr and wcschr, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_strrchr (const uint8_t *STR, ucs4_t UC)
 -- Function: uint16_t * u16_strrchr (const uint16_t *STR, ucs4_t UC)
 -- Function: uint32_t * u32_strrchr (const uint32_t *STR, ucs4_t UC)
     Finds the last occurrence of UC in STR.

     This function is similar to strrchr and wcsrchr, except that it operates on Unicode strings.

The following functions search for the first occurrence of some Unicode character in or outside a given set of Unicode characters.

 -- Function: size_t u8_strcspn (const uint8_t *STR, const uint8_t *REJECT)
 -- Function: size_t u16_strcspn (const uint16_t *STR, const uint16_t *REJECT)
 -- Function: size_t u32_strcspn (const uint32_t *STR, const uint32_t *REJECT)
     Returns the length of the initial segment of STR which consists entirely of Unicode characters not in REJECT.

     This function is similar to strcspn and wcscspn, except that it operates on Unicode strings.

 -- Function: size_t u8_strspn (const uint8_t *STR, const uint8_t *ACCEPT)
 -- Function: size_t u16_strspn (const uint16_t *STR, const uint16_t *ACCEPT)
 -- Function: size_t u32_strspn (const uint32_t *STR, const uint32_t *ACCEPT)
     Returns the length of the initial segment of STR which consists entirely of Unicode characters in ACCEPT.

     This function is similar to strspn and wcsspn, except that it operates on Unicode strings.

 -- Function: uint8_t * u8_strpbrk (const uint8_t *STR, const uint8_t *ACCEPT)
 -- Function: uint16_t * u16_strpbrk (const uint16_t *STR, const uint16_t *ACCEPT)
 -- Function: uint32_t * u32_strpbrk (const uint32_t *STR, const uint32_t *ACCEPT)
     Finds the first occurrence in STR of any character in ACCEPT.

     This function is similar to strpbrk and wcspbrk, except that it operates on Unicode strings.

Searching for a substring in a NUL terminated Unicode string
------------------------------------------------------------

The following functions search whether a given Unicode string is a substring of another Unicode string.

 -- Function: uint8_t * u8_strstr (const uint8_t *HAYSTACK, const uint8_t *NEEDLE)
 -- Function: uint16_t * u16_strstr (const uint16_t *HAYSTACK, const uint16_t *NEEDLE)
 -- Function: uint32_t * u32_strstr (const uint32_t *HAYSTACK, const uint32_t *NEEDLE)
     Finds the first occurrence of NEEDLE in HAYSTACK.

     This function is similar to strstr and wcsstr, except that it operates on Unicode strings.

 -- Function: bool u8_startswith (const uint8_t *STR, const uint8_t *PREFIX)
 -- Function: bool u16_startswith (const uint16_t *STR, const uint16_t *PREFIX)
 -- Function: bool u32_startswith (const uint32_t *STR, const uint32_t *PREFIX)
     Tests whether STR starts with PREFIX.

 -- Function: bool u8_endswith (const uint8_t *STR, const uint8_t *SUFFIX)
 -- Function: bool u16_endswith (const uint16_t *STR, const uint16_t *SUFFIX)
 -- Function: bool u32_endswith (const uint32_t *STR, const uint32_t *SUFFIX)
     Tests whether STR ends with SUFFIX.

Tokenizing a NUL terminated Unicode string
------------------------------------------

The following function does one step in tokenizing a Unicode string.

 -- Function: uint8_t * u8_strtok (uint8_t *STR, const uint8_t *DELIM, uint8_t **PTR)
 -- Function: uint16_t * u16_strtok (uint16_t *STR, const uint16_t *DELIM, uint16_t **PTR)
 -- Function: uint32_t * u32_strtok (uint32_t *STR, const uint32_t *DELIM, uint32_t **PTR)
     Divides STR into tokens separated by characters in DELIM.

     This function is similar to strtok_r and wcstok, except that it operates on Unicode strings.  Its interface is actually more similar to wcstok than to strtok.