Debian Codegraph @

lib/unistr.in.h

raw
1 /* Elementary Unicode string functions.
2 Copyright (C) 2001-2002, 2005-2025 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #ifndef _UNISTR_H
18 #define _UNISTR_H
19
20 #include "unitypes.h"
21
22 /* Get bool. */
23 #include <stdbool.h>
24
25 /* Get size_t, ptrdiff_t. */
26 #include <stddef.h>
27
28 /* Get free(). */
29 #include <stdlib.h>
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35
36 /* Conventions:
37
38 All functions prefixed with u8_ operate on UTF-8 encoded strings.
39 Their unit is an uint8_t (1 byte).
40
41 All functions prefixed with u16_ operate on UTF-16 encoded strings.
42 Their unit is an uint16_t (a 2-byte word).
43
44 All functions prefixed with u32_ operate on UCS-4 encoded strings.
45 Their unit is an uint32_t (a 4-byte word).
46
47 All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
48 n units.
49
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.
55
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. */
62
63
64 /* Elementary string checks. */
65
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)
70 _UC_ATTRIBUTE_PURE;
71
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)
76 _UC_ATTRIBUTE_PURE;
77
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)
82 _UC_ATTRIBUTE_PURE;
83
84 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
85 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
86 want compiler warnings for 'const' related mistakes. */
87 # ifdef __cplusplus
88 template <typename T>
89 T * u8_check_template (T* s, size_t n);
90 template <>
91 inline uint8_t * u8_check_template (uint8_t *s, size_t n)
92 { return const_cast<uint8_t *>(u8_check (s, n)); }
93 template <>
94 inline const uint8_t * u8_check_template (const uint8_t *s, size_t n)
95 { return u8_check (s, n); }
96 # undef u8_check
97 # define u8_check u8_check_template
98 # elif !defined u8_check
99 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
100 || defined __ICC || defined __TINYC__ \
101 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
102 # define u8_check(s,n) \
103 _Generic ((s), \
104 uint8_t *: (uint8_t *) u8_check ((s), (n)), \
105 default : u8_check ((s), (n)))
106 # endif
107 # endif
108 # ifdef __cplusplus
109 template <typename T>
110 T * u16_check_template (T* s, size_t n);
111 template <>
112 inline uint16_t * u16_check_template (uint16_t *s, size_t n)
113 { return const_cast<uint16_t *>(u16_check (s, n)); }
114 template <>
115 inline const uint16_t * u16_check_template (const uint16_t *s, size_t n)
116 { return u16_check (s, n); }
117 # undef u16_check
118 # define u16_check u16_check_template
119 # elif !defined u16_check
120 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
121 || defined __ICC || defined __TINYC__ \
122 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
123 # define u16_check(s,n) \
124 _Generic ((s), \
125 uint16_t *: (uint16_t *) u16_check ((s), (n)), \
126 default : u16_check ((s), (n)))
127 # endif
128 # endif
129 # ifdef __cplusplus
130 template <typename T>
131 T * u32_check_template (T* s, size_t n);
132 template <>
133 inline uint32_t * u32_check_template (uint32_t *s, size_t n)
134 { return const_cast<uint32_t *>(u32_check (s, n)); }
135 template <>
136 inline const uint32_t * u32_check_template (const uint32_t *s, size_t n)
137 { return u32_check (s, n); }
138 # undef u32_check
139 # define u32_check u32_check_template
140 # elif !defined u32_check
141 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
142 || defined __ICC || defined __TINYC__ \
143 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
144 # define u32_check(s,n) \
145 _Generic ((s), \
146 uint32_t *: (uint32_t *) u32_check ((s), (n)), \
147 default : u32_check ((s), (n)))
148 # endif
149 # endif
150 #endif
151
152
153 /* Elementary string conversions. */
154
155 /* Convert an UTF-8 string to an UTF-16 string. */
156 extern uint16_t *
157 u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
158 size_t *lengthp);
159
160 /* Convert an UTF-8 string to an UCS-4 string. */
161 extern uint32_t *
162 u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
163 size_t *lengthp);
164
165 /* Convert an UTF-16 string to an UTF-8 string. */
166 extern uint8_t *
167 u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
168 size_t *lengthp);
169
170 /* Convert an UTF-16 string to an UCS-4 string. */
171 extern uint32_t *
172 u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
173 size_t *lengthp);
174
175 /* Convert an UCS-4 string to an UTF-8 string. */
176 extern uint8_t *
177 u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
178 size_t *lengthp);
179
180 /* Convert an UCS-4 string to an UTF-16 string. */
181 extern uint16_t *
182 u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
183 size_t *lengthp);
184
185
186 /* Elementary string functions. */
187
188 /* Return the length (number of units) of the first character in S, which is
189 no longer than N. Return 0 if it is the NUL character. Return -1 upon
190 failure. */
191 /* Similar to mblen(), except that s must not be NULL. */
192 extern int
193 u8_mblen (const uint8_t *s, size_t n)
194 _UC_ATTRIBUTE_PURE;
195 extern int
196 u16_mblen (const uint16_t *s, size_t n)
197 _UC_ATTRIBUTE_PURE;
198 extern int
199 u32_mblen (const uint32_t *s, size_t n)
200 _UC_ATTRIBUTE_PURE;
201
202 /* Return the length (number of units) of the first character in S, putting
203 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
204 and an appropriate number of units is returned.
205 The number of available units, N, must be > 0. */
206 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
207 and the NUL character is not treated specially. */
208 /* The variants with _unsafe suffix are for backward compatibility with
209 libunistring versions < 0.9.7. */
210
211 #if GNULIB_UNISTR_U8_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
212 # if !HAVE_INLINE
213 extern int
214 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
215 # else
216 extern int
217 u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
218 static inline int
219 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
220 {
221 uint8_t c = *s;
222
223 if (c < 0x80)
224 {
225 *puc = c;
226 return 1;
227 }
228 else
229 return u8_mbtouc_unsafe_aux (puc, s, n);
230 }
231 # endif
232 #endif
233
234 #if GNULIB_UNISTR_U16_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
235 # if !HAVE_INLINE
236 extern int
237 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
238 # else
239 extern int
240 u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
241 static inline int
242 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
243 {
244 uint16_t c = *s;
245
246 if (c < 0xd800 || c >= 0xe000)
247 {
248 *puc = c;
249 return 1;
250 }
251 else
252 return u16_mbtouc_unsafe_aux (puc, s, n);
253 }
254 # endif
255 #endif
256
257 #if GNULIB_UNISTR_U32_MBTOUC_UNSAFE || HAVE_LIBUNISTRING
258 # if !HAVE_INLINE
259 extern int
260 u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
261 # else
262 static inline int
263 u32_mbtouc_unsafe (ucs4_t *puc,
264 const uint32_t *s, _GL_ATTRIBUTE_MAYBE_UNUSED size_t n)
265 {
266 uint32_t c = *s;
267
268 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
269 *puc = c;
270 else
271 /* invalid multibyte character */
272 *puc = 0xfffd;
273 return 1;
274 }
275 # endif
276 #endif
277
278 #if GNULIB_UNISTR_U8_MBTOUC || HAVE_LIBUNISTRING
279 # if !HAVE_INLINE
280 extern int
281 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
282 # else
283 extern int
284 u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
285 static inline int
286 u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n)
287 {
288 uint8_t c = *s;
289
290 if (c < 0x80)
291 {
292 *puc = c;
293 return 1;
294 }
295 else
296 return u8_mbtouc_aux (puc, s, n);
297 }
298 # endif
299 #endif
300
301 #if GNULIB_UNISTR_U16_MBTOUC || HAVE_LIBUNISTRING
302 # if !HAVE_INLINE
303 extern int
304 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
305 # else
306 extern int
307 u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
308 static inline int
309 u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n)
310 {
311 uint16_t c = *s;
312
313 if (c < 0xd800 || c >= 0xe000)
314 {
315 *puc = c;
316 return 1;
317 }
318 else
319 return u16_mbtouc_aux (puc, s, n);
320 }
321 # endif
322 #endif
323
324 #if GNULIB_UNISTR_U32_MBTOUC || HAVE_LIBUNISTRING
325 # if !HAVE_INLINE
326 extern int
327 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
328 # else
329 static inline int
330 u32_mbtouc (ucs4_t *puc, const uint32_t *s,
331 _GL_ATTRIBUTE_MAYBE_UNUSED size_t n)
332 {
333 uint32_t c = *s;
334
335 if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
336 *puc = c;
337 else
338 /* invalid multibyte character */
339 *puc = 0xfffd;
340 return 1;
341 }
342 # endif
343 #endif
344
345 /* Return the length (number of units) of the first character in S, putting
346 its 'ucs4_t' representation in *PUC. Upon failure, *PUC is set to 0xfffd,
347 and -1 is returned for an invalid sequence of units, -2 is returned for an
348 incomplete sequence of units.
349 The number of available units, N, must be > 0. */
350 /* Similar to u*_mbtouc(), except that the return value gives more details
351 about the failure, similar to mbrtowc(). */
352
353 #if GNULIB_UNISTR_U8_MBTOUCR || HAVE_LIBUNISTRING
354 extern int
355 u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
356 #endif
357
358 #if GNULIB_UNISTR_U16_MBTOUCR || HAVE_LIBUNISTRING
359 extern int
360 u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
361 #endif
362
363 #if GNULIB_UNISTR_U32_MBTOUCR || HAVE_LIBUNISTRING
364 extern int
365 u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
366 #endif
367
368 /* Put the multibyte character represented by UC in S, returning its
369 length. Return -1 upon failure, -2 if the number of available units, N,
370 is too small. The latter case cannot occur if N >= 6/2/1, respectively. */
371 /* Similar to wctomb(), except that s must not be NULL, and the argument n
372 must be specified. */
373
374 #if GNULIB_UNISTR_U8_UCTOMB || HAVE_LIBUNISTRING
375 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr. */
376 extern int
377 u8_uctomb_aux (uint8_t *s, ucs4_t uc, ptrdiff_t n);
378 # if !HAVE_INLINE
379 extern int
380 u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n);
381 # else
382 static inline int
383 u8_uctomb (uint8_t *s, ucs4_t uc, ptrdiff_t n)
384 {
385 if (uc < 0x80 && n > 0)
386 {
387 s[0] = uc;
388 return 1;
389 }
390 else
391 return u8_uctomb_aux (s, uc, n);
392 }
393 # endif
394 #endif
395
396 #if GNULIB_UNISTR_U16_UCTOMB || HAVE_LIBUNISTRING
397 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr. */
398 extern int
399 u16_uctomb_aux (uint16_t *s, ucs4_t uc, ptrdiff_t n);
400 # if !HAVE_INLINE
401 extern int
402 u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n);
403 # else
404 static inline int
405 u16_uctomb (uint16_t *s, ucs4_t uc, ptrdiff_t n)
406 {
407 if (uc < 0xd800 && n > 0)
408 {
409 s[0] = uc;
410 return 1;
411 }
412 else
413 return u16_uctomb_aux (s, uc, n);
414 }
415 # endif
416 #endif
417
418 #if GNULIB_UNISTR_U32_UCTOMB || HAVE_LIBUNISTRING
419 # if !HAVE_INLINE
420 extern int
421 u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n);
422 # else
423 static inline int
424 u32_uctomb (uint32_t *s, ucs4_t uc, ptrdiff_t n)
425 {
426 if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
427 {
428 if (n > 0)
429 {
430 *s = uc;
431 return 1;
432 }
433 else
434 return -2;
435 }
436 else
437 return -1;
438 }
439 # endif
440 #endif
441
442 /* Copy N units from SRC to DEST. */
443 /* Similar to memcpy(). */
444 extern uint8_t *
445 u8_cpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
446 extern uint16_t *
447 u16_cpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
448 extern uint32_t *
449 u32_cpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
450
451 /* Copy N units from SRC to DEST, returning pointer after last written unit. */
452 /* Similar to mempcpy(). */
453 extern uint8_t *
454 u8_pcpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
455 extern uint16_t *
456 u16_pcpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
457 extern uint32_t *
458 u32_pcpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
459
460 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
461 overlapping memory areas. */
462 /* Similar to memmove(). */
463 extern uint8_t *
464 u8_move (uint8_t *dest, const uint8_t *src, size_t n);
465 extern uint16_t *
466 u16_move (uint16_t *dest, const uint16_t *src, size_t n);
467 extern uint32_t *
468 u32_move (uint32_t *dest, const uint32_t *src, size_t n);
469
470 /* Set the first N characters of S to UC. UC should be a character that
471 occupies only 1 unit. */
472 /* Similar to memset(). */
473 extern uint8_t *
474 u8_set (uint8_t *s, ucs4_t uc, size_t n);
475 extern uint16_t *
476 u16_set (uint16_t *s, ucs4_t uc, size_t n);
477 extern uint32_t *
478 u32_set (uint32_t *s, ucs4_t uc, size_t n);
479
480 /* Compare S1 and S2, each of length N. */
481 /* Similar to memcmp(). */
482 extern int
483 u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
484 _UC_ATTRIBUTE_PURE;
485 extern int
486 u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n)
487 _UC_ATTRIBUTE_PURE;
488 extern int
489 u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n)
490 _UC_ATTRIBUTE_PURE;
491
492 /* Compare S1 and S2. */
493 /* Similar to the gnulib function memcmp2(). */
494 extern int
495 u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2)
496 _UC_ATTRIBUTE_PURE;
497 extern int
498 u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2)
499 _UC_ATTRIBUTE_PURE;
500 extern int
501 u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2)
502 _UC_ATTRIBUTE_PURE;
503
504 /* Search the string at S for UC. */
505 /* Similar to memchr(). */
506 extern uint8_t *
507 u8_chr (const uint8_t *s, size_t n, ucs4_t uc)
508 _UC_ATTRIBUTE_PURE;
509 extern uint16_t *
510 u16_chr (const uint16_t *s, size_t n, ucs4_t uc)
511 _UC_ATTRIBUTE_PURE;
512 extern uint32_t *
513 u32_chr (const uint32_t *s, size_t n, ucs4_t uc)
514 _UC_ATTRIBUTE_PURE;
515 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
516 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
517 want compiler warnings for 'const' related mistakes. */
518 # ifdef __cplusplus
519 template <typename T>
520 T * u8_chr_template (T* str, size_t n, ucs4_t uc);
521 template <>
522 inline uint8_t * u8_chr_template (uint8_t *str, size_t n, ucs4_t uc)
523 { return u8_chr (str, n, uc); }
524 template <>
525 inline const uint8_t * u8_chr_template (const uint8_t *str, size_t n, ucs4_t uc)
526 { return u8_chr (str, n, uc); }
527 # undef u8_chr
528 # define u8_chr u8_chr_template
529 # elif !defined u8_chr
530 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
531 || defined __ICC || defined __TINYC__ \
532 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
533 # define u8_chr(s,n,u) \
534 _Generic ((s), \
535 uint8_t const *: (uint8_t const *) u8_chr ((s), (n), (u)), \
536 default : u8_chr ((s), (n), (u)))
537 # endif
538 # endif
539 # ifdef __cplusplus
540 template <typename T>
541 T * u16_chr_template (T* str, size_t n, ucs4_t uc);
542 template <>
543 inline uint16_t * u16_chr_template (uint16_t *str, size_t n, ucs4_t uc)
544 { return u16_chr (str, n, uc); }
545 template <>
546 inline const uint16_t * u16_chr_template (const uint16_t *str, size_t n, ucs4_t uc)
547 { return u16_chr (str, n, uc); }
548 # undef u16_chr
549 # define u16_chr u16_chr_template
550 # elif !defined u16_chr
551 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
552 || defined __ICC || defined __TINYC__ \
553 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
554 # define u16_chr(s,n,u) \
555 _Generic ((s), \
556 uint16_t const *: (uint16_t const *) u16_chr ((s), (n), (u)), \
557 default : u16_chr ((s), (n), (u)))
558 # endif
559 # endif
560 # ifdef __cplusplus
561 template <typename T>
562 T * u32_chr_template (T* str, size_t n, ucs4_t uc);
563 template <>
564 inline uint32_t * u32_chr_template (uint32_t *str, size_t n, ucs4_t uc)
565 { return u32_chr (str, n, uc); }
566 template <>
567 inline const uint32_t * u32_chr_template (const uint32_t *str, size_t n, ucs4_t uc)
568 { return u32_chr (str, n, uc); }
569 # undef u32_chr
570 # define u32_chr u32_chr_template
571 # elif !defined u32_chr
572 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
573 || defined __ICC || defined __TINYC__ \
574 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
575 # define u32_chr(s,n,u) \
576 _Generic ((s), \
577 uint32_t const *: (uint32_t const *) u32_chr ((s), (n), (u)), \
578 default : u32_chr ((s), (n), (u)))
579 # endif
580 # endif
581 #endif
582
583 /* Count the number of Unicode characters in the N units from S. */
584 /* Similar to mbsnlen(). */
585 extern size_t
586 u8_mbsnlen (const uint8_t *s, size_t n)
587 _UC_ATTRIBUTE_PURE;
588 extern size_t
589 u16_mbsnlen (const uint16_t *s, size_t n)
590 _UC_ATTRIBUTE_PURE;
591 extern size_t
592 u32_mbsnlen (const uint32_t *s, size_t n)
593 _UC_ATTRIBUTE_PURE;
594
595 /* Elementary string functions with memory allocation. */
596
597 /* Make a freshly allocated copy of S, of length N. */
598 extern uint8_t *
599 u8_cpy_alloc (const uint8_t *s, size_t n);
600 extern uint16_t *
601 u16_cpy_alloc (const uint16_t *s, size_t n);
602 extern uint32_t *
603 u32_cpy_alloc (const uint32_t *s, size_t n);
604
605 /* Elementary string functions on NUL terminated strings. */
606
607 /* Return the length (number of units) of the first character in S.
608 Return 0 if it is the NUL character. Return -1 upon failure. */
609 extern int
610 u8_strmblen (const uint8_t *s)
611 _UC_ATTRIBUTE_PURE;
612 extern int
613 u16_strmblen (const uint16_t *s)
614 _UC_ATTRIBUTE_PURE;
615 extern int
616 u32_strmblen (const uint32_t *s)
617 _UC_ATTRIBUTE_PURE;
618
619 /* Return the length (number of units) of the first character in S, putting
620 its 'ucs4_t' representation in *PUC. Return 0 if it is the NUL
621 character. Return -1 upon failure. */
622 extern int
623 u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
624 extern int
625 u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
626 extern int
627 u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
628
629 /* Forward iteration step. Advances the pointer past the next character,
630 or returns NULL if the end of the string has been reached. Puts the
631 character's 'ucs4_t' representation in *PUC. */
632 extern const uint8_t *
633 u8_next (ucs4_t *puc, const uint8_t *s);
634 extern const uint16_t *
635 u16_next (ucs4_t *puc, const uint16_t *s);
636 extern const uint32_t *
637 u32_next (ucs4_t *puc, const uint32_t *s);
638 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
639 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
640 want compiler warnings for 'const' related mistakes. */
641 # ifdef __cplusplus
642 template <typename T>
643 T * u8_next_template (ucs4_t *puc, T* s);
644 template <>
645 inline uint8_t * u8_next_template (ucs4_t *puc, uint8_t *s)
646 { return const_cast<uint8_t *>(u8_next (puc, s)); }
647 template <>
648 inline const uint8_t * u8_next_template (ucs4_t *puc, const uint8_t *s)
649 { return u8_next (puc, s); }
650 # undef u8_next
651 # define u8_next u8_next_template
652 # elif !defined u8_next
653 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
654 || defined __ICC || defined __TINYC__ \
655 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
656 # define u8_next(p,s) \
657 _Generic ((s), \
658 uint8_t *: (uint8_t *) u8_next ((p), (s)), \
659 default : u8_next ((p), (s)))
660 # endif
661 # endif
662 # ifdef __cplusplus
663 template <typename T>
664 T * u16_next_template (ucs4_t *puc, T* s);
665 template <>
666 inline uint16_t * u16_next_template (ucs4_t *puc, uint16_t *s)
667 { return const_cast<uint16_t *>(u16_next (puc, s)); }
668 template <>
669 inline const uint16_t * u16_next_template (ucs4_t *puc, const uint16_t *s)
670 { return u16_next (puc, s); }
671 # undef u16_next
672 # define u16_next u16_next_template
673 # elif !defined u16_next
674 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
675 || defined __ICC || defined __TINYC__ \
676 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
677 # define u16_next(p,s) \
678 _Generic ((s), \
679 uint16_t *: (uint16_t *) u16_next ((p), (s)), \
680 default : u16_next ((p), (s)))
681 # endif
682 # endif
683 # ifdef __cplusplus
684 template <typename T>
685 T * u32_next_template (ucs4_t *puc, T* s);
686 template <>
687 inline uint32_t * u32_next_template (ucs4_t *puc, uint32_t *s)
688 { return const_cast<uint32_t *>(u32_next (puc, s)); }
689 template <>
690 inline const uint32_t * u32_next_template (ucs4_t *puc, const uint32_t *s)
691 { return u32_next (puc, s); }
692 # undef u32_next
693 # define u32_next u32_next_template
694 # elif !defined u32_next
695 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
696 || defined __ICC || defined __TINYC__ \
697 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
698 # define u32_next(p,s) \
699 _Generic ((s), \
700 uint32_t *: (uint32_t *) u32_next ((p), (s)), \
701 default : u32_next ((p), (s)))
702 # endif
703 # endif
704 #endif
705
706 /* Backward iteration step. Advances the pointer to point to the previous
707 character, or returns NULL if the beginning of the string had been reached.
708 Puts the character's 'ucs4_t' representation in *PUC. */
709 extern const uint8_t *
710 u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
711 extern const uint16_t *
712 u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
713 extern const uint32_t *
714 u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
715 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
716 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
717 want compiler warnings for 'const' related mistakes. */
718 # ifdef __cplusplus
719 template <typename T>
720 T * u8_prev_template (ucs4_t *puc, T* s, const uint8_t *start);
721 template <>
722 inline uint8_t * u8_prev_template (ucs4_t *puc, uint8_t *s, const uint8_t *start)
723 { return const_cast<uint8_t *>(u8_prev (puc, s, start)); }
724 template <>
725 inline const uint8_t * u8_prev_template (ucs4_t *puc, const uint8_t *s, const uint8_t *start)
726 { return u8_prev (puc, s, start); }
727 # undef u8_prev
728 # define u8_prev u8_prev_template
729 # elif !defined u8_prev
730 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
731 || defined __ICC || defined __TINYC__ \
732 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
733 # define u8_prev(p,s,start) \
734 _Generic ((s), \
735 uint8_t *: (uint8_t *) u8_prev ((p), (s), (start)), \
736 default : u8_prev ((p), (s), (start)))
737 # endif
738 # endif
739 # ifdef __cplusplus
740 template <typename T>
741 T * u16_prev_template (ucs4_t *puc, T* s, const uint16_t *start);
742 template <>
743 inline uint16_t * u16_prev_template (ucs4_t *puc, uint16_t *s, const uint16_t *start)
744 { return const_cast<uint16_t *>(u16_prev (puc, s, start)); }
745 template <>
746 inline const uint16_t * u16_prev_template (ucs4_t *puc, const uint16_t *s, const uint16_t *start)
747 { return u16_prev (puc, s, start); }
748 # undef u16_prev
749 # define u16_prev u16_prev_template
750 # elif !defined u16_prev
751 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
752 || defined __ICC || defined __TINYC__ \
753 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
754 # define u16_prev(p,s,start) \
755 _Generic ((s), \
756 uint16_t *: (uint16_t *) u16_prev ((p), (s), (start)), \
757 default : u16_prev ((p), (s), (start)))
758 # endif
759 # endif
760 # ifdef __cplusplus
761 template <typename T>
762 T * u32_prev_template (ucs4_t *puc, T* s, const uint32_t *start);
763 template <>
764 inline uint32_t * u32_prev_template (ucs4_t *puc, uint32_t *s, const uint32_t *start)
765 { return const_cast<uint32_t *>(u32_prev (puc, s, start)); }
766 template <>
767 inline const uint32_t * u32_prev_template (ucs4_t *puc, const uint32_t *s, const uint32_t *start)
768 { return u32_prev (puc, s, start); }
769 # undef u32_prev
770 # define u32_prev u32_prev_template
771 # elif !defined u32_prev
772 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
773 || defined __ICC || defined __TINYC__ \
774 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
775 # define u32_prev(p,s,start) \
776 _Generic ((s), \
777 uint32_t *: (uint32_t *) u32_prev ((p), (s), (start)), \
778 default : u32_prev ((p), (s), (start)))
779 # endif
780 # endif
781 #endif
782
783 /* Return the number of units in S. */
784 /* Similar to strlen(), wcslen(). */
785 extern size_t
786 u8_strlen (const uint8_t *s)
787 _UC_ATTRIBUTE_PURE;
788 extern size_t
789 u16_strlen (const uint16_t *s)
790 _UC_ATTRIBUTE_PURE;
791 extern size_t
792 u32_strlen (const uint32_t *s)
793 _UC_ATTRIBUTE_PURE;
794
795 /* Return the number of units in S, but at most MAXLEN. */
796 /* Similar to strnlen(), wcsnlen(). */
797 extern size_t
798 u8_strnlen (const uint8_t *s, size_t maxlen)
799 _UC_ATTRIBUTE_PURE;
800 extern size_t
801 u16_strnlen (const uint16_t *s, size_t maxlen)
802 _UC_ATTRIBUTE_PURE;
803 extern size_t
804 u32_strnlen (const uint32_t *s, size_t maxlen)
805 _UC_ATTRIBUTE_PURE;
806
807 /* Copy SRC to DEST. */
808 /* Similar to strcpy(), wcscpy(). */
809 extern uint8_t *
810 u8_strcpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src);
811 extern uint16_t *
812 u16_strcpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src);
813 extern uint32_t *
814 u32_strcpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src);
815
816 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST. */
817 /* Similar to stpcpy(). */
818 extern uint8_t *
819 u8_stpcpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src);
820 extern uint16_t *
821 u16_stpcpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src);
822 extern uint32_t *
823 u32_stpcpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src);
824
825 /* Copy no more than N units of SRC to DEST. */
826 /* Similar to strncpy(), wcsncpy(). */
827 extern uint8_t *
828 u8_strncpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
829 extern uint16_t *
830 u16_strncpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
831 extern uint32_t *
832 u32_strncpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
833
834 /* Copy no more than N units of SRC to DEST. Return a pointer past the last
835 non-NUL unit written into DEST. */
836 /* Similar to stpncpy(). */
837 extern uint8_t *
838 u8_stpncpy (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
839 extern uint16_t *
840 u16_stpncpy (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
841 extern uint32_t *
842 u32_stpncpy (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
843
844 /* Append SRC onto DEST. */
845 /* Similar to strcat(), wcscat(). */
846 extern uint8_t *
847 u8_strcat (uint8_t *_UC_RESTRICT dest, const uint8_t *src);
848 extern uint16_t *
849 u16_strcat (uint16_t *_UC_RESTRICT dest, const uint16_t *src);
850 extern uint32_t *
851 u32_strcat (uint32_t *_UC_RESTRICT dest, const uint32_t *src);
852
853 /* Append no more than N units of SRC onto DEST. */
854 /* Similar to strncat(), wcsncat(). */
855 extern uint8_t *
856 u8_strncat (uint8_t *_UC_RESTRICT dest, const uint8_t *src, size_t n);
857 extern uint16_t *
858 u16_strncat (uint16_t *_UC_RESTRICT dest, const uint16_t *src, size_t n);
859 extern uint32_t *
860 u32_strncat (uint32_t *_UC_RESTRICT dest, const uint32_t *src, size_t n);
861
862 /* Compare S1 and S2. */
863 /* Similar to strcmp(), wcscmp(). */
864 #ifdef __sun
865 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc. */
866 extern int
867 u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2)
868 _UC_ATTRIBUTE_PURE;
869 # define u8_strcmp u8_strcmp_gnu
870 #else
871 extern int
872 u8_strcmp (const uint8_t *s1, const uint8_t *s2)
873 _UC_ATTRIBUTE_PURE;
874 #endif
875 extern int
876 u16_strcmp (const uint16_t *s1, const uint16_t *s2)
877 _UC_ATTRIBUTE_PURE;
878 extern int
879 u32_strcmp (const uint32_t *s1, const uint32_t *s2)
880 _UC_ATTRIBUTE_PURE;
881
882 /* Compare S1 and S2 using the collation rules of the current locale.
883 Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
884 Upon failure, set errno and return any value. */
885 /* Similar to strcoll(), wcscoll(). */
886 extern int
887 u8_strcoll (const uint8_t *s1, const uint8_t *s2);
888 extern int
889 u16_strcoll (const uint16_t *s1, const uint16_t *s2);
890 extern int
891 u32_strcoll (const uint32_t *s1, const uint32_t *s2);
892
893 /* Compare no more than N units of S1 and S2. */
894 /* Similar to strncmp(), wcsncmp(). */
895 extern int
896 u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n)
897 _UC_ATTRIBUTE_PURE;
898 extern int
899 u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n)
900 _UC_ATTRIBUTE_PURE;
901 extern int
902 u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n)
903 _UC_ATTRIBUTE_PURE;
904
905 /* Duplicate S, returning an identical malloc'd string. */
906 /* Similar to strdup(), wcsdup(). */
907 extern uint8_t *
908 u8_strdup (const uint8_t *s)
909 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
910 extern uint16_t *
911 u16_strdup (const uint16_t *s)
912 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
913 extern uint32_t *
914 u32_strdup (const uint32_t *s)
915 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE;
916
917 /* Find the first occurrence of UC in STR. */
918 /* Similar to strchr(), wcschr(). */
919 extern uint8_t *
920 u8_strchr (const uint8_t *str, ucs4_t uc)
921 _UC_ATTRIBUTE_PURE;
922 extern uint16_t *
923 u16_strchr (const uint16_t *str, ucs4_t uc)
924 _UC_ATTRIBUTE_PURE;
925 extern uint32_t *
926 u32_strchr (const uint32_t *str, ucs4_t uc)
927 _UC_ATTRIBUTE_PURE;
928 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
929 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
930 want compiler warnings for 'const' related mistakes. */
931 # ifdef __cplusplus
932 template <typename T>
933 T * u8_strchr_template (T* str, ucs4_t uc);
934 template <>
935 inline uint8_t * u8_strchr_template (uint8_t *str, ucs4_t uc)
936 { return u8_strchr (str, uc); }
937 template <>
938 inline const uint8_t * u8_strchr_template (const uint8_t *str, ucs4_t uc)
939 { return u8_strchr (str, uc); }
940 # undef u8_strchr
941 # define u8_strchr u8_strchr_template
942 # elif !defined u8_strchr
943 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
944 || defined __ICC || defined __TINYC__ \
945 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
946 # define u8_strchr(s,u) \
947 _Generic ((s), \
948 uint8_t const *: (uint8_t const *) u8_strchr ((s), (u)), \
949 default : u8_strchr ((s), (u)))
950 # endif
951 # endif
952 # ifdef __cplusplus
953 template <typename T>
954 T * u16_strchr_template (T* str, ucs4_t uc);
955 template <>
956 inline uint16_t * u16_strchr_template (uint16_t *str, ucs4_t uc)
957 { return u16_strchr (str, uc); }
958 template <>
959 inline const uint16_t * u16_strchr_template (const uint16_t *str, ucs4_t uc)
960 { return u16_strchr (str, uc); }
961 # undef u16_strchr
962 # define u16_strchr u16_strchr_template
963 # elif !defined u16_strchr
964 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
965 || defined __ICC || defined __TINYC__ \
966 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
967 # define u16_strchr(s,u) \
968 _Generic ((s), \
969 uint16_t const *: (uint16_t const *) u16_strchr ((s), (u)), \
970 default : u16_strchr ((s), (u)))
971 # endif
972 # endif
973 # ifdef __cplusplus
974 template <typename T>
975 T * u32_strchr_template (T* str, ucs4_t uc);
976 template <>
977 inline uint32_t * u32_strchr_template (uint32_t *str, ucs4_t uc)
978 { return u32_strchr (str, uc); }
979 template <>
980 inline const uint32_t * u32_strchr_template (const uint32_t *str, ucs4_t uc)
981 { return u32_strchr (str, uc); }
982 # undef u32_strchr
983 # define u32_strchr u32_strchr_template
984 # elif !defined u32_strchr
985 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
986 || defined __ICC || defined __TINYC__ \
987 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
988 # define u32_strchr(s,u) \
989 _Generic ((s), \
990 uint32_t const *: (uint32_t const *) u32_strchr ((s), (u)), \
991 default : u32_strchr ((s), (u)))
992 # endif
993 # endif
994 #endif
995
996 /* Find the last occurrence of UC in STR. */
997 /* Similar to strrchr(), wcsrchr(). */
998 extern uint8_t *
999 u8_strrchr (const uint8_t *str, ucs4_t uc)
1000 _UC_ATTRIBUTE_PURE;
1001 extern uint16_t *
1002 u16_strrchr (const uint16_t *str, ucs4_t uc)
1003 _UC_ATTRIBUTE_PURE;
1004 extern uint32_t *
1005 u32_strrchr (const uint32_t *str, ucs4_t uc)
1006 _UC_ATTRIBUTE_PURE;
1007 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
1008 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
1009 want compiler warnings for 'const' related mistakes. */
1010 # ifdef __cplusplus
1011 template <typename T>
1012 T * u8_strrchr_template (T* str, ucs4_t uc);
1013 template <>
1014 inline uint8_t * u8_strrchr_template (uint8_t *str, ucs4_t uc)
1015 { return u8_strrchr (str, uc); }
1016 template <>
1017 inline const uint8_t * u8_strrchr_template (const uint8_t *str, ucs4_t uc)
1018 { return u8_strrchr (str, uc); }
1019 # undef u8_strrchr
1020 # define u8_strrchr u8_strrchr_template
1021 # elif !defined u8_strrchr
1022 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1023 || defined __ICC || defined __TINYC__ \
1024 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1025 # define u8_strrchr(s,u) \
1026 _Generic ((s), \
1027 uint8_t const *: (uint8_t const *) u8_strrchr ((s), (u)), \
1028 default : u8_strrchr ((s), (u)))
1029 # endif
1030 # endif
1031 # ifdef __cplusplus
1032 template <typename T>
1033 T * u16_strrchr_template (T* str, ucs4_t uc);
1034 template <>
1035 inline uint16_t * u16_strrchr_template (uint16_t *str, ucs4_t uc)
1036 { return u16_strrchr (str, uc); }
1037 template <>
1038 inline const uint16_t * u16_strrchr_template (const uint16_t *str, ucs4_t uc)
1039 { return u16_strrchr (str, uc); }
1040 # undef u16_strrchr
1041 # define u16_strrchr u16_strrchr_template
1042 # elif !defined u16_strrchr
1043 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1044 || defined __ICC || defined __TINYC__ \
1045 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1046 # define u16_strrchr(s,u) \
1047 _Generic ((s), \
1048 uint16_t const *: (uint16_t const *) u16_strrchr ((s), (u)), \
1049 default : u16_strrchr ((s), (u)))
1050 # endif
1051 # endif
1052 # ifdef __cplusplus
1053 template <typename T>
1054 T * u32_strrchr_template (T* str, ucs4_t uc);
1055 template <>
1056 inline uint32_t * u32_strrchr_template (uint32_t *str, ucs4_t uc)
1057 { return u32_strrchr (str, uc); }
1058 template <>
1059 inline const uint32_t * u32_strrchr_template (const uint32_t *str, ucs4_t uc)
1060 { return u32_strrchr (str, uc); }
1061 # undef u32_strrchr
1062 # define u32_strrchr u32_strrchr_template
1063 # elif !defined u32_strrchr
1064 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1065 || defined __ICC || defined __TINYC__ \
1066 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1067 # define u32_strrchr(s,u) \
1068 _Generic ((s), \
1069 uint32_t const *: (uint32_t const *) u32_strrchr ((s), (u)), \
1070 default : u32_strrchr ((s), (u)))
1071 # endif
1072 # endif
1073 #endif
1074
1075 /* Return the length of the initial segment of STR which consists entirely
1076 of Unicode characters not in REJECT. */
1077 /* Similar to strcspn(), wcscspn(). */
1078 extern size_t
1079 u8_strcspn (const uint8_t *str, const uint8_t *reject)
1080 _UC_ATTRIBUTE_PURE;
1081 extern size_t
1082 u16_strcspn (const uint16_t *str, const uint16_t *reject)
1083 _UC_ATTRIBUTE_PURE;
1084 extern size_t
1085 u32_strcspn (const uint32_t *str, const uint32_t *reject)
1086 _UC_ATTRIBUTE_PURE;
1087
1088 /* Return the length of the initial segment of STR which consists entirely
1089 of Unicode characters in ACCEPT. */
1090 /* Similar to strspn(), wcsspn(). */
1091 extern size_t
1092 u8_strspn (const uint8_t *str, const uint8_t *accept)
1093 _UC_ATTRIBUTE_PURE;
1094 extern size_t
1095 u16_strspn (const uint16_t *str, const uint16_t *accept)
1096 _UC_ATTRIBUTE_PURE;
1097 extern size_t
1098 u32_strspn (const uint32_t *str, const uint32_t *accept)
1099 _UC_ATTRIBUTE_PURE;
1100
1101 /* Find the first occurrence in STR of any character in ACCEPT. */
1102 /* Similar to strpbrk(), wcspbrk(). */
1103 extern uint8_t *
1104 u8_strpbrk (const uint8_t *str, const uint8_t *accept)
1105 _UC_ATTRIBUTE_PURE;
1106 extern uint16_t *
1107 u16_strpbrk (const uint16_t *str, const uint16_t *accept)
1108 _UC_ATTRIBUTE_PURE;
1109 extern uint32_t *
1110 u32_strpbrk (const uint32_t *str, const uint32_t *accept)
1111 _UC_ATTRIBUTE_PURE;
1112 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
1113 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
1114 want compiler warnings for 'const' related mistakes. */
1115 # ifdef __cplusplus
1116 template <typename T>
1117 T * u8_strpbrk_template (T* str, const uint8_t *accept);
1118 template <>
1119 inline uint8_t * u8_strpbrk_template (uint8_t *str, const uint8_t *accept)
1120 { return u8_strpbrk (str, accept); }
1121 template <>
1122 inline const uint8_t * u8_strpbrk_template (const uint8_t *str, const uint8_t *accept)
1123 { return u8_strpbrk (str, accept); }
1124 # undef u8_strpbrk
1125 # define u8_strpbrk u8_strpbrk_template
1126 # elif !defined u8_strpbrk
1127 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1128 || defined __ICC || defined __TINYC__ \
1129 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1130 # define u8_strpbrk(s,a) \
1131 _Generic ((s), \
1132 uint8_t const *: (uint8_t const *) u8_strpbrk ((s), (a)), \
1133 default : u8_strpbrk ((s), (a)))
1134 # endif
1135 # endif
1136 # ifdef __cplusplus
1137 template <typename T>
1138 T * u16_strpbrk_template (T* str, const uint16_t *accept);
1139 template <>
1140 inline uint16_t * u16_strpbrk_template (uint16_t *str, const uint16_t *accept)
1141 { return u16_strpbrk (str, accept); }
1142 template <>
1143 inline const uint16_t * u16_strpbrk_template (const uint16_t *str, const uint16_t *accept)
1144 { return u16_strpbrk (str, accept); }
1145 # undef u16_strpbrk
1146 # define u16_strpbrk u16_strpbrk_template
1147 # elif !defined u16_strpbrk
1148 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1149 || defined __ICC || defined __TINYC__ \
1150 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1151 # define u16_strpbrk(s,a) \
1152 _Generic ((s), \
1153 uint16_t const *: (uint16_t const *) u16_strpbrk ((s), (a)), \
1154 default : u16_strpbrk ((s), (a)))
1155 # endif
1156 # endif
1157 # ifdef __cplusplus
1158 template <typename T>
1159 T * u32_strpbrk_template (T* str, const uint32_t *accept);
1160 template <>
1161 inline uint32_t * u32_strpbrk_template (uint32_t *str, const uint32_t *accept)
1162 { return u32_strpbrk (str, accept); }
1163 template <>
1164 inline const uint32_t * u32_strpbrk_template (const uint32_t *str, const uint32_t *accept)
1165 { return u32_strpbrk (str, accept); }
1166 # undef u32_strpbrk
1167 # define u32_strpbrk u32_strpbrk_template
1168 # elif !defined u32_strpbrk
1169 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1170 || defined __ICC || defined __TINYC__ \
1171 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1172 # define u32_strpbrk(s,a) \
1173 _Generic ((s), \
1174 uint32_t const *: (uint32_t const *) u32_strpbrk ((s), (a)), \
1175 default : u32_strpbrk ((s), (a)))
1176 # endif
1177 # endif
1178 #endif
1179
1180 /* Find the first occurrence of NEEDLE in HAYSTACK. */
1181 /* Similar to strstr(), wcsstr(). */
1182 extern uint8_t *
1183 u8_strstr (const uint8_t *haystack, const uint8_t *needle)
1184 _UC_ATTRIBUTE_PURE;
1185 extern uint16_t *
1186 u16_strstr (const uint16_t *haystack, const uint16_t *needle)
1187 _UC_ATTRIBUTE_PURE;
1188 extern uint32_t *
1189 u32_strstr (const uint32_t *haystack, const uint32_t *needle)
1190 _UC_ATTRIBUTE_PURE;
1191 #ifndef _LIBUNISTRING_NO_CONST_GENERICS
1192 /* Don't silently convert a 'const uintN_t *' to a 'uintN_t *'. Programmers
1193 want compiler warnings for 'const' related mistakes. */
1194 # ifdef __cplusplus
1195 template <typename T>
1196 T * u8_strstr_template (T* haystack, const uint8_t *needle);
1197 template <>
1198 inline uint8_t * u8_strstr_template (uint8_t *haystack, const uint8_t *needle)
1199 { return u8_strstr (haystack, needle); }
1200 template <>
1201 inline const uint8_t * u8_strstr_template (const uint8_t *haystack, const uint8_t *needle)
1202 { return u8_strstr (haystack, needle); }
1203 # undef u8_strstr
1204 # define u8_strstr u8_strstr_template
1205 # elif !defined u8_strstr
1206 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1207 || defined __ICC || defined __TINYC__ \
1208 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1209 # define u8_strstr(h,n) \
1210 _Generic ((h), \
1211 uint8_t const *: (uint8_t const *) u8_strstr ((h), (n)), \
1212 default : u8_strstr ((h), (n)))
1213 # endif
1214 # endif
1215 # ifdef __cplusplus
1216 template <typename T>
1217 T * u16_strstr_template (T* haystack, const uint16_t *needle);
1218 template <>
1219 inline uint16_t * u16_strstr_template (uint16_t *haystack, const uint16_t *needle)
1220 { return u16_strstr (haystack, needle); }
1221 template <>
1222 inline const uint16_t * u16_strstr_template (const uint16_t *haystack, const uint16_t *needle)
1223 { return u16_strstr (haystack, needle); }
1224 # undef u16_strstr
1225 # define u16_strstr u16_strstr_template
1226 # elif !defined u16_strstr
1227 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1228 || defined __ICC || defined __TINYC__ \
1229 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1230 # define u16_strstr(h,n) \
1231 _Generic ((h), \
1232 uint16_t const *: (uint16_t const *) u16_strstr ((h), (n)), \
1233 default : u16_strstr ((h), (n)))
1234 # endif
1235 # endif
1236 # ifdef __cplusplus
1237 template <typename T>
1238 T * u32_strstr_template (T* haystack, const uint32_t *needle);
1239 template <>
1240 inline uint32_t * u32_strstr_template (uint32_t *haystack, const uint32_t *needle)
1241 { return u32_strstr (haystack, needle); }
1242 template <>
1243 inline const uint32_t * u32_strstr_template (const uint32_t *haystack, const uint32_t *needle)
1244 { return u32_strstr (haystack, needle); }
1245 # undef u32_strstr
1246 # define u32_strstr u32_strstr_template
1247 # elif !defined u32_strstr
1248 # if ((__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) || (__clang_major__ >= 3) \
1249 || defined __ICC || defined __TINYC__ \
1250 || (__STDC_VERSION__ >= 201112L && !(defined __GNUC__ || defined __clang__)))
1251 # define u32_strstr(h,n) \
1252 _Generic ((h), \
1253 uint32_t const *: (uint32_t const *) u32_strstr ((h), (n)), \
1254 default : u32_strstr ((h), (n)))
1255 # endif
1256 # endif
1257 #endif
1258
1259 /* Test whether STR starts with PREFIX. */
1260 extern bool
1261 u8_startswith (const uint8_t *str, const uint8_t *prefix)
1262 _UC_ATTRIBUTE_PURE;
1263 extern bool
1264 u16_startswith (const uint16_t *str, const uint16_t *prefix)
1265 _UC_ATTRIBUTE_PURE;
1266 extern bool
1267 u32_startswith (const uint32_t *str, const uint32_t *prefix)
1268 _UC_ATTRIBUTE_PURE;
1269
1270 /* Test whether STR ends with SUFFIX. */
1271 extern bool
1272 u8_endswith (const uint8_t *str, const uint8_t *suffix)
1273 _UC_ATTRIBUTE_PURE;
1274 extern bool
1275 u16_endswith (const uint16_t *str, const uint16_t *suffix)
1276 _UC_ATTRIBUTE_PURE;
1277 extern bool
1278 u32_endswith (const uint32_t *str, const uint32_t *suffix)
1279 _UC_ATTRIBUTE_PURE;
1280
1281 /* Divide STR into tokens separated by characters in DELIM.
1282 This interface is actually more similar to wcstok than to strtok. */
1283 /* Similar to strtok_r(), wcstok(). */
1284 extern uint8_t *
1285 u8_strtok (uint8_t *_UC_RESTRICT str, const uint8_t *delim,
1286 uint8_t **ptr);
1287 extern uint16_t *
1288 u16_strtok (uint16_t *_UC_RESTRICT str, const uint16_t *delim,
1289 uint16_t **ptr);
1290 extern uint32_t *
1291 u32_strtok (uint32_t *_UC_RESTRICT str, const uint32_t *delim,
1292 uint32_t **ptr);
1293
1294
1295 #ifdef __cplusplus
1296 }
1297 #endif
1298
1299 #endif /* _UNISTR_H */