1
// Scintilla source code edit control
3
** Colourise for particular languages.
5
// Copyright 1998-2002 by Neil Hodgson <neilh@scintilla.org>
6
// The License.txt file describes the conditions under which this software may be distributed.
17
using namespace Scintilla;
21
* Creates an array that points into each word in the string and puts \0 terminators
24
static char **ArrayFromWordList(char *wordlist, int *len, bool onlyLineEnds = false) {
27
// For rapid determination of whether a character is a separator, build
29
bool wordSeparator[256];
30
for (int i=0; i<256; i++) {
31
wordSeparator[i] = false;
33
wordSeparator['\r'] = true;
34
wordSeparator['\n'] = true;
36
wordSeparator[' '] = true;
37
wordSeparator['\t'] = true;
39
for (int j = 0; wordlist[j]; j++) {
40
int curr = static_cast<unsigned char>(wordlist[j]);
41
if (!wordSeparator[curr] && wordSeparator[prev])
45
char **keywords = new char *[words + 1];
49
size_t slen = strlen(wordlist);
50
for (size_t k = 0; k < slen; k++) {
51
if (!wordSeparator[static_cast<unsigned char>(wordlist[k])]) {
53
keywords[words] = &wordlist[k];
61
keywords[words] = &wordlist[slen];
69
bool WordList::operator!=(const WordList &other) const {
72
for (int i=0; i<len; i++) {
73
if (strcmp(words[i], other.words[i]) != 0)
79
void WordList::Clear() {
89
extern "C" int cmpString(const void *a1, const void *a2) {
90
// Can't work out the correct incantation to use modern casts here
91
return strcmp(*(char **)(a1), *(char **)(a2));
94
static void SortWordList(char **words, unsigned int len) {
95
qsort(reinterpret_cast<void *>(words), len, sizeof(*words),
99
void WordList::Set(const char *s) {
101
list = new char[strlen(s) + 1];
103
words = ArrayFromWordList(list, &len, onlyLineEnds);
104
SortWordList(words, len);
105
for (unsigned int k = 0; k < (sizeof(starts) / sizeof(starts[0])); k++)
107
for (int l = len - 1; l >= 0; l--) {
108
unsigned char indexChar = words[l][0];
109
starts[indexChar] = l;
113
/** Check whether a string is in the list.
114
* List elements are either exact matches or prefixes.
115
* Prefix elements start with '^' and match all strings that start with the rest of the element
116
* so '^GTK_' matches 'GTK_X', 'GTK_MAJOR_VERSION', and 'GTK_'.
118
bool WordList::InList(const char *s) const {
121
unsigned char firstChar = s[0];
122
int j = starts[firstChar];
124
while ((unsigned char)words[j][0] == firstChar) {
125
if (s[1] == words[j][1]) {
126
const char *a = words[j] + 1;
127
const char *b = s + 1;
128
while (*a && *a == *b) {
140
while (words[j][0] == '^') {
141
const char *a = words[j] + 1;
143
while (*a && *a == *b) {
155
/** similar to InList, but word s can be a substring of keyword.
156
* eg. the keyword define is defined as def~ine. This means the word must start
157
* with def to be a keyword, but also defi, defin and define are valid.
158
* The marker is ~ in this case.
160
bool WordList::InListAbbreviated(const char *s, const char marker) const {
163
unsigned char firstChar = s[0];
164
int j = starts[firstChar];
166
while (words[j][0] == firstChar) {
167
bool isSubword = false;
169
if (words[j][1] == marker) {
173
if (s[1] == words[j][start]) {
174
const char *a = words[j] + start;
175
const char *b = s + 1;
176
while (*a && *a == *b) {
184
if ((!*a || isSubword) && !*b)
192
while (words[j][0] == '^') {
193
const char *a = words[j] + 1;
195
while (*a && *a == *b) {