Renamed case insensitive function and classes, and added test for case_insensitive_equal

This commit is contained in:
eidheim 2017-06-10 11:17:49 +02:00
commit 111aeeb7fc
3 changed files with 26 additions and 23 deletions

View file

@ -30,23 +30,23 @@ namespace SimpleWeb {
} }
#endif #endif
# ifndef CASE_INSENSITIVE_EQUALS_AND_HASH # ifndef CASE_INSENSITIVE_EQUAL_AND_HASH
# define CASE_INSENSITIVE_EQUALS_AND_HASH # define CASE_INSENSITIVE_EQUAL_AND_HASH
namespace SimpleWeb { namespace SimpleWeb {
bool iequals(const std::string &str1, const std::string &str2) { bool case_insensitive_equal(const std::string &str1, const std::string &str2) {
return str1.size() == str2.size() && return str1.size() == str2.size() &&
std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) { std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) {
return tolower(a) == tolower(b); return tolower(a) == tolower(b);
}); });
} }
class case_insensitive_equals { class CaseInsensitiveEqual {
public: public:
bool operator()(const std::string &str1, const std::string &str2) const { bool operator()(const std::string &str1, const std::string &str2) const {
return iequals(str1, str2); return case_insensitive_equal(str1, str2);
} }
}; };
// Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226 // Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226
class case_insensitive_hash { class CaseInsensitiveHash {
public: public:
size_t operator()(const std::string &str) const { size_t operator()(const std::string &str) const {
size_t h = 0; size_t h = 0;
@ -76,7 +76,7 @@ namespace SimpleWeb {
std::istream content; std::istream content;
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header; std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual> header;
private: private:
asio::streambuf content_buffer; asio::streambuf content_buffer;

View file

@ -28,23 +28,23 @@ namespace SimpleWeb {
} }
#endif #endif
# ifndef CASE_INSENSITIVE_EQUALS_AND_HASH # ifndef CASE_INSENSITIVE_EQUAL_AND_HASH
# define CASE_INSENSITIVE_EQUALS_AND_HASH # define CASE_INSENSITIVE_EQUAL_AND_HASH
namespace SimpleWeb { namespace SimpleWeb {
bool iequals(const std::string &str1, const std::string &str2) { bool case_insensitive_equal(const std::string &str1, const std::string &str2) {
return str1.size() == str2.size() && return str1.size() == str2.size() &&
std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) { std::equal(str1.begin(), str1.end(), str2.begin(), [](char a, char b) {
return tolower(a) == tolower(b); return tolower(a) == tolower(b);
}); });
} }
class case_insensitive_equals { class CaseInsensitiveEqual {
public: public:
bool operator()(const std::string &str1, const std::string &str2) const { bool operator()(const std::string &str1, const std::string &str2) const {
return iequals(str1, str2); return case_insensitive_equal(str1, str2);
} }
}; };
// Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226 // Based on https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x/2595226#2595226
class case_insensitive_hash { class CaseInsensitiveHash {
public: public:
size_t operator()(const std::string &str) const { size_t operator()(const std::string &str) const {
size_t h = 0; size_t h = 0;
@ -135,7 +135,7 @@ namespace SimpleWeb {
Content content; Content content;
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> header; std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual> header;
regex::smatch path_match; regex::smatch path_match;
@ -143,8 +143,8 @@ namespace SimpleWeb {
unsigned short remote_endpoint_port; unsigned short remote_endpoint_port;
/// Returns query keys with percent-decoded values. /// Returns query keys with percent-decoded values.
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> parse_query_string() { std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual> parse_query_string() {
std::unordered_multimap<std::string, std::string, case_insensitive_hash, case_insensitive_equals> result; std::unordered_multimap<std::string, std::string, CaseInsensitiveHash, CaseInsensitiveEqual> result;
auto qs_start_pos = path.find('?'); auto qs_start_pos = path.find('?');
if (qs_start_pos != std::string::npos && qs_start_pos + 1 < path.size()) { if (qs_start_pos != std::string::npos && qs_start_pos + 1 < path.size()) {
++qs_start_pos; ++qs_start_pos;
@ -453,9 +453,9 @@ namespace SimpleWeb {
auto range=request->header.equal_range("Connection"); auto range=request->header.equal_range("Connection");
for(auto it=range.first;it!=range.second;it++) { for(auto it=range.first;it!=range.second;it++) {
if(iequals(it->second, "close")) { if(case_insensitive_equal(it->second, "close")) {
return; return;
} else if (iequals(it->second, "keep-alive")) { } else if (case_insensitive_equal(it->second, "keep-alive")) {
this->read_request_and_content(response->socket); this->read_request_and_content(response->socket);
return; return;
} }

View file

@ -105,11 +105,14 @@ public:
}; };
int main() { int main() {
case_insensitive_equals equals; assert(case_insensitive_equal("Test", "tesT"));
assert(equals("Test", "tesT")); assert(case_insensitive_equal("tesT", "test"));
assert(equals("tesT", "test")); assert(!case_insensitive_equal("test", "tseT"));
assert(!equals("test", "tset")); CaseInsensitiveEqual equal;
case_insensitive_hash hash; assert(equal("Test", "tesT"));
assert(equal("tesT", "test"));
assert(!equal("test", "tset"));
CaseInsensitiveHash hash;
assert(hash("Test")==hash("tesT")); assert(hash("Test")==hash("tesT"));
assert(hash("tesT")==hash("test")); assert(hash("tesT")==hash("test"));
assert(hash("test")!=hash("tset")); assert(hash("test")!=hash("tset"));