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.
5 // Copyright 2007 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
23 setAlpha
=setLower
|setUpper
,
24 setAlphaNum
=setAlpha
|setDigits
26 CharacterSet(setBase base
=setNone
, const char *initialSet
="", int size_
=0x80, bool valueAfter_
=false) {
28 valueAfter
= valueAfter_
;
29 bset
= new bool[size
];
30 for (int i
=0; i
< size
; i
++) {
33 AddString(initialSet
);
35 AddString("abcdefghijklmnopqrstuvwxyz");
37 AddString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
39 AddString("0123456789");
41 CharacterSet(const CharacterSet
&other
) {
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
) {
53 valueAfter
= other
.valueAfter
;
65 CharacterSet
&operator=(const CharacterSet
&other
) {
67 bool *bsetNew
= new bool[other
.size
];
68 for (int i
=0; i
< other
.size
; i
++) {
69 bsetNew
[i
] = other
.bset
[i
];
73 valueAfter
= other
.valueAfter
;
83 void AddString(const char *setToAdd
) {
84 for (const char *cp
=setToAdd
; *cp
; cp
++) {
85 int val
= static_cast<unsigned char>(*cp
);
91 bool Contains(int val
) const {
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
) {
114 return (ch
>= '0') && (ch
< '0' + base
);
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
) {
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
))
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
== '~')
170 // Simple case functions for ASCII.
172 inline int MakeUpperCase(int ch
) {
173 if (ch
< 'a' || ch
> 'z')
176 return static_cast<char>(ch
- 'a' + 'A');
179 inline int MakeLowerCase(int ch
) {
180 if (ch
< 'A' || ch
> 'Z')
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
);