Browse Source

Fix correctness error in CaseInsensitiveCompare::operator() by rolling my own implementation.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
77c68c5602
2 changed files with 11 additions and 6 deletions
  1. +1
    -0
      include/string.hpp
  2. +10
    -6
      src/string.cpp

+ 1
- 0
include/string.hpp View File

@@ -45,6 +45,7 @@ Throws std::runtime_error if string is invalid.
std::vector<uint8_t> fromBase64(const std::string& str); std::vector<uint8_t> fromBase64(const std::string& str);


struct CaseInsensitiveCompare { struct CaseInsensitiveCompare {
/** Returns whether `a < b` using case-insensitive lexical comparison. */
bool operator()(const std::string& a, const std::string& b) const; bool operator()(const std::string& a, const std::string& b) const;
}; };




+ 10
- 6
src/string.cpp View File

@@ -184,12 +184,16 @@ std::vector<uint8_t> fromBase64(const std::string& str) {




bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const { bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const {
if (a.size() != b.size())
return false;
auto f = [](unsigned char a, unsigned char b) {
return std::tolower(a) == std::tolower(b);
};
return std::equal(a.begin(), a.end(), b.begin(), f);
for (size_t i = 0;; i++) {
char ai = std::tolower(a[i]);
char bi = std::tolower(b[i]);
if (ai < bi)
return true;
if (ai > bi)
return false;
if (!ai || !bi)
return false;
}
} }






Loading…
Cancel
Save