1 | // Scintilla source code edit control |
2 | /** @file CharacterType.h |
3 | ** Tests for character type and case-insensitive comparisons. |
4 | **/ |
5 | // Copyright 2007 by Neil Hodgson <neilh@scintilla.org> |
6 | // The License.txt file describes the conditions under which this software may be distributed. |
7 | |
8 | #ifndef CHARACTERTYPE_H |
9 | #define CHARACTERTYPE_H |
10 | |
11 | namespace Scintilla::Internal { |
12 | |
13 | // Functions for classifying characters |
14 | |
15 | /** |
16 | * Check if a character is a space. |
17 | * This is ASCII specific but is safe with chars >= 0x80. |
18 | */ |
19 | constexpr bool IsASpace(int ch) noexcept { |
20 | return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d)); |
21 | } |
22 | |
23 | constexpr bool IsSpaceOrTab(int ch) noexcept { |
24 | return (ch == ' ') || (ch == '\t'); |
25 | } |
26 | |
27 | constexpr bool IsControl(int ch) noexcept { |
28 | return ((ch >= 0) && (ch <= 0x1F)) || (ch == 0x7F); |
29 | } |
30 | |
31 | constexpr bool IsEOLCharacter(int ch) noexcept { |
32 | return ch == '\r' || ch == '\n'; |
33 | } |
34 | |
35 | constexpr bool IsBreakSpace(int ch) noexcept { |
36 | // used for text breaking, treat C0 control character as space. |
37 | // by default C0 control character is handled as special representation, |
38 | // so not appears in normal text. 0x7F DEL is omitted to simplify the code. |
39 | return ch >= 0 && ch <= ' '; |
40 | } |
41 | |
42 | constexpr bool IsADigit(int ch) noexcept { |
43 | return (ch >= '0') && (ch <= '9'); |
44 | } |
45 | |
46 | constexpr bool IsADigit(int ch, int base) noexcept { |
47 | if (base <= 10) { |
48 | return (ch >= '0') && (ch < '0' + base); |
49 | } else { |
50 | return ((ch >= '0') && (ch <= '9')) || |
51 | ((ch >= 'A') && (ch < 'A' + base - 10)) || |
52 | ((ch >= 'a') && (ch < 'a' + base - 10)); |
53 | } |
54 | } |
55 | |
56 | constexpr bool IsASCII(int ch) noexcept { |
57 | return (ch >= 0) && (ch < 0x80); |
58 | } |
59 | |
60 | constexpr bool IsLowerCase(int ch) noexcept { |
61 | return (ch >= 'a') && (ch <= 'z'); |
62 | } |
63 | |
64 | constexpr bool IsUpperCase(int ch) noexcept { |
65 | return (ch >= 'A') && (ch <= 'Z'); |
66 | } |
67 | |
68 | constexpr bool IsUpperOrLowerCase(int ch) noexcept { |
69 | return IsUpperCase(ch) || IsLowerCase(ch); |
70 | } |
71 | |
72 | constexpr bool IsAlphaNumeric(int ch) noexcept { |
73 | return |
74 | ((ch >= '0') && (ch <= '9')) || |
75 | ((ch >= 'a') && (ch <= 'z')) || |
76 | ((ch >= 'A') && (ch <= 'Z')); |
77 | } |
78 | |
79 | constexpr bool IsPunctuation(int ch) noexcept { |
80 | switch (ch) { |
81 | case '!': |
82 | case '"': |
83 | case '#': |
84 | case '$': |
85 | case '%': |
86 | case '&': |
87 | case '\'': |
88 | case '(': |
89 | case ')': |
90 | case '*': |
91 | case '+': |
92 | case ',': |
93 | case '-': |
94 | case '.': |
95 | case '/': |
96 | case ':': |
97 | case ';': |
98 | case '<': |
99 | case '=': |
100 | case '>': |
101 | case '?': |
102 | case '@': |
103 | case '[': |
104 | case '\\': |
105 | case ']': |
106 | case '^': |
107 | case '_': |
108 | case '`': |
109 | case '{': |
110 | case '|': |
111 | case '}': |
112 | case '~': |
113 | return true; |
114 | default: |
115 | return false; |
116 | } |
117 | } |
118 | |
119 | // Simple case functions for ASCII supersets. |
120 | |
121 | template <typename T> |
122 | constexpr T MakeUpperCase(T ch) noexcept { |
123 | if (ch < 'a' || ch > 'z') |
124 | return ch; |
125 | else |
126 | return ch - 'a' + 'A'; |
127 | } |
128 | |
129 | template <typename T> |
130 | constexpr T MakeLowerCase(T ch) noexcept { |
131 | if (ch < 'A' || ch > 'Z') |
132 | return ch; |
133 | else |
134 | return ch - 'A' + 'a'; |
135 | } |
136 | |
137 | int CompareCaseInsensitive(const char *a, const char *b) noexcept; |
138 | int CompareNCaseInsensitive(const char *a, const char *b, size_t len) noexcept; |
139 | |
140 | } |
141 | |
142 | #endif |
143 | |