1 /* Elementary Unicode string functions.
2 Copyright (C) 2001-2002, 2005-2025 Free Software Foundation, Inc.
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 This file is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
25 /* Get size_t, ptrdiff_t. */
38 All functions prefixed with u8_ operate on UTF-8 encoded strings.
39 Their unit is an uint8_t (1 byte).
41 All functions prefixed with u16_ operate on UTF-16 encoded strings.
42 Their unit is an uint16_t (a 2-byte word).
44 All functions prefixed with u32_ operate on UCS-4 encoded strings.
45 Their unit is an uint32_t (a 4-byte word).
47 All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
50 All arguments starting with "str" and the arguments of functions starting
51 with u8_str/u16_str/u32_str denote a NUL terminated string, i.e. a string
52 which terminates at the first NUL unit. This termination unit is
53 considered part of the string for all memory allocation purposes, but
54 is not considered part of the string for all other logical purposes.
56 Functions returning a string result take a (resultbuf, lengthp) argument
57 pair. If resultbuf is not NULL and the result fits into *lengthp units,
58 it is put in resultbuf, and resultbuf is returned. Otherwise, a freshly
59 allocated string is returned. In both cases, *lengthp is set to the
60 length (number of units) of the returned string. In case of error,
61 NULL is returned and errno is set. */
64 /* Elementary string checks. */
66 /* Check whether an UTF-8 string is well-formed.
67 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
68 extern const uint8_t *
69 u8_check (const uint8_t *s
, size_t n
)
72 /* Check whether an UTF-16 string is well-formed.
73 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
74 extern const uint16_t *
75 u16_check (const uint16_t *s
, size_t n
)
78 /* Check whether an UCS-4 string is well-formed.
79 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
80 extern const uint32_t *
81 u32_check (const uint32_t *s
, size_t n
)
85 /* Elementary string conversions. */
87 /* Convert an UTF-8 string to an UTF-16 string. */
89 u8_to_u16 (const uint8_t *s
, size_t n
, uint16_t *resultbuf
,
92 /* Convert an UTF-8 string to an UCS-4 string. */
94 u8_to_u32 (const uint8_t *s
, size_t n
, uint32_t *resultbuf
,
97 /* Convert an UTF-16 string to an UTF-8 string. */
99 u16_to_u8 (const uint16_t *s
, size_t n
, uint8_t *resultbuf
,
102 /* Convert an UTF-16 string to an UCS-4 string. */
104 u16_to_u32 (const uint16_t *s
, size_t n
, uint32_t *resultbuf
,
107 /* Convert an UCS-4 string to an UTF-8 string. */
109 u32_to_u8 (const uint32_t *s
, size_t n
, uint8_t *resultbuf
,
112 /* Convert an UCS-4 string to an UTF-16 string. */
114 u32_to_u16 (const uint32_t *s
, size_t n
, uint16_t *resultbuf
,
118 /* Elementary string functions. */
120 /* Return the length (number of units) of the first character in S, which is
121 no longer than N. Return 0 if it is the NUL character. Return -1 upon
123 /* Similar to mblen(), except that s must not be NULL. */
125 u8_mblen (const uint8_t *s
, size_t n
)
128 u16_mblen (const uint16_t *s
, size_t n
)
131 u32_mblen (const uint32_t *s
, size_t n
)
134 /* Return the length (number of units) of the first character in S, putting
135 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
136 and an appropriate number of units is returned.
137 The number of available units, N, must be > 0. */
138 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
139 and the NUL character is not treated specially. */
140 /* The variants with _unsafe suffix are for backward compatibility with
141 libunistring versions < 0.9.7. */
143 #if GNULIB_UNISTR_U8_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
146 u8_mbtouc_unsafe (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
149 u8_mbtouc_unsafe_aux (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
151 u8_mbtouc_unsafe (ucs4_t
*puc
, const uint8_t *s
, size_t n
)
161 return u8_mbtouc_unsafe_aux (puc
, s
, n
);
166 #if GNULIB_UNISTR_U16_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
169 u16_mbtouc_unsafe (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
172 u16_mbtouc_unsafe_aux (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
174 u16_mbtouc_unsafe (ucs4_t
*puc
, const uint16_t *s
, size_t n
)
178 if (c
< 0xd800 || c
>= 0xe000)
184 return u16_mbtouc_unsafe_aux (puc
, s
, n
);
189 #if GNULIB_UNISTR_U32_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
192 u32_mbtouc_unsafe (ucs4_t
*puc
, const uint32_t *s
, size_t n
);
195 u32_mbtouc_unsafe (ucs4_t
*puc
,
196 const uint32_t *s
, _GL_ATTRIBUTE_MAYBE_UNUSED
size_t n
)
200 if (c
< 0xd800 || (c
>= 0xe000 && c
< 0x110000))
203 /* invalid multibyte character */
210 #if GNULIB_UNISTR_U8_MBTOUC || HAVE_LIBUNISTRING
213 u8_mbtouc (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
216 u8_mbtouc_aux (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
218 u8_mbtouc (ucs4_t
*puc
, const uint8_t *s
, size_t n
)
228 return u8_mbtouc_aux (puc
, s
, n
);
233 #if GNULIB_UNISTR_U16_MBTOUC || HAVE_LIBUNISTRING
236 u16_mbtouc (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
239 u16_mbtouc_aux (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
241 u16_mbtouc (ucs4_t
*puc
, const uint16_t *s
, size_t n
)
245 if (c
< 0xd800 || c
>= 0xe000)
251 return u16_mbtouc_aux (puc
, s
, n
);
256 #if GNULIB_UNISTR_U32_MBTOUC || HAVE_LIBUNISTRING
259 u32_mbtouc (ucs4_t
*puc
, const uint32_t *s
, size_t n
);
262 u32_mbtouc (ucs4_t
*puc
, const uint32_t *s
,
263 _GL_ATTRIBUTE_MAYBE_UNUSED
size_t n
)
267 if (c
< 0xd800 || (c
>= 0xe000 && c
< 0x110000))
270 /* invalid multibyte character */
277 /* Return the length (number of units) of the first character in S, putting
278 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
279 and -1 is returned for an invalid sequence of units, -2 is returned for an
280 incomplete sequence of units.
281 The number of available units, N, must be > 0. */
282 /* Similar to u*_mbtouc(), except that the return value gives more details
283 about the failure, similar to mbrtowc(). */
285 #if GNULIB_UNISTR_U8_MBTOUCR || HAVE_LIBUNISTRING
287 u8_mbtoucr (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
290 #if GNULIB_UNISTR_U16_MBTOUCR || HAVE_LIBUNISTRING
292 u16_mbtoucr (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
295 #if GNULIB_UNISTR_U32_MBTOUCR || HAVE_LIBUNISTRING
297 u32_mbtoucr (ucs4_t
*puc
, const uint32_t *s
, size_t n
);
300 /* Put the multibyte character represented by UC in S, returning its
301 length. Return -1 upon failure, -2 if the number of available units, N,
302 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
303 /* Similar to wctomb(), except that s must not be NULL, and the argument n
304 must be specified. */
306 #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING
307 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
309 u8_uctomb_aux (uint8_t *s
, ucs4_t uc
, ptrdiff_t n
);
312 u8_uctomb (uint8_t *s
, ucs4_t uc
, ptrdiff_t n
);
315 u8_uctomb (uint8_t *s
, ucs4_t uc
, ptrdiff_t n
)
317 if (uc
< 0x80 && n
> 0)
323 return u8_uctomb_aux (s
, uc
, n
);
328 #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING
329 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
331 u16_uctomb_aux (uint16_t *s
, ucs4_t uc
, ptrdiff_t n
);
334 u16_uctomb (uint16_t *s
, ucs4_t uc
, ptrdiff_t n
);
337 u16_uctomb (uint16_t *s
, ucs4_t uc
, ptrdiff_t n
)
339 if (uc
< 0xd800 && n
> 0)
345 return u16_uctomb_aux (s
, uc
, n
);
350 #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING
353 u32_uctomb (uint32_t *s
, ucs4_t uc
, ptrdiff_t n
);
356 u32_uctomb (uint32_t *s
, ucs4_t uc
, ptrdiff_t n
)
358 if (uc
< 0xd800 || (uc
>= 0xe000 && uc
< 0x110000))
374 /* Copy N units from SRC to DEST. */
375 /* Similar to memcpy(). */
377 u8_cpy (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
, size_t n
);
379 u16_cpy (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
, size_t n
);
381 u32_cpy (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
, size_t n
);
383 /* Copy N units from SRC to DEST, returning pointer after last written unit. */
384 /* Similar to mempcpy(). */
386 u8_pcpy (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
, size_t n
);
388 u16_pcpy (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
, size_t n
);
390 u32_pcpy (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
, size_t n
);
392 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
393 overlapping memory areas. */
394 /* Similar to memmove(). */
396 u8_move (uint8_t *dest
, const uint8_t *src
, size_t n
);
398 u16_move (uint16_t *dest
, const uint16_t *src
, size_t n
);
400 u32_move (uint32_t *dest
, const uint32_t *src
, size_t n
);
402 /* Set the first N characters of S to UC. UC should be a character that
403 occupies only 1 unit. */
404 /* Similar to memset(). */
406 u8_set (uint8_t *s
, ucs4_t uc
, size_t n
);
408 u16_set (uint16_t *s
, ucs4_t uc
, size_t n
);
410 u32_set (uint32_t *s
, ucs4_t uc
, size_t n
);
412 /* Compare S1 and S2, each of length N. */
413 /* Similar to memcmp(). */
415 u8_cmp (const uint8_t *s1
, const uint8_t *s2
, size_t n
)
418 u16_cmp (const uint16_t *s1
, const uint16_t *s2
, size_t n
)
421 u32_cmp (const uint32_t *s1
, const uint32_t *s2
, size_t n
)
424 /* Compare S1 and S2. */
425 /* Similar to the gnulib function memcmp2(). */
427 u8_cmp2 (const uint8_t *s1
, size_t n1
, const uint8_t *s2
, size_t n2
)
430 u16_cmp2 (const uint16_t *s1
, size_t n1
, const uint16_t *s2
, size_t n2
)
433 u32_cmp2 (const uint32_t *s1
, size_t n1
, const uint32_t *s2
, size_t n2
)
436 /* Search the string at S for UC. */
437 /* Similar to memchr(). */
439 u8_chr (const uint8_t *s
, size_t n
, ucs4_t uc
)
442 u16_chr (const uint16_t *s
, size_t n
, ucs4_t uc
)
445 u32_chr (const uint32_t *s
, size_t n
, ucs4_t uc
)
448 /* Count the number of Unicode characters in the N units from S. */
449 /* Similar to mbsnlen(). */
451 u8_mbsnlen (const uint8_t *s
, size_t n
)
454 u16_mbsnlen (const uint16_t *s
, size_t n
)
457 u32_mbsnlen (const uint32_t *s
, size_t n
)
460 /* Elementary string functions with memory allocation. */
462 /* Make a freshly allocated copy of S, of length N. */
464 u8_cpy_alloc (const uint8_t *s
, size_t n
);
466 u16_cpy_alloc (const uint16_t *s
, size_t n
);
468 u32_cpy_alloc (const uint32_t *s
, size_t n
);
470 /* Elementary string functions on NUL terminated strings. */
472 /* Return the length (number of units) of the first character in S.
473 Return 0 if it is the NUL character. Return -1 upon failure. */
475 u8_strmblen (const uint8_t *s
)
478 u16_strmblen (const uint16_t *s
)
481 u32_strmblen (const uint32_t *s
)
484 /* Return the length (number of units) of the first character in S, putting
485 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
486 character. Return -1 upon failure. */
488 u8_strmbtouc (ucs4_t
*puc
, const uint8_t *s
);
490 u16_strmbtouc (ucs4_t
*puc
, const uint16_t *s
);
492 u32_strmbtouc (ucs4_t
*puc
, const uint32_t *s
);
494 /* Forward iteration step. Advances the pointer past the next character,
495 or returns NULL if the end of the string has been reached. Puts the
496 character's 'ucs4_t' representation in *PUC. */
497 extern const uint8_t *
498 u8_next (ucs4_t
*puc
, const uint8_t *s
);
499 extern const uint16_t *
500 u16_next (ucs4_t
*puc
, const uint16_t *s
);
501 extern const uint32_t *
502 u32_next (ucs4_t
*puc
, const uint32_t *s
);
504 /* Backward iteration step. Advances the pointer to point to the previous
505 character, or returns NULL if the beginning of the string had been reached.
506 Puts the character's 'ucs4_t' representation in *PUC. */
507 extern const uint8_t *
508 u8_prev (ucs4_t
*puc
, const uint8_t *s
, const uint8_t *start
);
509 extern const uint16_t *
510 u16_prev (ucs4_t
*puc
, const uint16_t *s
, const uint16_t *start
);
511 extern const uint32_t *
512 u32_prev (ucs4_t
*puc
, const uint32_t *s
, const uint32_t *start
);
514 /* Return the number of units in S. */
515 /* Similar to strlen(), wcslen(). */
517 u8_strlen (const uint8_t *s
)
520 u16_strlen (const uint16_t *s
)
523 u32_strlen (const uint32_t *s
)
526 /* Return the number of units in S, but at most MAXLEN. */
527 /* Similar to strnlen(), wcsnlen(). */
529 u8_strnlen (const uint8_t *s
, size_t maxlen
)
532 u16_strnlen (const uint16_t *s
, size_t maxlen
)
535 u32_strnlen (const uint32_t *s
, size_t maxlen
)
538 /* Copy SRC to DEST. */
539 /* Similar to strcpy(), wcscpy(). */
541 u8_strcpy (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
);
543 u16_strcpy (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
);
545 u32_strcpy (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
);
547 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
548 /* Similar to stpcpy(). */
550 u8_stpcpy (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
);
552 u16_stpcpy (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
);
554 u32_stpcpy (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
);
556 /* Copy no more than N units of SRC to DEST. */
557 /* Similar to strncpy(), wcsncpy(). */
559 u8_strncpy (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
, size_t n
);
561 u16_strncpy (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
, size_t n
);
563 u32_strncpy (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
, size_t n
);
565 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
566 non-NUL unit written into DEST. */
567 /* Similar to stpncpy(). */
569 u8_stpncpy (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
, size_t n
);
571 u16_stpncpy (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
, size_t n
);
573 u32_stpncpy (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
, size_t n
);
575 /* Append SRC onto DEST. */
576 /* Similar to strcat(), wcscat(). */
578 u8_strcat (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
);
580 u16_strcat (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
);
582 u32_strcat (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
);
584 /* Append no more than N units of SRC onto DEST. */
585 /* Similar to strncat(), wcsncat(). */
587 u8_strncat (uint8_t *_UC_RESTRICT dest
, const uint8_t *src
, size_t n
);
589 u16_strncat (uint16_t *_UC_RESTRICT dest
, const uint16_t *src
, size_t n
);
591 u32_strncat (uint32_t *_UC_RESTRICT dest
, const uint32_t *src
, size_t n
);
593 /* Compare S1 and S2. */
594 /* Similar to strcmp(), wcscmp(). */
596 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
598 u8_strcmp_gnu (const uint8_t *s1
, const uint8_t *s2
)
600 # define u8_strcmp u8_strcmp_gnu
603 u8_strcmp (const uint8_t *s1
, const uint8_t *s2
)
607 u16_strcmp (const uint16_t *s1
, const uint16_t *s2
)
610 u32_strcmp (const uint32_t *s1
, const uint32_t *s2
)
613 /* Compare S1 and S2 using the collation rules of the current locale.
614 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
615 Upon failure, set errno and return any value. */
616 /* Similar to strcoll(), wcscoll(). */
618 u8_strcoll (const uint8_t *s1
, const uint8_t *s2
);
620 u16_strcoll (const uint16_t *s1
, const uint16_t *s2
);
622 u32_strcoll (const uint32_t *s1
, const uint32_t *s2
);
624 /* Compare no more than N units of S1 and S2. */
625 /* Similar to strncmp(), wcsncmp(). */
627 u8_strncmp (const uint8_t *s1
, const uint8_t *s2
, size_t n
)
630 u16_strncmp (const uint16_t *s1
, const uint16_t *s2
, size_t n
)
633 u32_strncmp (const uint32_t *s1
, const uint32_t *s2
, size_t n
)
636 /* Duplicate S, returning an identical malloc'd string. */
637 /* Similar to strdup(), wcsdup(). */
639 u8_strdup (const uint8_t *s
)
640 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
;
642 u16_strdup (const uint16_t *s
)
643 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
;
645 u32_strdup (const uint32_t *s
)
646 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
;
648 /* Find the first occurrence of UC in STR. */
649 /* Similar to strchr(), wcschr(). */
651 u8_strchr (const uint8_t *str
, ucs4_t uc
)
654 u16_strchr (const uint16_t *str
, ucs4_t uc
)
657 u32_strchr (const uint32_t *str
, ucs4_t uc
)
660 /* Find the last occurrence of UC in STR. */
661 /* Similar to strrchr(), wcsrchr(). */
663 u8_strrchr (const uint8_t *str
, ucs4_t uc
)
666 u16_strrchr (const uint16_t *str
, ucs4_t uc
)
669 u32_strrchr (const uint32_t *str
, ucs4_t uc
)
672 /* Return the length of the initial segment of STR which consists entirely
673 of Unicode characters not in REJECT. */
674 /* Similar to strcspn(), wcscspn(). */
676 u8_strcspn (const uint8_t *str
, const uint8_t *reject
)
679 u16_strcspn (const uint16_t *str
, const uint16_t *reject
)
682 u32_strcspn (const uint32_t *str
, const uint32_t *reject
)
685 /* Return the length of the initial segment of STR which consists entirely
686 of Unicode characters in ACCEPT. */
687 /* Similar to strspn(), wcsspn(). */
689 u8_strspn (const uint8_t *str
, const uint8_t *accept
)
692 u16_strspn (const uint16_t *str
, const uint16_t *accept
)
695 u32_strspn (const uint32_t *str
, const uint32_t *accept
)
698 /* Find the first occurrence in STR of any character in ACCEPT. */
699 /* Similar to strpbrk(), wcspbrk(). */
701 u8_strpbrk (const uint8_t *str
, const uint8_t *accept
)
704 u16_strpbrk (const uint16_t *str
, const uint16_t *accept
)
707 u32_strpbrk (const uint32_t *str
, const uint32_t *accept
)
710 /* Find the first occurrence of NEEDLE in HAYSTACK. */
711 /* Similar to strstr(), wcsstr(). */
713 u8_strstr (const uint8_t *haystack
, const uint8_t *needle
)
716 u16_strstr (const uint16_t *haystack
, const uint16_t *needle
)
719 u32_strstr (const uint32_t *haystack
, const uint32_t *needle
)
722 /* Test whether STR starts with PREFIX. */
724 u8_startswith (const uint8_t *str
, const uint8_t *prefix
)
727 u16_startswith (const uint16_t *str
, const uint16_t *prefix
)
730 u32_startswith (const uint32_t *str
, const uint32_t *prefix
)
733 /* Test whether STR ends with SUFFIX. */
735 u8_endswith (const uint8_t *str
, const uint8_t *suffix
)
738 u16_endswith (const uint16_t *str
, const uint16_t *suffix
)
741 u32_endswith (const uint32_t *str
, const uint32_t *suffix
)
744 /* Divide STR into tokens separated by characters in DELIM.
745 This interface is actually more similar to wcstok than to strtok. */
746 /* Similar to strtok_r(), wcstok(). */
748 u8_strtok (uint8_t *_UC_RESTRICT str
, const uint8_t *delim
,
751 u16_strtok (uint16_t *_UC_RESTRICT str
, const uint16_t *delim
,
754 u32_strtok (uint32_t *_UC_RESTRICT str
, const uint32_t *delim
,
762 #endif /* _UNISTR_H */