February 18th, 2019

How can I check in Win32 whether a Unicode character is any kind of digit?

Suppose you have a Unicode code unit wchar_t and you want to know whether it represents a numeric digit. If you have the ICU library, you can check if its code point’s u_charType is U_DECIMAL_DIGIT_NUMBER. But what about plain Win32?

For Win32, you can use the Get­String­TypeW function to obtain properties for each code unit.

bool IsUnicodeDigit(wchar_t ch)
{
    WORD type;
    return GetStringTypeW(CT_CTYPE1, &ch, 1, &type) &&
           (type & C1_DIGIT);
}

We ask the Get­String­TypeW function for the CT_CTYPE1 value for one character, passing an output buffer of size 1. We then check whether the result says that it is a digit.

The Get­String­TypeW function produces a 16-bit value for each provided code unit. There are more than 16 things you can ask about, so they are broken into groups, and you specify which group you want. Group 1 contains the basic classifications that support POSIX functions like isdigit and isalnum.

Here’s one way it could be done. (I’m not saying this is how it actually is done.)

C runtime Category flags Win32 function
isalnum C1_ALPHA | C1_UPPER | C1_LOWER |
C1_DIGIT
IsCharAlphaNumeric sort of
isalpha C1_ALPHA | C1_UPPER | C1_LOWER IsCharAlpha sort of
isblank C1_BLANK
iscntrl C1_CNTRL
isdigit C1_DIGIT
isgraph C1_ALPHA | C1_UPPER | C1_LOWER |
C1_DIGIT | C1_PUNCT
islower C1_LOWER IsCharLower
isprint C1_ALPHA | C1_UPPER | C1_LOWER |
C1_DIGIT | C1_PUNCT | C1_BLANK
ispunct C1_PUNCT
isspace C1_SPACE
isupper C1_UPPER IsCharUpper
isxdigit C1_XDIGIT

Bonus reading: The difference between C1_SPACE-ing out and drawing a C1_BLANK.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.