1 /* Elementary Unicode string functions.
2 Copyright (C) 2001-2002, 2005-2012 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 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 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Get common macros for C. */
23 #include "unused-parameter.h"
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 _safe suffix are safe, even if the library is compiled
141 without --enable-safety. */
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
, size_t n _GL_UNUSED_PARAMETER
)
200 # if CONFIG_UNICODE_SAFETY
201 if (c
< 0xd800 || (c
>= 0xe000 && c
< 0x110000))
204 # if CONFIG_UNICODE_SAFETY
206 /* invalid multibyte character */
214 #if GNULIB_UNISTR_U8_MBTOUC || HAVE_LIBUNISTRING
217 u8_mbtouc (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
220 u8_mbtouc_aux (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
222 u8_mbtouc (ucs4_t
*puc
, const uint8_t *s
, size_t n
)
232 return u8_mbtouc_aux (puc
, s
, n
);
237 #if GNULIB_UNISTR_U16_MBTOUC || HAVE_LIBUNISTRING
240 u16_mbtouc (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
243 u16_mbtouc_aux (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
245 u16_mbtouc (ucs4_t
*puc
, const uint16_t *s
, size_t n
)
249 if (c
< 0xd800 || c
>= 0xe000)
255 return u16_mbtouc_aux (puc
, s
, n
);
260 #if GNULIB_UNISTR_U32_MBTOUC || HAVE_LIBUNISTRING
263 u32_mbtouc (ucs4_t
*puc
, const uint32_t *s
, size_t n
);
266 u32_mbtouc (ucs4_t
*puc
, const uint32_t *s
, size_t n _GL_UNUSED_PARAMETER
)
270 if (c
< 0xd800 || (c
>= 0xe000 && c
< 0x110000))
273 /* invalid multibyte character */
280 /* Return the length (number of units) of the first character in S, putting
281 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
282 and -1 is returned for an invalid sequence of units, -2 is returned for an
283 incomplete sequence of units.
284 The number of available units, N, must be > 0. */
285 /* Similar to u*_mbtouc(), except that the return value gives more details
286 about the failure, similar to mbrtowc(). */
288 #if GNULIB_UNISTR_U8_MBTOUCR || HAVE_LIBUNISTRING
290 u8_mbtoucr (ucs4_t
*puc
, const uint8_t *s
, size_t n
);
293 #if GNULIB_UNISTR_U16_MBTOUCR || HAVE_LIBUNISTRING
295 u16_mbtoucr (ucs4_t
*puc
, const uint16_t *s
, size_t n
);
298 #if GNULIB_UNISTR_U32_MBTOUCR || HAVE_LIBUNISTRING
300 u32_mbtoucr (ucs4_t
*puc
, const uint32_t *s
, size_t n
);
303 /* Put the multibyte character represented by UC in S, returning its
304 length. Return -1 upon failure, -2 if the number of available units, N,
305 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
306 /* Similar to wctomb(), except that s must not be NULL, and the argument n
307 must be specified. */
309 #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING
310 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
312 u8_uctomb_aux (uint8_t *s
, ucs4_t uc
, int n
);
315 u8_uctomb (uint8_t *s
, ucs4_t uc
, int n
);
318 u8_uctomb (uint8_t *s
, ucs4_t uc
, int n
)
320 if (uc
< 0x80 && n
> 0)
326 return u8_uctomb_aux (s
, uc
, n
);
331 #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING
332 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
334 u16_uctomb_aux (uint16_t *s
, ucs4_t uc
, int n
);
337 u16_uctomb (uint16_t *s
, ucs4_t uc
, int n
);
340 u16_uctomb (uint16_t *s
, ucs4_t uc
, int n
)
342 if (uc
< 0xd800 && n
> 0)
348 return u16_uctomb_aux (s
, uc
, n
);
353 #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING
356 u32_uctomb (uint32_t *s
, ucs4_t uc
, int n
);
359 u32_uctomb (uint32_t *s
, ucs4_t uc
, int n
)
361 if (uc
< 0xd800 || (uc
>= 0xe000 && uc
< 0x110000))
377 /* Copy N units from SRC to DEST. */
378 /* Similar to memcpy(). */
380 u8_cpy (uint8_t *dest
, const uint8_t *src
, size_t n
);
382 u16_cpy (uint16_t *dest
, const uint16_t *src
, size_t n
);
384 u32_cpy (uint32_t *dest
, const uint32_t *src
, size_t n
);
386 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
387 overlapping memory areas. */
388 /* Similar to memmove(). */
390 u8_move (uint8_t *dest
, const uint8_t *src
, size_t n
);
392 u16_move (uint16_t *dest
, const uint16_t *src
, size_t n
);
394 u32_move (uint32_t *dest
, const uint32_t *src
, size_t n
);
396 /* Set the first N characters of S to UC. UC should be a character that
397 occupies only 1 unit. */
398 /* Similar to memset(). */
400 u8_set (uint8_t *s
, ucs4_t uc
, size_t n
);
402 u16_set (uint16_t *s
, ucs4_t uc
, size_t n
);
404 u32_set (uint32_t *s
, ucs4_t uc
, size_t n
);
406 /* Compare S1 and S2, each of length N. */
407 /* Similar to memcmp(). */
409 u8_cmp (const uint8_t *s1
, const uint8_t *s2
, size_t n
)
412 u16_cmp (const uint16_t *s1
, const uint16_t *s2
, size_t n
)
415 u32_cmp (const uint32_t *s1
, const uint32_t *s2
, size_t n
)
418 /* Compare S1 and S2. */
419 /* Similar to the gnulib function memcmp2(). */
421 u8_cmp2 (const uint8_t *s1
, size_t n1
, const uint8_t *s2
, size_t n2
)
424 u16_cmp2 (const uint16_t *s1
, size_t n1
, const uint16_t *s2
, size_t n2
)
427 u32_cmp2 (const uint32_t *s1
, size_t n1
, const uint32_t *s2
, size_t n2
)
430 /* Search the string at S for UC. */
431 /* Similar to memchr(). */
433 u8_chr (const uint8_t *s
, size_t n
, ucs4_t uc
)
436 u16_chr (const uint16_t *s
, size_t n
, ucs4_t uc
)
439 u32_chr (const uint32_t *s
, size_t n
, ucs4_t uc
)
442 /* Count the number of Unicode characters in the N units from S. */
443 /* Similar to mbsnlen(). */
445 u8_mbsnlen (const uint8_t *s
, size_t n
)
448 u16_mbsnlen (const uint16_t *s
, size_t n
)
451 u32_mbsnlen (const uint32_t *s
, size_t n
)
454 /* Elementary string functions with memory allocation. */
456 /* Make a freshly allocated copy of S, of length N. */
458 u8_cpy_alloc (const uint8_t *s
, size_t n
);
460 u16_cpy_alloc (const uint16_t *s
, size_t n
);
462 u32_cpy_alloc (const uint32_t *s
, size_t n
);
464 /* Elementary string functions on NUL terminated strings. */
466 /* Return the length (number of units) of the first character in S.
467 Return 0 if it is the NUL character. Return -1 upon failure. */
469 u8_strmblen (const uint8_t *s
)
472 u16_strmblen (const uint16_t *s
)
475 u32_strmblen (const uint32_t *s
)
478 /* Return the length (number of units) of the first character in S, putting
479 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
480 character. Return -1 upon failure. */
482 u8_strmbtouc (ucs4_t
*puc
, const uint8_t *s
);
484 u16_strmbtouc (ucs4_t
*puc
, const uint16_t *s
);
486 u32_strmbtouc (ucs4_t
*puc
, const uint32_t *s
);
488 /* Forward iteration step. Advances the pointer past the next character,
489 or returns NULL if the end of the string has been reached. Puts the
490 character's 'ucs4_t' representation in *PUC. */
491 extern const uint8_t *
492 u8_next (ucs4_t
*puc
, const uint8_t *s
);
493 extern const uint16_t *
494 u16_next (ucs4_t
*puc
, const uint16_t *s
);
495 extern const uint32_t *
496 u32_next (ucs4_t
*puc
, const uint32_t *s
);
498 /* Backward iteration step. Advances the pointer to point to the previous
499 character, or returns NULL if the beginning of the string had been reached.
500 Puts the character's 'ucs4_t' representation in *PUC. */
501 extern const uint8_t *
502 u8_prev (ucs4_t
*puc
, const uint8_t *s
, const uint8_t *start
);
503 extern const uint16_t *
504 u16_prev (ucs4_t
*puc
, const uint16_t *s
, const uint16_t *start
);
505 extern const uint32_t *
506 u32_prev (ucs4_t
*puc
, const uint32_t *s
, const uint32_t *start
);
508 /* Return the number of units in S. */
509 /* Similar to strlen(), wcslen(). */
511 u8_strlen (const uint8_t *s
)
514 u16_strlen (const uint16_t *s
)
517 u32_strlen (const uint32_t *s
)
520 /* Return the number of units in S, but at most MAXLEN. */
521 /* Similar to strnlen(), wcsnlen(). */
523 u8_strnlen (const uint8_t *s
, size_t maxlen
)
526 u16_strnlen (const uint16_t *s
, size_t maxlen
)
529 u32_strnlen (const uint32_t *s
, size_t maxlen
)
532 /* Copy SRC to DEST. */
533 /* Similar to strcpy(), wcscpy(). */
535 u8_strcpy (uint8_t *dest
, const uint8_t *src
);
537 u16_strcpy (uint16_t *dest
, const uint16_t *src
);
539 u32_strcpy (uint32_t *dest
, const uint32_t *src
);
541 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
542 /* Similar to stpcpy(). */
544 u8_stpcpy (uint8_t *dest
, const uint8_t *src
);
546 u16_stpcpy (uint16_t *dest
, const uint16_t *src
);
548 u32_stpcpy (uint32_t *dest
, const uint32_t *src
);
550 /* Copy no more than N units of SRC to DEST. */
551 /* Similar to strncpy(), wcsncpy(). */
553 u8_strncpy (uint8_t *dest
, const uint8_t *src
, size_t n
);
555 u16_strncpy (uint16_t *dest
, const uint16_t *src
, size_t n
);
557 u32_strncpy (uint32_t *dest
, const uint32_t *src
, size_t n
);
559 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
560 non-NUL unit written into DEST. */
561 /* Similar to stpncpy(). */
563 u8_stpncpy (uint8_t *dest
, const uint8_t *src
, size_t n
);
565 u16_stpncpy (uint16_t *dest
, const uint16_t *src
, size_t n
);
567 u32_stpncpy (uint32_t *dest
, const uint32_t *src
, size_t n
);
569 /* Append SRC onto DEST. */
570 /* Similar to strcat(), wcscat(). */
572 u8_strcat (uint8_t *dest
, const uint8_t *src
);
574 u16_strcat (uint16_t *dest
, const uint16_t *src
);
576 u32_strcat (uint32_t *dest
, const uint32_t *src
);
578 /* Append no more than N units of SRC onto DEST. */
579 /* Similar to strncat(), wcsncat(). */
581 u8_strncat (uint8_t *dest
, const uint8_t *src
, size_t n
);
583 u16_strncat (uint16_t *dest
, const uint16_t *src
, size_t n
);
585 u32_strncat (uint32_t *dest
, const uint32_t *src
, size_t n
);
587 /* Compare S1 and S2. */
588 /* Similar to strcmp(), wcscmp(). */
590 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
592 u8_strcmp_gnu (const uint8_t *s1
, const uint8_t *s2
)
594 # define u8_strcmp u8_strcmp_gnu
597 u8_strcmp (const uint8_t *s1
, const uint8_t *s2
)
601 u16_strcmp (const uint16_t *s1
, const uint16_t *s2
)
604 u32_strcmp (const uint32_t *s1
, const uint32_t *s2
)
607 /* Compare S1 and S2 using the collation rules of the current locale.
608 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
609 Upon failure, set errno and return any value. */
610 /* Similar to strcoll(), wcscoll(). */
612 u8_strcoll (const uint8_t *s1
, const uint8_t *s2
);
614 u16_strcoll (const uint16_t *s1
, const uint16_t *s2
);
616 u32_strcoll (const uint32_t *s1
, const uint32_t *s2
);
618 /* Compare no more than N units of S1 and S2. */
619 /* Similar to strncmp(), wcsncmp(). */
621 u8_strncmp (const uint8_t *s1
, const uint8_t *s2
, size_t n
)
624 u16_strncmp (const uint16_t *s1
, const uint16_t *s2
, size_t n
)
627 u32_strncmp (const uint32_t *s1
, const uint32_t *s2
, size_t n
)
630 /* Duplicate S, returning an identical malloc'd string. */
631 /* Similar to strdup(), wcsdup(). */
633 u8_strdup (const uint8_t *s
);
635 u16_strdup (const uint16_t *s
);
637 u32_strdup (const uint32_t *s
);
639 /* Find the first occurrence of UC in STR. */
640 /* Similar to strchr(), wcschr(). */
642 u8_strchr (const uint8_t *str
, ucs4_t uc
)
645 u16_strchr (const uint16_t *str
, ucs4_t uc
)
648 u32_strchr (const uint32_t *str
, ucs4_t uc
)
651 /* Find the last occurrence of UC in STR. */
652 /* Similar to strrchr(), wcsrchr(). */
654 u8_strrchr (const uint8_t *str
, ucs4_t uc
)
657 u16_strrchr (const uint16_t *str
, ucs4_t uc
)
660 u32_strrchr (const uint32_t *str
, ucs4_t uc
)
663 /* Return the length of the initial segment of STR which consists entirely
664 of Unicode characters not in REJECT. */
665 /* Similar to strcspn(), wcscspn(). */
667 u8_strcspn (const uint8_t *str
, const uint8_t *reject
)
670 u16_strcspn (const uint16_t *str
, const uint16_t *reject
)
673 u32_strcspn (const uint32_t *str
, const uint32_t *reject
)
676 /* Return the length of the initial segment of STR which consists entirely
677 of Unicode characters in ACCEPT. */
678 /* Similar to strspn(), wcsspn(). */
680 u8_strspn (const uint8_t *str
, const uint8_t *accept
)
683 u16_strspn (const uint16_t *str
, const uint16_t *accept
)
686 u32_strspn (const uint32_t *str
, const uint32_t *accept
)
689 /* Find the first occurrence in STR of any character in ACCEPT. */
690 /* Similar to strpbrk(), wcspbrk(). */
692 u8_strpbrk (const uint8_t *str
, const uint8_t *accept
)
695 u16_strpbrk (const uint16_t *str
, const uint16_t *accept
)
698 u32_strpbrk (const uint32_t *str
, const uint32_t *accept
)
701 /* Find the first occurrence of NEEDLE in HAYSTACK. */
702 /* Similar to strstr(), wcsstr(). */
704 u8_strstr (const uint8_t *haystack
, const uint8_t *needle
)
707 u16_strstr (const uint16_t *haystack
, const uint16_t *needle
)
710 u32_strstr (const uint32_t *haystack
, const uint32_t *needle
)
713 /* Test whether STR starts with PREFIX. */
715 u8_startswith (const uint8_t *str
, const uint8_t *prefix
)
718 u16_startswith (const uint16_t *str
, const uint16_t *prefix
)
721 u32_startswith (const uint32_t *str
, const uint32_t *prefix
)
724 /* Test whether STR ends with SUFFIX. */
726 u8_endswith (const uint8_t *str
, const uint8_t *suffix
)
729 u16_endswith (const uint16_t *str
, const uint16_t *suffix
)
732 u32_endswith (const uint32_t *str
, const uint32_t *suffix
)
735 /* Divide STR into tokens separated by characters in DELIM.
736 This interface is actually more similar to wcstok than to strtok. */
737 /* Similar to strtok_r(), wcstok(). */
739 u8_strtok (uint8_t *str
, const uint8_t *delim
, uint8_t **ptr
);
741 u16_strtok (uint16_t *str
, const uint16_t *delim
, uint16_t **ptr
);
743 u32_strtok (uint32_t *str
, const uint32_t *delim
, uint32_t **ptr
);
750 #endif /* _UNISTR_H */