1 /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
20 * ISO C Standard, Amendment 1, 7.16.4
21 * General wide-string utilities <wchar.h>
29 /* Get FILE definition. */
33 /* Get size_t, wchar_t, wint_t and NULL from <stddef.h>. */
35 #define __need_wchar_t
40 /* We try to get wint_t from <stddef.h>, but not all GCC versions define it
41 there. So define it ourselves if it remains undefined. */
43 /* Integral type unchanged by default argument promotions that can
44 hold any value corresponding to members of the extended character
45 set, as well as at least one value that does not correspond to any
46 member of the extended character set. */
48 typedef unsigned int wint_t;
52 /* Conversion state information. */
55 int count
; /* Number of bytes needed for the current character. */
56 wint_t value
; /* Value so far. */
60 /* These constants might also be defined in <inttypes.h>. */
61 # define WCHAR_MIN ((wchar_t) 0)
62 # define WCHAR_MAX (~WCHAR_MIN)
66 # define WEOF (0xffffffffu)
69 /* This incomplete type is defined in <time.h> but needed here because
76 /* Copy SRC to DEST. */
77 extern wchar_t *wcscpy
__P ((wchar_t *__restrict __dest
,
78 __const
wchar_t *__restrict __src
));
79 /* Copy no more than N wide-characters of SRC to DEST. */
80 extern wchar_t *wcsncpy
__P ((wchar_t *__restrict __dest
,
81 __const
wchar_t *__restrict __src
, size_t __n
));
83 /* Append SRC onto DEST. */
84 extern wchar_t *wcscat
__P ((wchar_t *__restrict __dest
,
85 __const
wchar_t *__restrict __src
));
86 /* Append no more than N wide-characters of SRC onto DEST. */
87 extern wchar_t *wcsncat
__P ((wchar_t *__restrict __dest
,
88 __const
wchar_t *__restrict __src
, size_t __n
));
90 /* Compare S1 and S2. */
91 extern int wcscmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
92 /* Compare N wide-characters of S1 and S2. */
93 extern int wcsncmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
97 /* Compare S1 and S2, ignoring case. */
98 extern int __wcscasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
99 extern int wcscasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
101 /* Compare no more than N chars of S1 and S2, ignoring case. */
102 extern int __wcsncasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
104 extern int wcsncasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
107 /* Similar to the two functions above but take the information from
108 the provided locale and not the global locale. */
109 # include <xlocale.h>
111 extern int __wcscasecmp_l
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
114 extern int __wcsncasecmp_l
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
115 size_t __n
, __locale_t __loc
));
118 /* Compare S1 and S2, both interpreted as appropriate to the
119 LC_COLLATE category of the current locale. */
120 extern int wcscoll
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
121 /* Transform S2 into array pointed to by S1 such that if wcscmp is
122 applied to two transformed strings the result is the as applying
123 `wcscoll' to the original strings. */
124 extern size_t wcsxfrm
__P ((wchar_t *__restrict __s1
,
125 __const
wchar_t *__restrict __s2
, size_t __n
));
128 /* Similar to the two functions above but take the information from
129 the provided locale and not the global locale. */
131 /* Compare S1 and S2, both interpreted as appropriate to the
132 LC_COLLATE category of the given locale. */
133 extern int __wcscoll_l
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
135 /* Transform S2 into array pointed to by S1 such that if wcscmp is
136 applied to two transformed strings the result is the as applying
137 `wcscoll' to the original strings. */
138 extern size_t __wcsxfrm_l
__P ((wchar_t *__s1
, __const
wchar_t *__s2
,
139 size_t __n
, __locale_t __loc
));
141 /* Duplicate S, returning an identical malloc'd string. */
142 extern wchar_t *wcsdup
__P ((__const
wchar_t *__s
));
145 /* Find the first occurrence of WC in WCS. */
146 extern wchar_t *wcschr
__P ((__const
wchar_t *__wcs
, wchar_t __wc
));
147 /* Find the last occurrence of WC in WCS. */
148 extern wchar_t *wcsrchr
__P ((__const
wchar_t *__wcs
, wchar_t __wc
));
150 /* Return the length of the initial segmet of WCS which
151 consists entirely of wide characters not in REJECT. */
152 extern size_t wcscspn
__P ((__const
wchar_t *__wcs
,
153 __const
wchar_t *__reject
));
154 /* Return the length of the initial segmet of WCS which
155 consists entirely of wide characters in ACCEPT. */
156 extern size_t wcsspn
__P ((__const
wchar_t *__wcs
, __const
wchar_t *__accept
));
157 /* Find the first occurrence in WCS of any character in ACCEPT. */
158 extern wchar_t *wcspbrk
__P ((__const
wchar_t *__wcs
,
159 __const
wchar_t *__accept
));
160 /* Find the first occurrence of NEEDLE in HAYSTACK. */
161 extern wchar_t *wcsstr
__P ((__const
wchar_t *__haystack
,
162 __const
wchar_t *__needle
));
163 /* Divide WCS into tokens separated by characters in DELIM. */
164 extern wchar_t *wcstok
__P ((wchar_t *__restrict __s
,
165 __const
wchar_t *__restrict __delim
,
166 wchar_t **__restrict __ptr
));
168 /* Return the number of wide characters in S. */
169 extern size_t wcslen
__P ((__const
wchar_t *__s
));
172 /* Search N wide characters of S for C. */
173 extern wchar_t *wmemchr
__P ((__const
wchar_t *__s
, wchar_t __c
, size_t __n
));
175 /* Compare N wide characters of S1 and S2. */
176 extern int wmemcmp
__P ((__const
wchar_t *__restrict __s1
,
177 __const
wchar_t *__restrict __s2
, size_t __n
));
179 /* Copy N wide characters of SRC to DEST. */
180 extern wchar_t *wmemcpy
__P ((wchar_t *__restrict __s1
,
181 __const
wchar_t *__restrict __s2
, size_t __n
));
183 /* Copy N wide characters of SRC to DEST, guaranteeing
184 correct behavior for overlapping strings. */
185 extern wchar_t *wmemmove
__P ((wchar_t *__s1
, __const
wchar_t *__s2
,
188 /* Set N wide characters of S to C. */
189 extern wchar_t *wmemset
__P ((wchar_t *__s
, wchar_t __c
, size_t __n
));
192 /* Determine whether C constitutes a valid (one-byte) multibyte
194 extern wint_t btowc
__P ((int __c
));
196 /* Determine whether C corresponds to a member of the extended
197 character set whose multibyte representation is a single byte. */
198 extern int wctob
__P ((wint_t __c
));
200 /* Determine whether PS points to an object representing the initial
202 extern int mbsinit
__P ((__const
mbstate_t *__ps
));
204 /* Write wide character representation of multibyte character pointed
206 extern size_t __mbrtowc
__P ((wchar_t *__restrict __pwc
,
207 __const
char *__restrict __s
, size_t __n
,
208 mbstate_t *__restrict __p
));
209 extern size_t mbrtowc
__P ((wchar_t *__restrict __pwc
,
210 __const
char *__restrict __s
, size_t __n
,
213 /* Write multibyte representation of wide character WC to S. */
214 extern size_t __wcrtomb
__P ((char *__restrict __s
, wchar_t __wc
,
215 mbstate_t *__restrict __ps
));
216 extern size_t wcrtomb
__P ((char *__restrict __s
, wchar_t __wc
,
217 mbstate_t *__restrict __ps
));
219 /* Return number of bytes in multibyte character pointed to by S. */
220 extern size_t __mbrlen
__P ((__const
char *__restrict __s
, size_t __n
,
221 mbstate_t *__restrict __ps
));
222 extern size_t mbrlen
__P ((__const
char *__restrict __s
, size_t __n
,
223 mbstate_t *__restrict __ps
));
225 #if defined __OPTIMIZE__ \
226 && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
227 /* Define inline function as optimization. */
228 extern __inline
size_t mbrlen (__const
char *__restrict __s
, size_t __n
,
229 mbstate_t *__restrict __ps
)
230 { return (__ps
!= NULL
231 ? __mbrtowc (NULL
, __s
, __n
, __ps
) : __mbrlen (__s
, __n
, NULL
)); }
234 /* Write wide character representation of multibyte character string
236 extern size_t __mbsrtowcs
__P ((wchar_t *__restrict __dst
,
237 __const
char **__restrict __src
,
238 size_t __len
, mbstate_t *__restrict __ps
));
239 extern size_t mbsrtowcs
__P ((wchar_t *__restrict __dst
,
240 __const
char **__restrict __src
,
241 size_t __len
, mbstate_t *__restrict __ps
));
243 /* Write multibyte character representation of wide character string
245 extern size_t __wcsrtombs
__P ((char *__restrict __dst
,
246 __const
wchar_t **__restrict __src
,
247 size_t __len
, mbstate_t *__restrict __ps
));
248 extern size_t wcsrtombs
__P ((char *__restrict __dst
,
249 __const
wchar_t **__restrict __src
,
250 size_t __len
, mbstate_t *__restrict __ps
));
254 /* Write wide character representation of at most NMC bytes of the
255 multibyte character string SRC to DST. */
256 extern size_t __mbsnrtowcs
__P ((wchar_t *__restrict __dst
,
257 __const
char **__restrict __src
, size_t __nmc
,
258 size_t __len
, mbstate_t *__restrict __ps
));
259 extern size_t mbsnrtowcs
__P ((wchar_t *__restrict __dst
,
260 __const
char **__restrict __src
, size_t __nmc
,
261 size_t __len
, mbstate_t *__restrict __ps
));
263 /* Similar function to the above but this does not stop at NUL bytes. */
264 extern size_t __wmemrtowcs
__P ((wchar_t *__restrict __dst
,
265 __const
char **__restrict __src
,
266 size_t __nmc
, size_t __len
,
267 mbstate_t *__restrict __ps
));
268 extern size_t wmemrtowcs
__P ((wchar_t *__restrict __dst
,
269 __const
char **__restrict __src
,
270 size_t __nmc
, size_t __len
,
271 mbstate_t *__restrict __ps
));
273 /* Write multibyte character representation of at most NWC characters
274 from the wide character string SRC to DST. */
275 extern size_t __wcsnrtombs
__P ((char *__restrict __dst
,
276 __const
wchar_t **__restrict __src
,
277 size_t __nwc
, size_t __len
,
278 mbstate_t *__restrict __ps
));
279 extern size_t wcsnrtombs
__P ((char *__restrict __dst
,
280 __const
wchar_t **__restrict __src
,
281 size_t __nwc
, size_t __len
,
282 mbstate_t *__restrict __ps
));
284 /* Similar function to the above but this does not stop at NUL bytes. */
285 extern size_t __wmemrtombs
__P ((char *__restrict __dst
,
286 __const
wchar_t **__restrict __src
,
287 size_t __nwc
, size_t len
,
288 mbstate_t *__restrict __ps
));
289 extern size_t wmemrtombs
__P ((char *__restrict __dst
,
290 __const
wchar_t **__restrict __src
,
291 size_t __nwc
, size_t len
,
292 mbstate_t *__restrict __ps
));
296 /* The following functions are extensions found in X/Open CAE. */
298 /* Determine number of column positions required for C. */
299 extern int wcwidth
__P ((wint_t __c
));
301 /* Determine number of column positions required for first N wide
302 characters (or fewer if S ends before this) in S. */
303 extern int wcswidth
__P ((__const
wchar_t *__s
, size_t __n
));
304 #endif /* Use X/Open. */
307 /* Convert initial portion of the wide string NPTR to `double'
309 extern double wcstod
__P ((__const
wchar_t *__restrict __nptr
,
310 wchar_t **__restrict __endptr
));
313 /* Likewise for `float' and `long double' sizes of floating-point numbers. */
314 extern float wcstof
__P ((__const
wchar_t *__restrict __nptr
,
315 wchar_t **__restrict __endptr
));
316 extern __long_double_t wcstold
__P ((__const
wchar_t *__restrict __nptr
,
317 wchar_t **__restrict __endptr
));
321 /* Convert initial portion of wide string NPTR to `long int'
323 extern long int wcstol
__P ((__const
wchar_t *__restrict __nptr
,
324 wchar_t **__restrict __endptr
, int __base
));
326 /* Convert initial portion of wide string NPTR to `unsigned long int'
328 extern unsigned long int wcstoul
__P ((__const
wchar_t *__restrict __nptr
,
329 wchar_t **__restrict __endptr
,
332 #if defined __GNUC__ && defined __USE_GNU
333 /* Convert initial portion of wide string NPTR to `long int'
335 extern long long int wcstoq
__P ((__const
wchar_t *__restrict __nptr
,
336 wchar_t **__restrict __endptr
, int __base
));
338 /* Convert initial portion of wide string NPTR to `unsigned long long int'
340 extern unsigned long long int wcstouq
__P ((__const
wchar_t *__restrict __nptr
,
341 wchar_t **__restrict __endptr
,
343 #endif /* GCC and use GNU. */
345 #if defined __USE_ISOC9X || (defined __GNUC__ && defined __USE_GNU)
346 /* Convert initial portion of wide string NPTR to `long int'
348 extern long long int wcstoll
__P ((__const
wchar_t *__restrict __nptr
,
349 wchar_t **__restrict __endptr
, int __base
));
351 /* Convert initial portion of wide string NPTR to `unsigned long long int'
353 extern unsigned long long int wcstoull
__P ((__const
wchar_t *
355 wchar_t **__restrict __endptr
,
357 #endif /* ISO C 9X or GCC and GNU. */
360 /* The concept of one static locale per category is not very well
361 thought out. Many applications will need to process its data using
362 information from several different locales. Another application is
363 the implementation of the internationalization handling in the
364 upcoming ISO C++ standard library. To support this another set of
365 the functions using locale data exist which have an additional
368 Attention: all these functions are *not* standardized in any form.
369 This is a proof-of-concept implementation. */
371 /* Structure for reentrant locale using functions. This is an
372 (almost) opaque type for the user level programs. */
373 # include <xlocale.h>
375 /* Special versions of the functions above which take the locale to
376 use as an additional parameter. */
377 extern long int __wcstol_l
__P ((__const
wchar_t *__restrict __nptr
,
378 wchar_t **__restrict __endptr
, int __base
,
381 extern unsigned long int __wcstoul_l
__P ((__const
wchar_t *__restrict __nptr
,
382 wchar_t **__restrict __endptr
,
383 int __base
, __locale_t __loc
));
385 extern long long int __wcstoll_l
__P ((__const
wchar_t *__restrict __nptr
,
386 wchar_t **__restrict __endptr
,
387 int __base
, __locale_t __loc
));
389 extern unsigned long long int __wcstoull_l
__P ((__const
wchar_t *__restrict
391 wchar_t **__restrict __endptr
,
395 extern double __wcstod_l
__P ((__const
wchar_t *__restrict __nptr
,
396 wchar_t **__restrict __endptr
,
399 extern float __wcstof_l
__P ((__const
wchar_t *__restrict __nptr
,
400 wchar_t **__restrict __endptr
,
403 extern __long_double_t __wcstold_l
__P ((__const
wchar_t *__restrict __nptr
,
404 wchar_t **__restrict __endptr
,
409 /* The internal entry points for `wcstoX' take an extra flag argument
410 saying whether or not to parse locale-dependent number grouping. */
411 extern double __wcstod_internal
__P ((__const
wchar_t *__restrict __nptr
,
412 wchar_t **__restrict __endptr
,
414 extern float __wcstof_internal
__P ((__const
wchar_t *__restrict __nptr
,
415 wchar_t **__restrict __endptr
,
417 extern __long_double_t __wcstold_internal
__P ((__const
wchar_t *
419 wchar_t **__restrict __endptr
,
422 #ifndef __wcstol_internal_defined
423 extern long int __wcstol_internal
__P ((__const
wchar_t *__restrict __nptr
,
424 wchar_t **__restrict __endptr
,
425 int __base
, int __group
));
426 # define __wcstol_internal_defined 1
428 #ifndef __wcstoul_internal_defined
429 extern unsigned long int __wcstoul_internal
__P ((__const
wchar_t *
433 int __base
, int __group
));
434 # define __wcstoul_internal_defined 1
436 #ifndef __wcstoll_internal_defined
437 extern long long int __wcstoll_internal
__P ((__const
wchar_t *
439 wchar_t **__restrict __endptr
,
440 int __base
, int __group
));
441 # define __wcstoll_internal_defined 1
443 #ifndef __wcstoull_internal_defined
444 extern unsigned long long int __wcstoull_internal
__P ((__const
wchar_t *
450 # define __wcstoull_internal_defined 1
454 #if defined __OPTIMIZE__ && __GNUC__ >= 2
455 /* Define inline functions which call the internal entry points. */
457 extern __inline
double wcstod (__const
wchar_t *__restrict __nptr
,
458 wchar_t **__restrict __endptr
)
459 { return __wcstod_internal (__nptr
, __endptr
, 0); }
460 extern __inline
long int wcstol (__const
wchar_t *__restrict __nptr
,
461 wchar_t **__restrict __endptr
, int __base
)
462 { return __wcstol_internal (__nptr
, __endptr
, __base
, 0); }
463 extern __inline
unsigned long int wcstoul (__const
wchar_t *__restrict __nptr
,
464 wchar_t **__restrict __endptr
,
466 { return __wcstoul_internal (__nptr
, __endptr
, __base
, 0); }
469 extern __inline
float wcstof (__const
wchar_t *__restrict __nptr
,
470 wchar_t **__restrict __endptr
)
471 { return __wcstof_internal (__nptr
, __endptr
, 0); }
472 extern __inline __long_double_t
wcstold (__const
wchar_t *__restrict __nptr
,
473 wchar_t **__restrict __endptr
)
474 { return __wcstold_internal (__nptr
, __endptr
, 0); }
477 extern __inline
long long int wcstoq (__const
wchar_t *__restrict __nptr
,
478 wchar_t **__restrict __endptr
,
480 { return __wcstoll_internal (__nptr
, __endptr
, __base
, 0); }
481 extern __inline
unsigned long long int wcstouq (__const
wchar_t *
483 wchar_t **__restrict __endptr
,
485 { return __wcstoull_internal (__nptr
, __endptr
, __base
, 0); }
486 # endif /* Use GNU. */
487 #endif /* Optimizing GCC >=2. */
491 /* Copy SRC to DEST, returning the address of the terminating L'\0' in
493 extern wchar_t *__wcpcpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
));
494 extern wchar_t *wcpcpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
));
496 /* Copy no more than N characters of SRC to DEST, returning the address of
497 the last character written into DEST. */
498 extern wchar_t *__wcpncpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
,
500 extern wchar_t *wcpncpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
,
505 /* The X/Open standard demands that most of the functions defined in
506 the <wctype.h> header must also appear here. This is probably
507 because some X/Open members wrote their implementation before the
508 ISO C standard was published and introduced the better solution.
509 We have to provide these definitions for compliance reasons but we
510 do this nonsense only if really necessary. */
511 #if defined __USE_UNIX98 && !defined __USE_GNU
512 # define __need_iswxxx