clang  14.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 
17 namespace clang {
18 namespace 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_RAWDEL = 0x0200, // {}[]#<>%:;?*+-/^&|~!=,"'
32  CHAR_PUNCT = 0x0400 // `$@()
33  };
34 
35  enum {
38  };
39 } // end namespace charinfo
40 
41 /// Returns true if this is an ASCII character.
42 LLVM_READNONE inline bool isASCII(char c) {
43  return static_cast<unsigned char>(c) <= 127;
44 }
45 
46 LLVM_READNONE inline bool isASCII(unsigned char c) { return c <= 127; }
47 
48 /// Returns true if this is an ASCII character.
49 LLVM_READNONE inline bool isASCII(uint32_t c) { return c <= 127; }
50 
51 /// Returns true if this is a valid first character of a C identifier,
52 /// which is [a-zA-Z_].
53 LLVM_READONLY inline bool isAsciiIdentifierStart(unsigned char c,
54  bool AllowDollar = false) {
55  using namespace charinfo;
57  return true;
58  return AllowDollar && c == '$';
59 }
60 
61 /// Returns true if this is a body character of a C identifier,
62 /// which is [a-zA-Z0-9_].
63 LLVM_READONLY inline bool isAsciiIdentifierContinue(unsigned char c,
64  bool AllowDollar = false) {
65  using namespace charinfo;
67  return true;
68  return AllowDollar && c == '$';
69 }
70 
71 /// Returns true if this character is horizontal ASCII whitespace:
72 /// ' ', '\\t', '\\f', '\\v'.
73 ///
74 /// Note that this returns false for '\\0'.
75 LLVM_READONLY inline bool isHorizontalWhitespace(unsigned char c) {
76  using namespace charinfo;
77  return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_SPACE)) != 0;
78 }
79 
80 /// Returns true if this character is vertical ASCII whitespace: '\\n', '\\r'.
81 ///
82 /// Note that this returns false for '\\0'.
83 LLVM_READONLY inline bool isVerticalWhitespace(unsigned char c) {
84  using namespace charinfo;
85  return (InfoTable[c] & CHAR_VERT_WS) != 0;
86 }
87 
88 /// Return true if this character is horizontal or vertical ASCII whitespace:
89 /// ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.
90 ///
91 /// Note that this returns false for '\\0'.
92 LLVM_READONLY inline bool isWhitespace(unsigned char c) {
93  using namespace charinfo;
94  return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_VERT_WS|CHAR_SPACE)) != 0;
95 }
96 
97 /// Return true if this character is an ASCII digit: [0-9]
98 LLVM_READONLY inline bool isDigit(unsigned char c) {
99  using namespace charinfo;
100  return (InfoTable[c] & CHAR_DIGIT) != 0;
101 }
102 
103 /// Return true if this character is a lowercase ASCII letter: [a-z]
104 LLVM_READONLY inline bool isLowercase(unsigned char c) {
105  using namespace charinfo;
106  return (InfoTable[c] & CHAR_LOWER) != 0;
107 }
108 
109 /// Return true if this character is an uppercase ASCII letter: [A-Z]
110 LLVM_READONLY inline bool isUppercase(unsigned char c) {
111  using namespace charinfo;
112  return (InfoTable[c] & CHAR_UPPER) != 0;
113 }
114 
115 /// Return true if this character is an ASCII letter: [a-zA-Z]
116 LLVM_READONLY inline bool isLetter(unsigned char c) {
117  using namespace charinfo;
118  return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER)) != 0;
119 }
120 
121 /// Return true if this character is an ASCII letter or digit: [a-zA-Z0-9]
122 LLVM_READONLY inline bool isAlphanumeric(unsigned char c) {
123  using namespace charinfo;
124  return (InfoTable[c] & (CHAR_DIGIT|CHAR_UPPER|CHAR_LOWER)) != 0;
125 }
126 
127 /// Return true if this character is an ASCII hex digit: [0-9a-fA-F]
128 LLVM_READONLY inline bool isHexDigit(unsigned char c) {
129  using namespace charinfo;
130  return (InfoTable[c] & (CHAR_DIGIT|CHAR_XLETTER)) != 0;
131 }
132 
133 /// Return true if this character is an ASCII punctuation character.
134 ///
135 /// Note that '_' is both a punctuation character and an identifier character!
136 LLVM_READONLY inline bool isPunctuation(unsigned char c) {
137  using namespace charinfo;
139 }
140 
141 /// Return true if this character is an ASCII printable character; that is, a
142 /// character that should take exactly one column to print in a fixed-width
143 /// terminal.
144 LLVM_READONLY inline bool isPrintable(unsigned char c) {
145  using namespace charinfo;
148 }
149 
150 /// Return true if this is the body character of a C preprocessing number,
151 /// which is [a-zA-Z0-9_.].
152 LLVM_READONLY inline bool isPreprocessingNumberBody(unsigned char c) {
153  using namespace charinfo;
154  return (InfoTable[c] &
156 }
157 
158 /// Return true if this is the body character of a C++ raw string delimiter.
159 LLVM_READONLY inline bool isRawStringDelimBody(unsigned char c) {
160  using namespace charinfo;
163 }
164 
165 
166 /// Converts the given ASCII character to its lowercase equivalent.
167 ///
168 /// If the character is not an uppercase character, it is returned as is.
169 LLVM_READONLY inline char toLowercase(char c) {
170  if (isUppercase(c))
171  return c + 'a' - 'A';
172  return c;
173 }
174 
175 /// Converts the given ASCII character to its uppercase equivalent.
176 ///
177 /// If the character is not a lowercase character, it is returned as is.
178 LLVM_READONLY inline char toUppercase(char c) {
179  if (isLowercase(c))
180  return c + 'A' - 'a';
181  return c;
182 }
183 
184 
185 /// Return true if this is a valid ASCII identifier.
186 ///
187 /// Note that this is a very simple check; it does not accept UCNs as valid
188 /// identifier characters.
189 LLVM_READONLY inline bool isValidAsciiIdentifier(StringRef S,
190  bool AllowDollar = false) {
191  if (S.empty() || !isAsciiIdentifierStart(S[0], AllowDollar))
192  return false;
193 
194  for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I)
195  if (!isAsciiIdentifierContinue(*I, AllowDollar))
196  return false;
197 
198  return true;
199 }
200 
201 } // end namespace clang
202 
203 #endif
clang::charinfo::InfoTable
const uint16_t InfoTable[256]
Definition: CharInfo.cpp:15
clang::charinfo::CHAR_DIGIT
@ CHAR_DIGIT
Definition: CharInfo.h:25
clang::isPrintable
LLVM_READONLY bool isPrintable(unsigned char c)
Return true if this character is an ASCII printable character; that is, a character that should take ...
Definition: CharInfo.h:144
clang::charinfo::CHAR_PUNCT
@ CHAR_PUNCT
Definition: CharInfo.h:32
clang::charinfo::CHAR_SPACE
@ CHAR_SPACE
Definition: CharInfo.h:24
clang::charinfo::CHAR_RAWDEL
@ CHAR_RAWDEL
Definition: CharInfo.h:31
clang::isLowercase
LLVM_READONLY bool isLowercase(unsigned char c)
Return true if this character is a lowercase ASCII letter: [a-z].
Definition: CharInfo.h:104
clang::isPunctuation
LLVM_READONLY bool isPunctuation(unsigned char c)
Return true if this character is an ASCII punctuation character.
Definition: CharInfo.h:136
clang::toLowercase
LLVM_READONLY char toLowercase(char c)
Converts the given ASCII character to its lowercase equivalent.
Definition: CharInfo.h:169
clang::isPreprocessingNumberBody
LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
Return true if this is the body character of a C preprocessing number, which is [a-zA-Z0-9_.
Definition: CharInfo.h:152
clang::isValidAsciiIdentifier
LLVM_READONLY bool isValidAsciiIdentifier(StringRef S, bool AllowDollar=false)
Return true if this is a valid ASCII identifier.
Definition: CharInfo.h:189
clang::isLetter
LLVM_READONLY bool isLetter(unsigned char c)
Return true if this character is an ASCII letter: [a-zA-Z].
Definition: CharInfo.h:116
clang::isRawStringDelimBody
LLVM_READONLY bool isRawStringDelimBody(unsigned char c)
Return true if this is the body character of a C++ raw string delimiter.
Definition: CharInfo.h:159
clang::charinfo::CHAR_VERT_WS
@ CHAR_VERT_WS
Definition: CharInfo.h:23
clang::charinfo::CHAR_XLETTER
@ CHAR_XLETTER
Definition: CharInfo.h:26
clang::charinfo::CHAR_LOWER
@ CHAR_LOWER
Definition: CharInfo.h:28
clang::isAsciiIdentifierContinue
LLVM_READONLY bool isAsciiIdentifierContinue(unsigned char c, bool AllowDollar=false)
Returns true if this is a body character of a C identifier, which is [a-zA-Z0-9_].
Definition: CharInfo.h:63
clang::charinfo::CHAR_UNDER
@ CHAR_UNDER
Definition: CharInfo.h:29
clang::toUppercase
LLVM_READONLY char toUppercase(char c)
Converts the given ASCII character to its uppercase equivalent.
Definition: CharInfo.h:178
clang::charinfo::CHAR_XLOWER
@ CHAR_XLOWER
Definition: CharInfo.h:37
clang::isHexDigit
LLVM_READONLY bool isHexDigit(unsigned char c)
Return true if this character is an ASCII hex digit: [0-9a-fA-F].
Definition: CharInfo.h:128
clang::isWhitespace
LLVM_READONLY bool isWhitespace(unsigned char c)
Return true if this character is horizontal or vertical ASCII whitespace: ' ', '\t',...
Definition: CharInfo.h:92
clang::charinfo::CHAR_XUPPER
@ CHAR_XUPPER
Definition: CharInfo.h:36
clang::isAlphanumeric
LLVM_READONLY bool isAlphanumeric(unsigned char c)
Return true if this character is an ASCII letter or digit: [a-zA-Z0-9].
Definition: CharInfo.h:122
LLVM.h
clang::charinfo::CHAR_PERIOD
@ CHAR_PERIOD
Definition: CharInfo.h:30
clang::charinfo::CHAR_HORZ_WS
@ CHAR_HORZ_WS
Definition: CharInfo.h:22
clang::isVerticalWhitespace
LLVM_READONLY bool isVerticalWhitespace(unsigned char c)
Returns true if this character is vertical ASCII whitespace: '\n', '\r'.
Definition: CharInfo.h:83
clang
Definition: CalledOnceCheck.h:17
clang::charinfo::CHAR_UPPER
@ CHAR_UPPER
Definition: CharInfo.h:27
clang::isDigit
LLVM_READONLY bool isDigit(unsigned char c)
Return true if this character is an ASCII digit: [0-9].
Definition: CharInfo.h:98
c
__device__ __2f16 float c
Definition: __clang_hip_libdevice_declares.h:315
clang::isHorizontalWhitespace
LLVM_READONLY bool isHorizontalWhitespace(unsigned char c)
Returns true if this character is horizontal ASCII whitespace: ' ', '\t', '\f', '\v'.
Definition: CharInfo.h:75
clang::isUppercase
LLVM_READONLY bool isUppercase(unsigned char c)
Return true if this character is an uppercase ASCII letter: [A-Z].
Definition: CharInfo.h:110
clang::isAsciiIdentifierStart
LLVM_READONLY bool isAsciiIdentifierStart(unsigned char c, bool AllowDollar=false)
Returns true if this is a valid first character of a C identifier, which is [a-zA-Z_].
Definition: CharInfo.h:53
clang::isASCII
LLVM_READNONE bool isASCII(char c)
Returns true if this is an ASCII character.
Definition: CharInfo.h:42