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

This post has been republished via RSS; it originally appeared at: MSDN Blogs.


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
.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.