From 885f8452e455c142f703187f2cbef23317363dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 30 Sep 2017 11:25:46 +0200 Subject: [PATCH] reuse case insensitive string comparison function --- lib/importproject.h | 11 ++--------- lib/path.cpp | 13 +------------ lib/utils.h | 13 +++++++++++++ 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/lib/importproject.h b/lib/importproject.h index 1a994b445..7163a7642 100644 --- a/lib/importproject.h +++ b/lib/importproject.h @@ -23,6 +23,7 @@ #include "config.h" #include "platform.h" +#include "utils.h" #include #include @@ -37,15 +38,7 @@ namespace cppcheck { struct stricmp { bool operator()(const std::string &lhs, const std::string &rhs) const { - if (lhs.size() != rhs.size()) - return lhs.size() < rhs.size(); - for (unsigned int i = 0; i < lhs.size(); ++i) { - char c1 = std::toupper((unsigned char)lhs[i]); - char c2 = std::toupper((unsigned char)rhs[i]); - if (c1 != c2) - return c1 < c2; - } - return false; + return caseInsensitiveStringCompare(lhs,rhs) < 0; } }; } diff --git a/lib/path.cpp b/lib/path.cpp index ec43d2a69..effe81470 100644 --- a/lib/path.cpp +++ b/lib/path.cpp @@ -86,20 +86,9 @@ std::string Path::getPathFromFilename(const std::string &filename) return ""; } - bool Path::sameFileName(const std::string &fname1, const std::string &fname2) { -#if defined(__linux__) || defined(__sun) || defined(__hpux) - return (fname1 == fname2); -#elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32)) - return (_stricmp(fname1.c_str(), fname2.c_str()) == 0); -#elif defined(__GNUC__) - return (strcasecmp(fname1.c_str(), fname2.c_str()) == 0); -#elif defined(__BORLANDC__) - return (stricmp(fname1.c_str(), fname2.c_str()) == 0); -#else -#error Platform filename compare function needed -#endif + return caseInsensitiveFilesystem() ? (caseInsensitiveStringCompare(fname1, fname2) == 0) : (fname1 == fname2); } // This wrapper exists because Sun's CC does not allow a static_cast diff --git a/lib/utils.h b/lib/utils.h index d7b421eaa..bc3482339 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -78,6 +78,19 @@ inline static const char *getOrdinalText(int i) return "th"; } +inline static int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs) +{ + if (lhs.size() != rhs.size()) + return (lhs.size() < rhs.size()) ? -1 : ((lhs.size() == rhs.size()) ? 0 : 1); + for (unsigned int i = 0; i < lhs.size(); ++i) { + int c1 = std::toupper(lhs[i]); + int c2 = std::toupper(rhs[i]); + if (c1 != c2) + return (c1 < c2) ? -1 : ((c1 == c2) ? 0 : 1); + } + return 0; +} + #define UNUSED(x) (void)(x) #endif