1 | //===-- Collection of utils for implementing ctype functions-------*-C++-*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #ifndef LLVM_LIBC_SRC___SUPPORT_CTYPE_UTILS_H |
10 | #define LLVM_LIBC_SRC___SUPPORT_CTYPE_UTILS_H |
11 | |
12 | #include "src/__support/macros/attributes.h" |
13 | |
14 | namespace LIBC_NAMESPACE { |
15 | namespace internal { |
16 | |
17 | // ------------------------------------------------------ |
18 | // Rationale: Since these classification functions are |
19 | // called in other functions, we will avoid the overhead |
20 | // of a function call by inlining them. |
21 | // ------------------------------------------------------ |
22 | |
23 | LIBC_INLINE static constexpr bool isalpha(unsigned ch) { |
24 | return (ch | 32) - 'a' < 26; |
25 | } |
26 | |
27 | LIBC_INLINE static constexpr bool isdigit(unsigned ch) { |
28 | return (ch - '0') < 10; |
29 | } |
30 | |
31 | LIBC_INLINE static constexpr bool isalnum(unsigned ch) { |
32 | return isalpha(ch) || isdigit(ch); |
33 | } |
34 | |
35 | LIBC_INLINE static constexpr bool isgraph(unsigned ch) { |
36 | return 0x20 < ch && ch < 0x7f; |
37 | } |
38 | |
39 | LIBC_INLINE static constexpr bool islower(unsigned ch) { |
40 | return (ch - 'a') < 26; |
41 | } |
42 | |
43 | LIBC_INLINE static constexpr bool isupper(unsigned ch) { |
44 | return (ch - 'A') < 26; |
45 | } |
46 | |
47 | LIBC_INLINE static constexpr bool isspace(unsigned ch) { |
48 | return ch == ' ' || (ch - '\t') < 5; |
49 | } |
50 | |
51 | LIBC_INLINE static constexpr int tolower(int ch) { |
52 | if (isupper(ch)) |
53 | return ch + ('a' - 'A'); |
54 | return ch; |
55 | } |
56 | |
57 | } // namespace internal |
58 | } // namespace LIBC_NAMESPACE |
59 | |
60 | #endif // LLVM_LIBC_SRC___SUPPORT_CTYPE_UTILS_H |
61 | |