3 ms·
I'm 100% human, but also 100% amateur. Until 5 minutes ago, I understood <ctype h> to be necessary for me to use typedef to enable me to refer to structs by an
by csmachines 2y ago
I'm 100% human, but also 100% amateur. Until 5 minutes ago, I understood <ctype h> to be necessary for me to use typedef to enable me to refer to structs by an alias, instead of writing "struct"! The perils of being self-taught. Now I can see the only errors thrown up when I remove the include are isdigit and isalnum! This is why I posted on HN!
EDIT: I think I can fix the isalnum bug risk that you identified by checking that the value of the ch variable in the get_string_input function is non-negative before allowing it to be appended to str.
- tomcam 2y agoLove your attitude. This will take you a long way.
- kazinator 2y agoThe problem is that multi-byte data like UTF-8, stored in char strings, produces negative values. The can't just throw those away. The usual workaround for the nasty <ctype.h> functions is to cast the char value to unsigned char. if (isalnum((unsigned char) str[i])) .. type of thing. The <ctype.h> functions also have locale-specific behavior. What is an alphabetic character depends on locale. For instance various ISO Latin characters may be alphabetic. Unfortunately, those characters are also in the range of UTF-8 continuation bytes (128-255). If you blindly apply <ctype.h> on UTF-8 data in a program that has called setlocale() to establish some locale, you can get weird results. If you intend to test for the ASCII alphabetic characters, and nothing else, no matter what the locale is set to, then your best bet is (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z').
- csmachines 2y agoGreat, thanks for the advice. I'll include this in the weekend's updates.