summaryrefslogtreecommitdiff
path: root/internal/strings/ascii_ctype.h
blob: acc1fe5b7f95fca0ef9bd7211be3d8ade780dc44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Character classification functions similar to standard <ctype.h>.
// Some C++ implementations provide locale-sensitive implementations
// of some <ctype.h> functions.  These ascii_* functions are
// hard-wired for ASCII.  Hard-wired for ASCII is much faster.
//
// ascii_isalnum, ascii_isalpha, ascii_isascii, ascii_isblank,
// ascii_iscntrl, ascii_isdigit, ascii_isgraph, ascii_islower,
// ascii_isprint, ascii_ispunct, ascii_isspace, ascii_isupper,
// ascii_isxdigit
//   Similar to the <ctype.h> functions with similar names.
//   Input parameter is an unsigned char.  Return value is a bool.
//   If the input has a numerical value greater than 127
//   then the output is "false".
//
// ascii_tolower, ascii_toupper
//   Similar to the <ctype.h> functions with similar names.
//   Input parameter is an unsigned char.  Return value is a char.
//   If the input is not an ascii {lower,upper}-case letter
//   (including numerical values greater than 127)
//   then the output is the same as the input.

#ifndef DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_  // NOLINT
#define DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_  // NOLINT

namespace dynamic_depth {

// Array of character information.  This is an implementation detail.
// The individual bits do not have names because the array definition is
// already tightly coupled to these functions.  Names would just make it
// harder to read and debug.

extern const unsigned char kAsciiPropertyBits[256];

// Public functions.

static inline bool ascii_isalpha(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x01) != 0;
}

static inline bool ascii_isalnum(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x04) != 0;
}

static inline bool ascii_isspace(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x08) != 0;
}

static inline bool ascii_ispunct(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x10) != 0;
}

static inline bool ascii_isblank(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x20) != 0;
}

static inline bool ascii_iscntrl(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x40) != 0;
}

static inline bool ascii_isxdigit(unsigned char c) {
  return (kAsciiPropertyBits[c] & 0x80) != 0;
}

static inline bool ascii_isdigit(unsigned char c) {
  return c >= '0' && c <= '9';
}

static inline bool ascii_isprint(unsigned char c) { return c >= 32 && c < 127; }

static inline bool ascii_isgraph(unsigned char c) { return c > 32 && c < 127; }

static inline bool ascii_isupper(unsigned char c) {
  return c >= 'A' && c <= 'Z';
}

static inline bool ascii_islower(unsigned char c) {
  return c >= 'a' && c <= 'z';
}

static inline bool ascii_isascii(unsigned char c) { return c < 128; }

extern const char kAsciiToLower[256];
static inline char ascii_tolower(unsigned char c) { return kAsciiToLower[c]; }
extern const char kAsciiToUpper[256];
static inline char ascii_toupper(unsigned char c) { return kAsciiToUpper[c]; }

}  // namespace dynamic_depth

#endif  // DYNAMIC_DEPTH_INTERNAL_STRINGS_ASCII_CTYPE_H_  // NOLINT