reuse case insensitive string comparison function
This commit is contained in:
parent
2b7ef7156a
commit
885f8452e4
|
@ -23,6 +23,7 @@
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
@ -37,15 +38,7 @@
|
||||||
namespace cppcheck {
|
namespace cppcheck {
|
||||||
struct stricmp {
|
struct stricmp {
|
||||||
bool operator()(const std::string &lhs, const std::string &rhs) const {
|
bool operator()(const std::string &lhs, const std::string &rhs) const {
|
||||||
if (lhs.size() != rhs.size())
|
return caseInsensitiveStringCompare(lhs,rhs) < 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
13
lib/path.cpp
13
lib/path.cpp
|
@ -86,20 +86,9 @@ std::string Path::getPathFromFilename(const std::string &filename)
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
|
bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
|
||||||
{
|
{
|
||||||
#if defined(__linux__) || defined(__sun) || defined(__hpux)
|
return caseInsensitiveFilesystem() ? (caseInsensitiveStringCompare(fname1, fname2) == 0) : (fname1 == fname2);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This wrapper exists because Sun's CC does not allow a static_cast
|
// This wrapper exists because Sun's CC does not allow a static_cast
|
||||||
|
|
13
lib/utils.h
13
lib/utils.h
|
@ -78,6 +78,19 @@ inline static const char *getOrdinalText(int i)
|
||||||
return "th";
|
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)
|
#define UNUSED(x) (void)(x)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue