1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2 /*
3 * Copyright (C) 2015-2020 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 *
5 * Specialized constant-time ctype.h reimplementations that aren't locale-specific.
6 */
7
8 #ifndef CTYPE_H
9 #define CTYPE_H
10
11 #include <stdbool.h>
12
char_is_space(int c)13 static inline bool char_is_space(int c)
14 {
15 unsigned char d = c - 9;
16 return (0x80001FU >> (d & 31)) & (1U >> (d >> 5));
17 }
18
char_is_digit(int c)19 static inline bool char_is_digit(int c)
20 {
21 return (unsigned int)(('0' - 1 - c) & (c - ('9' + 1))) >> (sizeof(c) * 8 - 1);
22 }
23
char_is_alpha(int c)24 static inline bool char_is_alpha(int c)
25 {
26 return (unsigned int)(('a' - 1 - (c | 32)) & ((c | 32) - ('z' + 1))) >> (sizeof(c) * 8 - 1);
27 }
28
29 #endif
30