reuse case insensitive string comparison function

This commit is contained in:
Daniel Marjamäki 2017-09-30 11:25:46 +02:00
parent 2b7ef7156a
commit 885f8452e4
3 changed files with 16 additions and 21 deletions

View File

@ -23,6 +23,7 @@
#include "config.h"
#include "platform.h"
#include "utils.h"
#include <list>
#include <map>
@ -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;
}
};
}

View File

@ -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

View File

@ -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