1 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
2 /* Elementary Unicode string functions.
3 Copyright (C) 2001-2002, 2005-2014 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Get common macros for C. */
24 #include "unused-parameter.h"
39 All functions prefixed with u8_ operate on UTF-8 encoded strings.
40 Their unit is an uint8_t (1 byte).
42 All functions prefixed with u16_ operate on UTF-16 encoded strings.
43 Their unit is an uint16_t (a 2-byte word).
45 All functions prefixed with u32_ operate on UCS-4 encoded strings.
46 Their unit is an uint32_t (a 4-byte word).
48 All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
51 All arguments starting with "str" and the arguments of functions starting
52 with u8_str/u16_str/u32_str denote a NUL terminated string, i.e. a string
53 which terminates at the first NUL unit. This termination unit is
54 considered part of the string for all memory allocation purposes, but
55 is not considered part of the string for all other logical purposes.
57 Functions returning a string result take a (resultbuf, lengthp) argument
58 pair. If resultbuf is not NULL and the result fits into *lengthp units,
59 it is put in resultbuf, and resultbuf is returned. Otherwise, a freshly
60 allocated string is returned. In both cases, *lengthp is set to the
61 length (number of units) of the returned string. In case of error,
62 NULL is returned and errno is set. */
65 /* Elementary string checks. */
67 /* Check whether an UTF-8 string is well-formed.
68 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
69 extern const uint8_t *
70 u8_check (const uint8_t *s, size_t n)
73 /* Check whether an UTF-16 string is well-formed.
74 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
75 extern const uint16_t *
76 u16_check (const uint16_t *s, size_t n)
79 /* Check whether an UCS-4 string is well-formed.
80 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
81 extern const uint32_t *
82 u32_check (const uint32_t *s, size_t n)
86 /* Elementary string conversions. */
88 /* Convert an UTF-8 string to an UTF-16 string. */
90 u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
93 /* Convert an UTF-8 string to an UCS-4 string. */
95 u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
98 /* Convert an UTF-16 string to an UTF-8 string. */
100 u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
103 /* Convert an UTF-16 string to an UCS-4 string. */
105 u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
108 /* Convert an UCS-4 string to an UTF-8 string. */
110 u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
113 /* Convert an UCS-4 string to an UTF-16 string. */
115 u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
119 /* Elementary string functions. */
121 /* Return the length (number of units) of the first character in S, which is
122 no longer than N. Return 0 if it is the NUL character. Return -1 upon
124 /* Similar to mblen(), except that s must not be NULL. */
126 u8_mblen (const uint8_t *s, size_t n)
129 u16_mblen (const uint16_t *s, size_t n)
132 u32_mblen (const uint32_t *s, size_t n)
135 /* Return the length (number of units) of the first character in S, putting
136 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
137 and an appropriate number of units is returned.
138 The number of available units, N, must be > 0. */
139 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
140 and the NUL character is not treated specially. */
141 /* The variants with _safe suffix are safe, even if the library is compiled
142 without --enable-safety. */
144 #if GNULIB_UNISTR_U8_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
147 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
150 u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
152 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
162 return u8_mbtouc_unsafe_aux (puc, s, n);
167 #if GNULIB_UNISTR_U16_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
170 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
173 u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
175 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
179 if (c < 0xd800 || c >= 0xe000)
185 return u16_mbtouc_unsafe_aux (puc, s, n);
190 #if GNULIB_UNISTR_U32_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
193 u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
196 u32_mbtouc_unsafe (ucs4_t *puc,
197 const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
201 # if CONFIG_UNICODE_SAFETY
202 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
205 # if CONFIG_UNICODE_SAFETY
207 /* invalid multibyte character */
215 #if GNULIB_UNISTR_U8_MBTOUC || HAVE_LIBUNISTRING
218 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
221 u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
223 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
233 return u8_mbtouc_aux (puc, s, n);
238 #if GNULIB_UNISTR_U16_MBTOUC || HAVE_LIBUNISTRING
241 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
244 u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
246 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n)
250 if (c < 0xd800 || c >= 0xe000)
256 return u16_mbtouc_aux (puc, s, n);
261 #if GNULIB_UNISTR_U32_MBTOUC || HAVE_LIBUNISTRING
264 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
267 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
271 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
274 /* invalid multibyte character */
281 /* Return the length (number of units) of the first character in S, putting
282 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
283 and -1 is returned for an invalid sequence of units, -2 is returned for an
284 incomplete sequence of units.
285 The number of available units, N, must be > 0. */
286 /* Similar to u*_mbtouc(), except that the return value gives more details
287 about the failure, similar to mbrtowc(). */
289 #if GNULIB_UNISTR_U8_MBTOUCR || HAVE_LIBUNISTRING
291 u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
294 #if GNULIB_UNISTR_U16_MBTOUCR || HAVE_LIBUNISTRING
296 u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
299 #if GNULIB_UNISTR_U32_MBTOUCR || HAVE_LIBUNISTRING
301 u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
304 /* Put the multibyte character represented by UC in S, returning its
305 length. Return -1 upon failure, -2 if the number of available units, N,
306 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
307 /* Similar to wctomb(), except that s must not be NULL, and the argument n
308 must be specified. */
310 #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING
311 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
313 u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n);
316 u8_uctomb (uint8_t *s, ucs4_t uc, int n);
319 u8_uctomb (uint8_t *s, ucs4_t uc, int n)
321 if (uc < 0x80 && n > 0)
327 return u8_uctomb_aux (s, uc, n);
332 #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING
333 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
335 u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n);
338 u16_uctomb (uint16_t *s, ucs4_t uc, int n);
341 u16_uctomb (uint16_t *s, ucs4_t uc, int n)
343 if (uc < 0xd800 && n > 0)
349 return u16_uctomb_aux (s, uc, n);
354 #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING
357 u32_uctomb (uint32_t *s, ucs4_t uc, int n);
360 u32_uctomb (uint32_t *s, ucs4_t uc, int n)
362 if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
378 /* Copy N units from SRC to DEST. */
379 /* Similar to memcpy(). */
381 u8_cpy (uint8_t *dest, const uint8_t *src, size_t n);
383 u16_cpy (uint16_t *dest, const uint16_t *src, size_t n);
385 u32_cpy (uint32_t *dest, const uint32_t *src, size_t n);
387 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
388 overlapping memory areas. */
389 /* Similar to memmove(). */
391 u8_move (uint8_t *dest, const uint8_t *src, size_t n);
393 u16_move (uint16_t *dest, const uint16_t *src, size_t n);
395 u32_move (uint32_t *dest, const uint32_t *src, size_t n);
397 /* Set the first N characters of S to UC. UC should be a character that
398 occupies only 1 unit. */
399 /* Similar to memset(). */
401 u8_set (uint8_t *s, ucs4_t uc, size_t n);
403 u16_set (uint16_t *s, ucs4_t uc, size_t n);
405 u32_set (uint32_t *s, ucs4_t uc, size_t n);
407 /* Compare S1 and S2, each of length N. */
408 /* Similar to memcmp(). */
410 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
413 u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n)
416 u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n)
419 /* Compare S1 and S2. */
420 /* Similar to the gnulib function memcmp2(). */
422 u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2)
425 u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2)
428 u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2)
431 /* Search the string at S for UC. */
432 /* Similar to memchr(). */
434 u8_chr (const uint8_t *s, size_t n, ucs4_t uc)
437 u16_chr (const uint16_t *s, size_t n, ucs4_t uc)
440 u32_chr (const uint32_t *s, size_t n, ucs4_t uc)
443 /* Count the number of Unicode characters in the N units from S. */
444 /* Similar to mbsnlen(). */
446 u8_mbsnlen (const uint8_t *s, size_t n)
449 u16_mbsnlen (const uint16_t *s, size_t n)
452 u32_mbsnlen (const uint32_t *s, size_t n)
455 /* Elementary string functions with memory allocation. */
457 /* Make a freshly allocated copy of S, of length N. */
459 u8_cpy_alloc (const uint8_t *s, size_t n);
461 u16_cpy_alloc (const uint16_t *s, size_t n);
463 u32_cpy_alloc (const uint32_t *s, size_t n);
465 /* Elementary string functions on NUL terminated strings. */
467 /* Return the length (number of units) of the first character in S.
468 Return 0 if it is the NUL character. Return -1 upon failure. */
470 u8_strmblen (const uint8_t *s)
473 u16_strmblen (const uint16_t *s)
476 u32_strmblen (const uint32_t *s)
479 /* Return the length (number of units) of the first character in S, putting
480 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
481 character. Return -1 upon failure. */
483 u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
485 u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
487 u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
489 /* Forward iteration step. Advances the pointer past the next character,
490 or returns NULL if the end of the string has been reached. Puts the
491 character's 'ucs4_t' representation in *PUC. */
492 extern const uint8_t *
493 u8_next (ucs4_t *puc, const uint8_t *s);
494 extern const uint16_t *
495 u16_next (ucs4_t *puc, const uint16_t *s);
496 extern const uint32_t *
497 u32_next (ucs4_t *puc, const uint32_t *s);
499 /* Backward iteration step. Advances the pointer to point to the previous
500 character, or returns NULL if the beginning of the string had been reached.
501 Puts the character's 'ucs4_t' representation in *PUC. */
502 extern const uint8_t *
503 u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
504 extern const uint16_t *
505 u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
506 extern const uint32_t *
507 u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
509 /* Return the number of units in S. */
510 /* Similar to strlen(), wcslen(). */
512 u8_strlen (const uint8_t *s)
515 u16_strlen (const uint16_t *s)
518 u32_strlen (const uint32_t *s)
521 /* Return the number of units in S, but at most MAXLEN. */
522 /* Similar to strnlen(), wcsnlen(). */
524 u8_strnlen (const uint8_t *s, size_t maxlen)
527 u16_strnlen (const uint16_t *s, size_t maxlen)
530 u32_strnlen (const uint32_t *s, size_t maxlen)
533 /* Copy SRC to DEST. */
534 /* Similar to strcpy(), wcscpy(). */
536 u8_strcpy (uint8_t *dest, const uint8_t *src);
538 u16_strcpy (uint16_t *dest, const uint16_t *src);
540 u32_strcpy (uint32_t *dest, const uint32_t *src);
542 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
543 /* Similar to stpcpy(). */
545 u8_stpcpy (uint8_t *dest, const uint8_t *src);
547 u16_stpcpy (uint16_t *dest, const uint16_t *src);
549 u32_stpcpy (uint32_t *dest, const uint32_t *src);
551 /* Copy no more than N units of SRC to DEST. */
552 /* Similar to strncpy(), wcsncpy(). */
554 u8_strncpy (uint8_t *dest, const uint8_t *src, size_t n);
556 u16_strncpy (uint16_t *dest, const uint16_t *src, size_t n);
558 u32_strncpy (uint32_t *dest, const uint32_t *src, size_t n);
560 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
561 non-NUL unit written into DEST. */
562 /* Similar to stpncpy(). */
564 u8_stpncpy (uint8_t *dest, const uint8_t *src, size_t n);
566 u16_stpncpy (uint16_t *dest, const uint16_t *src, size_t n);
568 u32_stpncpy (uint32_t *dest, const uint32_t *src, size_t n);
570 /* Append SRC onto DEST. */
571 /* Similar to strcat(), wcscat(). */
573 u8_strcat (uint8_t *dest, const uint8_t *src);
575 u16_strcat (uint16_t *dest, const uint16_t *src);
577 u32_strcat (uint32_t *dest, const uint32_t *src);
579 /* Append no more than N units of SRC onto DEST. */
580 /* Similar to strncat(), wcsncat(). */
582 u8_strncat (uint8_t *dest, const uint8_t *src, size_t n);
584 u16_strncat (uint16_t *dest, const uint16_t *src, size_t n);
586 u32_strncat (uint32_t *dest, const uint32_t *src, size_t n);
588 /* Compare S1 and S2. */
589 /* Similar to strcmp(), wcscmp(). */
591 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
593 u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2)
595 # define u8_strcmp u8_strcmp_gnu
598 u8_strcmp (const uint8_t *s1, const uint8_t *s2)
602 u16_strcmp (const uint16_t *s1, const uint16_t *s2)
605 u32_strcmp (const uint32_t *s1, const uint32_t *s2)
608 /* Compare S1 and S2 using the collation rules of the current locale.
609 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
610 Upon failure, set errno and return any value. */
611 /* Similar to strcoll(), wcscoll(). */
613 u8_strcoll (const uint8_t *s1, const uint8_t *s2);
615 u16_strcoll (const uint16_t *s1, const uint16_t *s2);
617 u32_strcoll (const uint32_t *s1, const uint32_t *s2);
619 /* Compare no more than N units of S1 and S2. */
620 /* Similar to strncmp(), wcsncmp(). */
622 u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n)
625 u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n)
628 u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n)
631 /* Duplicate S, returning an identical malloc'd string. */
632 /* Similar to strdup(), wcsdup(). */
634 u8_strdup (const uint8_t *s);
636 u16_strdup (const uint16_t *s);
638 u32_strdup (const uint32_t *s);
640 /* Find the first occurrence of UC in STR. */
641 /* Similar to strchr(), wcschr(). */
643 u8_strchr (const uint8_t *str, ucs4_t uc)
646 u16_strchr (const uint16_t *str, ucs4_t uc)
649 u32_strchr (const uint32_t *str, ucs4_t uc)
652 /* Find the last occurrence of UC in STR. */
653 /* Similar to strrchr(), wcsrchr(). */
655 u8_strrchr (const uint8_t *str, ucs4_t uc)
658 u16_strrchr (const uint16_t *str, ucs4_t uc)
661 u32_strrchr (const uint32_t *str, ucs4_t uc)
664 /* Return the length of the initial segment of STR which consists entirely
665 of Unicode characters not in REJECT. */
666 /* Similar to strcspn(), wcscspn(). */
668 u8_strcspn (const uint8_t *str, const uint8_t *reject)
671 u16_strcspn (const uint16_t *str, const uint16_t *reject)
674 u32_strcspn (const uint32_t *str, const uint32_t *reject)
677 /* Return the length of the initial segment of STR which consists entirely
678 of Unicode characters in ACCEPT. */
679 /* Similar to strspn(), wcsspn(). */
681 u8_strspn (const uint8_t *str, const uint8_t *accept)
684 u16_strspn (const uint16_t *str, const uint16_t *accept)
687 u32_strspn (const uint32_t *str, const uint32_t *accept)
690 /* Find the first occurrence in STR of any character in ACCEPT. */
691 /* Similar to strpbrk(), wcspbrk(). */
693 u8_strpbrk (const uint8_t *str, const uint8_t *accept)
696 u16_strpbrk (const uint16_t *str, const uint16_t *accept)
699 u32_strpbrk (const uint32_t *str, const uint32_t *accept)
702 /* Find the first occurrence of NEEDLE in HAYSTACK. */
703 /* Similar to strstr(), wcsstr(). */
705 u8_strstr (const uint8_t *haystack, const uint8_t *needle)
708 u16_strstr (const uint16_t *haystack, const uint16_t *needle)
711 u32_strstr (const uint32_t *haystack, const uint32_t *needle)
714 /* Test whether STR starts with PREFIX. */
716 u8_startswith (const uint8_t *str, const uint8_t *prefix)
719 u16_startswith (const uint16_t *str, const uint16_t *prefix)
722 u32_startswith (const uint32_t *str, const uint32_t *prefix)
725 /* Test whether STR ends with SUFFIX. */
727 u8_endswith (const uint8_t *str, const uint8_t *suffix)
730 u16_endswith (const uint16_t *str, const uint16_t *suffix)
733 u32_endswith (const uint32_t *str, const uint32_t *suffix)
736 /* Divide STR into tokens separated by characters in DELIM.
737 This interface is actually more similar to wcstok than to strtok. */
738 /* Similar to strtok_r(), wcstok(). */
740 u8_strtok (uint8_t *str, const uint8_t *delim, uint8_t **ptr);
742 u16_strtok (uint16_t *str, const uint16_t *delim, uint16_t **ptr);
744 u32_strtok (uint32_t *str, const uint32_t *delim, uint32_t **ptr);
751 #endif /* _UNISTR_H */