Scintilla 4.0.3
[TortoiseGit.git] / ext / scintilla / lexlib / CharacterSet.h
blob0841bf2715a824e4a6e25c36eb1a7d48e349423d
1 // Scintilla source code edit control
2 /** @file CharacterSet.h
3 ** Encapsulates a set of characters. Used to test if a character is within a set.
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.
8 #ifndef CHARACTERSET_H
9 #define CHARACTERSET_H
11 namespace Scintilla {
13 class CharacterSet {
14 int size;
15 bool valueAfter;
16 bool *bset;
17 public:
18 enum setBase {
19 setNone=0,
20 setLower=1,
21 setUpper=2,
22 setDigits=4,
23 setAlpha=setLower|setUpper,
24 setAlphaNum=setAlpha|setDigits
26 CharacterSet(setBase base=setNone, const char *initialSet="", int size_=0x80, bool valueAfter_=false) {
27 size = size_;
28 valueAfter = valueAfter_;
29 bset = new bool[size];
30 for (int i=0; i < size; i++) {
31 bset[i] = false;
33 AddString(initialSet);
34 if (base & setLower)
35 AddString("abcdefghijklmnopqrstuvwxyz");
36 if (base & setUpper)
37 AddString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
38 if (base & setDigits)
39 AddString("0123456789");
41 CharacterSet(const CharacterSet &other) {
42 size = other.size;
43 valueAfter = other.valueAfter;
44 bset = new bool[size];
45 for (int i=0; i < size; i++) {
46 bset[i] = other.bset[i];
49 CharacterSet &operator=(CharacterSet &&other) {
50 if (this != &other) {
51 delete []bset;
52 size = other.size;
53 valueAfter = other.valueAfter;
54 bset = other.bset;
55 other.size = 0;
56 other.bset = nullptr;
58 return *this;
60 ~CharacterSet() {
61 delete []bset;
62 bset = 0;
63 size = 0;
65 CharacterSet &operator=(const CharacterSet &other) {
66 if (this != &other) {
67 bool *bsetNew = new bool[other.size];
68 for (int i=0; i < other.size; i++) {
69 bsetNew[i] = other.bset[i];
71 delete []bset;
72 size = other.size;
73 valueAfter = other.valueAfter;
74 bset = bsetNew;
76 return *this;
78 void Add(int val) {
79 assert(val >= 0);
80 assert(val < size);
81 bset[val] = true;
83 void AddString(const char *setToAdd) {
84 for (const char *cp=setToAdd; *cp; cp++) {
85 int val = static_cast<unsigned char>(*cp);
86 assert(val >= 0);
87 assert(val < size);
88 bset[val] = true;
91 bool Contains(int val) const {
92 assert(val >= 0);
93 if (val < 0) return false;
94 return (val < size) ? bset[val] : valueAfter;
98 // Functions for classifying characters
100 inline bool IsASpace(int ch) {
101 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
104 inline bool IsASpaceOrTab(int ch) {
105 return (ch == ' ') || (ch == '\t');
108 inline bool IsADigit(int ch) {
109 return (ch >= '0') && (ch <= '9');
112 inline bool IsADigit(int ch, int base) {
113 if (base <= 10) {
114 return (ch >= '0') && (ch < '0' + base);
115 } else {
116 return ((ch >= '0') && (ch <= '9')) ||
117 ((ch >= 'A') && (ch < 'A' + base - 10)) ||
118 ((ch >= 'a') && (ch < 'a' + base - 10));
122 inline bool IsASCII(int ch) {
123 return (ch >= 0) && (ch < 0x80);
126 inline bool IsLowerCase(int ch) {
127 return (ch >= 'a') && (ch <= 'z');
130 inline bool IsUpperCase(int ch) {
131 return (ch >= 'A') && (ch <= 'Z');
134 inline bool IsAlphaNumeric(int ch) {
135 return
136 ((ch >= '0') && (ch <= '9')) ||
137 ((ch >= 'a') && (ch <= 'z')) ||
138 ((ch >= 'A') && (ch <= 'Z'));
142 * Check if a character is a space.
143 * This is ASCII specific but is safe with chars >= 0x80.
145 inline bool isspacechar(int ch) {
146 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
149 inline bool iswordchar(int ch) {
150 return IsAlphaNumeric(ch) || ch == '.' || ch == '_';
153 inline bool iswordstart(int ch) {
154 return IsAlphaNumeric(ch) || ch == '_';
157 inline bool isoperator(int ch) {
158 if (IsAlphaNumeric(ch))
159 return false;
160 if (ch == '%' || ch == '^' || ch == '&' || ch == '*' ||
161 ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
162 ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
163 ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
164 ch == '<' || ch == '>' || ch == ',' || ch == '/' ||
165 ch == '?' || ch == '!' || ch == '.' || ch == '~')
166 return true;
167 return false;
170 // Simple case functions for ASCII.
172 inline int MakeUpperCase(int ch) {
173 if (ch < 'a' || ch > 'z')
174 return ch;
175 else
176 return static_cast<char>(ch - 'a' + 'A');
179 inline int MakeLowerCase(int ch) {
180 if (ch < 'A' || ch > 'Z')
181 return ch;
182 else
183 return ch - 'A' + 'a';
186 int CompareCaseInsensitive(const char *a, const char *b);
187 int CompareNCaseInsensitive(const char *a, const char *b, size_t len);
191 #endif