Blame manual/ctype.texi

Packit Service 82fcde
@node Character Handling, String and Array Utilities, Memory, Top
Packit Service 82fcde
@c %MENU% Character testing and conversion functions
Packit Service 82fcde
@chapter Character Handling
Packit Service 82fcde
Packit Service 82fcde
Programs that work with characters and strings often need to classify a
Packit Service 82fcde
character---is it alphabetic, is it a digit, is it whitespace, and so
Packit Service 82fcde
on---and perform case conversion operations on characters.  The
Packit Service 82fcde
functions in the header file @file{ctype.h} are provided for this
Packit Service 82fcde
purpose.
Packit Service 82fcde
@pindex ctype.h
Packit Service 82fcde
Packit Service 82fcde
Since the choice of locale and character set can alter the
Packit Service 82fcde
classifications of particular character codes, all of these functions
Packit Service 82fcde
are affected by the current locale.  (More precisely, they are affected
Packit Service 82fcde
by the locale currently selected for character classification---the
Packit Service 82fcde
@code{LC_CTYPE} category; see @ref{Locale Categories}.)
Packit Service 82fcde
Packit Service 82fcde
The @w{ISO C} standard specifies two different sets of functions.  The
Packit Service 82fcde
one set works on @code{char} type characters, the other one on
Packit Service 82fcde
@code{wchar_t} wide characters (@pxref{Extended Char Intro}).
Packit Service 82fcde
Packit Service 82fcde
@menu
Packit Service 82fcde
* Classification of Characters::       Testing whether characters are
Packit Service 82fcde
			                letters, digits, punctuation, etc.
Packit Service 82fcde
Packit Service 82fcde
* Case Conversion::                    Case mapping, and the like.
Packit Service 82fcde
* Classification of Wide Characters::  Character class determination for
Packit Service 82fcde
                                        wide characters.
Packit Service 82fcde
* Using Wide Char Classes::            Notes on using the wide character
Packit Service 82fcde
                                        classes.
Packit Service 82fcde
* Wide Character Case Conversion::     Mapping of wide characters.
Packit Service 82fcde
@end menu
Packit Service 82fcde
Packit Service 82fcde
@node Classification of Characters, Case Conversion,  , Character Handling
Packit Service 82fcde
@section Classification of Characters
Packit Service 82fcde
@cindex character testing
Packit Service 82fcde
@cindex classification of characters
Packit Service 82fcde
@cindex predicates on characters
Packit Service 82fcde
@cindex character predicates
Packit Service 82fcde
Packit Service 82fcde
This section explains the library functions for classifying characters.
Packit Service 82fcde
For example, @code{isalpha} is the function to test for an alphabetic
Packit Service 82fcde
character.  It takes one argument, the character to test, and returns a
Packit Service 82fcde
nonzero integer if the character is alphabetic, and zero otherwise.  You
Packit Service 82fcde
would use it like this:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
if (isalpha (c))
Packit Service 82fcde
  printf ("The character `%c' is alphabetic.\n", c);
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Each of the functions in this section tests for membership in a
Packit Service 82fcde
particular class of characters; each has a name starting with @samp{is}.
Packit Service 82fcde
Each of them takes one argument, which is a character to test, and
Packit Service 82fcde
returns an @code{int} which is treated as a boolean value.  The
Packit Service 82fcde
character argument is passed as an @code{int}, and it may be the
Packit Service 82fcde
constant value @code{EOF} instead of a real character.
Packit Service 82fcde
Packit Service 82fcde
The attributes of any given character can vary between locales.
Packit Service 82fcde
@xref{Locales}, for more information on locales.@refill
Packit Service 82fcde
Packit Service 82fcde
These functions are declared in the header file @file{ctype.h}.
Packit Service 82fcde
@pindex ctype.h
Packit Service 82fcde
Packit Service 82fcde
@cindex lower-case character
Packit Service 82fcde
@deftypefun int islower (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c The is* macros call __ctype_b_loc to get the ctype array from the
Packit Service 82fcde
@c current locale, and then index it by c.  __ctype_b_loc reads from
Packit Service 82fcde
@c thread-local memory the (indirect) pointer to the ctype array, which
Packit Service 82fcde
@c may involve one word access to the global locale object, if that's
Packit Service 82fcde
@c the active locale for the thread, and the array, being part of the
Packit Service 82fcde
@c locale data, is undeletable, so there's no thread-safety issue.  We
Packit Service 82fcde
@c might want to mark these with @mtslocale to flag to callers that
Packit Service 82fcde
@c changing locales might affect them, even if not these simpler
Packit Service 82fcde
@c functions.
Packit Service 82fcde
Returns true if @var{c} is a lower-case letter.  The letter need not be
Packit Service 82fcde
from the Latin alphabet, any alphabet representable is valid.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex upper-case character
Packit Service 82fcde
@deftypefun int isupper (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is an upper-case letter.  The letter need not be
Packit Service 82fcde
from the Latin alphabet, any alphabet representable is valid.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex alphabetic character
Packit Service 82fcde
@deftypefun int isalpha (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is an alphabetic character (a letter).  If
Packit Service 82fcde
@code{islower} or @code{isupper} is true of a character, then
Packit Service 82fcde
@code{isalpha} is also true.
Packit Service 82fcde
Packit Service 82fcde
In some locales, there may be additional characters for which
Packit Service 82fcde
@code{isalpha} is true---letters which are neither upper case nor lower
Packit Service 82fcde
case.  But in the standard @code{"C"} locale, there are no such
Packit Service 82fcde
additional characters.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex digit character
Packit Service 82fcde
@cindex decimal digit character
Packit Service 82fcde
@deftypefun int isdigit (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a decimal digit (@samp{0} through @samp{9}).
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex alphanumeric character
Packit Service 82fcde
@deftypefun int isalnum (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is an alphanumeric character (a letter or
Packit Service 82fcde
number); in other words, if either @code{isalpha} or @code{isdigit} is
Packit Service 82fcde
true of a character, then @code{isalnum} is also true.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex hexadecimal digit character
Packit Service 82fcde
@deftypefun int isxdigit (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a hexadecimal digit.
Packit Service 82fcde
Hexadecimal digits include the normal decimal digits @samp{0} through
Packit Service 82fcde
@samp{9} and the letters @samp{A} through @samp{F} and
Packit Service 82fcde
@samp{a} through @samp{f}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex punctuation character
Packit Service 82fcde
@deftypefun int ispunct (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a punctuation character.
Packit Service 82fcde
This means any printing character that is not alphanumeric or a space
Packit Service 82fcde
character.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex whitespace character
Packit Service 82fcde
@deftypefun int isspace (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a @dfn{whitespace} character.  In the standard
Packit Service 82fcde
@code{"C"} locale, @code{isspace} returns true for only the standard
Packit Service 82fcde
whitespace characters:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item ' '
Packit Service 82fcde
space
Packit Service 82fcde
Packit Service 82fcde
@item '\f'
Packit Service 82fcde
formfeed
Packit Service 82fcde
Packit Service 82fcde
@item '\n'
Packit Service 82fcde
newline
Packit Service 82fcde
Packit Service 82fcde
@item '\r'
Packit Service 82fcde
carriage return
Packit Service 82fcde
Packit Service 82fcde
@item '\t'
Packit Service 82fcde
horizontal tab
Packit Service 82fcde
Packit Service 82fcde
@item '\v'
Packit Service 82fcde
vertical tab
Packit Service 82fcde
@end table
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex blank character
Packit Service 82fcde
@deftypefun int isblank (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a blank character; that is, a space or a tab.
Packit Service 82fcde
This function was originally a GNU extension, but was added in @w{ISO C99}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex graphic character
Packit Service 82fcde
@deftypefun int isgraph (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a graphic character; that is, a character
Packit Service 82fcde
that has a glyph associated with it.  The whitespace characters are not
Packit Service 82fcde
considered graphic.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex printing character
Packit Service 82fcde
@deftypefun int isprint (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a printing character.  Printing characters
Packit Service 82fcde
include all the graphic characters, plus the space (@samp{ }) character.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex control character
Packit Service 82fcde
@deftypefun int iscntrl (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a control character (that is, a character that
Packit Service 82fcde
is not a printing character).
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex ASCII character
Packit Service 82fcde
@deftypefun int isascii (int @var{c})
Packit Service 82fcde
@standards{SVID, ctype.h}
Packit Service 82fcde
@standards{BSD, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{c} is a 7-bit @code{unsigned char} value that fits
Packit Service 82fcde
into the US/UK ASCII character set.  This function is a BSD extension
Packit Service 82fcde
and is also an SVID extension.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Case Conversion, Classification of Wide Characters, Classification of Characters, Character Handling
Packit Service 82fcde
@section Case Conversion
Packit Service 82fcde
@cindex character case conversion
Packit Service 82fcde
@cindex case conversion of characters
Packit Service 82fcde
@cindex converting case of characters
Packit Service 82fcde
Packit Service 82fcde
This section explains the library functions for performing conversions
Packit Service 82fcde
such as case mappings on characters.  For example, @code{toupper}
Packit Service 82fcde
converts any character to upper case if possible.  If the character
Packit Service 82fcde
can't be converted, @code{toupper} returns it unchanged.
Packit Service 82fcde
Packit Service 82fcde
These functions take one argument of type @code{int}, which is the
Packit Service 82fcde
character to convert, and return the converted character as an
Packit Service 82fcde
@code{int}.  If the conversion is not applicable to the argument given,
Packit Service 82fcde
the argument is returned unchanged.
Packit Service 82fcde
Packit Service 82fcde
@strong{Compatibility Note:} In pre-@w{ISO C} dialects, instead of
Packit Service 82fcde
returning the argument unchanged, these functions may fail when the
Packit Service 82fcde
argument is not suitable for the conversion.  Thus for portability, you
Packit Service 82fcde
may need to write @code{islower(c) ? toupper(c) : c} rather than just
Packit Service 82fcde
@code{toupper(c)}.
Packit Service 82fcde
Packit Service 82fcde
These functions are declared in the header file @file{ctype.h}.
Packit Service 82fcde
@pindex ctype.h
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int tolower (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c The to* macros/functions call different functions that use different
Packit Service 82fcde
@c arrays than those of__ctype_b_loc, but the access patterns and
Packit Service 82fcde
@c thus safety guarantees are the same.
Packit Service 82fcde
If @var{c} is an upper-case letter, @code{tolower} returns the corresponding
Packit Service 82fcde
lower-case letter.  If @var{c} is not an upper-case letter,
Packit Service 82fcde
@var{c} is returned unchanged.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int toupper (int @var{c})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
If @var{c} is a lower-case letter, @code{toupper} returns the corresponding
Packit Service 82fcde
upper-case letter.  Otherwise @var{c} is returned unchanged.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int toascii (int @var{c})
Packit Service 82fcde
@standards{SVID, ctype.h}
Packit Service 82fcde
@standards{BSD, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
This function converts @var{c} to a 7-bit @code{unsigned char} value
Packit Service 82fcde
that fits into the US/UK ASCII character set, by clearing the high-order
Packit Service 82fcde
bits.  This function is a BSD extension and is also an SVID extension.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int _tolower (int @var{c})
Packit Service 82fcde
@standards{SVID, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
This is identical to @code{tolower}, and is provided for compatibility
Packit Service 82fcde
with the SVID.  @xref{SVID}.@refill
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int _toupper (int @var{c})
Packit Service 82fcde
@standards{SVID, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
This is identical to @code{toupper}, and is provided for compatibility
Packit Service 82fcde
with the SVID.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Classification of Wide Characters, Using Wide Char Classes, Case Conversion, Character Handling
Packit Service 82fcde
@section Character class determination for wide characters
Packit Service 82fcde
Packit Service 82fcde
@w{Amendment 1} to @w{ISO C90} defines functions to classify wide
Packit Service 82fcde
characters.  Although the original @w{ISO C90} standard already defined
Packit Service 82fcde
the type @code{wchar_t}, no functions operating on them were defined.
Packit Service 82fcde
Packit Service 82fcde
The general design of the classification functions for wide characters
Packit Service 82fcde
is more general.  It allows extensions to the set of available
Packit Service 82fcde
classifications, beyond those which are always available.  The POSIX
Packit Service 82fcde
standard specifies how extensions can be made, and this is already
Packit Service 82fcde
implemented in the @glibcadj{} implementation of the @code{localedef}
Packit Service 82fcde
program.
Packit Service 82fcde
Packit Service 82fcde
The character class functions are normally implemented with bitsets,
Packit Service 82fcde
with a bitset per character.  For a given character, the appropriate
Packit Service 82fcde
bitset is read from a table and a test is performed as to whether a
Packit Service 82fcde
certain bit is set.  Which bit is tested for is determined by the
Packit Service 82fcde
class.
Packit Service 82fcde
Packit Service 82fcde
For the wide character classification functions this is made visible.
Packit Service 82fcde
There is a type classification type defined, a function to retrieve this
Packit Service 82fcde
value for a given class, and a function to test whether a given
Packit Service 82fcde
character is in this class, using the classification value.  On top of
Packit Service 82fcde
this the normal character classification functions as used for
Packit Service 82fcde
@code{char} objects can be defined.
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data type} wctype_t
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
The @code{wctype_t} can hold a value which represents a character class.
Packit Service 82fcde
The only defined way to generate such a value is by using the
Packit Service 82fcde
@code{wctype} function.
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
This type is defined in @file{wctype.h}.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun wctype_t wctype (const char *@var{property})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Although the source code of wctype contains multiple references to
Packit Service 82fcde
@c the locale, that could each reference different locale_data objects
Packit Service 82fcde
@c should the global locale object change while active, the compiler can
Packit Service 82fcde
@c and does combine them all into a single dereference that resolves
Packit Service 82fcde
@c once to the LCTYPE locale object used throughout the function, so it
Packit Service 82fcde
@c is safe in (optimized) practice, if not in theory, even when the
Packit Service 82fcde
@c locale changes.  Ideally we'd explicitly save the resolved
Packit Service 82fcde
@c locale_data object to make it visibly safe instead of safe only under
Packit Service 82fcde
@c compiler optimizations, but given the decision that setlocale is
Packit Service 82fcde
@c MT-Unsafe, all this would afford us would be the ability to not mark
Packit Service 82fcde
@c this function with @mtslocale.
Packit Service 82fcde
@code{wctype} returns a value representing a class of wide
Packit Service 82fcde
characters which is identified by the string @var{property}.  Besides
Packit Service 82fcde
some standard properties each locale can define its own ones.  In case
Packit Service 82fcde
no property with the given name is known for the current locale
Packit Service 82fcde
selected for the @code{LC_CTYPE} category, the function returns zero.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
The properties known in every locale are:
Packit Service 82fcde
Packit Service 82fcde
@multitable @columnfractions .25 .25 .25 .25
Packit Service 82fcde
@item
Packit Service 82fcde
@code{"alnum"} @tab @code{"alpha"} @tab @code{"cntrl"} @tab @code{"digit"}
Packit Service 82fcde
@item
Packit Service 82fcde
@code{"graph"} @tab @code{"lower"} @tab @code{"print"} @tab @code{"punct"}
Packit Service 82fcde
@item
Packit Service 82fcde
@code{"space"} @tab @code{"upper"} @tab @code{"xdigit"}
Packit Service 82fcde
@end multitable
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
This function is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
To test the membership of a character to one of the non-standard classes
Packit Service 82fcde
the @w{ISO C} standard defines a completely new function.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun int iswctype (wint_t @var{wc}, wctype_t @var{desc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c The compressed lookup table returned by wctype is read-only.
Packit Service 82fcde
This function returns a nonzero value if @var{wc} is in the character
Packit Service 82fcde
class specified by @var{desc}.  @var{desc} must previously be returned
Packit Service 82fcde
by a successful call to @code{wctype}.
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
This function is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
To make it easier to use the commonly-used classification functions,
Packit Service 82fcde
they are defined in the C library.  There is no need to use
Packit Service 82fcde
@code{wctype} if the property string is one of the known character
Packit Service 82fcde
classes.  In some situations it is desirable to construct the property
Packit Service 82fcde
strings, and then it is important that @code{wctype} can also handle the
Packit Service 82fcde
standard classes.
Packit Service 82fcde
Packit Service 82fcde
@cindex alphanumeric character
Packit Service 82fcde
@deftypefun int iswalnum (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c The implicit wctype call in the isw* functions is actually an
Packit Service 82fcde
@c optimized version because the category has a known offset, but the
Packit Service 82fcde
@c wctype is equally safe when optimized, unsafe with changing locales
Packit Service 82fcde
@c if not optimized (thus @mtslocale).  Since it's not a macro, we
Packit Service 82fcde
@c always optimize, and the locale can't change in any MT-Safe way, it's
Packit Service 82fcde
@c fine.  The test whether wc is ASCII to use the non-wide is*
Packit Service 82fcde
@c macro/function doesn't bring any other safety issues: the test does
Packit Service 82fcde
@c not depend on the locale, and each path after the decision resolves
Packit Service 82fcde
@c the locale object only once.
Packit Service 82fcde
This function returns a nonzero value if @var{wc} is an alphanumeric
Packit Service 82fcde
character (a letter or number); in other words, if either @code{iswalpha}
Packit Service 82fcde
or @code{iswdigit} is true of a character, then @code{iswalnum} is also
Packit Service 82fcde
true.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("alnum"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex alphabetic character
Packit Service 82fcde
@deftypefun int iswalpha (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is an alphabetic character (a letter).  If
Packit Service 82fcde
@code{iswlower} or @code{iswupper} is true of a character, then
Packit Service 82fcde
@code{iswalpha} is also true.
Packit Service 82fcde
Packit Service 82fcde
In some locales, there may be additional characters for which
Packit Service 82fcde
@code{iswalpha} is true---letters which are neither upper case nor lower
Packit Service 82fcde
case.  But in the standard @code{"C"} locale, there are no such
Packit Service 82fcde
additional characters.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("alpha"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex control character
Packit Service 82fcde
@deftypefun int iswcntrl (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a control character (that is, a character that
Packit Service 82fcde
is not a printing character).
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("cntrl"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex digit character
Packit Service 82fcde
@deftypefun int iswdigit (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a digit (e.g., @samp{0} through @samp{9}).
Packit Service 82fcde
Please note that this function does not only return a nonzero value for
Packit Service 82fcde
@emph{decimal} digits, but for all kinds of digits.  A consequence is
Packit Service 82fcde
that code like the following will @strong{not} work unconditionally for
Packit Service 82fcde
wide characters:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
n = 0;
Packit Service 82fcde
while (iswdigit (*wc))
Packit Service 82fcde
  @{
Packit Service 82fcde
    n *= 10;
Packit Service 82fcde
    n += *wc++ - L'0';
Packit Service 82fcde
  @}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("digit"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex graphic character
Packit Service 82fcde
@deftypefun int iswgraph (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a graphic character; that is, a character
Packit Service 82fcde
that has a glyph associated with it.  The whitespace characters are not
Packit Service 82fcde
considered graphic.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("graph"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex lower-case character
Packit Service 82fcde
@deftypefun int iswlower (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, ctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a lower-case letter.  The letter need not be
Packit Service 82fcde
from the Latin alphabet, any alphabet representable is valid.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("lower"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex printing character
Packit Service 82fcde
@deftypefun int iswprint (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a printing character.  Printing characters
Packit Service 82fcde
include all the graphic characters, plus the space (@samp{ }) character.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("print"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex punctuation character
Packit Service 82fcde
@deftypefun int iswpunct (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a punctuation character.
Packit Service 82fcde
This means any printing character that is not alphanumeric or a space
Packit Service 82fcde
character.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("punct"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex whitespace character
Packit Service 82fcde
@deftypefun int iswspace (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a @dfn{whitespace} character.  In the standard
Packit Service 82fcde
@code{"C"} locale, @code{iswspace} returns true for only the standard
Packit Service 82fcde
whitespace characters:
Packit Service 82fcde
Packit Service 82fcde
@table @code
Packit Service 82fcde
@item L' '
Packit Service 82fcde
space
Packit Service 82fcde
Packit Service 82fcde
@item L'\f'
Packit Service 82fcde
formfeed
Packit Service 82fcde
Packit Service 82fcde
@item L'\n'
Packit Service 82fcde
newline
Packit Service 82fcde
Packit Service 82fcde
@item L'\r'
Packit Service 82fcde
carriage return
Packit Service 82fcde
Packit Service 82fcde
@item L'\t'
Packit Service 82fcde
horizontal tab
Packit Service 82fcde
Packit Service 82fcde
@item L'\v'
Packit Service 82fcde
vertical tab
Packit Service 82fcde
@end table
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("space"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex upper-case character
Packit Service 82fcde
@deftypefun int iswupper (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is an upper-case letter.  The letter need not be
Packit Service 82fcde
from the Latin alphabet, any alphabet representable is valid.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("upper"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@cindex hexadecimal digit character
Packit Service 82fcde
@deftypefun int iswxdigit (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a hexadecimal digit.
Packit Service 82fcde
Hexadecimal digits include the normal decimal digits @samp{0} through
Packit Service 82fcde
@samp{9} and the letters @samp{A} through @samp{F} and
Packit Service 82fcde
@samp{a} through @samp{f}.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
iswctype (wc, wctype ("xdigit"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
It is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@Theglibc{} also provides a function which is not defined in the
Packit Service 82fcde
@w{ISO C} standard but which is available as a version for single byte
Packit Service 82fcde
characters as well.
Packit Service 82fcde
Packit Service 82fcde
@cindex blank character
Packit Service 82fcde
@deftypefun int iswblank (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
Returns true if @var{wc} is a blank character; that is, a space or a tab.
Packit Service 82fcde
This function was originally a GNU extension, but was added in @w{ISO C99}.
Packit Service 82fcde
It is declared in @file{wchar.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@node Using Wide Char Classes, Wide Character Case Conversion, Classification of Wide Characters, Character Handling
Packit Service 82fcde
@section Notes on using the wide character classes
Packit Service 82fcde
Packit Service 82fcde
The first note is probably not astonishing but still occasionally a
Packit Service 82fcde
cause of problems.  The @code{isw@var{XXX}} functions can be implemented
Packit Service 82fcde
using macros and in fact, @theglibc{} does this.  They are still
Packit Service 82fcde
available as real functions but when the @file{wctype.h} header is
Packit Service 82fcde
included the macros will be used.  This is the same as the
Packit Service 82fcde
@code{char} type versions of these functions.
Packit Service 82fcde
Packit Service 82fcde
The second note covers something new.  It can be best illustrated by a
Packit Service 82fcde
(real-world) example.  The first piece of code is an excerpt from the
Packit Service 82fcde
original code.  It is truncated a bit but the intention should be clear.
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int
Packit Service 82fcde
is_in_class (int c, const char *class)
Packit Service 82fcde
@{
Packit Service 82fcde
  if (strcmp (class, "alnum") == 0)
Packit Service 82fcde
    return isalnum (c);
Packit Service 82fcde
  if (strcmp (class, "alpha") == 0)
Packit Service 82fcde
    return isalpha (c);
Packit Service 82fcde
  if (strcmp (class, "cntrl") == 0)
Packit Service 82fcde
    return iscntrl (c);
Packit Service 82fcde
  @dots{}
Packit Service 82fcde
  return 0;
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
Now, with the @code{wctype} and @code{iswctype} you can avoid the
Packit Service 82fcde
@code{if} cascades, but rewriting the code as follows is wrong:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int
Packit Service 82fcde
is_in_class (int c, const char *class)
Packit Service 82fcde
@{
Packit Service 82fcde
  wctype_t desc = wctype (class);
Packit Service 82fcde
  return desc ? iswctype ((wint_t) c, desc) : 0;
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
The problem is that it is not guaranteed that the wide character
Packit Service 82fcde
representation of a single-byte character can be found using casting.
Packit Service 82fcde
In fact, usually this fails miserably.  The correct solution to this
Packit Service 82fcde
problem is to write the code as follows:
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
int
Packit Service 82fcde
is_in_class (int c, const char *class)
Packit Service 82fcde
@{
Packit Service 82fcde
  wctype_t desc = wctype (class);
Packit Service 82fcde
  return desc ? iswctype (btowc (c), desc) : 0;
Packit Service 82fcde
@}
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@xref{Converting a Character}, for more information on @code{btowc}.
Packit Service 82fcde
Note that this change probably does not improve the performance
Packit Service 82fcde
of the program a lot since the @code{wctype} function still has to make
Packit Service 82fcde
the string comparisons.  It gets really interesting if the
Packit Service 82fcde
@code{is_in_class} function is called more than once for the
Packit Service 82fcde
same class name.  In this case the variable @var{desc} could be computed
Packit Service 82fcde
once and reused for all the calls.  Therefore the above form of the
Packit Service 82fcde
function is probably not the final one.
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
@node Wide Character Case Conversion, , Using Wide Char Classes, Character Handling
Packit Service 82fcde
@section Mapping of wide characters.
Packit Service 82fcde
Packit Service 82fcde
The classification functions are also generalized by the @w{ISO C}
Packit Service 82fcde
standard.  Instead of just allowing the two standard mappings, a
Packit Service 82fcde
locale can contain others.  Again, the @code{localedef} program
Packit Service 82fcde
already supports generating such locale data files.
Packit Service 82fcde
Packit Service 82fcde
@deftp {Data Type} wctrans_t
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
This data type is defined as a scalar type which can hold a value
Packit Service 82fcde
representing the locale-dependent character mapping.  There is no way to
Packit Service 82fcde
construct such a value apart from using the return value of the
Packit Service 82fcde
@code{wctrans} function.
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
@noindent
Packit Service 82fcde
This type is defined in @file{wctype.h}.
Packit Service 82fcde
@end deftp
Packit Service 82fcde
Packit Service 82fcde
@deftypefun wctrans_t wctrans (const char *@var{property})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Similar implementation, same caveats as wctype.
Packit Service 82fcde
The @code{wctrans} function has to be used to find out whether a named
Packit Service 82fcde
mapping is defined in the current locale selected for the
Packit Service 82fcde
@code{LC_CTYPE} category.  If the returned value is non-zero, you can use
Packit Service 82fcde
it afterwards in calls to @code{towctrans}.  If the return value is
Packit Service 82fcde
zero no such mapping is known in the current locale.
Packit Service 82fcde
Packit Service 82fcde
Beside locale-specific mappings there are two mappings which are
Packit Service 82fcde
guaranteed to be available in every locale:
Packit Service 82fcde
Packit Service 82fcde
@multitable @columnfractions .5 .5
Packit Service 82fcde
@item
Packit Service 82fcde
@code{"tolower"} @tab @code{"toupper"}
Packit Service 82fcde
@end multitable
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
@noindent
Packit Service 82fcde
These functions are declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun wint_t towctrans (wint_t @var{wc}, wctrans_t @var{desc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Same caveats as iswctype.
Packit Service 82fcde
@code{towctrans} maps the input character @var{wc}
Packit Service 82fcde
according to the rules of the mapping for which @var{desc} is a
Packit Service 82fcde
descriptor, and returns the value it finds.  @var{desc} must be
Packit Service 82fcde
obtained by a successful call to @code{wctrans}.
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
For the generally available mappings, the @w{ISO C} standard defines
Packit Service 82fcde
convenient shortcuts so that it is not necessary to call @code{wctrans}
Packit Service 82fcde
for them.
Packit Service 82fcde
Packit Service 82fcde
@deftypefun wint_t towlower (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
@c Same caveats as iswalnum, just using a wctrans rather than a wctype
Packit Service 82fcde
@c table.
Packit Service 82fcde
If @var{wc} is an upper-case letter, @code{towlower} returns the corresponding
Packit Service 82fcde
lower-case letter.  If @var{wc} is not an upper-case letter,
Packit Service 82fcde
@var{wc} is returned unchanged.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
@code{towlower} can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
towctrans (wc, wctrans ("tolower"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
@deftypefun wint_t towupper (wint_t @var{wc})
Packit Service 82fcde
@standards{ISO, wctype.h}
Packit Service 82fcde
@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
Packit Service 82fcde
If @var{wc} is a lower-case letter, @code{towupper} returns the corresponding
Packit Service 82fcde
upper-case letter.  Otherwise @var{wc} is returned unchanged.
Packit Service 82fcde
Packit Service 82fcde
@noindent
Packit Service 82fcde
@code{towupper} can be implemented using
Packit Service 82fcde
Packit Service 82fcde
@smallexample
Packit Service 82fcde
towctrans (wc, wctrans ("toupper"))
Packit Service 82fcde
@end smallexample
Packit Service 82fcde
Packit Service 82fcde
@pindex wctype.h
Packit Service 82fcde
@noindent
Packit Service 82fcde
This function is declared in @file{wctype.h}.
Packit Service 82fcde
@end deftypefun
Packit Service 82fcde
Packit Service 82fcde
The same warnings given in the last section for the use of the wide
Packit Service 82fcde
character classification functions apply here.  It is not possible to
Packit Service 82fcde
simply cast a @code{char} type value to a @code{wint_t} and use it as an
Packit Service 82fcde
argument to @code{towctrans} calls.