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.
25 setAlpha
=setLower
|setUpper
,
26 setAlphaNum
=setAlpha
|setDigits
28 CharacterSet(setBase base
=setNone
, const char *initialSet
="", int size_
=0x80, bool valueAfter_
=false) {
30 valueAfter
= valueAfter_
;
31 bset
= new bool[size
];
32 for (int i
=0; i
< size
; i
++) {
35 AddString(initialSet
);
37 AddString("abcdefghijklmnopqrstuvwxyz");
39 AddString("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
41 AddString("0123456789");
53 void AddString(const char *setToAdd
) {
54 for (const char *cp
=setToAdd
; *cp
; cp
++) {
55 int val
= static_cast<unsigned char>(*cp
);
61 bool Contains(int val
) const {
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
) {
84 return (ch
>= '0') && (ch
< '0' + base
);
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
) {
96 inline bool IsAlphaNumeric(int ch
) {
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
))
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
== '~')
132 // Simple case functions for ASCII.
134 inline char MakeUpperCase(char ch
) {
135 if (ch
< 'a' || ch
> 'z')
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
);