Skip to content

Commit 885f845

Browse files
committed
reuse case insensitive string comparison function
1 parent 2b7ef71 commit 885f845

3 files changed

Lines changed: 16 additions & 21 deletions

File tree

lib/importproject.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include "config.h"
2525
#include "platform.h"
26+
#include "utils.h"
2627

2728
#include <list>
2829
#include <map>
@@ -37,15 +38,7 @@
3738
namespace cppcheck {
3839
struct stricmp {
3940
bool operator()(const std::string &lhs, const std::string &rhs) const {
40-
if (lhs.size() != rhs.size())
41-
return lhs.size() < rhs.size();
42-
for (unsigned int i = 0; i < lhs.size(); ++i) {
43-
char c1 = std::toupper((unsigned char)lhs[i]);
44-
char c2 = std::toupper((unsigned char)rhs[i]);
45-
if (c1 != c2)
46-
return c1 < c2;
47-
}
48-
return false;
41+
return caseInsensitiveStringCompare(lhs,rhs) < 0;
4942
}
5043
};
5144
}

lib/path.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,9 @@ std::string Path::getPathFromFilename(const std::string &filename)
8686
return "";
8787
}
8888

89-
9089
bool Path::sameFileName(const std::string &fname1, const std::string &fname2)
9190
{
92-
#if defined(__linux__) || defined(__sun) || defined(__hpux)
93-
return (fname1 == fname2);
94-
#elif defined(_MSC_VER) || (defined(__GNUC__) && defined(_WIN32))
95-
return (_stricmp(fname1.c_str(), fname2.c_str()) == 0);
96-
#elif defined(__GNUC__)
97-
return (strcasecmp(fname1.c_str(), fname2.c_str()) == 0);
98-
#elif defined(__BORLANDC__)
99-
return (stricmp(fname1.c_str(), fname2.c_str()) == 0);
100-
#else
101-
#error Platform filename compare function needed
102-
#endif
91+
return caseInsensitiveFilesystem() ? (caseInsensitiveStringCompare(fname1, fname2) == 0) : (fname1 == fname2);
10392
}
10493

10594
// This wrapper exists because Sun's CC does not allow a static_cast

lib/utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,19 @@ inline static const char *getOrdinalText(int i)
7878
return "th";
7979
}
8080

81+
inline static int caseInsensitiveStringCompare(const std::string &lhs, const std::string &rhs)
82+
{
83+
if (lhs.size() != rhs.size())
84+
return (lhs.size() < rhs.size()) ? -1 : ((lhs.size() == rhs.size()) ? 0 : 1);
85+
for (unsigned int i = 0; i < lhs.size(); ++i) {
86+
int c1 = std::toupper(lhs[i]);
87+
int c2 = std::toupper(rhs[i]);
88+
if (c1 != c2)
89+
return (c1 < c2) ? -1 : ((c1 == c2) ? 0 : 1);
90+
}
91+
return 0;
92+
}
93+
8194
#define UNUSED(x) (void)(x)
8295

8396
#endif

0 commit comments

Comments
 (0)