1 /* Elementary Unicode string functions.
2 Copyright (C) 2001-2002, 2005-2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as published
6 by the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program 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 GNU
12 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 <http://www.gnu.org/licenses/>. */
22 /* Get common macros for C. */
23 #include <unistring/cdefs.h>
25 /* Get inline if available. */
26 #include <unistring/inline.h>
29 #include <unistring/stdbool.h>
41 All functions prefixed with u8_ operate on UTF-8 encoded strings.
42 Their unit is an uint8_t (1 byte).
44 All functions prefixed with u16_ operate on UTF-16 encoded strings.
45 Their unit is an uint16_t (a 2-byte word).
47 All functions prefixed with u32_ operate on UCS-4 encoded strings.
48 Their unit is an uint32_t (a 4-byte word).
50 All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
53 All arguments starting with "str" and the arguments of functions starting
54 with u8_str/u16_str/u32_str denote a NUL terminated string, i.e. a string
55 which terminates at the first NUL unit. This termination unit is
56 considered part of the string for all memory allocation purposes, but
57 is not considered part of the string for all other logical purposes.
59 Functions returning a string result take a (resultbuf, lengthp) argument
60 pair. If resultbuf is not NULL and the result fits into *lengthp units,
61 it is put in resultbuf, and resultbuf is returned. Otherwise, a freshly
62 allocated string is returned. In both cases, *lengthp is set to the
63 length (number of units) of the returned string. In case of error,
64 NULL is returned and errno is set. */
67 /* Elementary string checks. */
69 /* Check whether an UTF-8 string is well-formed.
70 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
71 extern const uint8_t *
72 u8_check (const uint8_t *s, size_t n)
75 /* Check whether an UTF-16 string is well-formed.
76 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
77 extern const uint16_t *
78 u16_check (const uint16_t *s, size_t n)
81 /* Check whether an UCS-4 string is well-formed.
82 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
83 extern const uint32_t *
84 u32_check (const uint32_t *s, size_t n)
88 /* Elementary string conversions. */
90 /* Convert an UTF-8 string to an UTF-16 string. */
92 u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
95 /* Convert an UTF-8 string to an UCS-4 string. */
97 u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
100 /* Convert an UTF-16 string to an UTF-8 string. */
102 u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
105 /* Convert an UTF-16 string to an UCS-4 string. */
107 u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
110 /* Convert an UCS-4 string to an UTF-8 string. */
112 u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
115 /* Convert an UCS-4 string to an UTF-16 string. */
117 u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
121 /* Elementary string functions. */
123 /* Return the length (number of units) of the first character in S, which is
124 no longer than N. Return 0 if it is the NUL character. Return -1 upon
126 /* Similar to mblen(), except that s must not be NULL. */
128 u8_mblen (const uint8_t *s, size_t n)
131 u16_mblen (const uint16_t *s, size_t n)
134 u32_mblen (const uint32_t *s, size_t n)
137 /* Return the length (number of units) of the first character in S, putting
138 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
139 and an appropriate number of units is returned.
140 The number of available units, N, must be > 0. */
141 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
142 and the NUL character is not treated specially. */
143 /* The variants with _safe suffix are safe, even if the library is compiled
144 without --enable-safety. */
147 # if !UNISTRING_HAVE_INLINE
149 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
152 u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
154 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
164 return u8_mbtouc_unsafe_aux (puc, s, n);
170 # if !UNISTRING_HAVE_INLINE
172 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
175 u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
177 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
181 if (c < 0xd800 || c >= 0xe000)
187 return u16_mbtouc_unsafe_aux (puc, s, n);
193 # if !UNISTRING_HAVE_INLINE
195 u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
198 u32_mbtouc_unsafe (ucs4_t *puc,
199 const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
203 # if CONFIG_UNICODE_SAFETY
204 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
207 # if CONFIG_UNICODE_SAFETY
209 /* invalid multibyte character */
218 # if !UNISTRING_HAVE_INLINE
220 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
223 u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
225 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
235 return u8_mbtouc_aux (puc, s, n);
241 # if !UNISTRING_HAVE_INLINE
243 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
246 u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
248 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n)
252 if (c < 0xd800 || c >= 0xe000)
258 return u16_mbtouc_aux (puc, s, n);
264 # if !UNISTRING_HAVE_INLINE
266 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
269 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
273 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
276 /* invalid multibyte character */
283 /* Return the length (number of units) of the first character in S, putting
284 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
285 and -1 is returned for an invalid sequence of units, -2 is returned for an
286 incomplete sequence of units.
287 The number of available units, N, must be > 0. */
288 /* Similar to u*_mbtouc(), except that the return value gives more details
289 about the failure, similar to mbrtowc(). */
293 u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
298 u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
303 u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
306 /* Put the multibyte character represented by UC in S, returning its
307 length. Return -1 upon failure, -2 if the number of available units, N,
308 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
309 /* Similar to wctomb(), except that s must not be NULL, and the argument n
310 must be specified. */
313 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
315 u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n);
316 # if !UNISTRING_HAVE_INLINE
318 u8_uctomb (uint8_t *s, ucs4_t uc, int n);
321 u8_uctomb (uint8_t *s, ucs4_t uc, int n)
323 if (uc < 0x80 && n > 0)
329 return u8_uctomb_aux (s, uc, n);
335 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
337 u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n);
338 # if !UNISTRING_HAVE_INLINE
340 u16_uctomb (uint16_t *s, ucs4_t uc, int n);
343 u16_uctomb (uint16_t *s, ucs4_t uc, int n)
345 if (uc < 0xd800 && n > 0)
351 return u16_uctomb_aux (s, uc, n);
357 # if !UNISTRING_HAVE_INLINE
359 u32_uctomb (uint32_t *s, ucs4_t uc, int n);
362 u32_uctomb (uint32_t *s, ucs4_t uc, int n)
364 if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
380 /* Copy N units from SRC to DEST. */
381 /* Similar to memcpy(). */
383 u8_cpy (uint8_t *dest, const uint8_t *src, size_t n);
385 u16_cpy (uint16_t *dest, const uint16_t *src, size_t n);
387 u32_cpy (uint32_t *dest, const uint32_t *src, size_t n);
389 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
390 overlapping memory areas. */
391 /* Similar to memmove(). */
393 u8_move (uint8_t *dest, const uint8_t *src, size_t n);
395 u16_move (uint16_t *dest, const uint16_t *src, size_t n);
397 u32_move (uint32_t *dest, const uint32_t *src, size_t n);
399 /* Set the first N characters of S to UC. UC should be a character that
400 occupies only 1 unit. */
401 /* Similar to memset(). */
403 u8_set (uint8_t *s, ucs4_t uc, size_t n);
405 u16_set (uint16_t *s, ucs4_t uc, size_t n);
407 u32_set (uint32_t *s, ucs4_t uc, size_t n);
409 /* Compare S1 and S2, each of length N. */
410 /* Similar to memcmp(). */
412 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
415 u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n)
418 u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n)
421 /* Compare S1 and S2. */
422 /* Similar to the gnulib function memcmp2(). */
424 u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2)
427 u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2)
430 u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2)
433 /* Search the string at S for UC. */
434 /* Similar to memchr(). */
436 u8_chr (const uint8_t *s, size_t n, ucs4_t uc)
439 u16_chr (const uint16_t *s, size_t n, ucs4_t uc)
442 u32_chr (const uint32_t *s, size_t n, ucs4_t uc)
445 /* Count the number of Unicode characters in the N units from S. */
446 /* Similar to mbsnlen(). */
448 u8_mbsnlen (const uint8_t *s, size_t n)
451 u16_mbsnlen (const uint16_t *s, size_t n)
454 u32_mbsnlen (const uint32_t *s, size_t n)
457 /* Elementary string functions with memory allocation. */
459 /* Make a freshly allocated copy of S, of length N. */
461 u8_cpy_alloc (const uint8_t *s, size_t n);
463 u16_cpy_alloc (const uint16_t *s, size_t n);
465 u32_cpy_alloc (const uint32_t *s, size_t n);
467 /* Elementary string functions on NUL terminated strings. */
469 /* Return the length (number of units) of the first character in S.
470 Return 0 if it is the NUL character. Return -1 upon failure. */
472 u8_strmblen (const uint8_t *s)
475 u16_strmblen (const uint16_t *s)
478 u32_strmblen (const uint32_t *s)
481 /* Return the length (number of units) of the first character in S, putting
482 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
483 character. Return -1 upon failure. */
485 u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
487 u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
489 u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
491 /* Forward iteration step. Advances the pointer past the next character,
492 or returns NULL if the end of the string has been reached. Puts the
493 character's 'ucs4_t' representation in *PUC. */
494 extern const uint8_t *
495 u8_next (ucs4_t *puc, const uint8_t *s);
496 extern const uint16_t *
497 u16_next (ucs4_t *puc, const uint16_t *s);
498 extern const uint32_t *
499 u32_next (ucs4_t *puc, const uint32_t *s);
501 /* Backward iteration step. Advances the pointer to point to the previous
502 character, or returns NULL if the beginning of the string had been reached.
503 Puts the character's 'ucs4_t' representation in *PUC. */
504 extern const uint8_t *
505 u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
506 extern const uint16_t *
507 u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
508 extern const uint32_t *
509 u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
511 /* Return the number of units in S. */
512 /* Similar to strlen(), wcslen(). */
514 u8_strlen (const uint8_t *s)
517 u16_strlen (const uint16_t *s)
520 u32_strlen (const uint32_t *s)
523 /* Return the number of units in S, but at most MAXLEN. */
524 /* Similar to strnlen(), wcsnlen(). */
526 u8_strnlen (const uint8_t *s, size_t maxlen)
529 u16_strnlen (const uint16_t *s, size_t maxlen)
532 u32_strnlen (const uint32_t *s, size_t maxlen)
535 /* Copy SRC to DEST. */
536 /* Similar to strcpy(), wcscpy(). */
538 u8_strcpy (uint8_t *dest, const uint8_t *src);
540 u16_strcpy (uint16_t *dest, const uint16_t *src);
542 u32_strcpy (uint32_t *dest, const uint32_t *src);
544 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
545 /* Similar to stpcpy(). */
547 u8_stpcpy (uint8_t *dest, const uint8_t *src);
549 u16_stpcpy (uint16_t *dest, const uint16_t *src);
551 u32_stpcpy (uint32_t *dest, const uint32_t *src);
553 /* Copy no more than N units of SRC to DEST. */
554 /* Similar to strncpy(), wcsncpy(). */
556 u8_strncpy (uint8_t *dest, const uint8_t *src, size_t n);
558 u16_strncpy (uint16_t *dest, const uint16_t *src, size_t n);
560 u32_strncpy (uint32_t *dest, const uint32_t *src, size_t n);
562 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
563 non-NUL unit written into DEST. */
564 /* Similar to stpncpy(). */
566 u8_stpncpy (uint8_t *dest, const uint8_t *src, size_t n);
568 u16_stpncpy (uint16_t *dest, const uint16_t *src, size_t n);
570 u32_stpncpy (uint32_t *dest, const uint32_t *src, size_t n);
572 /* Append SRC onto DEST. */
573 /* Similar to strcat(), wcscat(). */
575 u8_strcat (uint8_t *dest, const uint8_t *src);
577 u16_strcat (uint16_t *dest, const uint16_t *src);
579 u32_strcat (uint32_t *dest, const uint32_t *src);
581 /* Append no more than N units of SRC onto DEST. */
582 /* Similar to strncat(), wcsncat(). */
584 u8_strncat (uint8_t *dest, const uint8_t *src, size_t n);
586 u16_strncat (uint16_t *dest, const uint16_t *src, size_t n);
588 u32_strncat (uint32_t *dest, const uint32_t *src, size_t n);
590 /* Compare S1 and S2. */
591 /* Similar to strcmp(), wcscmp(). */
593 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
595 u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2)
597 # define u8_strcmp u8_strcmp_gnu
600 u8_strcmp (const uint8_t *s1, const uint8_t *s2)
604 u16_strcmp (const uint16_t *s1, const uint16_t *s2)
607 u32_strcmp (const uint32_t *s1, const uint32_t *s2)
610 /* Compare S1 and S2 using the collation rules of the current locale.
611 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
612 Upon failure, set errno and return any value. */
613 /* Similar to strcoll(), wcscoll(). */
615 u8_strcoll (const uint8_t *s1, const uint8_t *s2);
617 u16_strcoll (const uint16_t *s1, const uint16_t *s2);
619 u32_strcoll (const uint32_t *s1, const uint32_t *s2);
621 /* Compare no more than N units of S1 and S2. */
622 /* Similar to strncmp(), wcsncmp(). */
624 u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n)
627 u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n)
630 u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n)
633 /* Duplicate S, returning an identical malloc'd string. */
634 /* Similar to strdup(), wcsdup(). */
636 u8_strdup (const uint8_t *s);
638 u16_strdup (const uint16_t *s);
640 u32_strdup (const uint32_t *s);
642 /* Find the first occurrence of UC in STR. */
643 /* Similar to strchr(), wcschr(). */
645 u8_strchr (const uint8_t *str, ucs4_t uc)
648 u16_strchr (const uint16_t *str, ucs4_t uc)
651 u32_strchr (const uint32_t *str, ucs4_t uc)
654 /* Find the last occurrence of UC in STR. */
655 /* Similar to strrchr(), wcsrchr(). */
657 u8_strrchr (const uint8_t *str, ucs4_t uc)
660 u16_strrchr (const uint16_t *str, ucs4_t uc)
663 u32_strrchr (const uint32_t *str, ucs4_t uc)
666 /* Return the length of the initial segment of STR which consists entirely
667 of Unicode characters not in REJECT. */
668 /* Similar to strcspn(), wcscspn(). */
670 u8_strcspn (const uint8_t *str, const uint8_t *reject)
673 u16_strcspn (const uint16_t *str, const uint16_t *reject)
676 u32_strcspn (const uint32_t *str, const uint32_t *reject)
679 /* Return the length of the initial segment of STR which consists entirely
680 of Unicode characters in ACCEPT. */
681 /* Similar to strspn(), wcsspn(). */
683 u8_strspn (const uint8_t *str, const uint8_t *accept)
686 u16_strspn (const uint16_t *str, const uint16_t *accept)
689 u32_strspn (const uint32_t *str, const uint32_t *accept)
692 /* Find the first occurrence in STR of any character in ACCEPT. */
693 /* Similar to strpbrk(), wcspbrk(). */
695 u8_strpbrk (const uint8_t *str, const uint8_t *accept)
698 u16_strpbrk (const uint16_t *str, const uint16_t *accept)
701 u32_strpbrk (const uint32_t *str, const uint32_t *accept)
704 /* Find the first occurrence of NEEDLE in HAYSTACK. */
705 /* Similar to strstr(), wcsstr(). */
707 u8_strstr (const uint8_t *haystack, const uint8_t *needle)
710 u16_strstr (const uint16_t *haystack, const uint16_t *needle)
713 u32_strstr (const uint32_t *haystack, const uint32_t *needle)
716 /* Test whether STR starts with PREFIX. */
718 u8_startswith (const uint8_t *str, const uint8_t *prefix)
721 u16_startswith (const uint16_t *str, const uint16_t *prefix)
724 u32_startswith (const uint32_t *str, const uint32_t *prefix)
727 /* Test whether STR ends with SUFFIX. */
729 u8_endswith (const uint8_t *str, const uint8_t *suffix)
732 u16_endswith (const uint16_t *str, const uint16_t *suffix)
735 u32_endswith (const uint32_t *str, const uint32_t *suffix)
738 /* Divide STR into tokens separated by characters in DELIM.
739 This interface is actually more similar to wcstok than to strtok. */
740 /* Similar to strtok_r(), wcstok(). */
742 u8_strtok (uint8_t *str, const uint8_t *delim, uint8_t **ptr);
744 u16_strtok (uint16_t *str, const uint16_t *delim, uint16_t **ptr);
746 u32_strtok (uint32_t *str, const uint32_t *delim, uint32_t **ptr);
753 #endif /* _UNISTR_H */