1 /* Copyright (C) 1995, 1996, 1997, 1998 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 /* For XPG4 compliance we have to define the stuff from <wctype.h> here
71 #if defined __USE_XOPEN && !defined __USE_UNIX98
75 /* This incomplete type is defined in <time.h> but needed here because
82 /* Copy SRC to DEST. */
83 extern wchar_t *wcscpy
__P ((wchar_t *__restrict __dest
,
84 __const
wchar_t *__restrict __src
));
85 /* Copy no more than N wide-characters of SRC to DEST. */
86 extern wchar_t *wcsncpy
__P ((wchar_t *__restrict __dest
,
87 __const
wchar_t *__restrict __src
, size_t __n
));
89 /* Append SRC onto DEST. */
90 extern wchar_t *wcscat
__P ((wchar_t *__restrict __dest
,
91 __const
wchar_t *__restrict __src
));
92 /* Append no more than N wide-characters of SRC onto DEST. */
93 extern wchar_t *wcsncat
__P ((wchar_t *__restrict __dest
,
94 __const
wchar_t *__restrict __src
, size_t __n
));
96 /* Compare S1 and S2. */
97 extern int wcscmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
98 /* Compare N wide-characters of S1 and S2. */
99 extern int wcsncmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
103 /* Compare S1 and S2, ignoring case. */
104 extern int __wcscasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
105 extern int wcscasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
107 /* Compare no more than N chars of S1 and S2, ignoring case. */
108 extern int __wcsncasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
110 extern int wcsncasecmp
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
113 /* Similar to the two functions above but take the information from
114 the provided locale and not the global locale. */
115 # include <xlocale.h>
117 extern int __wcscasecmp_l
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
120 extern int __wcsncasecmp_l
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
121 size_t __n
, __locale_t __loc
));
124 /* Compare S1 and S2, both interpreted as appropriate to the
125 LC_COLLATE category of the current locale. */
126 extern int wcscoll
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
));
127 /* Transform S2 into array pointed to by S1 such that if wcscmp is
128 applied to two transformed strings the result is the as applying
129 `wcscoll' to the original strings. */
130 extern size_t wcsxfrm
__P ((wchar_t *__restrict __s1
,
131 __const
wchar_t *__restrict __s2
, size_t __n
));
134 /* Similar to the two functions above but take the information from
135 the provided locale and not the global locale. */
137 /* Compare S1 and S2, both interpreted as appropriate to the
138 LC_COLLATE category of the given locale. */
139 extern int __wcscoll_l
__P ((__const
wchar_t *__s1
, __const
wchar_t *__s2
,
141 /* Transform S2 into array pointed to by S1 such that if wcscmp is
142 applied to two transformed strings the result is the as applying
143 `wcscoll' to the original strings. */
144 extern size_t __wcsxfrm_l
__P ((wchar_t *__s1
, __const
wchar_t *__s2
,
145 size_t __n
, __locale_t __loc
));
147 /* Duplicate S, returning an identical malloc'd string. */
148 extern wchar_t *wcsdup
__P ((__const
wchar_t *__s
));
151 /* Find the first occurrence of WC in WCS. */
152 extern wchar_t *wcschr
__P ((__const
wchar_t *__wcs
, wchar_t __wc
));
153 /* Find the last occurrence of WC in WCS. */
154 extern wchar_t *wcsrchr
__P ((__const
wchar_t *__wcs
, wchar_t __wc
));
156 /* Return the length of the initial segmet of WCS which
157 consists entirely of wide characters not in REJECT. */
158 extern size_t wcscspn
__P ((__const
wchar_t *__wcs
,
159 __const
wchar_t *__reject
));
160 /* Return the length of the initial segmet of WCS which
161 consists entirely of wide characters in ACCEPT. */
162 extern size_t wcsspn
__P ((__const
wchar_t *__wcs
, __const
wchar_t *__accept
));
163 /* Find the first occurrence in WCS of any character in ACCEPT. */
164 extern wchar_t *wcspbrk
__P ((__const
wchar_t *__wcs
,
165 __const
wchar_t *__accept
));
166 /* Find the first occurrence of NEEDLE in HAYSTACK. */
167 extern wchar_t *wcsstr
__P ((__const
wchar_t *__haystack
,
168 __const
wchar_t *__needle
));
170 #if defined __USE_XOPEN && !defined __USE_UNIX98
171 /* Another name for `wcsstr' from XPG4. */
172 extern wchar_t *wcswcs
__P ((__const
wchar_t *__haystack
,
173 __const
wchar_t *__needle
));
176 /* Divide WCS into tokens separated by characters in DELIM. */
177 extern wchar_t *wcstok
__P ((wchar_t *__restrict __s
,
178 __const
wchar_t *__restrict __delim
,
179 wchar_t **__restrict __ptr
));
181 /* Return the number of wide characters in S. */
182 extern size_t __wcslen
__P ((__const
wchar_t *__s
));
183 extern size_t wcslen
__P ((__const
wchar_t *__s
));
186 /* Return the number of wide characters in S, but at most MAXLEN. */
187 extern size_t __wcsnlen
__P ((__const
wchar_t *__s
, size_t __maxlen
));
188 extern size_t wcsnlen
__P ((__const
wchar_t *__s
, size_t __maxlen
));
192 /* Search N wide characters of S for C. */
193 extern wchar_t *wmemchr
__P ((__const
wchar_t *__s
, wchar_t __c
, size_t __n
));
195 /* Compare N wide characters of S1 and S2. */
196 extern int wmemcmp
__P ((__const
wchar_t *__restrict __s1
,
197 __const
wchar_t *__restrict __s2
, size_t __n
));
199 /* Copy N wide characters of SRC to DEST. */
200 extern wchar_t *wmemcpy
__P ((wchar_t *__restrict __s1
,
201 __const
wchar_t *__restrict __s2
, size_t __n
));
203 /* Copy N wide characters of SRC to DEST, guaranteeing
204 correct behavior for overlapping strings. */
205 extern wchar_t *wmemmove
__P ((wchar_t *__s1
, __const
wchar_t *__s2
,
208 /* Set N wide characters of S to C. */
209 extern wchar_t *wmemset
__P ((wchar_t *__s
, wchar_t __c
, size_t __n
));
212 /* Determine whether C constitutes a valid (one-byte) multibyte
214 extern wint_t __btowc
__P ((int __c
));
215 extern wint_t btowc
__P ((int __c
));
217 /* Determine whether C corresponds to a member of the extended
218 character set whose multibyte representation is a single byte. */
219 extern int wctob
__P ((wint_t __c
));
221 /* Determine whether PS points to an object representing the initial
223 extern int __mbsinit
__P ((__const
mbstate_t *__ps
));
224 extern int mbsinit
__P ((__const
mbstate_t *__ps
));
226 /* Write wide character representation of multibyte character pointed
228 extern size_t __mbrtowc
__P ((wchar_t *__restrict __pwc
,
229 __const
char *__restrict __s
, size_t __n
,
230 mbstate_t *__restrict __p
));
231 extern size_t mbrtowc
__P ((wchar_t *__restrict __pwc
,
232 __const
char *__restrict __s
, size_t __n
,
235 /* Write multibyte representation of wide character WC to S. */
236 extern size_t __wcrtomb
__P ((char *__restrict __s
, wchar_t __wc
,
237 mbstate_t *__restrict __ps
));
238 extern size_t wcrtomb
__P ((char *__restrict __s
, wchar_t __wc
,
239 mbstate_t *__restrict __ps
));
241 /* Return number of bytes in multibyte character pointed to by S. */
242 extern size_t __mbrlen
__P ((__const
char *__restrict __s
, size_t __n
,
243 mbstate_t *__restrict __ps
));
244 extern size_t mbrlen
__P ((__const
char *__restrict __s
, size_t __n
,
245 mbstate_t *__restrict __ps
));
247 #if defined __OPTIMIZE__ \
248 && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
249 /* Define inline function as optimization. */
250 extern __inline
size_t mbrlen (__const
char *__restrict __s
, size_t __n
,
251 mbstate_t *__restrict __ps
)
252 { return (__ps
!= NULL
253 ? __mbrtowc (NULL
, __s
, __n
, __ps
) : __mbrlen (__s
, __n
, NULL
)); }
256 /* Write wide character representation of multibyte character string
258 extern size_t __mbsrtowcs
__P ((wchar_t *__restrict __dst
,
259 __const
char **__restrict __src
,
260 size_t __len
, mbstate_t *__restrict __ps
));
261 extern size_t mbsrtowcs
__P ((wchar_t *__restrict __dst
,
262 __const
char **__restrict __src
,
263 size_t __len
, mbstate_t *__restrict __ps
));
265 /* Write multibyte character representation of wide character string
267 extern size_t __wcsrtombs
__P ((char *__restrict __dst
,
268 __const
wchar_t **__restrict __src
,
269 size_t __len
, mbstate_t *__restrict __ps
));
270 extern size_t wcsrtombs
__P ((char *__restrict __dst
,
271 __const
wchar_t **__restrict __src
,
272 size_t __len
, mbstate_t *__restrict __ps
));
276 /* Write wide character representation of at most NMC bytes of the
277 multibyte character string SRC to DST. */
278 extern size_t __mbsnrtowcs
__P ((wchar_t *__restrict __dst
,
279 __const
char **__restrict __src
, size_t __nmc
,
280 size_t __len
, mbstate_t *__restrict __ps
));
281 extern size_t mbsnrtowcs
__P ((wchar_t *__restrict __dst
,
282 __const
char **__restrict __src
, size_t __nmc
,
283 size_t __len
, mbstate_t *__restrict __ps
));
285 /* Write multibyte character representation of at most NWC characters
286 from the wide character string SRC to DST. */
287 extern size_t __wcsnrtombs
__P ((char *__restrict __dst
,
288 __const
wchar_t **__restrict __src
,
289 size_t __nwc
, size_t __len
,
290 mbstate_t *__restrict __ps
));
291 extern size_t wcsnrtombs
__P ((char *__restrict __dst
,
292 __const
wchar_t **__restrict __src
,
293 size_t __nwc
, size_t __len
,
294 mbstate_t *__restrict __ps
));
298 /* The following functions are extensions found in X/Open CAE. */
300 /* Determine number of column positions required for C. */
301 extern int wcwidth
__P ((wint_t __c
));
303 /* Determine number of column positions required for first N wide
304 characters (or fewer if S ends before this) in S. */
305 extern int wcswidth
__P ((__const
wchar_t *__s
, size_t __n
));
306 #endif /* Use X/Open. */
309 /* Convert initial portion of the wide string NPTR to `double'
311 extern double wcstod
__P ((__const
wchar_t *__restrict __nptr
,
312 wchar_t **__restrict __endptr
));
315 /* Likewise for `float' and `long double' sizes of floating-point numbers. */
316 extern float wcstof
__P ((__const
wchar_t *__restrict __nptr
,
317 wchar_t **__restrict __endptr
));
318 extern __long_double_t wcstold
__P ((__const
wchar_t *__restrict __nptr
,
319 wchar_t **__restrict __endptr
));
323 /* Convert initial portion of wide string NPTR to `long int'
325 extern long int wcstol
__P ((__const
wchar_t *__restrict __nptr
,
326 wchar_t **__restrict __endptr
, int __base
));
328 /* Convert initial portion of wide string NPTR to `unsigned long int'
330 extern unsigned long int wcstoul
__P ((__const
wchar_t *__restrict __nptr
,
331 wchar_t **__restrict __endptr
,
334 #if defined __GNUC__ && defined __USE_GNU
335 /* Convert initial portion of wide string NPTR to `long int'
337 extern long long int wcstoq
__P ((__const
wchar_t *__restrict __nptr
,
338 wchar_t **__restrict __endptr
, int __base
));
340 /* Convert initial portion of wide string NPTR to `unsigned long long int'
342 extern unsigned long long int wcstouq
__P ((__const
wchar_t *__restrict __nptr
,
343 wchar_t **__restrict __endptr
,
345 #endif /* GCC and use GNU. */
347 #if defined __USE_ISOC9X || (defined __GNUC__ && defined __USE_GNU)
348 /* Convert initial portion of wide string NPTR to `long int'
350 extern long long int wcstoll
__P ((__const
wchar_t *__restrict __nptr
,
351 wchar_t **__restrict __endptr
, int __base
));
353 /* Convert initial portion of wide string NPTR to `unsigned long long int'
355 extern unsigned long long int wcstoull
__P ((__const
wchar_t *
357 wchar_t **__restrict __endptr
,
359 #endif /* ISO C 9X or GCC and GNU. */
362 /* The concept of one static locale per category is not very well
363 thought out. Many applications will need to process its data using
364 information from several different locales. Another application is
365 the implementation of the internationalization handling in the
366 upcoming ISO C++ standard library. To support this another set of
367 the functions using locale data exist which have an additional
370 Attention: all these functions are *not* standardized in any form.
371 This is a proof-of-concept implementation. */
373 /* Structure for reentrant locale using functions. This is an
374 (almost) opaque type for the user level programs. */
375 # include <xlocale.h>
377 /* Special versions of the functions above which take the locale to
378 use as an additional parameter. */
379 extern long int __wcstol_l
__P ((__const
wchar_t *__restrict __nptr
,
380 wchar_t **__restrict __endptr
, int __base
,
383 extern unsigned long int __wcstoul_l
__P ((__const
wchar_t *__restrict __nptr
,
384 wchar_t **__restrict __endptr
,
385 int __base
, __locale_t __loc
));
387 extern long long int __wcstoll_l
__P ((__const
wchar_t *__restrict __nptr
,
388 wchar_t **__restrict __endptr
,
389 int __base
, __locale_t __loc
));
391 extern unsigned long long int __wcstoull_l
__P ((__const
wchar_t *__restrict
393 wchar_t **__restrict __endptr
,
397 extern double __wcstod_l
__P ((__const
wchar_t *__restrict __nptr
,
398 wchar_t **__restrict __endptr
,
401 extern float __wcstof_l
__P ((__const
wchar_t *__restrict __nptr
,
402 wchar_t **__restrict __endptr
,
405 extern __long_double_t __wcstold_l
__P ((__const
wchar_t *__restrict __nptr
,
406 wchar_t **__restrict __endptr
,
411 /* The internal entry points for `wcstoX' take an extra flag argument
412 saying whether or not to parse locale-dependent number grouping. */
413 extern double __wcstod_internal
__P ((__const
wchar_t *__restrict __nptr
,
414 wchar_t **__restrict __endptr
,
416 extern float __wcstof_internal
__P ((__const
wchar_t *__restrict __nptr
,
417 wchar_t **__restrict __endptr
,
419 extern __long_double_t __wcstold_internal
__P ((__const
wchar_t *
421 wchar_t **__restrict __endptr
,
424 #ifndef __wcstol_internal_defined
425 extern long int __wcstol_internal
__P ((__const
wchar_t *__restrict __nptr
,
426 wchar_t **__restrict __endptr
,
427 int __base
, int __group
));
428 # define __wcstol_internal_defined 1
430 #ifndef __wcstoul_internal_defined
431 extern unsigned long int __wcstoul_internal
__P ((__const
wchar_t *
435 int __base
, int __group
));
436 # define __wcstoul_internal_defined 1
438 #ifndef __wcstoll_internal_defined
439 extern long long int __wcstoll_internal
__P ((__const
wchar_t *
441 wchar_t **__restrict __endptr
,
442 int __base
, int __group
));
443 # define __wcstoll_internal_defined 1
445 #ifndef __wcstoull_internal_defined
446 extern unsigned long long int __wcstoull_internal
__P ((__const
wchar_t *
452 # define __wcstoull_internal_defined 1
456 #if defined __OPTIMIZE__ && __GNUC__ >= 2
457 /* Define inline functions which call the internal entry points. */
459 extern __inline
double wcstod (__const
wchar_t *__restrict __nptr
,
460 wchar_t **__restrict __endptr
)
461 { return __wcstod_internal (__nptr
, __endptr
, 0); }
462 extern __inline
long int wcstol (__const
wchar_t *__restrict __nptr
,
463 wchar_t **__restrict __endptr
, int __base
)
464 { return __wcstol_internal (__nptr
, __endptr
, __base
, 0); }
465 extern __inline
unsigned long int wcstoul (__const
wchar_t *__restrict __nptr
,
466 wchar_t **__restrict __endptr
,
468 { return __wcstoul_internal (__nptr
, __endptr
, __base
, 0); }
471 extern __inline
float wcstof (__const
wchar_t *__restrict __nptr
,
472 wchar_t **__restrict __endptr
)
473 { return __wcstof_internal (__nptr
, __endptr
, 0); }
474 extern __inline __long_double_t
wcstold (__const
wchar_t *__restrict __nptr
,
475 wchar_t **__restrict __endptr
)
476 { return __wcstold_internal (__nptr
, __endptr
, 0); }
479 extern __inline
long long int wcstoq (__const
wchar_t *__restrict __nptr
,
480 wchar_t **__restrict __endptr
,
482 { return __wcstoll_internal (__nptr
, __endptr
, __base
, 0); }
483 extern __inline
unsigned long long int wcstouq (__const
wchar_t *
485 wchar_t **__restrict __endptr
,
487 { return __wcstoull_internal (__nptr
, __endptr
, __base
, 0); }
488 # endif /* Use GNU. */
489 #endif /* Optimizing GCC >=2. */
493 /* Copy SRC to DEST, returning the address of the terminating L'\0' in
495 extern wchar_t *__wcpcpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
));
496 extern wchar_t *wcpcpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
));
498 /* Copy no more than N characters of SRC to DEST, returning the address of
499 the last character written into DEST. */
500 extern wchar_t *__wcpncpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
,
502 extern wchar_t *wcpncpy
__P ((wchar_t *__dest
, __const
wchar_t *__src
,
507 /* The X/Open standard demands that most of the functions defined in
508 the <wctype.h> header must also appear here. This is probably
509 because some X/Open members wrote their implementation before the
510 ISO C standard was published and introduced the better solution.
511 We have to provide these definitions for compliance reasons but we
512 do this nonsense only if really necessary. */
513 #if defined __USE_UNIX98 && !defined __USE_GNU
514 # define __need_iswxxx