updated Scintilla to 2.29
[TortoiseGit.git] / ext / scintilla / lexlib / CharacterSet.h
blob9ce6c102a8a04504e1e0682aee5c013682c057cb
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 #ifdef SCI_NAMESPACE
12 namespace Scintilla {
13 #endif
15 class CharacterSet {
16 int size;
17 bool valueAfter;
18 bool *bset;
19 public:
20 enum setBase {
21 setNone=0,
22 setLower=1,
23 setUpper=2,
24 setDigits=4,
25 setAlpha=setLower|setUpper,
26 setAlphaNum=setAlpha|setDigits
28 CharacterSet(setBase base=setNone, const char *initialSet="", int size_=0x80, bool valueAfter_=false) {
29 size = size_;
30 valueAfter = valueAfter_;
31 bset = new bool[size];
32 for (int i=0; i < size; i++) {
33 bset[i] = false;
35 AddString(initialSet);
36 if (base & setLower)
37 AddString("abcdefghijklmnopqrstuvwxyz");
38 if (base & setUpper)
39 AddString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
40 if (base & setDigits)
41 AddString("0123456789");
43 ~CharacterSet() {
44 delete []bset;
45 bset = 0;
46 size = 0;
48 void Add(int val) {
49 assert(val >= 0);
50 assert(val < size);
51 bset[val] = true;
53 void AddString(const char *setToAdd) {
54 for (const char *cp=setToAdd; *cp; cp++) {
55 int val = static_cast<unsigned char>(*cp);
56 assert(val >= 0);
57 assert(val < size);
58 bset[val] = true;
61 bool Contains(int val) const {
62 assert(val >= 0);
63 if (val < 0) return false;
64 return (val < size) ? bset[val] : valueAfter;
68 // Functions for classifying characters
70 inline bool IsASpace(int ch) {
71 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
74 inline bool IsASpaceOrTab(int ch) {
75 return (ch == ' ') || (ch == '\t');
78 inline bool IsADigit(int ch) {
79 return (ch >= '0') && (ch <= '9');
82 inline bool IsADigit(int ch, int base) {
83 if (base <= 10) {
84 return (ch >= '0') && (ch < '0' + base);
85 } else {
86 return ((ch >= '0') && (ch <= '9')) ||
87 ((ch >= 'A') && (ch < 'A' + base - 10)) ||
88 ((ch >= 'a') && (ch < 'a' + base - 10));
92 inline bool IsASCII(int ch) {
93 return ch < 0x80;
96 inline bool IsAlphaNumeric(int ch) {
97 return
98 ((ch >= '0') && (ch <= '9')) ||
99 ((ch >= 'a') && (ch <= 'z')) ||
100 ((ch >= 'A') && (ch <= 'Z'));
104 * Check if a character is a space.
105 * This is ASCII specific but is safe with chars >= 0x80.
107 inline bool isspacechar(int ch) {
108 return (ch == ' ') || ((ch >= 0x09) && (ch <= 0x0d));
111 inline bool iswordchar(int ch) {
112 return IsASCII(ch) && (IsAlphaNumeric(ch) || ch == '.' || ch == '_');
115 inline bool iswordstart(int ch) {
116 return IsASCII(ch) && (IsAlphaNumeric(ch) || ch == '_');
119 inline bool isoperator(int ch) {
120 if (IsASCII(ch) && IsAlphaNumeric(ch))
121 return false;
122 if (ch == '%' || ch == '^' || ch == '&' || ch == '*' ||
123 ch == '(' || ch == ')' || ch == '-' || ch == '+' ||
124 ch == '=' || ch == '|' || ch == '{' || ch == '}' ||
125 ch == '[' || ch == ']' || ch == ':' || ch == ';' ||
126 ch == '<' || ch == '>' || ch == ',' || ch == '/' ||
127 ch == '?' || ch == '!' || ch == '.' || ch == '~')
128 return true;
129 return false;
132 // Simple case functions for ASCII.
134 inline char MakeUpperCase(char ch) {
135 if (ch < 'a' || ch > 'z')
136 return ch;
137 else
138 return static_cast<char>(ch - 'a' + 'A');
141 int CompareCaseInsensitive(const char *a, const char *b);
142 int CompareNCaseInsensitive(const char *a, const char *b, size_t len);
144 #ifdef SCI_NAMESPACE
146 #endif
148 #endif