libio: Improve fortify with clang
[glibc.git] / manual / ctype.texi
blobd09249c6cf88a340bc782ae3f8333bda71e6df65
1 @node Character Handling, String and Array Utilities, Memory, Top
2 @c %MENU% Character testing and conversion functions
3 @chapter Character Handling
5 Programs that work with characters and strings often need to classify a
6 character---is it alphabetic, is it a digit, is it whitespace, and so
7 on---and perform case conversion operations on characters.  The
8 functions in the header file @file{ctype.h} are provided for this
9 purpose.
10 @pindex ctype.h
12 Since the choice of locale and character set can alter the
13 classifications of particular character codes, all of these functions
14 are affected by the current locale.  (More precisely, they are affected
15 by the locale currently selected for character classification---the
16 @code{LC_CTYPE} category; see @ref{Locale Categories}.)
18 The @w{ISO C} standard specifies two different sets of functions.  The
19 one set works on @code{char} type characters, the other one on
20 @code{wchar_t} wide characters (@pxref{Extended Char Intro}).
22 @menu
23 * Classification of Characters::       Testing whether characters are
24                                         letters, digits, punctuation, etc.
26 * Case Conversion::                    Case mapping, and the like.
27 * Classification of Wide Characters::  Character class determination for
28                                         wide characters.
29 * Using Wide Char Classes::            Notes on using the wide character
30                                         classes.
31 * Wide Character Case Conversion::     Mapping of wide characters.
32 @end menu
34 @node Classification of Characters, Case Conversion,  , Character Handling
35 @section Classification of Characters
36 @cindex character testing
37 @cindex classification of characters
38 @cindex predicates on characters
39 @cindex character predicates
41 This section explains the library functions for classifying characters.
42 For example, @code{isalpha} is the function to test for an alphabetic
43 character.  It takes one argument, the character to test as an
44 @code{unsigned char} value, and returns a nonzero integer if the
45 character is alphabetic, and zero otherwise.  You would use it like
46 this:
48 @smallexample
49 if (isalpha ((unsigned char) c))
50   printf ("The character `%c' is alphabetic.\n", c);
51 @end smallexample
53 Each of the functions in this section tests for membership in a
54 particular class of characters; each has a name starting with @samp{is}.
55 Each of them takes one argument, which is a character to test.  The
56 character argument must be in the value range of @code{unsigned char} (0
57 to 255 for @theglibc{}).  On a machine where the @code{char} type is
58 signed, it may be necessary to cast the argument to @code{unsigned
59 char}, or mask it with @samp{& 0xff}.  (On @code{unsigned char}
60 machines, this step is harmless, so portable code should always perform
61 it.)  The @samp{is} functions return an @code{int} which is treated as a
62 boolean value.
64 All @samp{is} functions accept the special value @code{EOF} and return
65 zero.  (Note that @code{EOF} must not be cast to @code{unsigned char}
66 for this to work.)
68 As an extension, @theglibc{} accepts signed @code{char} values as
69 @samp{is} functions arguments in the range -128 to -2, and returns the
70 result for the corresponding unsigned character.  However, as there
71 might be an actual character corresponding to the @code{EOF} integer
72 constant, doing so may introduce bugs, and it is recommended to apply
73 the conversion to the unsigned character range as appropriate.
75 The attributes of any given character can vary between locales.
76 @xref{Locales}, for more information on locales.
78 These functions are declared in the header file @file{ctype.h}.
79 @pindex ctype.h
81 @cindex lower-case character
82 @deftypefun int islower (int @var{c})
83 @standards{ISO, ctype.h}
84 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
85 @c The is* macros call __ctype_b_loc to get the ctype array from the
86 @c current locale, and then index it by c.  __ctype_b_loc reads from
87 @c thread-local memory the (indirect) pointer to the ctype array, which
88 @c may involve one word access to the global locale object, if that's
89 @c the active locale for the thread, and the array, being part of the
90 @c locale data, is undeletable, so there's no thread-safety issue.  We
91 @c might want to mark these with @mtslocale to flag to callers that
92 @c changing locales might affect them, even if not these simpler
93 @c functions.
94 Returns true if @var{c} is a lower-case letter.  The letter need not be
95 from the Latin alphabet, any alphabet representable is valid.
96 @end deftypefun
98 @cindex upper-case character
99 @deftypefun int isupper (int @var{c})
100 @standards{ISO, ctype.h}
101 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
102 Returns true if @var{c} is an upper-case letter.  The letter need not be
103 from the Latin alphabet, any alphabet representable is valid.
104 @end deftypefun
106 @cindex alphabetic character
107 @deftypefun int isalpha (int @var{c})
108 @standards{ISO, ctype.h}
109 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
110 Returns true if @var{c} is an alphabetic character (a letter).  If
111 @code{islower} or @code{isupper} is true of a character, then
112 @code{isalpha} is also true.
114 In some locales, there may be additional characters for which
115 @code{isalpha} is true---letters which are neither upper case nor lower
116 case.  But in the standard @code{"C"} locale, there are no such
117 additional characters.
118 @end deftypefun
120 @cindex digit character
121 @cindex decimal digit character
122 @deftypefun int isdigit (int @var{c})
123 @standards{ISO, ctype.h}
124 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
125 Returns true if @var{c} is a decimal digit (@samp{0} through @samp{9}).
126 @end deftypefun
128 @cindex alphanumeric character
129 @deftypefun int isalnum (int @var{c})
130 @standards{ISO, ctype.h}
131 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
132 Returns true if @var{c} is an alphanumeric character (a letter or
133 number); in other words, if either @code{isalpha} or @code{isdigit} is
134 true of a character, then @code{isalnum} is also true.
135 @end deftypefun
137 @cindex hexadecimal digit character
138 @deftypefun int isxdigit (int @var{c})
139 @standards{ISO, ctype.h}
140 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
141 Returns true if @var{c} is a hexadecimal digit.
142 Hexadecimal digits include the normal decimal digits @samp{0} through
143 @samp{9} and the letters @samp{A} through @samp{F} and
144 @samp{a} through @samp{f}.
145 @end deftypefun
147 @cindex punctuation character
148 @deftypefun int ispunct (int @var{c})
149 @standards{ISO, ctype.h}
150 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
151 Returns true if @var{c} is a punctuation character.
152 This means any printing character that is not alphanumeric or a space
153 character.
154 @end deftypefun
156 @cindex whitespace character
157 @deftypefun int isspace (int @var{c})
158 @standards{ISO, ctype.h}
159 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
160 Returns true if @var{c} is a @dfn{whitespace} character.  In the standard
161 @code{"C"} locale, @code{isspace} returns true for only the standard
162 whitespace characters:
164 @table @code
165 @item ' '
166 space
168 @item '\f'
169 formfeed
171 @item '\n'
172 newline
174 @item '\r'
175 carriage return
177 @item '\t'
178 horizontal tab
180 @item '\v'
181 vertical tab
182 @end table
183 @end deftypefun
185 @cindex blank character
186 @deftypefun int isblank (int @var{c})
187 @standards{ISO, ctype.h}
188 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
189 Returns true if @var{c} is a blank character; that is, a space or a tab.
190 This function was originally a GNU extension, but was added in @w{ISO C99}.
191 @end deftypefun
193 @cindex graphic character
194 @deftypefun int isgraph (int @var{c})
195 @standards{ISO, ctype.h}
196 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
197 Returns true if @var{c} is a graphic character; that is, a character
198 that has a glyph associated with it.  The whitespace characters are not
199 considered graphic.
200 @end deftypefun
202 @cindex printing character
203 @deftypefun int isprint (int @var{c})
204 @standards{ISO, ctype.h}
205 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
206 Returns true if @var{c} is a printing character.  Printing characters
207 include all the graphic characters, plus the space (@samp{ }) character.
208 @end deftypefun
210 @cindex control character
211 @deftypefun int iscntrl (int @var{c})
212 @standards{ISO, ctype.h}
213 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
214 Returns true if @var{c} is a control character (that is, a character that
215 is not a printing character).
216 @end deftypefun
218 @cindex ASCII character
219 @deftypefun int isascii (int @var{c})
220 @standards{SVID, ctype.h}
221 @standards{BSD, ctype.h}
222 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
223 Returns true if @var{c} is a 7-bit @code{unsigned char} value that fits
224 into the US/UK ASCII character set.  This function is a BSD extension
225 and is also an SVID extension.
226 @end deftypefun
228 @node Case Conversion, Classification of Wide Characters, Classification of Characters, Character Handling
229 @section Case Conversion
230 @cindex character case conversion
231 @cindex case conversion of characters
232 @cindex converting case of characters
234 This section explains the library functions for performing conversions
235 such as case mappings on characters.  For example, @code{toupper}
236 converts any character to upper case if possible.  If the character
237 can't be converted, @code{toupper} returns it unchanged.
239 These functions take one argument of type @code{int}, which is the
240 character to convert, and return the converted character as an
241 @code{int}.  If the conversion is not applicable to the argument given,
242 the argument is returned unchanged.
244 @strong{Compatibility Note:} In pre-@w{ISO C} dialects, instead of
245 returning the argument unchanged, these functions may fail when the
246 argument is not suitable for the conversion.  Thus for portability, you
247 may need to write @code{islower(c) ? toupper(c) : c} rather than just
248 @code{toupper(c)}.
250 These functions are declared in the header file @file{ctype.h}.
251 @pindex ctype.h
253 @deftypefun int tolower (int @var{c})
254 @standards{ISO, ctype.h}
255 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
256 @c The to* macros/functions call different functions that use different
257 @c arrays than those of__ctype_b_loc, but the access patterns and
258 @c thus safety guarantees are the same.
259 If @var{c} is an upper-case letter, @code{tolower} returns the corresponding
260 lower-case letter.  If @var{c} is not an upper-case letter,
261 @var{c} is returned unchanged.
262 @end deftypefun
264 @deftypefun int toupper (int @var{c})
265 @standards{ISO, ctype.h}
266 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
267 If @var{c} is a lower-case letter, @code{toupper} returns the corresponding
268 upper-case letter.  Otherwise @var{c} is returned unchanged.
269 @end deftypefun
271 @deftypefun int toascii (int @var{c})
272 @standards{SVID, ctype.h}
273 @standards{BSD, ctype.h}
274 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
275 This function converts @var{c} to a 7-bit @code{unsigned char} value
276 that fits into the US/UK ASCII character set, by clearing the high-order
277 bits.  This function is a BSD extension and is also an SVID extension.
278 @end deftypefun
280 @deftypefun int _tolower (int @var{c})
281 @standards{SVID, ctype.h}
282 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
283 This is identical to @code{tolower}, and is provided for compatibility
284 with the SVID.  @xref{SVID}.
285 @end deftypefun
287 @deftypefun int _toupper (int @var{c})
288 @standards{SVID, ctype.h}
289 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
290 This is identical to @code{toupper}, and is provided for compatibility
291 with the SVID.
292 @end deftypefun
295 @node Classification of Wide Characters, Using Wide Char Classes, Case Conversion, Character Handling
296 @section Character class determination for wide characters
298 @w{Amendment 1} to @w{ISO C90} defines functions to classify wide
299 characters.  Although the original @w{ISO C90} standard already defined
300 the type @code{wchar_t}, no functions operating on them were defined.
302 The general design of the classification functions for wide characters
303 is more general.  It allows extensions to the set of available
304 classifications, beyond those which are always available.  The POSIX
305 standard specifies how extensions can be made, and this is already
306 implemented in the @glibcadj{} implementation of the @code{localedef}
307 program.
309 The character class functions are normally implemented with bitsets,
310 with a bitset per character.  For a given character, the appropriate
311 bitset is read from a table and a test is performed as to whether a
312 certain bit is set.  Which bit is tested for is determined by the
313 class.
315 For the wide character classification functions this is made visible.
316 There is a type classification type defined, a function to retrieve this
317 value for a given class, and a function to test whether a given
318 character is in this class, using the classification value.  On top of
319 this the normal character classification functions as used for
320 @code{char} objects can be defined.
322 @deftp {Data type} wctype_t
323 @standards{ISO, wctype.h}
324 The @code{wctype_t} can hold a value which represents a character class.
325 The only defined way to generate such a value is by using the
326 @code{wctype} function.
328 @pindex wctype.h
329 This type is defined in @file{wctype.h}.
330 @end deftp
332 @deftypefun wctype_t wctype (const char *@var{property})
333 @standards{ISO, wctype.h}
334 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
335 @c Although the source code of wctype contains multiple references to
336 @c the locale, that could each reference different locale_data objects
337 @c should the global locale object change while active, the compiler can
338 @c and does combine them all into a single dereference that resolves
339 @c once to the LCTYPE locale object used throughout the function, so it
340 @c is safe in (optimized) practice, if not in theory, even when the
341 @c locale changes.  Ideally we'd explicitly save the resolved
342 @c locale_data object to make it visibly safe instead of safe only under
343 @c compiler optimizations, but given the decision that setlocale is
344 @c MT-Unsafe, all this would afford us would be the ability to not mark
345 @c this function with @mtslocale.
346 @code{wctype} returns a value representing a class of wide
347 characters which is identified by the string @var{property}.  Besides
348 some standard properties each locale can define its own ones.  In case
349 no property with the given name is known for the current locale
350 selected for the @code{LC_CTYPE} category, the function returns zero.
352 @noindent
353 The properties known in every locale are:
355 @multitable @columnfractions .25 .25 .25 .25
356 @item
357 @code{"alnum"} @tab @code{"alpha"} @tab @code{"cntrl"} @tab @code{"digit"}
358 @item
359 @code{"graph"} @tab @code{"lower"} @tab @code{"print"} @tab @code{"punct"}
360 @item
361 @code{"space"} @tab @code{"upper"} @tab @code{"xdigit"}
362 @end multitable
364 @pindex wctype.h
365 This function is declared in @file{wctype.h}.
366 @end deftypefun
368 To test the membership of a character to one of the non-standard classes
369 the @w{ISO C} standard defines a completely new function.
371 @deftypefun int iswctype (wint_t @var{wc}, wctype_t @var{desc})
372 @standards{ISO, wctype.h}
373 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
374 @c The compressed lookup table returned by wctype is read-only.
375 This function returns a nonzero value if @var{wc} is in the character
376 class specified by @var{desc}.  @var{desc} must previously be returned
377 by a successful call to @code{wctype}.
379 @pindex wctype.h
380 This function is declared in @file{wctype.h}.
381 @end deftypefun
383 To make it easier to use the commonly-used classification functions,
384 they are defined in the C library.  There is no need to use
385 @code{wctype} if the property string is one of the known character
386 classes.  In some situations it is desirable to construct the property
387 strings, and then it is important that @code{wctype} can also handle the
388 standard classes.
390 @cindex alphanumeric character
391 @deftypefun int iswalnum (wint_t @var{wc})
392 @standards{ISO, wctype.h}
393 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
394 @c The implicit wctype call in the isw* functions is actually an
395 @c optimized version because the category has a known offset, but the
396 @c wctype is equally safe when optimized, unsafe with changing locales
397 @c if not optimized (thus @mtslocale).  Since it's not a macro, we
398 @c always optimize, and the locale can't change in any MT-Safe way, it's
399 @c fine.  The test whether wc is ASCII to use the non-wide is*
400 @c macro/function doesn't bring any other safety issues: the test does
401 @c not depend on the locale, and each path after the decision resolves
402 @c the locale object only once.
403 This function returns a nonzero value if @var{wc} is an alphanumeric
404 character (a letter or number); in other words, if either @code{iswalpha}
405 or @code{iswdigit} is true of a character, then @code{iswalnum} is also
406 true.
408 @noindent
409 This function can be implemented using
411 @smallexample
412 iswctype (wc, wctype ("alnum"))
413 @end smallexample
415 @pindex wctype.h
416 It is declared in @file{wctype.h}.
417 @end deftypefun
419 @cindex alphabetic character
420 @deftypefun int iswalpha (wint_t @var{wc})
421 @standards{ISO, wctype.h}
422 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
423 Returns true if @var{wc} is an alphabetic character (a letter).  If
424 @code{iswlower} or @code{iswupper} is true of a character, then
425 @code{iswalpha} is also true.
427 In some locales, there may be additional characters for which
428 @code{iswalpha} is true---letters which are neither upper case nor lower
429 case.  But in the standard @code{"C"} locale, there are no such
430 additional characters.
432 @noindent
433 This function can be implemented using
435 @smallexample
436 iswctype (wc, wctype ("alpha"))
437 @end smallexample
439 @pindex wctype.h
440 It is declared in @file{wctype.h}.
441 @end deftypefun
443 @cindex control character
444 @deftypefun int iswcntrl (wint_t @var{wc})
445 @standards{ISO, wctype.h}
446 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
447 Returns true if @var{wc} is a control character (that is, a character that
448 is not a printing character).
450 @noindent
451 This function can be implemented using
453 @smallexample
454 iswctype (wc, wctype ("cntrl"))
455 @end smallexample
457 @pindex wctype.h
458 It is declared in @file{wctype.h}.
459 @end deftypefun
461 @cindex digit character
462 @deftypefun int iswdigit (wint_t @var{wc})
463 @standards{ISO, wctype.h}
464 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
465 Returns true if @var{wc} is a digit (e.g., @samp{0} through @samp{9}).
466 Please note that this function does not only return a nonzero value for
467 @emph{decimal} digits, but for all kinds of digits.  A consequence is
468 that code like the following will @strong{not} work unconditionally for
469 wide characters:
471 @smallexample
472 n = 0;
473 while (iswdigit (*wc))
474   @{
475     n *= 10;
476     n += *wc++ - L'0';
477   @}
478 @end smallexample
480 @noindent
481 This function can be implemented using
483 @smallexample
484 iswctype (wc, wctype ("digit"))
485 @end smallexample
487 @pindex wctype.h
488 It is declared in @file{wctype.h}.
489 @end deftypefun
491 @cindex graphic character
492 @deftypefun int iswgraph (wint_t @var{wc})
493 @standards{ISO, wctype.h}
494 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
495 Returns true if @var{wc} is a graphic character; that is, a character
496 that has a glyph associated with it.  The whitespace characters are not
497 considered graphic.
499 @noindent
500 This function can be implemented using
502 @smallexample
503 iswctype (wc, wctype ("graph"))
504 @end smallexample
506 @pindex wctype.h
507 It is declared in @file{wctype.h}.
508 @end deftypefun
510 @cindex lower-case character
511 @deftypefun int iswlower (wint_t @var{wc})
512 @standards{ISO, ctype.h}
513 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
514 Returns true if @var{wc} is a lower-case letter.  The letter need not be
515 from the Latin alphabet, any alphabet representable is valid.
517 @noindent
518 This function can be implemented using
520 @smallexample
521 iswctype (wc, wctype ("lower"))
522 @end smallexample
524 @pindex wctype.h
525 It is declared in @file{wctype.h}.
526 @end deftypefun
528 @cindex printing character
529 @deftypefun int iswprint (wint_t @var{wc})
530 @standards{ISO, wctype.h}
531 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
532 Returns true if @var{wc} is a printing character.  Printing characters
533 include all the graphic characters, plus the space (@samp{ }) character.
535 @noindent
536 This function can be implemented using
538 @smallexample
539 iswctype (wc, wctype ("print"))
540 @end smallexample
542 @pindex wctype.h
543 It is declared in @file{wctype.h}.
544 @end deftypefun
546 @cindex punctuation character
547 @deftypefun int iswpunct (wint_t @var{wc})
548 @standards{ISO, wctype.h}
549 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
550 Returns true if @var{wc} is a punctuation character.
551 This means any printing character that is not alphanumeric or a space
552 character.
554 @noindent
555 This function can be implemented using
557 @smallexample
558 iswctype (wc, wctype ("punct"))
559 @end smallexample
561 @pindex wctype.h
562 It is declared in @file{wctype.h}.
563 @end deftypefun
565 @cindex whitespace character
566 @deftypefun int iswspace (wint_t @var{wc})
567 @standards{ISO, wctype.h}
568 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
569 Returns true if @var{wc} is a @dfn{whitespace} character.  In the standard
570 @code{"C"} locale, @code{iswspace} returns true for only the standard
571 whitespace characters:
573 @table @code
574 @item L' '
575 space
577 @item L'\f'
578 formfeed
580 @item L'\n'
581 newline
583 @item L'\r'
584 carriage return
586 @item L'\t'
587 horizontal tab
589 @item L'\v'
590 vertical tab
591 @end table
593 @noindent
594 This function can be implemented using
596 @smallexample
597 iswctype (wc, wctype ("space"))
598 @end smallexample
600 @pindex wctype.h
601 It is declared in @file{wctype.h}.
602 @end deftypefun
604 @cindex upper-case character
605 @deftypefun int iswupper (wint_t @var{wc})
606 @standards{ISO, wctype.h}
607 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
608 Returns true if @var{wc} is an upper-case letter.  The letter need not be
609 from the Latin alphabet, any alphabet representable is valid.
611 @noindent
612 This function can be implemented using
614 @smallexample
615 iswctype (wc, wctype ("upper"))
616 @end smallexample
618 @pindex wctype.h
619 It is declared in @file{wctype.h}.
620 @end deftypefun
622 @cindex hexadecimal digit character
623 @deftypefun int iswxdigit (wint_t @var{wc})
624 @standards{ISO, wctype.h}
625 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
626 Returns true if @var{wc} is a hexadecimal digit.
627 Hexadecimal digits include the normal decimal digits @samp{0} through
628 @samp{9} and the letters @samp{A} through @samp{F} and
629 @samp{a} through @samp{f}.
631 @noindent
632 This function can be implemented using
634 @smallexample
635 iswctype (wc, wctype ("xdigit"))
636 @end smallexample
638 @pindex wctype.h
639 It is declared in @file{wctype.h}.
640 @end deftypefun
642 @Theglibc{} also provides a function which is not defined in the
643 @w{ISO C} standard but which is available as a version for single byte
644 characters as well.
646 @cindex blank character
647 @deftypefun int iswblank (wint_t @var{wc})
648 @standards{ISO, wctype.h}
649 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
650 Returns true if @var{wc} is a blank character; that is, a space or a tab.
651 This function was originally a GNU extension, but was added in @w{ISO C99}.
652 It is declared in @file{wchar.h}.
653 @end deftypefun
655 @node Using Wide Char Classes, Wide Character Case Conversion, Classification of Wide Characters, Character Handling
656 @section Notes on using the wide character classes
658 The first note is probably not astonishing but still occasionally a
659 cause of problems.  The @code{isw@var{XXX}} functions can be implemented
660 using macros and in fact, @theglibc{} does this.  They are still
661 available as real functions but when the @file{wctype.h} header is
662 included the macros will be used.  This is the same as the
663 @code{char} type versions of these functions.
665 The second note covers something new.  It can be best illustrated by a
666 (real-world) example.  The first piece of code is an excerpt from the
667 original code.  It is truncated a bit but the intention should be clear.
669 @smallexample
671 is_in_class (int c, const char *class)
673   if (strcmp (class, "alnum") == 0)
674     return isalnum (c);
675   if (strcmp (class, "alpha") == 0)
676     return isalpha (c);
677   if (strcmp (class, "cntrl") == 0)
678     return iscntrl (c);
679   @dots{}
680   return 0;
682 @end smallexample
684 Now, with the @code{wctype} and @code{iswctype} you can avoid the
685 @code{if} cascades, but rewriting the code as follows is wrong:
687 @smallexample
689 is_in_class (int c, const char *class)
691   wctype_t desc = wctype (class);
692   return desc ? iswctype ((wint_t) c, desc) : 0;
694 @end smallexample
696 The problem is that it is not guaranteed that the wide character
697 representation of a single-byte character can be found using casting.
698 In fact, usually this fails miserably.  The correct solution to this
699 problem is to write the code as follows:
701 @smallexample
703 is_in_class (int c, const char *class)
705   wctype_t desc = wctype (class);
706   return desc ? iswctype (btowc (c), desc) : 0;
708 @end smallexample
710 @xref{Converting a Character}, for more information on @code{btowc}.
711 Note that this change probably does not improve the performance
712 of the program a lot since the @code{wctype} function still has to make
713 the string comparisons.  It gets really interesting if the
714 @code{is_in_class} function is called more than once for the
715 same class name.  In this case the variable @var{desc} could be computed
716 once and reused for all the calls.  Therefore the above form of the
717 function is probably not the final one.
720 @node Wide Character Case Conversion, , Using Wide Char Classes, Character Handling
721 @section Mapping of wide characters.
723 The classification functions are also generalized by the @w{ISO C}
724 standard.  Instead of just allowing the two standard mappings, a
725 locale can contain others.  Again, the @code{localedef} program
726 already supports generating such locale data files.
728 @deftp {Data Type} wctrans_t
729 @standards{ISO, wctype.h}
730 This data type is defined as a scalar type which can hold a value
731 representing the locale-dependent character mapping.  There is no way to
732 construct such a value apart from using the return value of the
733 @code{wctrans} function.
735 @pindex wctype.h
736 @noindent
737 This type is defined in @file{wctype.h}.
738 @end deftp
740 @deftypefun wctrans_t wctrans (const char *@var{property})
741 @standards{ISO, wctype.h}
742 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
743 @c Similar implementation, same caveats as wctype.
744 The @code{wctrans} function has to be used to find out whether a named
745 mapping is defined in the current locale selected for the
746 @code{LC_CTYPE} category.  If the returned value is non-zero, you can use
747 it afterwards in calls to @code{towctrans}.  If the return value is
748 zero no such mapping is known in the current locale.
750 Beside locale-specific mappings there are two mappings which are
751 guaranteed to be available in every locale:
753 @multitable @columnfractions .5 .5
754 @item
755 @code{"tolower"} @tab @code{"toupper"}
756 @end multitable
758 @pindex wctype.h
759 @noindent
760 These functions are declared in @file{wctype.h}.
761 @end deftypefun
763 @deftypefun wint_t towctrans (wint_t @var{wc}, wctrans_t @var{desc})
764 @standards{ISO, wctype.h}
765 @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
766 @c Same caveats as iswctype.
767 @code{towctrans} maps the input character @var{wc}
768 according to the rules of the mapping for which @var{desc} is a
769 descriptor, and returns the value it finds.  @var{desc} must be
770 obtained by a successful call to @code{wctrans}.
772 @pindex wctype.h
773 @noindent
774 This function is declared in @file{wctype.h}.
775 @end deftypefun
777 For the generally available mappings, the @w{ISO C} standard defines
778 convenient shortcuts so that it is not necessary to call @code{wctrans}
779 for them.
781 @deftypefun wint_t towlower (wint_t @var{wc})
782 @standards{ISO, wctype.h}
783 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
784 @c Same caveats as iswalnum, just using a wctrans rather than a wctype
785 @c table.
786 If @var{wc} is an upper-case letter, @code{towlower} returns the corresponding
787 lower-case letter.  If @var{wc} is not an upper-case letter,
788 @var{wc} is returned unchanged.
790 @noindent
791 @code{towlower} can be implemented using
793 @smallexample
794 towctrans (wc, wctrans ("tolower"))
795 @end smallexample
797 @pindex wctype.h
798 @noindent
799 This function is declared in @file{wctype.h}.
800 @end deftypefun
802 @deftypefun wint_t towupper (wint_t @var{wc})
803 @standards{ISO, wctype.h}
804 @safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
805 If @var{wc} is a lower-case letter, @code{towupper} returns the corresponding
806 upper-case letter.  Otherwise @var{wc} is returned unchanged.
808 @noindent
809 @code{towupper} can be implemented using
811 @smallexample
812 towctrans (wc, wctrans ("toupper"))
813 @end smallexample
815 @pindex wctype.h
816 @noindent
817 This function is declared in @file{wctype.h}.
818 @end deftypefun
820 The same warnings given in the last section for the use of the wide
821 character classification functions apply here.  It is not possible to
822 simply cast a @code{char} type value to a @code{wint_t} and use it as an
823 argument to @code{towctrans} calls.