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 Lesser 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 /* Get common macros for C. */
24 #include <unistring/cdefs.h>
26 /* Get inline if available. */
27 #include <unistring/inline.h>
30 #include <unistring/stdbool.h>
42 All functions prefixed with u8_ operate on UTF-8 encoded strings.
43 Their unit is an uint8_t (1 byte).
45 All functions prefixed with u16_ operate on UTF-16 encoded strings.
46 Their unit is an uint16_t (a 2-byte word).
48 All functions prefixed with u32_ operate on UCS-4 encoded strings.
49 Their unit is an uint32_t (a 4-byte word).
51 All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
54 All arguments starting with "str" and the arguments of functions starting
55 with u8_str/u16_str/u32_str denote a NUL terminated string, i.e. a string
56 which terminates at the first NUL unit. This termination unit is
57 considered part of the string for all memory allocation purposes, but
58 is not considered part of the string for all other logical purposes.
60 Functions returning a string result take a (resultbuf, lengthp) argument
61 pair. If resultbuf is not NULL and the result fits into *lengthp units,
62 it is put in resultbuf, and resultbuf is returned. Otherwise, a freshly
63 allocated string is returned. In both cases, *lengthp is set to the
64 length (number of units) of the returned string. In case of error,
65 NULL is returned and errno is set. */
68 /* Elementary string checks. */
70 /* Check whether an UTF-8 string is well-formed.
71 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
72 extern const uint8_t *
73 u8_check (const uint8_t *s, size_t n)
76 /* Check whether an UTF-16 string is well-formed.
77 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
78 extern const uint16_t *
79 u16_check (const uint16_t *s, size_t n)
82 /* Check whether an UCS-4 string is well-formed.
83 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
84 extern const uint32_t *
85 u32_check (const uint32_t *s, size_t n)
89 /* Elementary string conversions. */
91 /* Convert an UTF-8 string to an UTF-16 string. */
93 u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
96 /* Convert an UTF-8 string to an UCS-4 string. */
98 u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
101 /* Convert an UTF-16 string to an UTF-8 string. */
103 u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
106 /* Convert an UTF-16 string to an UCS-4 string. */
108 u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
111 /* Convert an UCS-4 string to an UTF-8 string. */
113 u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
116 /* Convert an UCS-4 string to an UTF-16 string. */
118 u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
122 /* Elementary string functions. */
124 /* Return the length (number of units) of the first character in S, which is
125 no longer than N. Return 0 if it is the NUL character. Return -1 upon
127 /* Similar to mblen(), except that s must not be NULL. */
129 u8_mblen (const uint8_t *s, size_t n)
132 u16_mblen (const uint16_t *s, size_t n)
135 u32_mblen (const uint32_t *s, size_t n)
138 /* Return the length (number of units) of the first character in S, putting
139 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
140 and an appropriate number of units is returned.
141 The number of available units, N, must be > 0. */
142 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
143 and the NUL character is not treated specially. */
144 /* The variants with _safe suffix are safe, even if the library is compiled
145 without --enable-safety. */
148 # if !UNISTRING_HAVE_INLINE
150 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
153 u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
155 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
165 return u8_mbtouc_unsafe_aux (puc, s, n);
171 # if !UNISTRING_HAVE_INLINE
173 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
176 u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
178 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
182 if (c < 0xd800 || c >= 0xe000)
188 return u16_mbtouc_unsafe_aux (puc, s, n);
194 # if !UNISTRING_HAVE_INLINE
196 u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
199 u32_mbtouc_unsafe (ucs4_t *puc,
200 const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
204 # if CONFIG_UNICODE_SAFETY
205 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
208 # if CONFIG_UNICODE_SAFETY
210 /* invalid multibyte character */
219 # if !UNISTRING_HAVE_INLINE
221 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
224 u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
226 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
236 return u8_mbtouc_aux (puc, s, n);
242 # if !UNISTRING_HAVE_INLINE
244 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
247 u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
249 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n)
253 if (c < 0xd800 || c >= 0xe000)
259 return u16_mbtouc_aux (puc, s, n);
265 # if !UNISTRING_HAVE_INLINE
267 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
270 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
274 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
277 /* invalid multibyte character */
284 /* Return the length (number of units) of the first character in S, putting
285 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
286 and -1 is returned for an invalid sequence of units, -2 is returned for an
287 incomplete sequence of units.
288 The number of available units, N, must be > 0. */
289 /* Similar to u*_mbtouc(), except that the return value gives more details
290 about the failure, similar to mbrtowc(). */
294 u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
299 u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
304 u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
307 /* Put the multibyte character represented by UC in S, returning its
308 length. Return -1 upon failure, -2 if the number of available units, N,
309 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
310 /* Similar to wctomb(), except that s must not be NULL, and the argument n
311 must be specified. */
314 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
316 u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n);
317 # if !UNISTRING_HAVE_INLINE
319 u8_uctomb (uint8_t *s, ucs4_t uc, int n);
322 u8_uctomb (uint8_t *s, ucs4_t uc, int n)
324 if (uc < 0x80 && n > 0)
330 return u8_uctomb_aux (s, uc, n);
336 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
338 u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n);
339 # if !UNISTRING_HAVE_INLINE
341 u16_uctomb (uint16_t *s, ucs4_t uc, int n);
344 u16_uctomb (uint16_t *s, ucs4_t uc, int n)
346 if (uc < 0xd800 && n > 0)
352 return u16_uctomb_aux (s, uc, n);
358 # if !UNISTRING_HAVE_INLINE
360 u32_uctomb (uint32_t *s, ucs4_t uc, int n);
363 u32_uctomb (uint32_t *s, ucs4_t uc, int n)
365 if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
381 /* Copy N units from SRC to DEST. */
382 /* Similar to memcpy(). */
384 u8_cpy (uint8_t *dest, const uint8_t *src, size_t n);
386 u16_cpy (uint16_t *dest, const uint16_t *src, size_t n);
388 u32_cpy (uint32_t *dest, const uint32_t *src, size_t n);
390 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
391 overlapping memory areas. */
392 /* Similar to memmove(). */
394 u8_move (uint8_t *dest, const uint8_t *src, size_t n);
396 u16_move (uint16_t *dest, const uint16_t *src, size_t n);
398 u32_move (uint32_t *dest, const uint32_t *src, size_t n);
400 /* Set the first N characters of S to UC. UC should be a character that
401 occupies only 1 unit. */
402 /* Similar to memset(). */
404 u8_set (uint8_t *s, ucs4_t uc, size_t n);
406 u16_set (uint16_t *s, ucs4_t uc, size_t n);
408 u32_set (uint32_t *s, ucs4_t uc, size_t n);
410 /* Compare S1 and S2, each of length N. */
411 /* Similar to memcmp(). */
413 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
416 u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n)
419 u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n)
422 /* Compare S1 and S2. */
423 /* Similar to the gnulib function memcmp2(). */
425 u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2)
428 u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2)
431 u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2)
434 /* Search the string at S for UC. */
435 /* Similar to memchr(). */
437 u8_chr (const uint8_t *s, size_t n, ucs4_t uc)
440 u16_chr (const uint16_t *s, size_t n, ucs4_t uc)
443 u32_chr (const uint32_t *s, size_t n, ucs4_t uc)
446 /* Count the number of Unicode characters in the N units from S. */
447 /* Similar to mbsnlen(). */
449 u8_mbsnlen (const uint8_t *s, size_t n)
452 u16_mbsnlen (const uint16_t *s, size_t n)
455 u32_mbsnlen (const uint32_t *s, size_t n)
458 /* Elementary string functions with memory allocation. */
460 /* Make a freshly allocated copy of S, of length N. */
462 u8_cpy_alloc (const uint8_t *s, size_t n);
464 u16_cpy_alloc (const uint16_t *s, size_t n);
466 u32_cpy_alloc (const uint32_t *s, size_t n);
468 /* Elementary string functions on NUL terminated strings. */
470 /* Return the length (number of units) of the first character in S.
471 Return 0 if it is the NUL character. Return -1 upon failure. */
473 u8_strmblen (const uint8_t *s)
476 u16_strmblen (const uint16_t *s)
479 u32_strmblen (const uint32_t *s)
482 /* Return the length (number of units) of the first character in S, putting
483 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
484 character. Return -1 upon failure. */
486 u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
488 u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
490 u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
492 /* Forward iteration step. Advances the pointer past the next character,
493 or returns NULL if the end of the string has been reached. Puts the
494 character's 'ucs4_t' representation in *PUC. */
495 extern const uint8_t *
496 u8_next (ucs4_t *puc, const uint8_t *s);
497 extern const uint16_t *
498 u16_next (ucs4_t *puc, const uint16_t *s);
499 extern const uint32_t *
500 u32_next (ucs4_t *puc, const uint32_t *s);
502 /* Backward iteration step. Advances the pointer to point to the previous
503 character, or returns NULL if the beginning of the string had been reached.
504 Puts the character's 'ucs4_t' representation in *PUC. */
505 extern const uint8_t *
506 u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
507 extern const uint16_t *
508 u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
509 extern const uint32_t *
510 u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
512 /* Return the number of units in S. */
513 /* Similar to strlen(), wcslen(). */
515 u8_strlen (const uint8_t *s)
518 u16_strlen (const uint16_t *s)
521 u32_strlen (const uint32_t *s)
524 /* Return the number of units in S, but at most MAXLEN. */
525 /* Similar to strnlen(), wcsnlen(). */
527 u8_strnlen (const uint8_t *s, size_t maxlen)
530 u16_strnlen (const uint16_t *s, size_t maxlen)
533 u32_strnlen (const uint32_t *s, size_t maxlen)
536 /* Copy SRC to DEST. */
537 /* Similar to strcpy(), wcscpy(). */
539 u8_strcpy (uint8_t *dest, const uint8_t *src);
541 u16_strcpy (uint16_t *dest, const uint16_t *src);
543 u32_strcpy (uint32_t *dest, const uint32_t *src);
545 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
546 /* Similar to stpcpy(). */
548 u8_stpcpy (uint8_t *dest, const uint8_t *src);
550 u16_stpcpy (uint16_t *dest, const uint16_t *src);
552 u32_stpcpy (uint32_t *dest, const uint32_t *src);
554 /* Copy no more than N units of SRC to DEST. */
555 /* Similar to strncpy(), wcsncpy(). */
557 u8_strncpy (uint8_t *dest, const uint8_t *src, size_t n);
559 u16_strncpy (uint16_t *dest, const uint16_t *src, size_t n);
561 u32_strncpy (uint32_t *dest, const uint32_t *src, size_t n);
563 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
564 non-NUL unit written into DEST. */
565 /* Similar to stpncpy(). */
567 u8_stpncpy (uint8_t *dest, const uint8_t *src, size_t n);
569 u16_stpncpy (uint16_t *dest, const uint16_t *src, size_t n);
571 u32_stpncpy (uint32_t *dest, const uint32_t *src, size_t n);
573 /* Append SRC onto DEST. */
574 /* Similar to strcat(), wcscat(). */
576 u8_strcat (uint8_t *dest, const uint8_t *src);
578 u16_strcat (uint16_t *dest, const uint16_t *src);
580 u32_strcat (uint32_t *dest, const uint32_t *src);
582 /* Append no more than N units of SRC onto DEST. */
583 /* Similar to strncat(), wcsncat(). */
585 u8_strncat (uint8_t *dest, const uint8_t *src, size_t n);
587 u16_strncat (uint16_t *dest, const uint16_t *src, size_t n);
589 u32_strncat (uint32_t *dest, const uint32_t *src, size_t n);
591 /* Compare S1 and S2. */
592 /* Similar to strcmp(), wcscmp(). */
594 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
596 u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2)
598 # define u8_strcmp u8_strcmp_gnu
601 u8_strcmp (const uint8_t *s1, const uint8_t *s2)
605 u16_strcmp (const uint16_t *s1, const uint16_t *s2)
608 u32_strcmp (const uint32_t *s1, const uint32_t *s2)
611 /* Compare S1 and S2 using the collation rules of the current locale.
612 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
613 Upon failure, set errno and return any value. */
614 /* Similar to strcoll(), wcscoll(). */
616 u8_strcoll (const uint8_t *s1, const uint8_t *s2);
618 u16_strcoll (const uint16_t *s1, const uint16_t *s2);
620 u32_strcoll (const uint32_t *s1, const uint32_t *s2);
622 /* Compare no more than N units of S1 and S2. */
623 /* Similar to strncmp(), wcsncmp(). */
625 u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n)
628 u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n)
631 u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n)
634 /* Duplicate S, returning an identical malloc'd string. */
635 /* Similar to strdup(), wcsdup(). */
637 u8_strdup (const uint8_t *s);
639 u16_strdup (const uint16_t *s);
641 u32_strdup (const uint32_t *s);
643 /* Find the first occurrence of UC in STR. */
644 /* Similar to strchr(), wcschr(). */
646 u8_strchr (const uint8_t *str, ucs4_t uc)
649 u16_strchr (const uint16_t *str, ucs4_t uc)
652 u32_strchr (const uint32_t *str, ucs4_t uc)
655 /* Find the last occurrence of UC in STR. */
656 /* Similar to strrchr(), wcsrchr(). */
658 u8_strrchr (const uint8_t *str, ucs4_t uc)
661 u16_strrchr (const uint16_t *str, ucs4_t uc)
664 u32_strrchr (const uint32_t *str, ucs4_t uc)
667 /* Return the length of the initial segment of STR which consists entirely
668 of Unicode characters not in REJECT. */
669 /* Similar to strcspn(), wcscspn(). */
671 u8_strcspn (const uint8_t *str, const uint8_t *reject)
674 u16_strcspn (const uint16_t *str, const uint16_t *reject)
677 u32_strcspn (const uint32_t *str, const uint32_t *reject)
680 /* Return the length of the initial segment of STR which consists entirely
681 of Unicode characters in ACCEPT. */
682 /* Similar to strspn(), wcsspn(). */
684 u8_strspn (const uint8_t *str, const uint8_t *accept)
687 u16_strspn (const uint16_t *str, const uint16_t *accept)
690 u32_strspn (const uint32_t *str, const uint32_t *accept)
693 /* Find the first occurrence in STR of any character in ACCEPT. */
694 /* Similar to strpbrk(), wcspbrk(). */
696 u8_strpbrk (const uint8_t *str, const uint8_t *accept)
699 u16_strpbrk (const uint16_t *str, const uint16_t *accept)
702 u32_strpbrk (const uint32_t *str, const uint32_t *accept)
705 /* Find the first occurrence of NEEDLE in HAYSTACK. */
706 /* Similar to strstr(), wcsstr(). */
708 u8_strstr (const uint8_t *haystack, const uint8_t *needle)
711 u16_strstr (const uint16_t *haystack, const uint16_t *needle)
714 u32_strstr (const uint32_t *haystack, const uint32_t *needle)
717 /* Test whether STR starts with PREFIX. */
719 u8_startswith (const uint8_t *str, const uint8_t *prefix)
722 u16_startswith (const uint16_t *str, const uint16_t *prefix)
725 u32_startswith (const uint32_t *str, const uint32_t *prefix)
728 /* Test whether STR ends with SUFFIX. */
730 u8_endswith (const uint8_t *str, const uint8_t *suffix)
733 u16_endswith (const uint16_t *str, const uint16_t *suffix)
736 u32_endswith (const uint32_t *str, const uint32_t *suffix)
739 /* Divide STR into tokens separated by characters in DELIM.
740 This interface is actually more similar to wcstok than to strtok. */
741 /* Similar to strtok_r(), wcstok(). */
743 u8_strtok (uint8_t *str, const uint8_t *delim, uint8_t **ptr);
745 u16_strtok (uint16_t *str, const uint16_t *delim, uint16_t **ptr);
747 u32_strtok (uint32_t *str, const uint32_t *delim, uint32_t **ptr);
754 #endif /* _UNISTR_H */