mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Make struct Uppercase have constexpr methods
This commit is contained in:
@@ -3,6 +3,9 @@
|
|||||||
#ifndef RGBDS_UTIL_HPP
|
#ifndef RGBDS_UTIL_HPP
|
||||||
#define RGBDS_UTIL_HPP
|
#define RGBDS_UTIL_HPP
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <ctype.h>
|
||||||
|
#include <numeric>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
@@ -15,8 +18,19 @@ bool continuesIdentifier(int c);
|
|||||||
char const *printChar(int c);
|
char const *printChar(int c);
|
||||||
|
|
||||||
struct Uppercase {
|
struct Uppercase {
|
||||||
size_t operator()(std::string const &str) const;
|
// FNV-1a hash of an uppercased string
|
||||||
bool operator()(std::string const &str1, std::string const &str2) const;
|
constexpr size_t operator()(std::string const &str) const {
|
||||||
|
return std::accumulate(RANGE(str), 0x811C9DC5, [](size_t hash, char c) {
|
||||||
|
return (hash ^ toupper(c)) * 16777619;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare two strings without case-sensitivity (by converting to uppercase)
|
||||||
|
constexpr bool 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);
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
18
src/util.cpp
18
src/util.cpp
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
#include "util.hpp"
|
#include "util.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <ctype.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -61,19 +59,3 @@ char const *printChar(int c) {
|
|||||||
buf[4] = '\0';
|
buf[4] = '\0';
|
||||||
return buf;
|
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user