From 30dbb454409a912cfc190dcc97344f1076cb9407 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Thu, 18 Oct 2001 01:27:16 +0000 Subject: [PATCH] A fix for Scintilla's case insensitive sort routine git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12051 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/src/stc/scintilla/src/PropSet.cxx | 47 +++++++++++++++-------- src/stc/scintilla/src/PropSet.cxx | 47 +++++++++++++++-------- 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/contrib/src/stc/scintilla/src/PropSet.cxx b/contrib/src/stc/scintilla/src/PropSet.cxx index 701b2a82c7..4e87508796 100644 --- a/contrib/src/stc/scintilla/src/PropSet.cxx +++ b/contrib/src/stc/scintilla/src/PropSet.cxx @@ -26,13 +26,23 @@ static inline char MakeUpperCase(char ch) { return static_cast(ch - 'a' + 'A'); } +static inline bool IsLetter(char ch) { + return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')); +} + + int CompareCaseInsensitive(const char *a, const char *b) { while (*a && *b) { if (*a != *b) { - char upperA = MakeUpperCase(*a); - char upperB = MakeUpperCase(*b); - if (upperA != upperB) - return upperA - upperB; + if (IsLetter(*a) && IsLetter(*b)) { + char upperA = MakeUpperCase(*a); + char upperB = MakeUpperCase(*b); + if (upperA != upperB) + return upperA - upperB; + } + else { + return *a - *b; + } } a++; b++; @@ -44,10 +54,15 @@ int CompareCaseInsensitive(const char *a, const char *b) { int CompareNCaseInsensitive(const char *a, const char *b, int len) { while (*a && *b && len) { if (*a != *b) { - char upperA = MakeUpperCase(*a); - char upperB = MakeUpperCase(*b); - if (upperA != upperB) - return upperA - upperB; + if (IsLetter(*a) && IsLetter(*b)) { + char upperA = MakeUpperCase(*a); + char upperB = MakeUpperCase(*b); + if (upperA != upperB) + return upperA - upperB; + } + else { + return *a - *b; + } } a++; b++; @@ -94,8 +109,8 @@ void PropSet::Set(const char *key, const char *val, int lenKey, int lenVal) { lenVal = strlen(val); unsigned int hash = HashString(key, lenKey); for (Property *p = props[hash % hashRoots]; p; p = p->next) { - if ((hash == p->hash) && - ((strlen(p->key) == static_cast(lenKey)) && + if ((hash == p->hash) && + ((strlen(p->key) == static_cast(lenKey)) && (0 == strncmp(p->key, key, lenKey)))) { // Replace current value delete [](p->val); @@ -660,13 +675,13 @@ char *WordList::GetNearestWords( if (!cond) { // Find first match while ((pivot > start) && - (0 == CompareNCaseInsensitive(wordStart, + (0 == CompareNCaseInsensitive(wordStart, wordsNoCase[pivot-1], searchLen))) { --pivot; } // Grab each match while ((pivot <= end) && - (0 == CompareNCaseInsensitive(wordStart, + (0 == CompareNCaseInsensitive(wordStart, wordsNoCase[pivot], searchLen))) { wordlen = LengthWord(wordsNoCase[pivot], otherSeparator) + 1; wordsNear.append(wordsNoCase[pivot], wordlen, ' '); @@ -686,14 +701,14 @@ char *WordList::GetNearestWords( if (!cond) { // Find first match while ((pivot > start) && - (0 == strncmp(wordStart, - words[pivot-1], searchLen))) { + (0 == strncmp(wordStart, + words[pivot-1], searchLen))) { --pivot; } // Grab each match while ((pivot <= end) && - (0 == strncmp(wordStart, - words[pivot], searchLen))) { + (0 == strncmp(wordStart, + words[pivot], searchLen))) { wordlen = LengthWord(words[pivot], otherSeparator) + 1; wordsNear.append(words[pivot], wordlen, ' '); ++pivot; diff --git a/src/stc/scintilla/src/PropSet.cxx b/src/stc/scintilla/src/PropSet.cxx index 701b2a82c7..4e87508796 100644 --- a/src/stc/scintilla/src/PropSet.cxx +++ b/src/stc/scintilla/src/PropSet.cxx @@ -26,13 +26,23 @@ static inline char MakeUpperCase(char ch) { return static_cast(ch - 'a' + 'A'); } +static inline bool IsLetter(char ch) { + return ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')); +} + + int CompareCaseInsensitive(const char *a, const char *b) { while (*a && *b) { if (*a != *b) { - char upperA = MakeUpperCase(*a); - char upperB = MakeUpperCase(*b); - if (upperA != upperB) - return upperA - upperB; + if (IsLetter(*a) && IsLetter(*b)) { + char upperA = MakeUpperCase(*a); + char upperB = MakeUpperCase(*b); + if (upperA != upperB) + return upperA - upperB; + } + else { + return *a - *b; + } } a++; b++; @@ -44,10 +54,15 @@ int CompareCaseInsensitive(const char *a, const char *b) { int CompareNCaseInsensitive(const char *a, const char *b, int len) { while (*a && *b && len) { if (*a != *b) { - char upperA = MakeUpperCase(*a); - char upperB = MakeUpperCase(*b); - if (upperA != upperB) - return upperA - upperB; + if (IsLetter(*a) && IsLetter(*b)) { + char upperA = MakeUpperCase(*a); + char upperB = MakeUpperCase(*b); + if (upperA != upperB) + return upperA - upperB; + } + else { + return *a - *b; + } } a++; b++; @@ -94,8 +109,8 @@ void PropSet::Set(const char *key, const char *val, int lenKey, int lenVal) { lenVal = strlen(val); unsigned int hash = HashString(key, lenKey); for (Property *p = props[hash % hashRoots]; p; p = p->next) { - if ((hash == p->hash) && - ((strlen(p->key) == static_cast(lenKey)) && + if ((hash == p->hash) && + ((strlen(p->key) == static_cast(lenKey)) && (0 == strncmp(p->key, key, lenKey)))) { // Replace current value delete [](p->val); @@ -660,13 +675,13 @@ char *WordList::GetNearestWords( if (!cond) { // Find first match while ((pivot > start) && - (0 == CompareNCaseInsensitive(wordStart, + (0 == CompareNCaseInsensitive(wordStart, wordsNoCase[pivot-1], searchLen))) { --pivot; } // Grab each match while ((pivot <= end) && - (0 == CompareNCaseInsensitive(wordStart, + (0 == CompareNCaseInsensitive(wordStart, wordsNoCase[pivot], searchLen))) { wordlen = LengthWord(wordsNoCase[pivot], otherSeparator) + 1; wordsNear.append(wordsNoCase[pivot], wordlen, ' '); @@ -686,14 +701,14 @@ char *WordList::GetNearestWords( if (!cond) { // Find first match while ((pivot > start) && - (0 == strncmp(wordStart, - words[pivot-1], searchLen))) { + (0 == strncmp(wordStart, + words[pivot-1], searchLen))) { --pivot; } // Grab each match while ((pivot <= end) && - (0 == strncmp(wordStart, - words[pivot], searchLen))) { + (0 == strncmp(wordStart, + words[pivot], searchLen))) { wordlen = LengthWord(words[pivot], otherSeparator) + 1; wordsNear.append(words[pivot], wordlen, ' '); ++pivot; -- 2.45.2