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 namespace __llvm_libc {
13 namespace internal {
14 
15 // ------------------------------------------------------
16 // Rationale: Since these classification functions are
17 // called in other functions, we will avoid the overhead
18 // of a function call by inlining them.
19 // ------------------------------------------------------
20 
isalpha(unsigned ch)21 static constexpr bool isalpha(unsigned ch) { return (ch | 32) - 'a' < 26; }
22 
isdigit(unsigned ch)23 static constexpr bool isdigit(unsigned ch) { return (ch - '0') < 10; }
24 
isalnum(unsigned ch)25 static constexpr bool isalnum(unsigned ch) {
26   return isalpha(ch) || isdigit(ch);
27 }
28 
isgraph(unsigned ch)29 static constexpr bool isgraph(unsigned ch) { return 0x20 < ch && ch < 0x7f; }
30 
islower(unsigned ch)31 static constexpr bool islower(unsigned ch) { return (ch - 'a') < 26; }
32 
isupper(unsigned ch)33 static constexpr bool isupper(unsigned ch) { return (ch - 'A') < 26; }
34 
isspace(unsigned ch)35 static constexpr bool isspace(unsigned ch) {
36   return ch == ' ' || (ch - '\t') < 5;
37 }
38 
39 } // namespace internal
40 } // namespace __llvm_libc
41 
42 #endif //  LLVM_LIBC_SRC_SUPPORT_CTYPE_UTILS_H
43