1 // Scintilla source code edit control
3 ** Hold a list of words.
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
;
20 * Creates an array that points into each word in the string and puts \0 terminators
23 static char **ArrayFromWordList(char *wordlist
, int *len
, bool onlyLineEnds
= false) {
26 // For rapid determination of whether a character is a separator, build
28 bool wordSeparator
[256] = {}; // Initialise all to false.
29 wordSeparator
[static_cast<unsigned int>('\r')] = true;
30 wordSeparator
[static_cast<unsigned int>('\n')] = true;
32 wordSeparator
[static_cast<unsigned int>(' ')] = true;
33 wordSeparator
[static_cast<unsigned int>('\t')] = true;
35 for (int j
= 0; wordlist
[j
]; j
++) {
36 const int curr
= static_cast<unsigned char>(wordlist
[j
]);
37 if (!wordSeparator
[curr
] && wordSeparator
[prev
])
41 char **keywords
= new char *[words
+ 1];
43 const size_t slen
= strlen(wordlist
);
46 for (size_t k
= 0; k
< slen
; k
++) {
47 if (!wordSeparator
[static_cast<unsigned char>(wordlist
[k
])]) {
49 keywords
[wordsStore
] = &wordlist
[k
];
58 assert(wordsStore
< (words
+ 1));
59 keywords
[wordsStore
] = &wordlist
[slen
];
64 WordList::WordList(bool onlyLineEnds_
) :
65 words(0), list(0), len(0), onlyLineEnds(onlyLineEnds_
) {
66 // Prevent warnings by static analyzers about uninitialized starts.
70 WordList::~WordList() {
74 WordList::operator bool() const {
75 return len
? true : false;
78 bool WordList::operator!=(const WordList
&other
) const {
81 for (int i
=0; i
<len
; i
++) {
82 if (strcmp(words
[i
], other
.words
[i
]) != 0)
88 int WordList::Length() const {
92 void WordList::Clear() {
104 static bool cmpWords(const char *a
, const char *b
) {
105 return strcmp(a
, b
) < 0;
110 static int cmpWords(const void *a
, const void *b
) {
111 return strcmp(*static_cast<const char * const *>(a
), *static_cast<const char * const *>(b
));
114 static void SortWordList(char **words
, unsigned int len
) {
115 qsort(words
, len
, sizeof(*words
), cmpWords
);
120 void WordList::Set(const char *s
) {
122 const size_t lenS
= strlen(s
) + 1;
123 list
= new char[lenS
];
124 memcpy(list
, s
, lenS
);
125 words
= ArrayFromWordList(list
, &len
, onlyLineEnds
);
127 std::sort(words
, words
+ len
, cmpWords
);
129 SortWordList(words
, len
);
131 std::fill(starts
, std::end(starts
), -1);
132 for (int l
= len
- 1; l
>= 0; l
--) {
133 unsigned char indexChar
= words
[l
][0];
134 starts
[indexChar
] = l
;
138 /** Check whether a string is in the list.
139 * List elements are either exact matches or prefixes.
140 * Prefix elements start with '^' and match all strings that start with the rest of the element
141 * so '^GTK_' matches 'GTK_X', 'GTK_MAJOR_VERSION', and 'GTK_'.
143 bool WordList::InList(const char *s
) const {
146 const unsigned char firstChar
= s
[0];
147 int j
= starts
[firstChar
];
149 while (words
[j
][0] == firstChar
) {
150 if (s
[1] == words
[j
][1]) {
151 const char *a
= words
[j
] + 1;
152 const char *b
= s
+ 1;
153 while (*a
&& *a
== *b
) {
163 j
= starts
[static_cast<unsigned int>('^')];
165 while (words
[j
][0] == '^') {
166 const char *a
= words
[j
] + 1;
168 while (*a
&& *a
== *b
) {
180 /** similar to InList, but word s can be a substring of keyword.
181 * eg. the keyword define is defined as def~ine. This means the word must start
182 * with def to be a keyword, but also defi, defin and define are valid.
183 * The marker is ~ in this case.
185 bool WordList::InListAbbreviated(const char *s
, const char marker
) const {
188 const unsigned char firstChar
= s
[0];
189 int j
= starts
[firstChar
];
191 while (words
[j
][0] == firstChar
) {
192 bool isSubword
= false;
194 if (words
[j
][1] == marker
) {
198 if (s
[1] == words
[j
][start
]) {
199 const char *a
= words
[j
] + start
;
200 const char *b
= s
+ 1;
201 while (*a
&& *a
== *b
) {
209 if ((!*a
|| isSubword
) && !*b
)
215 j
= starts
[static_cast<unsigned int>('^')];
217 while (words
[j
][0] == '^') {
218 const char *a
= words
[j
] + 1;
220 while (*a
&& *a
== *b
) {
232 /** similar to InListAbbreviated, but word s can be a abridged version of a keyword.
233 * eg. the keyword is defined as "after.~:". This means the word must have a prefix (begins with) of
234 * "after." and suffix (ends with) of ":" to be a keyword, Hence "after.field:" , "after.form.item:" are valid.
235 * Similarly "~.is.valid" keyword is suffix only... hence "field.is.valid" , "form.is.valid" are valid.
236 * The marker is ~ in this case.
237 * No multiple markers check is done and wont work.
239 bool WordList::InListAbridged(const char *s
, const char marker
) const {
242 const unsigned char firstChar
= s
[0];
243 int j
= starts
[firstChar
];
245 while (words
[j
][0] == firstChar
) {
246 const char *a
= words
[j
];
248 while (*a
&& *a
== *b
) {
252 const size_t suffixLengthA
= strlen(a
);
253 const size_t suffixLengthB
= strlen(b
);
254 if (suffixLengthA
>= suffixLengthB
)
256 b
= b
+ suffixLengthB
- suffixLengthA
- 1;
266 j
= starts
[static_cast<unsigned int>(marker
)];
268 while (words
[j
][0] == marker
) {
269 const char *a
= words
[j
] + 1;
271 const size_t suffixLengthA
= strlen(a
);
272 const size_t suffixLengthB
= strlen(b
);
273 if (suffixLengthA
> suffixLengthB
) {
277 b
= b
+ suffixLengthB
- suffixLengthA
;
279 while (*a
&& *a
== *b
) {
292 const char *WordList::WordAt(int n
) const {