Bump to version 0.9.4
[platform/upstream/libunistring.git] / lib / unistr.in.h
1 /* Elementary Unicode string functions.
2    Copyright (C) 2001-2002, 2005-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify it
5    under the terms of the GNU Lesser General Public License as published
6    by the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #ifndef _UNISTR_H
18 #define _UNISTR_H
19
20 #include "unitypes.h"
21
22 /* Get common macros for C.  */
23 #include <unistring/cdefs.h>
24
25 /* Get inline if available.  */
26 #include <unistring/inline.h>
27
28 /* Get bool.  */
29 #include <unistring/stdbool.h>
30
31 /* Get size_t.  */
32 #include <stddef.h>
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38
39 /* Conventions:
40
41    All functions prefixed with u8_ operate on UTF-8 encoded strings.
42    Their unit is an uint8_t (1 byte).
43
44    All functions prefixed with u16_ operate on UTF-16 encoded strings.
45    Their unit is an uint16_t (a 2-byte word).
46
47    All functions prefixed with u32_ operate on UCS-4 encoded strings.
48    Their unit is an uint32_t (a 4-byte word).
49
50    All argument pairs (s, n) denote a Unicode string s[0..n-1] with exactly
51    n units.
52
53    All arguments starting with "str" and the arguments of functions starting
54    with u8_str/u16_str/u32_str denote a NUL terminated string, i.e. a string
55    which terminates at the first NUL unit.  This termination unit is
56    considered part of the string for all memory allocation purposes, but
57    is not considered part of the string for all other logical purposes.
58
59    Functions returning a string result take a (resultbuf, lengthp) argument
60    pair.  If resultbuf is not NULL and the result fits into *lengthp units,
61    it is put in resultbuf, and resultbuf is returned.  Otherwise, a freshly
62    allocated string is returned.  In both cases, *lengthp is set to the
63    length (number of units) of the returned string.  In case of error,
64    NULL is returned and errno is set.  */
65
66
67 /* Elementary string checks.  */
68
69 /* Check whether an UTF-8 string is well-formed.
70    Return NULL if valid, or a pointer to the first invalid unit otherwise.  */
71 extern const uint8_t *
72        u8_check (const uint8_t *s, size_t n)
73        _UC_ATTRIBUTE_PURE;
74
75 /* Check whether an UTF-16 string is well-formed.
76    Return NULL if valid, or a pointer to the first invalid unit otherwise.  */
77 extern const uint16_t *
78        u16_check (const uint16_t *s, size_t n)
79        _UC_ATTRIBUTE_PURE;
80
81 /* Check whether an UCS-4 string is well-formed.
82    Return NULL if valid, or a pointer to the first invalid unit otherwise.  */
83 extern const uint32_t *
84        u32_check (const uint32_t *s, size_t n)
85        _UC_ATTRIBUTE_PURE;
86
87
88 /* Elementary string conversions.  */
89
90 /* Convert an UTF-8 string to an UTF-16 string.  */
91 extern uint16_t *
92        u8_to_u16 (const uint8_t *s, size_t n, uint16_t *resultbuf,
93                   size_t *lengthp);
94
95 /* Convert an UTF-8 string to an UCS-4 string.  */
96 extern uint32_t *
97        u8_to_u32 (const uint8_t *s, size_t n, uint32_t *resultbuf,
98                   size_t *lengthp);
99
100 /* Convert an UTF-16 string to an UTF-8 string.  */
101 extern uint8_t *
102        u16_to_u8 (const uint16_t *s, size_t n, uint8_t *resultbuf,
103                   size_t *lengthp);
104
105 /* Convert an UTF-16 string to an UCS-4 string.  */
106 extern uint32_t *
107        u16_to_u32 (const uint16_t *s, size_t n, uint32_t *resultbuf,
108                    size_t *lengthp);
109
110 /* Convert an UCS-4 string to an UTF-8 string.  */
111 extern uint8_t *
112        u32_to_u8 (const uint32_t *s, size_t n, uint8_t *resultbuf,
113                   size_t *lengthp);
114
115 /* Convert an UCS-4 string to an UTF-16 string.  */
116 extern uint16_t *
117        u32_to_u16 (const uint32_t *s, size_t n, uint16_t *resultbuf,
118                    size_t *lengthp);
119
120
121 /* Elementary string functions.  */
122
123 /* Return the length (number of units) of the first character in S, which is
124    no longer than N.  Return 0 if it is the NUL character.  Return -1 upon
125    failure.  */
126 /* Similar to mblen(), except that s must not be NULL.  */
127 extern int
128        u8_mblen (const uint8_t *s, size_t n)
129        _UC_ATTRIBUTE_PURE;
130 extern int
131        u16_mblen (const uint16_t *s, size_t n)
132        _UC_ATTRIBUTE_PURE;
133 extern int
134        u32_mblen (const uint32_t *s, size_t n)
135        _UC_ATTRIBUTE_PURE;
136
137 /* Return the length (number of units) of the first character in S, putting
138    its 'ucs4_t' representation in *PUC.  Upon failure, *PUC is set to 0xfffd,
139    and an appropriate number of units is returned.
140    The number of available units, N, must be > 0.  */
141 /* Similar to mbtowc(), except that puc and s must not be NULL, n must be > 0,
142    and the NUL character is not treated specially.  */
143 /* The variants with _safe suffix are safe, even if the library is compiled
144    without --enable-safety.  */
145
146 #if 1
147 # if !UNISTRING_HAVE_INLINE
148 extern int
149        u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n);
150 # else
151 extern int
152        u8_mbtouc_unsafe_aux (ucs4_t *puc, const uint8_t *s, size_t n);
153 static inline int
154 u8_mbtouc_unsafe (ucs4_t *puc, const uint8_t *s, size_t n)
155 {
156   uint8_t c = *s;
157
158   if (c < 0x80)
159     {
160       *puc = c;
161       return 1;
162     }
163   else
164     return u8_mbtouc_unsafe_aux (puc, s, n);
165 }
166 # endif
167 #endif
168
169 #if 1
170 # if !UNISTRING_HAVE_INLINE
171 extern int
172        u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n);
173 # else
174 extern int
175        u16_mbtouc_unsafe_aux (ucs4_t *puc, const uint16_t *s, size_t n);
176 static inline int
177 u16_mbtouc_unsafe (ucs4_t *puc, const uint16_t *s, size_t n)
178 {
179   uint16_t c = *s;
180
181   if (c < 0xd800 || c >= 0xe000)
182     {
183       *puc = c;
184       return 1;
185     }
186   else
187     return u16_mbtouc_unsafe_aux (puc, s, n);
188 }
189 # endif
190 #endif
191
192 #if 1
193 # if !UNISTRING_HAVE_INLINE
194 extern int
195        u32_mbtouc_unsafe (ucs4_t *puc, const uint32_t *s, size_t n);
196 # else
197 static inline int
198 u32_mbtouc_unsafe (ucs4_t *puc,
199                    const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
200 {
201   uint32_t c = *s;
202
203 #  if CONFIG_UNICODE_SAFETY
204   if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
205 #  endif
206     *puc = c;
207 #  if CONFIG_UNICODE_SAFETY
208   else
209     /* invalid multibyte character */
210     *puc = 0xfffd;
211 #  endif
212   return 1;
213 }
214 # endif
215 #endif
216
217 #if 1
218 # if !UNISTRING_HAVE_INLINE
219 extern int
220        u8_mbtouc (ucs4_t *puc, const uint8_t *s, size_t n);
221 # else
222 extern int
223        u8_mbtouc_aux (ucs4_t *puc, const uint8_t *s, size_t n);
224 static inline int
225 u8_mbtouc (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_aux (puc, s, n);
236 }
237 # endif
238 #endif
239
240 #if 1
241 # if !UNISTRING_HAVE_INLINE
242 extern int
243        u16_mbtouc (ucs4_t *puc, const uint16_t *s, size_t n);
244 # else
245 extern int
246        u16_mbtouc_aux (ucs4_t *puc, const uint16_t *s, size_t n);
247 static inline int
248 u16_mbtouc (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_aux (puc, s, n);
259 }
260 # endif
261 #endif
262
263 #if 1
264 # if !UNISTRING_HAVE_INLINE
265 extern int
266        u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n);
267 # else
268 static inline int
269 u32_mbtouc (ucs4_t *puc, const uint32_t *s, size_t n _GL_UNUSED_PARAMETER)
270 {
271   uint32_t c = *s;
272
273   if (c < 0xd800 || (c >= 0xe000 && c < 0x110000))
274     *puc = c;
275   else
276     /* invalid multibyte character */
277     *puc = 0xfffd;
278   return 1;
279 }
280 # endif
281 #endif
282
283 /* Return the length (number of units) of the first character in S, putting
284    its 'ucs4_t' representation in *PUC.  Upon failure, *PUC is set to 0xfffd,
285    and -1 is returned for an invalid sequence of units, -2 is returned for an
286    incomplete sequence of units.
287    The number of available units, N, must be > 0.  */
288 /* Similar to u*_mbtouc(), except that the return value gives more details
289    about the failure, similar to mbrtowc().  */
290
291 #if 1
292 extern int
293        u8_mbtoucr (ucs4_t *puc, const uint8_t *s, size_t n);
294 #endif
295
296 #if 1
297 extern int
298        u16_mbtoucr (ucs4_t *puc, const uint16_t *s, size_t n);
299 #endif
300
301 #if 1
302 extern int
303        u32_mbtoucr (ucs4_t *puc, const uint32_t *s, size_t n);
304 #endif
305
306 /* Put the multibyte character represented by UC in S, returning its
307    length.  Return -1 upon failure, -2 if the number of available units, N,
308    is too small.  The latter case cannot occur if N >= 6/2/1, respectively.  */
309 /* Similar to wctomb(), except that s must not be NULL, and the argument n
310    must be specified.  */
311
312 #if 1
313 /* Auxiliary function, also used by u8_chr, u8_strchr, u8_strrchr.  */
314 extern int
315        u8_uctomb_aux (uint8_t *s, ucs4_t uc, int n);
316 # if !UNISTRING_HAVE_INLINE
317 extern int
318        u8_uctomb (uint8_t *s, ucs4_t uc, int n);
319 # else
320 static inline int
321 u8_uctomb (uint8_t *s, ucs4_t uc, int n)
322 {
323   if (uc < 0x80 && n > 0)
324     {
325       s[0] = uc;
326       return 1;
327     }
328   else
329     return u8_uctomb_aux (s, uc, n);
330 }
331 # endif
332 #endif
333
334 #if 1
335 /* Auxiliary function, also used by u16_chr, u16_strchr, u16_strrchr.  */
336 extern int
337        u16_uctomb_aux (uint16_t *s, ucs4_t uc, int n);
338 # if !UNISTRING_HAVE_INLINE
339 extern int
340        u16_uctomb (uint16_t *s, ucs4_t uc, int n);
341 # else
342 static inline int
343 u16_uctomb (uint16_t *s, ucs4_t uc, int n)
344 {
345   if (uc < 0xd800 && n > 0)
346     {
347       s[0] = uc;
348       return 1;
349     }
350   else
351     return u16_uctomb_aux (s, uc, n);
352 }
353 # endif
354 #endif
355
356 #if 1
357 # if !UNISTRING_HAVE_INLINE
358 extern int
359        u32_uctomb (uint32_t *s, ucs4_t uc, int n);
360 # else
361 static inline int
362 u32_uctomb (uint32_t *s, ucs4_t uc, int n)
363 {
364   if (uc < 0xd800 || (uc >= 0xe000 && uc < 0x110000))
365     {
366       if (n > 0)
367         {
368           *s = uc;
369           return 1;
370         }
371       else
372         return -2;
373     }
374   else
375     return -1;
376 }
377 # endif
378 #endif
379
380 /* Copy N units from SRC to DEST.  */
381 /* Similar to memcpy().  */
382 extern uint8_t *
383        u8_cpy (uint8_t *dest, const uint8_t *src, size_t n);
384 extern uint16_t *
385        u16_cpy (uint16_t *dest, const uint16_t *src, size_t n);
386 extern uint32_t *
387        u32_cpy (uint32_t *dest, const uint32_t *src, size_t n);
388
389 /* Copy N units from SRC to DEST, guaranteeing correct behavior for
390    overlapping memory areas.  */
391 /* Similar to memmove().  */
392 extern uint8_t *
393        u8_move (uint8_t *dest, const uint8_t *src, size_t n);
394 extern uint16_t *
395        u16_move (uint16_t *dest, const uint16_t *src, size_t n);
396 extern uint32_t *
397        u32_move (uint32_t *dest, const uint32_t *src, size_t n);
398
399 /* Set the first N characters of S to UC.  UC should be a character that
400    occupies only 1 unit.  */
401 /* Similar to memset().  */
402 extern uint8_t *
403        u8_set (uint8_t *s, ucs4_t uc, size_t n);
404 extern uint16_t *
405        u16_set (uint16_t *s, ucs4_t uc, size_t n);
406 extern uint32_t *
407        u32_set (uint32_t *s, ucs4_t uc, size_t n);
408
409 /* Compare S1 and S2, each of length N.  */
410 /* Similar to memcmp().  */
411 extern int
412        u8_cmp (const uint8_t *s1, const uint8_t *s2, size_t n)
413        _UC_ATTRIBUTE_PURE;
414 extern int
415        u16_cmp (const uint16_t *s1, const uint16_t *s2, size_t n)
416        _UC_ATTRIBUTE_PURE;
417 extern int
418        u32_cmp (const uint32_t *s1, const uint32_t *s2, size_t n)
419        _UC_ATTRIBUTE_PURE;
420
421 /* Compare S1 and S2.  */
422 /* Similar to the gnulib function memcmp2().  */
423 extern int
424        u8_cmp2 (const uint8_t *s1, size_t n1, const uint8_t *s2, size_t n2)
425        _UC_ATTRIBUTE_PURE;
426 extern int
427        u16_cmp2 (const uint16_t *s1, size_t n1, const uint16_t *s2, size_t n2)
428        _UC_ATTRIBUTE_PURE;
429 extern int
430        u32_cmp2 (const uint32_t *s1, size_t n1, const uint32_t *s2, size_t n2)
431        _UC_ATTRIBUTE_PURE;
432
433 /* Search the string at S for UC.  */
434 /* Similar to memchr().  */
435 extern uint8_t *
436        u8_chr (const uint8_t *s, size_t n, ucs4_t uc)
437        _UC_ATTRIBUTE_PURE;
438 extern uint16_t *
439        u16_chr (const uint16_t *s, size_t n, ucs4_t uc)
440        _UC_ATTRIBUTE_PURE;
441 extern uint32_t *
442        u32_chr (const uint32_t *s, size_t n, ucs4_t uc)
443        _UC_ATTRIBUTE_PURE;
444
445 /* Count the number of Unicode characters in the N units from S.  */
446 /* Similar to mbsnlen().  */
447 extern size_t
448        u8_mbsnlen (const uint8_t *s, size_t n)
449        _UC_ATTRIBUTE_PURE;
450 extern size_t
451        u16_mbsnlen (const uint16_t *s, size_t n)
452        _UC_ATTRIBUTE_PURE;
453 extern size_t
454        u32_mbsnlen (const uint32_t *s, size_t n)
455        _UC_ATTRIBUTE_PURE;
456
457 /* Elementary string functions with memory allocation.  */
458
459 /* Make a freshly allocated copy of S, of length N.  */
460 extern uint8_t *
461        u8_cpy_alloc (const uint8_t *s, size_t n);
462 extern uint16_t *
463        u16_cpy_alloc (const uint16_t *s, size_t n);
464 extern uint32_t *
465        u32_cpy_alloc (const uint32_t *s, size_t n);
466
467 /* Elementary string functions on NUL terminated strings.  */
468
469 /* Return the length (number of units) of the first character in S.
470    Return 0 if it is the NUL character.  Return -1 upon failure.  */
471 extern int
472        u8_strmblen (const uint8_t *s)
473        _UC_ATTRIBUTE_PURE;
474 extern int
475        u16_strmblen (const uint16_t *s)
476        _UC_ATTRIBUTE_PURE;
477 extern int
478        u32_strmblen (const uint32_t *s)
479        _UC_ATTRIBUTE_PURE;
480
481 /* Return the length (number of units) of the first character in S, putting
482    its 'ucs4_t' representation in *PUC.  Return 0 if it is the NUL
483    character.  Return -1 upon failure.  */
484 extern int
485        u8_strmbtouc (ucs4_t *puc, const uint8_t *s);
486 extern int
487        u16_strmbtouc (ucs4_t *puc, const uint16_t *s);
488 extern int
489        u32_strmbtouc (ucs4_t *puc, const uint32_t *s);
490
491 /* Forward iteration step.  Advances the pointer past the next character,
492    or returns NULL if the end of the string has been reached.  Puts the
493    character's 'ucs4_t' representation in *PUC.  */
494 extern const uint8_t *
495        u8_next (ucs4_t *puc, const uint8_t *s);
496 extern const uint16_t *
497        u16_next (ucs4_t *puc, const uint16_t *s);
498 extern const uint32_t *
499        u32_next (ucs4_t *puc, const uint32_t *s);
500
501 /* Backward iteration step.  Advances the pointer to point to the previous
502    character, or returns NULL if the beginning of the string had been reached.
503    Puts the character's 'ucs4_t' representation in *PUC.  */
504 extern const uint8_t *
505        u8_prev (ucs4_t *puc, const uint8_t *s, const uint8_t *start);
506 extern const uint16_t *
507        u16_prev (ucs4_t *puc, const uint16_t *s, const uint16_t *start);
508 extern const uint32_t *
509        u32_prev (ucs4_t *puc, const uint32_t *s, const uint32_t *start);
510
511 /* Return the number of units in S.  */
512 /* Similar to strlen(), wcslen().  */
513 extern size_t
514        u8_strlen (const uint8_t *s)
515        _UC_ATTRIBUTE_PURE;
516 extern size_t
517        u16_strlen (const uint16_t *s)
518        _UC_ATTRIBUTE_PURE;
519 extern size_t
520        u32_strlen (const uint32_t *s)
521        _UC_ATTRIBUTE_PURE;
522
523 /* Return the number of units in S, but at most MAXLEN.  */
524 /* Similar to strnlen(), wcsnlen().  */
525 extern size_t
526        u8_strnlen (const uint8_t *s, size_t maxlen)
527        _UC_ATTRIBUTE_PURE;
528 extern size_t
529        u16_strnlen (const uint16_t *s, size_t maxlen)
530        _UC_ATTRIBUTE_PURE;
531 extern size_t
532        u32_strnlen (const uint32_t *s, size_t maxlen)
533        _UC_ATTRIBUTE_PURE;
534
535 /* Copy SRC to DEST.  */
536 /* Similar to strcpy(), wcscpy().  */
537 extern uint8_t *
538        u8_strcpy (uint8_t *dest, const uint8_t *src);
539 extern uint16_t *
540        u16_strcpy (uint16_t *dest, const uint16_t *src);
541 extern uint32_t *
542        u32_strcpy (uint32_t *dest, const uint32_t *src);
543
544 /* Copy SRC to DEST, returning the address of the terminating NUL in DEST.  */
545 /* Similar to stpcpy().  */
546 extern uint8_t *
547        u8_stpcpy (uint8_t *dest, const uint8_t *src);
548 extern uint16_t *
549        u16_stpcpy (uint16_t *dest, const uint16_t *src);
550 extern uint32_t *
551        u32_stpcpy (uint32_t *dest, const uint32_t *src);
552
553 /* Copy no more than N units of SRC to DEST.  */
554 /* Similar to strncpy(), wcsncpy().  */
555 extern uint8_t *
556        u8_strncpy (uint8_t *dest, const uint8_t *src, size_t n);
557 extern uint16_t *
558        u16_strncpy (uint16_t *dest, const uint16_t *src, size_t n);
559 extern uint32_t *
560        u32_strncpy (uint32_t *dest, const uint32_t *src, size_t n);
561
562 /* Copy no more than N units of SRC to DEST.  Return a pointer past the last
563    non-NUL unit written into DEST.  */
564 /* Similar to stpncpy().  */
565 extern uint8_t *
566        u8_stpncpy (uint8_t *dest, const uint8_t *src, size_t n);
567 extern uint16_t *
568        u16_stpncpy (uint16_t *dest, const uint16_t *src, size_t n);
569 extern uint32_t *
570        u32_stpncpy (uint32_t *dest, const uint32_t *src, size_t n);
571
572 /* Append SRC onto DEST.  */
573 /* Similar to strcat(), wcscat().  */
574 extern uint8_t *
575        u8_strcat (uint8_t *dest, const uint8_t *src);
576 extern uint16_t *
577        u16_strcat (uint16_t *dest, const uint16_t *src);
578 extern uint32_t *
579        u32_strcat (uint32_t *dest, const uint32_t *src);
580
581 /* Append no more than N units of SRC onto DEST.  */
582 /* Similar to strncat(), wcsncat().  */
583 extern uint8_t *
584        u8_strncat (uint8_t *dest, const uint8_t *src, size_t n);
585 extern uint16_t *
586        u16_strncat (uint16_t *dest, const uint16_t *src, size_t n);
587 extern uint32_t *
588        u32_strncat (uint32_t *dest, const uint32_t *src, size_t n);
589
590 /* Compare S1 and S2.  */
591 /* Similar to strcmp(), wcscmp().  */
592 #ifdef __sun
593 /* Avoid a collision with the u8_strcmp() function in Solaris 11 libc.  */
594 extern int
595        u8_strcmp_gnu (const uint8_t *s1, const uint8_t *s2)
596        _UC_ATTRIBUTE_PURE;
597 # define u8_strcmp u8_strcmp_gnu
598 #else
599 extern int
600        u8_strcmp (const uint8_t *s1, const uint8_t *s2)
601        _UC_ATTRIBUTE_PURE;
602 #endif
603 extern int
604        u16_strcmp (const uint16_t *s1, const uint16_t *s2)
605        _UC_ATTRIBUTE_PURE;
606 extern int
607        u32_strcmp (const uint32_t *s1, const uint32_t *s2)
608        _UC_ATTRIBUTE_PURE;
609
610 /* Compare S1 and S2 using the collation rules of the current locale.
611    Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
612    Upon failure, set errno and return any value.  */
613 /* Similar to strcoll(), wcscoll().  */
614 extern int
615        u8_strcoll (const uint8_t *s1, const uint8_t *s2);
616 extern int
617        u16_strcoll (const uint16_t *s1, const uint16_t *s2);
618 extern int
619        u32_strcoll (const uint32_t *s1, const uint32_t *s2);
620
621 /* Compare no more than N units of S1 and S2.  */
622 /* Similar to strncmp(), wcsncmp().  */
623 extern int
624        u8_strncmp (const uint8_t *s1, const uint8_t *s2, size_t n)
625        _UC_ATTRIBUTE_PURE;
626 extern int
627        u16_strncmp (const uint16_t *s1, const uint16_t *s2, size_t n)
628        _UC_ATTRIBUTE_PURE;
629 extern int
630        u32_strncmp (const uint32_t *s1, const uint32_t *s2, size_t n)
631        _UC_ATTRIBUTE_PURE;
632
633 /* Duplicate S, returning an identical malloc'd string.  */
634 /* Similar to strdup(), wcsdup().  */
635 extern uint8_t *
636        u8_strdup (const uint8_t *s);
637 extern uint16_t *
638        u16_strdup (const uint16_t *s);
639 extern uint32_t *
640        u32_strdup (const uint32_t *s);
641
642 /* Find the first occurrence of UC in STR.  */
643 /* Similar to strchr(), wcschr().  */
644 extern uint8_t *
645        u8_strchr (const uint8_t *str, ucs4_t uc)
646        _UC_ATTRIBUTE_PURE;
647 extern uint16_t *
648        u16_strchr (const uint16_t *str, ucs4_t uc)
649        _UC_ATTRIBUTE_PURE;
650 extern uint32_t *
651        u32_strchr (const uint32_t *str, ucs4_t uc)
652        _UC_ATTRIBUTE_PURE;
653
654 /* Find the last occurrence of UC in STR.  */
655 /* Similar to strrchr(), wcsrchr().  */
656 extern uint8_t *
657        u8_strrchr (const uint8_t *str, ucs4_t uc)
658        _UC_ATTRIBUTE_PURE;
659 extern uint16_t *
660        u16_strrchr (const uint16_t *str, ucs4_t uc)
661        _UC_ATTRIBUTE_PURE;
662 extern uint32_t *
663        u32_strrchr (const uint32_t *str, ucs4_t uc)
664        _UC_ATTRIBUTE_PURE;
665
666 /* Return the length of the initial segment of STR which consists entirely
667    of Unicode characters not in REJECT.  */
668 /* Similar to strcspn(), wcscspn().  */
669 extern size_t
670        u8_strcspn (const uint8_t *str, const uint8_t *reject)
671        _UC_ATTRIBUTE_PURE;
672 extern size_t
673        u16_strcspn (const uint16_t *str, const uint16_t *reject)
674        _UC_ATTRIBUTE_PURE;
675 extern size_t
676        u32_strcspn (const uint32_t *str, const uint32_t *reject)
677        _UC_ATTRIBUTE_PURE;
678
679 /* Return the length of the initial segment of STR which consists entirely
680    of Unicode characters in ACCEPT.  */
681 /* Similar to strspn(), wcsspn().  */
682 extern size_t
683        u8_strspn (const uint8_t *str, const uint8_t *accept)
684        _UC_ATTRIBUTE_PURE;
685 extern size_t
686        u16_strspn (const uint16_t *str, const uint16_t *accept)
687        _UC_ATTRIBUTE_PURE;
688 extern size_t
689        u32_strspn (const uint32_t *str, const uint32_t *accept)
690        _UC_ATTRIBUTE_PURE;
691
692 /* Find the first occurrence in STR of any character in ACCEPT.  */
693 /* Similar to strpbrk(), wcspbrk().  */
694 extern uint8_t *
695        u8_strpbrk (const uint8_t *str, const uint8_t *accept)
696        _UC_ATTRIBUTE_PURE;
697 extern uint16_t *
698        u16_strpbrk (const uint16_t *str, const uint16_t *accept)
699        _UC_ATTRIBUTE_PURE;
700 extern uint32_t *
701        u32_strpbrk (const uint32_t *str, const uint32_t *accept)
702        _UC_ATTRIBUTE_PURE;
703
704 /* Find the first occurrence of NEEDLE in HAYSTACK.  */
705 /* Similar to strstr(), wcsstr().  */
706 extern uint8_t *
707        u8_strstr (const uint8_t *haystack, const uint8_t *needle)
708        _UC_ATTRIBUTE_PURE;
709 extern uint16_t *
710        u16_strstr (const uint16_t *haystack, const uint16_t *needle)
711        _UC_ATTRIBUTE_PURE;
712 extern uint32_t *
713        u32_strstr (const uint32_t *haystack, const uint32_t *needle)
714        _UC_ATTRIBUTE_PURE;
715
716 /* Test whether STR starts with PREFIX.  */
717 extern bool
718        u8_startswith (const uint8_t *str, const uint8_t *prefix)
719        _UC_ATTRIBUTE_PURE;
720 extern bool
721        u16_startswith (const uint16_t *str, const uint16_t *prefix)
722        _UC_ATTRIBUTE_PURE;
723 extern bool
724        u32_startswith (const uint32_t *str, const uint32_t *prefix)
725        _UC_ATTRIBUTE_PURE;
726
727 /* Test whether STR ends with SUFFIX.  */
728 extern bool
729        u8_endswith (const uint8_t *str, const uint8_t *suffix)
730        _UC_ATTRIBUTE_PURE;
731 extern bool
732        u16_endswith (const uint16_t *str, const uint16_t *suffix)
733        _UC_ATTRIBUTE_PURE;
734 extern bool
735        u32_endswith (const uint32_t *str, const uint32_t *suffix)
736        _UC_ATTRIBUTE_PURE;
737
738 /* Divide STR into tokens separated by characters in DELIM.
739    This interface is actually more similar to wcstok than to strtok.  */
740 /* Similar to strtok_r(), wcstok().  */
741 extern uint8_t *
742        u8_strtok (uint8_t *str, const uint8_t *delim, uint8_t **ptr);
743 extern uint16_t *
744        u16_strtok (uint16_t *str, const uint16_t *delim, uint16_t **ptr);
745 extern uint32_t *
746        u32_strtok (uint32_t *str, const uint32_t *delim, uint32_t **ptr);
747
748
749 #ifdef __cplusplus
750 }
751 #endif
752
753 #endif /* _UNISTR_H */