Skip to content

Instantly share code, notes, and snippets.

@dbremner
Created July 22, 2023 11:58
Show Gist options
  • Save dbremner/5de2ef59196968baf032a17b70e438aa to your computer and use it in GitHub Desktop.
Save dbremner/5de2ef59196968baf032a17b70e438aa to your computer and use it in GitHub Desktop.
These avoid issues with out of range values for ctype.h functions
#ifndef CHAR_PREDICATES_HPP
#define CHAR_PREDICATES_HPP
[[nodiscard]] static inline bool
is_digit(int ch)
{
return ((ch >= '0') && (ch <= '9'));
}
[[nodiscard]] static inline bool
is_alpha(int ch)
{
const bool lower = (ch >= 'a') && (ch <= 'z');
const bool upper = (ch >= 'A') && (ch <= 'Z');
const bool either = lower || upper;
return either;
}
[[nodiscard]] static inline bool
is_ident(int ch)
{
const bool any = (is_digit(ch)) || (is_alpha(ch)) || (ch == '_');
return any;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment