The Wide-Character Classification Functions. McGraw-Hill The Complete Reference Fourth Edition
Quảng cáo
Quảng cáo
776
C + + : T h e C o m p l e t e R e f e r e n c e
I n 1995, a number of wide-character functions were added to Standard C and subsequently adopted by Standard C++. The wide-character functions operate on characters of type wchar_t, which are 16 bits. For the most part these functions parallel their char equivalents. For example, the function iswspace( ) is the widecharacter version of isspace( ). In general, the wide-character functions use the same names as their char equivalents, except that a "w" is added.
The wide-character functions use two headers: <cwchar> and <cwctype>. The C header files wchar.h and wctype.h are also supported.
The header <cwctype> defines the types wint_t, wctrans_t, and wctype_t. Many of the wide-character functions receive a wide character as a parameter. The type of this parameter is wint_t. It is capable of holding a wide character. The use of the wint_t type in the wide-character functions parallels the use of int in the char-based functions.
wctrans_t
and wctype_t are the types of objects used to represent a character mapping
(i.e., character translation) and the classification of a character, respectively. The widecharacter EOF mark is defined as WEOF.
In addition to defining win_t, the header <cwchar> defines the types wchar_t,
size_t
, and mbstate_t. The wchar_t type creates a wide character object, and size_t is the type of value returned by sizeof. The mbstate_t type describes an object that holds the state of a multibyte-to-wide-character conversion. The <cwchar> header also defines the macros NULL, WEOF, WCHAR_MAX, and WCHAR_MIN. The last two define the maximum and minimum value that can be held in an object of type wchar_t.
Although the standard function library's support for wide characters is quite extensive, these functions are not frequently used. One reason for this is that the Standard C++
I/O system and class libraries provide both normal and wide-character support through the use of template classes. Also, interest in wide-character-compliant programs has been less than expected. Of course, this situation may change.
Since most of the wide-character functions simply parallel their char equivalents and are not frequently used by most C++ programmers, only a brief description of these functions is provided.
The Wide-Character Classification Functions
The header <cwctype> provides the prototypes for the wide-character functions that support character classification. These functions categorize wide characters as to their type or convert the case of a character. Table 31-1 lists these functions along with their char equivalents, which are described in Chapter 26.
In addition to the functions shown in Table 31-1, <cwctype> defines the following ones, which provide an open-ended means of classifying characters.
wctype_t wctype(const char *attr); int iswctype(wint_t ch, wctype_t attr_ob);
C h a p t e r 3 1 : T h e W i d e - C h a r a c t e r F u n c t i o n s
777
Function int iswalnum(wint_t ch) int iswalpha(wint_t ch) int iswcntrl(wint_t ch) int iswdigit(wint_t ch) int iswgraph(wint_t ch) int iswlower(wint_t ch) int iswprint(wint_t ch) int iswpunct(wint_t c) int iswspace(wint_t ch) int iswupper(wint_t ch) int iswxdigit(wint_t ch) wint_t tolower(wint_t ch) wint_t toupper(wint_t ch) char Equivalent isalnum( ) isalpha( ) iscntrl( ) isdigit( ) isgraph( ) islower( ) isprint( ) ispunct( ) isspace( ) isupper( ) isxdigit( ) tolower( ) toupper( )
Table 31-1.
The Wide-Character Classification Functions
The function wctype( ) returns a value that can be passed to the attr_ob parameter to iswctype( ). The string pointed to by attr specifies a property that a character must have. The value in attr_ob is used to determine if ch is a character that has that property.
If it does, iswctype( ) returns nonzero. Otherwise, it returns zero. The following property strings are defined for all execution environments.
alnum graph space alpha lower upper cntrl print xdigit digit punct
The following program demonstrates the wctype( ) and iswctype( ) functions.
#include <iostream>
#include <cwctype> using namespace std;
778
C + + : T h e C o m p l e t e R e f e r e n c e int main()
{ wctype_t x; x = wctype("space"); if(iswctype(L' ', x)) cout << "Is a space.\n";
} return 0;
This program displays "Is a space."
The functions wctrans( ) and towctrans( ) are also defined in <cwctype>. They are shown here: wctrans_t wctrans(const char *mapping); wint_t towctrans(wint_t ch, wctrans_t mapping_ob);
The function wctrans( ) returns a value that can be passed to the mapping_ob parameter to towctrans( ). Here, the string pointed to by mapping specifies a mapping of one character to another. This value can then be used by iswctrans( ) to map ch. The mapped value is returned. The following mapping strings are supported in all execution environments.
tolower toupper
Here is a short example that demonstrates wctrans( ) and towctrans( ).
#include <iostream>
#include <cwctype> using namespace std; int main()
{ wctrans_t x; x = wctrans("tolower");
Tải xuống
Quảng cáo