1 // Character classification functions similar to standard <ctype.h>.
2 // Some C++ implementations provide locale-sensitive implementations
3 // of some <ctype.h> functions. These ascii_* functions are
4 // hard-wired for ASCII. Hard-wired for ASCII is much faster.
5 //
6 // ascii_isalnum, ascii_isalpha, ascii_isascii, ascii_isblank,
7 // ascii_iscntrl, ascii_isdigit, ascii_isgraph, ascii_islower,
8 // ascii_isprint, ascii_ispunct, ascii_isspace, ascii_isupper,
9 // ascii_isxdigit
10 // Similar to the <ctype.h> functions with similar names.
11 // Input parameter is an unsigned char. Return value is a bool.
12 // If the input has a numerical value greater than 127
13 // then the output is "false".
14 //
15 // ascii_tolower, ascii_toupper
16 // Similar to the <ctype.h> functions with similar names.
17 // Input parameter is an unsigned char. Return value is a char.
18 // If the input is not an ascii {lower,upper}-case letter
19 // (including numerical values greater than 127)
20 // then the output is the same as the input.
21
22 #ifndef DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_ // NOLINT
23 #define DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_ // NOLINT
24
25 namespace dynamic_depth {
26
27 // Array of character information. This is an implementation detail.
28 // The individual bits do not have names because the array definition is
29 // already tightly coupled to these functions. Names would just make it
30 // harder to read and debug.
31
32 extern const unsigned char kAsciiPropertyBits[256];
33
34 // Public functions.
35
ascii_isalpha(unsigned char c)36 static inline bool ascii_isalpha(unsigned char c) {
37 return (kAsciiPropertyBits[c] & 0x01) != 0;
38 }
39
ascii_isalnum(unsigned char c)40 static inline bool ascii_isalnum(unsigned char c) {
41 return (kAsciiPropertyBits[c] & 0x04) != 0;
42 }
43
ascii_isspace(unsigned char c)44 static inline bool ascii_isspace(unsigned char c) {
45 return (kAsciiPropertyBits[c] & 0x08) != 0;
46 }
47
ascii_ispunct(unsigned char c)48 static inline bool ascii_ispunct(unsigned char c) {
49 return (kAsciiPropertyBits[c] & 0x10) != 0;
50 }
51
ascii_isblank(unsigned char c)52 static inline bool ascii_isblank(unsigned char c) {
53 return (kAsciiPropertyBits[c] & 0x20) != 0;
54 }
55
ascii_iscntrl(unsigned char c)56 static inline bool ascii_iscntrl(unsigned char c) {
57 return (kAsciiPropertyBits[c] & 0x40) != 0;
58 }
59
ascii_isxdigit(unsigned char c)60 static inline bool ascii_isxdigit(unsigned char c) {
61 return (kAsciiPropertyBits[c] & 0x80) != 0;
62 }
63
ascii_isdigit(unsigned char c)64 static inline bool ascii_isdigit(unsigned char c) {
65 return c >= '0' && c <= '9';
66 }
67
ascii_isprint(unsigned char c)68 static inline bool ascii_isprint(unsigned char c) { return c >= 32 && c < 127; }
69
ascii_isgraph(unsigned char c)70 static inline bool ascii_isgraph(unsigned char c) { return c > 32 && c < 127; }
71
ascii_isupper(unsigned char c)72 static inline bool ascii_isupper(unsigned char c) {
73 return c >= 'A' && c <= 'Z';
74 }
75
ascii_islower(unsigned char c)76 static inline bool ascii_islower(unsigned char c) {
77 return c >= 'a' && c <= 'z';
78 }
79
ascii_isascii(unsigned char c)80 static inline bool ascii_isascii(unsigned char c) { return c < 128; }
81
82 extern const char kAsciiToLower[256];
ascii_tolower(unsigned char c)83 static inline char ascii_tolower(unsigned char c) { return kAsciiToLower[c]; }
84 extern const char kAsciiToUpper[256];
ascii_toupper(unsigned char c)85 static inline char ascii_toupper(unsigned char c) { return kAsciiToUpper[c]; }
86
87 } // namespace dynamic_depth
88
89 #endif // DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_ // NOLINT
90