CharInfo.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- clang/Basic/CharInfo.h - Classifying ASCII Characters --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CLANG_BASIC_CHARINFO_H
  14. #define LLVM_CLANG_BASIC_CHARINFO_H
  15. #include "clang/Basic/LLVM.h"
  16. #include "llvm/ADT/StringRef.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/DataTypes.h"
  19. namespace clang {
  20. namespace charinfo {
  21. extern const uint16_t InfoTable[256];
  22. enum {
  23. CHAR_HORZ_WS = 0x0001, // '\t', '\f', '\v'. Note, no '\0'
  24. CHAR_VERT_WS = 0x0002, // '\r', '\n'
  25. CHAR_SPACE = 0x0004, // ' '
  26. CHAR_DIGIT = 0x0008, // 0-9
  27. CHAR_XLETTER = 0x0010, // a-f,A-F
  28. CHAR_UPPER = 0x0020, // A-Z
  29. CHAR_LOWER = 0x0040, // a-z
  30. CHAR_UNDER = 0x0080, // _
  31. CHAR_PERIOD = 0x0100, // .
  32. CHAR_RAWDEL = 0x0200, // {}[]#<>%:;?*+-/^&|~!=,"'
  33. CHAR_PUNCT = 0x0400 // `$@()
  34. };
  35. enum {
  36. CHAR_XUPPER = CHAR_XLETTER | CHAR_UPPER,
  37. CHAR_XLOWER = CHAR_XLETTER | CHAR_LOWER
  38. };
  39. } // end namespace charinfo
  40. /// Returns true if this is an ASCII character.
  41. LLVM_READNONE inline bool isASCII(char c) {
  42. return static_cast<unsigned char>(c) <= 127;
  43. }
  44. LLVM_READNONE inline bool isASCII(unsigned char c) { return c <= 127; }
  45. /// Returns true if this is an ASCII character.
  46. LLVM_READNONE inline bool isASCII(uint32_t c) { return c <= 127; }
  47. /// Returns true if this is a valid first character of a C identifier,
  48. /// which is [a-zA-Z_].
  49. LLVM_READONLY inline bool isAsciiIdentifierStart(unsigned char c,
  50. bool AllowDollar = false) {
  51. using namespace charinfo;
  52. if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_UNDER))
  53. return true;
  54. return AllowDollar && c == '$';
  55. }
  56. /// Returns true if this is a body character of a C identifier,
  57. /// which is [a-zA-Z0-9_].
  58. LLVM_READONLY inline bool isAsciiIdentifierContinue(unsigned char c,
  59. bool AllowDollar = false) {
  60. using namespace charinfo;
  61. if (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER))
  62. return true;
  63. return AllowDollar && c == '$';
  64. }
  65. /// Returns true if this character is horizontal ASCII whitespace:
  66. /// ' ', '\\t', '\\f', '\\v'.
  67. ///
  68. /// Note that this returns false for '\\0'.
  69. LLVM_READONLY inline bool isHorizontalWhitespace(unsigned char c) {
  70. using namespace charinfo;
  71. return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_SPACE)) != 0;
  72. }
  73. /// Returns true if this character is vertical ASCII whitespace: '\\n', '\\r'.
  74. ///
  75. /// Note that this returns false for '\\0'.
  76. LLVM_READONLY inline bool isVerticalWhitespace(unsigned char c) {
  77. using namespace charinfo;
  78. return (InfoTable[c] & CHAR_VERT_WS) != 0;
  79. }
  80. /// Return true if this character is horizontal or vertical ASCII whitespace:
  81. /// ' ', '\\t', '\\f', '\\v', '\\n', '\\r'.
  82. ///
  83. /// Note that this returns false for '\\0'.
  84. LLVM_READONLY inline bool isWhitespace(unsigned char c) {
  85. using namespace charinfo;
  86. return (InfoTable[c] & (CHAR_HORZ_WS|CHAR_VERT_WS|CHAR_SPACE)) != 0;
  87. }
  88. /// Return true if this character is an ASCII digit: [0-9]
  89. LLVM_READONLY inline bool isDigit(unsigned char c) {
  90. using namespace charinfo;
  91. return (InfoTable[c] & CHAR_DIGIT) != 0;
  92. }
  93. /// Return true if this character is a lowercase ASCII letter: [a-z]
  94. LLVM_READONLY inline bool isLowercase(unsigned char c) {
  95. using namespace charinfo;
  96. return (InfoTable[c] & CHAR_LOWER) != 0;
  97. }
  98. /// Return true if this character is an uppercase ASCII letter: [A-Z]
  99. LLVM_READONLY inline bool isUppercase(unsigned char c) {
  100. using namespace charinfo;
  101. return (InfoTable[c] & CHAR_UPPER) != 0;
  102. }
  103. /// Return true if this character is an ASCII letter: [a-zA-Z]
  104. LLVM_READONLY inline bool isLetter(unsigned char c) {
  105. using namespace charinfo;
  106. return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER)) != 0;
  107. }
  108. /// Return true if this character is an ASCII letter or digit: [a-zA-Z0-9]
  109. LLVM_READONLY inline bool isAlphanumeric(unsigned char c) {
  110. using namespace charinfo;
  111. return (InfoTable[c] & (CHAR_DIGIT|CHAR_UPPER|CHAR_LOWER)) != 0;
  112. }
  113. /// Return true if this character is an ASCII hex digit: [0-9a-fA-F]
  114. LLVM_READONLY inline bool isHexDigit(unsigned char c) {
  115. using namespace charinfo;
  116. return (InfoTable[c] & (CHAR_DIGIT|CHAR_XLETTER)) != 0;
  117. }
  118. /// Return true if this character is an ASCII punctuation character.
  119. ///
  120. /// Note that '_' is both a punctuation character and an identifier character!
  121. LLVM_READONLY inline bool isPunctuation(unsigned char c) {
  122. using namespace charinfo;
  123. return (InfoTable[c] & (CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL|CHAR_PUNCT)) != 0;
  124. }
  125. /// Return true if this character is an ASCII printable character; that is, a
  126. /// character that should take exactly one column to print in a fixed-width
  127. /// terminal.
  128. LLVM_READONLY inline bool isPrintable(unsigned char c) {
  129. using namespace charinfo;
  130. return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|CHAR_PUNCT|
  131. CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL|CHAR_SPACE)) != 0;
  132. }
  133. /// Return true if this is the body character of a C preprocessing number,
  134. /// which is [a-zA-Z0-9_.].
  135. LLVM_READONLY inline bool isPreprocessingNumberBody(unsigned char c) {
  136. using namespace charinfo;
  137. return (InfoTable[c] &
  138. (CHAR_UPPER|CHAR_LOWER|CHAR_DIGIT|CHAR_UNDER|CHAR_PERIOD)) != 0;
  139. }
  140. /// Return true if this is the body character of a C++ raw string delimiter.
  141. LLVM_READONLY inline bool isRawStringDelimBody(unsigned char c) {
  142. using namespace charinfo;
  143. return (InfoTable[c] & (CHAR_UPPER|CHAR_LOWER|CHAR_PERIOD|
  144. CHAR_DIGIT|CHAR_UNDER|CHAR_RAWDEL)) != 0;
  145. }
  146. /// Converts the given ASCII character to its lowercase equivalent.
  147. ///
  148. /// If the character is not an uppercase character, it is returned as is.
  149. LLVM_READONLY inline char toLowercase(char c) {
  150. if (isUppercase(c))
  151. return c + 'a' - 'A';
  152. return c;
  153. }
  154. /// Converts the given ASCII character to its uppercase equivalent.
  155. ///
  156. /// If the character is not a lowercase character, it is returned as is.
  157. LLVM_READONLY inline char toUppercase(char c) {
  158. if (isLowercase(c))
  159. return c + 'A' - 'a';
  160. return c;
  161. }
  162. /// Return true if this is a valid ASCII identifier.
  163. ///
  164. /// Note that this is a very simple check; it does not accept UCNs as valid
  165. /// identifier characters.
  166. LLVM_READONLY inline bool isValidAsciiIdentifier(StringRef S,
  167. bool AllowDollar = false) {
  168. if (S.empty() || !isAsciiIdentifierStart(S[0], AllowDollar))
  169. return false;
  170. for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I)
  171. if (!isAsciiIdentifierContinue(*I, AllowDollar))
  172. return false;
  173. return true;
  174. }
  175. } // end namespace clang
  176. #endif
  177. #ifdef __GNUC__
  178. #pragma GCC diagnostic pop
  179. #endif