| 1 | #ifndef SANE_CTYPE_H |
| 2 | #define SANE_CTYPE_H |
| 3 | |
| 4 | /* |
| 5 | * Explicitly include <ctype.h> so that its header guards kick in from here on. |
| 6 | * This ensures that the file won't get included after "sane-ctype.h", as that |
| 7 | * would otherwise lead to a compiler error because the function declarations |
| 8 | * for `int isascii(int c)` et al would be mangled by our macros with the same |
| 9 | * name. |
| 10 | */ |
| 11 | #include <ctype.h> |
| 12 | |
| 13 | /* Sane ctype - no locale, and works with signed chars */ |
| 14 | #undef isascii |
| 15 | #undef isspace |
| 16 | #undef isdigit |
| 17 | #undef isalpha |
| 18 | #undef isalnum |
| 19 | #undef isprint |
| 20 | #undef islower |
| 21 | #undef isupper |
| 22 | #undef tolower |
| 23 | #undef toupper |
| 24 | #undef iscntrl |
| 25 | #undef ispunct |
| 26 | #undef isxdigit |
| 27 | |
| 28 | extern const unsigned char sane_ctype[256]; |
| 29 | extern const signed char hexval_table[256]; |
| 30 | #define GIT_SPACE 0x01 |
| 31 | #define GIT_DIGIT 0x02 |
| 32 | #define GIT_ALPHA 0x04 |
| 33 | #define GIT_GLOB_SPECIAL 0x08 |
| 34 | #define GIT_REGEX_SPECIAL 0x10 |
| 35 | #define GIT_PATHSPEC_MAGIC 0x20 |
| 36 | #define GIT_CNTRL 0x40 |
| 37 | #define GIT_PUNCT 0x80 |
| 38 | #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0) |
| 39 | #define isascii(x) (((x) & ~0x7f) == 0) |
| 40 | #define isspace(x) sane_istest(x,GIT_SPACE) |
| 41 | #define isdigit(x) sane_istest(x,GIT_DIGIT) |
| 42 | #define isalpha(x) sane_istest(x,GIT_ALPHA) |
| 43 | #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT) |
| 44 | #define isprint(x) ((x) >= 0x20 && (x) <= 0x7e) |
| 45 | #define islower(x) sane_iscase(x, 1) |
| 46 | #define isupper(x) sane_iscase(x, 0) |
| 47 | #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL) |
| 48 | #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL) |
| 49 | #define iscntrl(x) (sane_istest(x,GIT_CNTRL)) |
| 50 | #define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \ |
| 51 | GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC) |
| 52 | #define isxdigit(x) (hexval_table[(unsigned char)(x)] != -1) |
| 53 | #define tolower(x) sane_case((unsigned char)(x), 0x20) |
| 54 | #define toupper(x) sane_case((unsigned char)(x), 0) |
| 55 | #define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC) |
| 56 | |
| 57 | static inline int sane_case(int x, int high) |
| 58 | { |
| 59 | if (sane_istest(x, GIT_ALPHA)) |
| 60 | x = (x & ~0x20) | high; |
| 61 | return x; |
| 62 | } |
| 63 | |
| 64 | static inline int sane_iscase(int x, int is_lower) |
| 65 | { |
| 66 | if (!sane_istest(x, GIT_ALPHA)) |
| 67 | return 0; |
| 68 | |
| 69 | if (is_lower) |
| 70 | return (x & 0x20) != 0; |
| 71 | else |
| 72 | return (x & 0x20) == 0; |
| 73 | } |
| 74 | |
| 75 | #endif |