Browse Source

Add string::strcasecmp().

tags/v2.6.5
Andrew Belt 3 months ago
parent
commit
26d2b7b7c8
2 changed files with 15 additions and 10 deletions
  1. +2
    -0
      include/string.hpp
  2. +13
    -10
      src/string.cpp

+ 2
- 0
include/string.hpp View File

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

int strcasecmp(const char* s1, const char* s2);

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


+ 13
- 10
src/string.cpp View File

@@ -238,17 +238,20 @@ std::vector<uint8_t> fromBase64(const std::string& str) {
}


bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const {
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;
int strcasecmp(const char* s1, const char* s2) {
for (size_t i = 0; s1[i] || s2[i]; i++) {
// Cast [-128, -1] char to [128, 255] because negative ints are undefined
int c1 = std::tolower((unsigned char) s1[i]);
int c2 = std::tolower((unsigned char) s2[i]);
if (c1 != c2)
return c1 - c2;
}
return 0;
}


bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const {
return strcasecmp(a.c_str(), b.c_str()) < 0;
}




Loading…
Cancel
Save