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

Raymond Chen

Raymond

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 runtimeCategory flagsWin32 function
isalnumC1_ALPHA | C1_UPPER | C1_LOWER |
C1_DIGIT
IsCharAlphaNumeric sort of
isalphaC1_ALPHA | C1_UPPER | C1_LOWER IsCharAlpha sort of
isblankC1_BLANK
iscntrlC1_CNTRL
isdigitC1_DIGIT
isgraphC1_ALPHA | C1_UPPER | C1_LOWER |
C1_DIGIT | C1_PUNCT
islowerC1_LOWERIsCharLower
isprintC1_ALPHA | C1_UPPER | C1_LOWER |
C1_DIGIT | C1_PUNCT | C1_BLANK
ispunctC1_PUNCT
isspaceC1_SPACE
isupperC1_UPPERIsCharUpper
isxdigitC1_XDIGIT

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

0 comments

Comments are closed.