Factor out an UpperMap for case-insensitive matching

This commit is contained in:
Rangi42 committed 2025-07-28 00:00:24 -04:00
1 parent d16751f56a
commit 75aed1afd5
8 files changed
+87 -84

No files matched your search

+17
View File
@@ -2,6 +2,7 @@
#include "util.hpp"
#include <algorithm>
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
@@ -60,3 +61,19 @@ char const *printChar(int c) {
buf[4] = '\0';
return buf;
}
// FNV-1a hash of an uppercased string
size_t Uppercase::operator()(std::string const &str) const {
size_t hash = 0x811C9DC5;
for (char const &c : str) {
hash = (hash ^ toupper(c)) * 16777619;
}
return hash;
}
// Compare two strings without case-sensitivity (by converting to uppercase)
bool Uppercase::operator()(std::string const &str1, std::string const &str2) const {
return std::equal(RANGE(str1), RANGE(str2), [](char c1, char c2) {
return toupper(c1) == toupper(c2);
});
}