diff --git a/modules/juce_core/text/juce_CharPointer_UTF8.h b/modules/juce_core/text/juce_CharPointer_UTF8.h index ba4ac186e9..facd5a57ad 100644 --- a/modules/juce_core/text/juce_CharPointer_UTF8.h +++ b/modules/juce_core/text/juce_CharPointer_UTF8.h @@ -420,13 +420,7 @@ public: /** Compares this string with another one. */ int compareIgnoreCase (const CharPointer_UTF8 other) const noexcept { - #if JUCE_MINGW || (JUCE_WINDOWS && JUCE_CLANG) return CharacterFunctions::compareIgnoreCase (*this, other); - #elif JUCE_WINDOWS - return stricmp (data, other.data); - #else - return strcasecmp (data, other.data); - #endif } /** Compares this string with another one, up to a specified number of characters. */ diff --git a/modules/juce_core/text/juce_CharacterFunctions.h b/modules/juce_core/text/juce_CharacterFunctions.h index 3fce59b5bb..9628e792a4 100644 --- a/modules/juce_core/text/juce_CharacterFunctions.h +++ b/modules/juce_core/text/juce_CharacterFunctions.h @@ -391,18 +391,28 @@ public: dest.writeNull(); } + /** Compares two characters. */ + static inline int compare (juce_wchar char1, juce_wchar char2) noexcept + { + if (int diff = static_cast (char1) - static_cast (char2)) + return diff < 0 ? -1 : 1; + + return 0; + } + /** Compares two null-terminated character strings. */ template static int compare (CharPointerType1 s1, CharPointerType2 s2) noexcept { for (;;) { - const int c1 = (int) s1.getAndAdvance(); - const int c2 = (int) s2.getAndAdvance(); - const int diff = c1 - c2; + const juce_wchar c1 = s1.getAndAdvance(); + + if (int diff = compare (c1, s2.getAndAdvance())) + return diff; - if (diff != 0) return diff < 0 ? -1 : 1; - if (c1 == 0) break; + if (c1 == 0) + break; } return 0; @@ -414,31 +424,37 @@ public: { while (--maxChars >= 0) { - const int c1 = (int) s1.getAndAdvance(); - const int c2 = (int) s2.getAndAdvance(); - const int diff = c1 - c2; + const juce_wchar c1 = s1.getAndAdvance(); - if (diff != 0) return diff < 0 ? -1 : 1; - if (c1 == 0) break; + if (int diff = compare (c1, s2.getAndAdvance())) + return diff; + + if (c1 == 0) + break; } return 0; } + /** Compares two characters, using a case-independant match. */ + static inline int compareIgnoreCase (juce_wchar char1, juce_wchar char2) noexcept + { + return char1 != char2 ? compare (toUpperCase (char1), toUpperCase (char2)) : 0; + } + /** Compares two null-terminated character strings, using a case-independant match. */ template static int compareIgnoreCase (CharPointerType1 s1, CharPointerType2 s2) noexcept { for (;;) { - const int c1 = (int) s1.toUpperCase(); - const int c2 = (int) s2.toUpperCase(); - const int diff = c1 - c2; + const juce_wchar c1 = s1.getAndAdvance(); - if (diff != 0) return diff < 0 ? -1 : 1; - if (c1 == 0) break; + if (int diff = compareIgnoreCase (c1, s2.getAndAdvance())) + return diff; - ++s1; ++s2; + if (c1 == 0) + break; } return 0; @@ -450,14 +466,13 @@ public: { while (--maxChars >= 0) { - const int c1 = (int) s1.toUpperCase(); - const int c2 = (int) s2.toUpperCase(); - const int diff = c1 - c2; + const juce_wchar c1 = s1.getAndAdvance(); - if (diff != 0) return diff < 0 ? -1 : 1; - if (c1 == 0) break; + if (int diff = compareIgnoreCase (c1, s2.getAndAdvance())) + return diff; - ++s1; ++s2; + if (c1 == 0) + break; } return 0;