1 /* Elementary Unicode string functions.
2 Copyright (C) 2001-2002, 2005-2010 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 "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);
71 /* Check whether an UTF-16 string is well-formed.
72 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
73 extern const uint16_t *
74 u16_check (const uint16_t *s, size_t n);
76 /* Check whether an UCS-4 string is well-formed.
77 Return NULL if valid, or a pointer to the first invalid unit otherwise. */
78 extern const uint32_t *
79 u32_check (const uint32_t *s, size_t n);
82 /* Elementary string conversions. */
84 /* Convert an UTF-8 string to an UTF-16 string. */
86 u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
89 /* Convert an UTF-8 string to an UCS-4 string. */
91 u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
94 /* Convert an UTF-16 string to an UTF-8 string. */
96 u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
99 /* Convert an UTF-16 string to an UCS-4 string. */
101 u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
104 /* Convert an UCS-4 string to an UTF-8 string. */
106 u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
109 /* Convert an UCS-4 string to an UTF-16 string. */
111 u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
115 /* Elementary string functions. */
117 /* Return the length (number of units) of the first character in S, which is
118 no longer than N. Return 0 if it is the NUL character. Return -1 upon
120 /* Similar to mblen(), except that s must not be NULL. */
122 u8_mblen (const uint8_t *s, size_t n);
124 u16_mblen (const uint16_t *s, size_t n);
126 u32_mblen (const uint32_t *s, size_t n);
128 /* Return the length (number of units) of the first character in S, putting
129 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
130 and an appropriate number of units is returned.
131 The number of available units, N, must be > 0. */
132 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
133 and the NUL character is not treated specially. */
134 /* The variants with _safe suffix are safe, even if the library is compiled
135 without --enable-safety. */
137 #if GNULIB_UNISTR_U8_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
140 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
143 u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
145 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
155 return u8_mbtouc_unsafe_aux (puc, s, n);
160 #if GNULIB_UNISTR_U16_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
163 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
166 u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
168 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
172 if (c < 0xd800 || c >= 0xe000)
178 return u16_mbtouc_unsafe_aux (puc, s, n);
183 #if GNULIB_UNISTR_U32_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
186 u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
189 u32_mbtouc_unsafe (ucs4_t *puc,
190 const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
194 # if CONFIG_UNICODE_SAFETY
195 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
198 # if CONFIG_UNICODE_SAFETY
200 /* invalid multibyte character */
208 #if GNULIB_UNISTR_U8_MBTOUC || HAVE_LIBUNISTRING
211 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
214 u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
216 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
226 return u8_mbtouc_aux (puc, s, n);
231 #if GNULIB_UNISTR_U16_MBTOUC || HAVE_LIBUNISTRING
234 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
237 u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
239 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n)
243 if (c < 0xd800 || c >= 0xe000)
249 return u16_mbtouc_aux (puc, s, n);
254 #if GNULIB_UNISTR_U32_MBTOUC || HAVE_LIBUNISTRING
257 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
260 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
264 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
267 /* invalid multibyte character */
274 /* Return the length (number of units) of the first character in S, putting
275 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
276 and -1 is returned for an invalid sequence of units, -2 is returned for an
277 incomplete sequence of units.
278 The number of available units, N, must be > 0. */
279 /* Similar to u*_mbtouc(), except that the return value gives more details
280 about the failure, similar to mbrtowc(). */
282 #if GNULIB_UNISTR_U8_MBTOUCR || HAVE_LIBUNISTRING
284 u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
287 #if GNULIB_UNISTR_U16_MBTOUCR || HAVE_LIBUNISTRING
289 u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
292 #if GNULIB_UNISTR_U32_MBTOUCR || HAVE_LIBUNISTRING
294 u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
297 /* Put the multibyte character represented by UC in S, returning its
298 length. Return -1 upon failure, -2 if the number of available units, N,
299 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
300 /* Similar to wctomb(), except that s must not be NULL, and the argument n
301 must be specified. */
303 #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING
304 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
306 u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n);
309 u8_uctomb (uint8_t *s, ucs4_t uc, int n);
312 u8_uctomb (uint8_t *s, ucs4_t uc, int n)
314 if (uc < 0x80 && n > 0)
320 return u8_uctomb_aux (s, uc, n);
325 #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING
326 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
328 u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n);
331 u16_uctomb (uint16_t *s, ucs4_t uc, int n);
334 u16_uctomb (uint16_t *s, ucs4_t uc, int n)
336 if (uc < 0xd800 && n > 0)
342 return u16_uctomb_aux (s, uc, n);
347 #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING
350 u32_uctomb (uint32_t *s, ucs4_t uc, int n);
353 u32_uctomb (uint32_t *s, ucs4_t uc, int n)
355 if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
371 /* Copy N units from SRC to DEST. */
372 /* Similar to memcpy(). */
374 u8_cpy (uint8_t *dest, const uint8_t *src, size_t n);
376 u16_cpy (uint16_t *dest, const uint16_t *src, size_t n);
378 u32_cpy (uint32_t *dest, const uint32_t *src, size_t n);
380 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
381 overlapping memory areas. */
382 /* Similar to memmove(). */
384 u8_move (uint8_t *dest, const uint8_t *src, size_t n);
386 u16_move (uint16_t *dest, const uint16_t *src, size_t n);
388 u32_move (uint32_t *dest, const uint32_t *src, size_t n);
390 /* Set the first N characters of S to UC. UC should be a character that
391 occupies only 1 unit. */
392 /* Similar to memset(). */
394 u8_set (uint8_t *s, ucs4_t uc, size_t n);
396 u16_set (uint16_t *s, ucs4_t uc, size_t n);
398 u32_set (uint32_t *s, ucs4_t uc, size_t n);
400 /* Compare S1 and S2, each of length N. */
401 /* Similar to memcmp(). */
403 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n);
405 u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n);
407 u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n);
409 /* Compare S1 and S2. */
410 /* Similar to the gnulib function memcmp2(). */
412 u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2);
414 u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2);
416 u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2);
418 /* Search the string at S for UC. */
419 /* Similar to memchr(). */
421 u8_chr (const uint8_t *s, size_t n, ucs4_t uc);
423 u16_chr (const uint16_t *s, size_t n, ucs4_t uc);
425 u32_chr (const uint32_t *s, size_t n, ucs4_t uc);
427 /* Count the number of Unicode characters in the N units from S. */
428 /* Similar to mbsnlen(). */
430 u8_mbsnlen (const uint8_t *s, size_t n);
432 u16_mbsnlen (const uint16_t *s, size_t n);
434 u32_mbsnlen (const uint32_t *s, size_t n);
436 /* Elementary string functions with memory allocation. */
438 /* Make a freshly allocated copy of S, of length N. */
440 u8_cpy_alloc (const uint8_t *s, size_t n);
442 u16_cpy_alloc (const uint16_t *s, size_t n);
444 u32_cpy_alloc (const uint32_t *s, size_t n);
446 /* Elementary string functions on NUL terminated strings. */
448 /* Return the length (number of units) of the first character in S.
449 Return 0 if it is the NUL character. Return -1 upon failure. */
451 u8_strmblen (const uint8_t *s);
453 u16_strmblen (const uint16_t *s);
455 u32_strmblen (const uint32_t *s);
457 /* Return the length (number of units) of the first character in S, putting
458 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
459 character. Return -1 upon failure. */
461 u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
463 u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
465 u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
467 /* Forward iteration step. Advances the pointer past the next character,
468 or returns NULL if the end of the string has been reached. Puts the
469 character's 'ucs4_t' representation in *PUC. */
470 extern const uint8_t *
471 u8_next (ucs4_t *puc, const uint8_t *s);
472 extern const uint16_t *
473 u16_next (ucs4_t *puc, const uint16_t *s);
474 extern const uint32_t *
475 u32_next (ucs4_t *puc, const uint32_t *s);
477 /* Backward iteration step. Advances the pointer to point to the previous
478 character, or returns NULL if the beginning of the string had been reached.
479 Puts the character's 'ucs4_t' representation in *PUC. */
480 extern const uint8_t *
481 u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
482 extern const uint16_t *
483 u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
484 extern const uint32_t *
485 u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
487 /* Return the number of units in S. */
488 /* Similar to strlen(), wcslen(). */
490 u8_strlen (const uint8_t *s);
492 u16_strlen (const uint16_t *s);
494 u32_strlen (const uint32_t *s);
496 /* Return the number of units in S, but at most MAXLEN. */
497 /* Similar to strnlen(), wcsnlen(). */
499 u8_strnlen (const uint8_t *s, size_t maxlen);
501 u16_strnlen (const uint16_t *s, size_t maxlen);
503 u32_strnlen (const uint32_t *s, size_t maxlen);
505 /* Copy SRC to DEST. */
506 /* Similar to strcpy(), wcscpy(). */
508 u8_strcpy (uint8_t *dest, const uint8_t *src);
510 u16_strcpy (uint16_t *dest, const uint16_t *src);
512 u32_strcpy (uint32_t *dest, const uint32_t *src);
514 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
515 /* Similar to stpcpy(). */
517 u8_stpcpy (uint8_t *dest, const uint8_t *src);
519 u16_stpcpy (uint16_t *dest, const uint16_t *src);
521 u32_stpcpy (uint32_t *dest, const uint32_t *src);
523 /* Copy no more than N units of SRC to DEST. */
524 /* Similar to strncpy(), wcsncpy(). */
526 u8_strncpy (uint8_t *dest, const uint8_t *src, size_t n);
528 u16_strncpy (uint16_t *dest, const uint16_t *src, size_t n);
530 u32_strncpy (uint32_t *dest, const uint32_t *src, size_t n);
532 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
533 non-NUL unit written into DEST. */
534 /* Similar to stpncpy(). */
536 u8_stpncpy (uint8_t *dest, const uint8_t *src, size_t n);
538 u16_stpncpy (uint16_t *dest, const uint16_t *src, size_t n);
540 u32_stpncpy (uint32_t *dest, const uint32_t *src, size_t n);
542 /* Append SRC onto DEST. */
543 /* Similar to strcat(), wcscat(). */
545 u8_strcat (uint8_t *dest, const uint8_t *src);
547 u16_strcat (uint16_t *dest, const uint16_t *src);
549 u32_strcat (uint32_t *dest, const uint32_t *src);
551 /* Append no more than N units of SRC onto DEST. */
552 /* Similar to strncat(), wcsncat(). */
554 u8_strncat (uint8_t *dest, const uint8_t *src, size_t n);
556 u16_strncat (uint16_t *dest, const uint16_t *src, size_t n);
558 u32_strncat (uint32_t *dest, const uint32_t *src, size_t n);
560 /* Compare S1 and S2. */
561 /* Similar to strcmp(), wcscmp(). */
563 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
565 u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2);
566 # define u8_strcmp u8_strcmp_gnu
569 u8_strcmp (const uint8_t *s1, const uint8_t *s2);
572 u16_strcmp (const uint16_t *s1, const uint16_t *s2);
574 u32_strcmp (const uint32_t *s1, const uint32_t *s2);
576 /* Compare S1 and S2 using the collation rules of the current locale.
577 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
578 Upon failure, set errno and return any value. */
579 /* Similar to strcoll(), wcscoll(). */
581 u8_strcoll (const uint8_t *s1, const uint8_t *s2);
583 u16_strcoll (const uint16_t *s1, const uint16_t *s2);
585 u32_strcoll (const uint32_t *s1, const uint32_t *s2);
587 /* Compare no more than N units of S1 and S2. */
588 /* Similar to strncmp(), wcsncmp(). */
590 u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n);
592 u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n);
594 u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n);
596 /* Duplicate S, returning an identical malloc'd string. */
597 /* Similar to strdup(), wcsdup(). */
599 u8_strdup (const uint8_t *s);
601 u16_strdup (const uint16_t *s);
603 u32_strdup (const uint32_t *s);
605 /* Find the first occurrence of UC in STR. */
606 /* Similar to strchr(), wcschr(). */
608 u8_strchr (const uint8_t *str, ucs4_t uc);
610 u16_strchr (const uint16_t *str, ucs4_t uc);
612 u32_strchr (const uint32_t *str, ucs4_t uc);
614 /* Find the last occurrence of UC in STR. */
615 /* Similar to strrchr(), wcsrchr(). */
617 u8_strrchr (const uint8_t *str, ucs4_t uc);
619 u16_strrchr (const uint16_t *str, ucs4_t uc);
621 u32_strrchr (const uint32_t *str, ucs4_t uc);
623 /* Return the length of the initial segment of STR which consists entirely
624 of Unicode characters not in REJECT. */
625 /* Similar to strcspn(), wcscspn(). */
627 u8_strcspn (const uint8_t *str, const uint8_t *reject);
629 u16_strcspn (const uint16_t *str, const uint16_t *reject);
631 u32_strcspn (const uint32_t *str, const uint32_t *reject);
633 /* Return the length of the initial segment of STR which consists entirely
634 of Unicode characters in ACCEPT. */
635 /* Similar to strspn(), wcsspn(). */
637 u8_strspn (const uint8_t *str, const uint8_t *accept);
639 u16_strspn (const uint16_t *str, const uint16_t *accept);
641 u32_strspn (const uint32_t *str, const uint32_t *accept);
643 /* Find the first occurrence in STR of any character in ACCEPT. */
644 /* Similar to strpbrk(), wcspbrk(). */
646 u8_strpbrk (const uint8_t *str, const uint8_t *accept);
648 u16_strpbrk (const uint16_t *str, const uint16_t *accept);
650 u32_strpbrk (const uint32_t *str, const uint32_t *accept);
652 /* Find the first occurrence of NEEDLE in HAYSTACK. */
653 /* Similar to strstr(), wcsstr(). */
655 u8_strstr (const uint8_t *haystack, const uint8_t *needle);
657 u16_strstr (const uint16_t *haystack, const uint16_t *needle);
659 u32_strstr (const uint32_t *haystack, const uint32_t *needle);
661 /* Test whether STR starts with PREFIX. */
663 u8_startswith (const uint8_t *str, const uint8_t *prefix);
665 u16_startswith (const uint16_t *str, const uint16_t *prefix);
667 u32_startswith (const uint32_t *str, const uint32_t *prefix);
669 /* Test whether STR ends with SUFFIX. */
671 u8_endswith (const uint8_t *str, const uint8_t *suffix);
673 u16_endswith (const uint16_t *str, const uint16_t *suffix);
675 u32_endswith (const uint32_t *str, const uint32_t *suffix);
677 /* Divide STR into tokens separated by characters in DELIM.
678 This interface is actually more similar to wcstok than to strtok. */
679 /* Similar to strtok_r(), wcstok(). */
681 u8_strtok (uint8_t *str, const uint8_t *delim, uint8_t **ptr);
683 u16_strtok (uint16_t *str, const uint16_t *delim, uint16_t **ptr);
685 u32_strtok (uint32_t *str, const uint32_t *delim, uint32_t **ptr);
692 #endif /* _UNISTR_H */