clang 20.0.0git
CharInfo.h
Go to the documentation of this file.
1//===--- clang/Basic/CharInfo.h - Classifying ASCII Characters --*- 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_CLANG_BASIC_CHARINFO_H
10#define LLVM_CLANG_BASIC_CHARINFO_H
11
12#include "clang/Basic/LLVM.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/Compiler.h"
15#include "llvm/Support/DataTypes.h"
16
17namespace clang {
18namespace charinfo {
19 extern const uint16_t InfoTable[256];
20
21 enum {
22 CHAR_HORZ_WS = 0x0001, // '\t', '\f', '\v'. Note, no '\0'
23 CHAR_VERT_WS = 0x0002, // '\r', '\n'
24 CHAR_SPACE = 0x0004, // ' '
25 CHAR_DIGIT = 0x0008, // 0-9
26 CHAR_XLETTER = 0x0010, // a-f,A-F
27 CHAR_UPPER = 0x0020, // A-Z
28 CHAR_LOWER = 0x0040, // a-z
29 CHAR_UNDER = 0x0080, // _
30 CHAR_PERIOD = 0x0100, // .
31 CHAR_PUNCT = 0x0200, // {}[]#<>%:;?*+-/^&|~!=,"'`$@()
32 };
33
34 enum {
37 };
38} // end namespace charinfo
39
40/// Returns true if a byte is an ASCII character.
41LLVM_READNONE inline bool isASCII(char c) {
42 return static_cast<unsigned char>(c) <= 127;
43}
44
45LLVM_READNONE inline bool isASCII(unsigned char c) { return c <= 127; }
46
47/// Returns true if a codepoint is an ASCII character.
48LLVM_READNONE inline bool isASCII(uint32_t c) { return c <= 127; }
49LLVM_READNONE inline bool isASCII(int64_t c) { return 0 <= c && c <= 127; }
50
51/// Returns true if this is a valid first character of a C identifier,
52/// which is [a-zA-Z_].
53LLVM_READONLY inline bool isAsciiIdentifierStart(unsigned char c,
54 bool AllowDollar = false) {
55 using namespace charinfo;
56 if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_UNDER))
57 return true;
58 return AllowDollar && c == '$';
59}
60
61LLVM_READONLY inline bool isAsciiIdentifierContinue(unsigned char c) {
62 // Precomputed CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER
63 static constexpr unsigned char IDContinue[256] = {
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
67 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
68 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
69 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
72 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
73 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
74 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
75 return IDContinue[c];
76}
77
78/// Returns true if this is a body character of a C identifier,
79/// which is [a-zA-Z0-9_].
80LLVM_READONLY inline bool isAsciiIdentifierContinue(unsigned char c,
81 bool AllowDollar) {
83 return true;
84 return AllowDollar && c == '$';
85}
86
87/// Returns true if this character is horizontal ASCII whitespace:
88/// ' ', '\\t', '\\f', '\\v'.
89///
90/// Note that this returns false for '\\0'.
91LLVM_READONLY inline bool isHorizontalWhitespace(unsigned char c) {
92 using namespace charinfo;
93 return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_SPACE)) != 0;
94}
95
96/// Returns true if this character is vertical ASCII whitespace: '\\n', '\\r'.
97///
98/// Note that this returns false for '\\0'.
99LLVM_READONLY inline bool isVerticalWhitespace(unsigned char c) {
100 using namespace charinfo;
101 return (InfoTable[c] & CHAR_VERT_WS) != 0;
102}
103
104/// Return true if this character is horizontal or vertical ASCII whitespace:
105/// ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.
106///
107/// Note that this returns false for '\\0'.
108LLVM_READONLY inline bool isWhitespace(unsigned char c) {
109 using namespace charinfo;
110 return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_VERT_WS|CHAR_SPACE)) != 0;
111}
112
113/// Return true if this character is an ASCII digit: [0-9]
114LLVM_READONLY inline bool isDigit(unsigned char c) {
115 using namespace charinfo;
116 return (InfoTable[c] & CHAR_DIGIT) != 0;
117}
118
119/// Return true if this character is a lowercase ASCII letter: [a-z]
120LLVM_READONLY inline bool isLowercase(unsigned char c) {
121 using namespace charinfo;
122 return (InfoTable[c] & CHAR_LOWER) != 0;
123}
124
125/// Return true if this character is an uppercase ASCII letter: [A-Z]
126LLVM_READONLY inline bool isUppercase(unsigned char c) {
127 using namespace charinfo;
128 return (InfoTable[c] & CHAR_UPPER) != 0;
129}
130
131/// Return true if this character is an ASCII letter: [a-zA-Z]
132LLVM_READONLY inline bool isLetter(unsigned char c) {
133 using namespace charinfo;
134 return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER)) != 0;
135}
136
137/// Return true if this character is an ASCII letter or digit: [a-zA-Z0-9]
138LLVM_READONLY inline bool isAlphanumeric(unsigned char c) {
139 using namespace charinfo;
140 return (InfoTable[c] & (CHAR_DIGIT|CHAR_UPPER|CHAR_LOWER)) != 0;
141}
142
143/// Return true if this character is an ASCII hex digit: [0-9a-fA-F]
144LLVM_READONLY inline bool isHexDigit(unsigned char c) {
145 using namespace charinfo;
146 return (InfoTable[c] & (CHAR_DIGIT|CHAR_XLETTER)) != 0;
147}
148
149/// Return true if this character is an ASCII punctuation character.
150///
151/// Note that '_' is both a punctuation character and an identifier character!
152LLVM_READONLY inline bool isPunctuation(unsigned char c) {
153 using namespace charinfo;
154 return (InfoTable[c] & (CHAR_UNDER | CHAR_PERIOD | CHAR_PUNCT)) != 0;
155}
156
157/// Return true if this character is an ASCII printable character; that is, a
158/// character that should take exactly one column to print in a fixed-width
159/// terminal.
160LLVM_READONLY inline bool isPrintable(unsigned char c) {
161 using namespace charinfo;
162 return (InfoTable[c] & (CHAR_UPPER | CHAR_LOWER | CHAR_PERIOD | CHAR_PUNCT |
163 CHAR_DIGIT | CHAR_UNDER | CHAR_SPACE)) != 0;
164}
165
166/// Return true if this is the body character of a C preprocessing number,
167/// which is [a-zA-Z0-9_.].
168LLVM_READONLY inline bool isPreprocessingNumberBody(unsigned char c) {
169 using namespace charinfo;
170 return (InfoTable[c] &
171 (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER|CHAR_PERIOD)) != 0;
172}
173
174/// Return true if this is the body character of a C++ raw string delimiter.
175LLVM_READONLY inline bool isRawStringDelimBody(unsigned char c) {
176 using namespace charinfo;
177 return (InfoTable[c] & (CHAR_UPPER | CHAR_LOWER | CHAR_PERIOD | CHAR_DIGIT |
178 CHAR_UNDER | CHAR_PUNCT)) != 0 &&
179 c != '(' && c != ')' && c != '\\';
180}
181
182enum class EscapeChar {
183 Single = 1,
184 Double = 2,
185