diff options
Diffstat (limited to 'lexlib/WordList.cxx')
-rw-r--r-- | lexlib/WordList.cxx | 38 |
1 files changed, 29 insertions, 9 deletions
diff --git a/lexlib/WordList.cxx b/lexlib/WordList.cxx index c8b4cd63b..937f18948 100644 --- a/lexlib/WordList.cxx +++ b/lexlib/WordList.cxx @@ -20,7 +20,7 @@ using namespace Scintilla; * Creates an array that points into each word in the string and puts \0 terminators * after each word. */ -static char **ArrayFromWordList(char *wordlist, int *len, bool onlyLineEnds = false) { +static char **ArrayFromWordList(char *wordlist, size_t slen, int *len, bool onlyLineEnds = false) { int prev = '\n'; int words = 0; // For rapid determination of whether a character is a separator, build @@ -40,7 +40,6 @@ static char **ArrayFromWordList(char *wordlist, int *len, bool onlyLineEnds = fa } char **keywords = new char *[words + 1]; int wordsStore = 0; - const size_t slen = strlen(wordlist); if (words) { prev = '\0'; for (size_t k = 0; k < slen; k++) { @@ -117,22 +116,43 @@ static void SortWordList(char **words, unsigned int len) { #endif -void WordList::Set(const char *s) { - Clear(); +bool WordList::Set(const char *s) { const size_t lenS = strlen(s) + 1; - list = new char[lenS]; - memcpy(list, s, lenS); - words = ArrayFromWordList(list, &len, onlyLineEnds); + char *listTemp = new char[lenS]; + memcpy(listTemp, s, lenS); + int lenTemp = 0; + char **wordsTemp = ArrayFromWordList(listTemp, lenS - 1, &lenTemp, onlyLineEnds); #ifdef _MSC_VER - std::sort(words, words + len, cmpWords); + std::sort(wordsTemp, wordsTemp + lenTemp, cmpWords); #else - SortWordList(words, len); + SortWordList(wordsTemp, lenTemp); #endif + + if (lenTemp == len) { + bool changed = false; + for (int i = 0; i < lenTemp; i++) { + if (strcmp(words[i], wordsTemp[i]) != 0) { + changed = true; + break; + } + } + if (!changed) { + delete []listTemp; + delete []wordsTemp; + return false; + } + } + + Clear(); + words = wordsTemp; + list = listTemp; + len = lenTemp; std::fill(starts, std::end(starts), -1); for (int l = len - 1; l >= 0; l--) { unsigned char indexChar = words[l][0]; starts[indexChar] = l; } + return true; } /** Check whether a string is in the list. |