Debian Codegraph @

lib/unistr.in.h

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