mirror of
https://github.com/anope/anope.git
synced 2026-06-25 16:06:37 +02:00
Rewrote the hashing systems to properly handle the new Anope::strings which we have everywhere.
This commit is contained in:
+102
-118
@@ -10,16 +10,35 @@
|
||||
|
||||
#include "anope.h"
|
||||
|
||||
/******************************************************
|
||||
/*
|
||||
*
|
||||
* This is the implementation of our special irc::string
|
||||
* class which is a case-insensitive equivalent to
|
||||
* This is an implementation of two special string classes:
|
||||
*
|
||||
* irc::string which is a case-insensitive equivalent to
|
||||
* std::string which is not only case-insensitive but
|
||||
* can also do scandanavian comparisons, e.g. { = [, etc.
|
||||
*
|
||||
* This class depends on the const array 'national_case_insensitive_map'.
|
||||
* ci::string which is a case-insensitive equivalent to
|
||||
* std::string.
|
||||
*
|
||||
******************************************************/
|
||||
* These classes depend on rfc_case_insensitive_map and
|
||||
* ascii_case_insensitive_map
|
||||
*
|
||||
*/
|
||||
|
||||
/** Hash an Anope::string for unordered_map
|
||||
* @param s The string
|
||||
* @return A hash value for the string
|
||||
*/
|
||||
bool Anope::hash::operator()(const Anope::string &s) const
|
||||
{
|
||||
register size_t t = 0;
|
||||
|
||||
for (Anope::string::const_iterator it = s.begin(), it_end = s.end(); it != it_end; ++it)
|
||||
t = 5 * t + *it;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
bool irc::irc_char_traits::eq(char c1st, char c2nd)
|
||||
{
|
||||
@@ -62,6 +81,25 @@ const char *irc::irc_char_traits::find(const char *s1, int n, char c)
|
||||
return n >= 0 ? s1 : NULL;
|
||||
}
|
||||
|
||||
/** Hash an irc::string for unordered_map
|
||||
* @param s The string
|
||||
* @return A hash value for the string
|
||||
*/
|
||||
size_t irc::hash::operator()(const irc::string &s) const
|
||||
{
|
||||
register size_t t = 0;
|
||||
|
||||
for (irc::string::const_iterator it = s.begin(), it_end = s.end(); it != it_end; ++it)
|
||||
t = 5 * t + rfc_case_insensitive_map[static_cast<const unsigned char>(*it)];
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t irc::hash::operator()(const Anope::string &s) const
|
||||
{
|
||||
return operator()(s.irc_str());
|
||||
}
|
||||
|
||||
bool ci::ci_char_traits::eq(char c1st, char c2nd)
|
||||
{
|
||||
return ascii_case_insensitive_map[static_cast<unsigned char>(c1st)] == ascii_case_insensitive_map[static_cast<unsigned char>(c2nd)];
|
||||
@@ -103,6 +141,65 @@ const char *ci::ci_char_traits::find(const char *s1, int n, char c)
|
||||
return n >= 0 ? s1 : NULL;
|
||||
}
|
||||
|
||||
/** Hash a ci::string for unordered_map
|
||||
* @param s The string
|
||||
* @return A hash value for the string
|
||||
*/
|
||||
size_t ci::hash::operator()(const ci::string &s) const
|
||||
{
|
||||
register size_t t = 0;
|
||||
|
||||
for (ci::string::const_iterator it = s.begin(), it_end = s.end(); it != it_end; ++it)
|
||||
t = 5 * t + ascii_case_insensitive_map[static_cast<const unsigned char>(*it)];
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t ci::hash::operator()(const Anope::string &s) const
|
||||
{
|
||||
return operator()(s.ci_str());
|
||||
}
|
||||
|
||||
/** Compare two Anope::strings as ci::strings
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return true if they are equal
|
||||
*/
|
||||
bool std::equal_to<ci::string>::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return s1.ci_str() == s2.ci_str();
|
||||
}
|
||||
|
||||
/** Compare two Anope::strings as irc::strings
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return true if they are equal
|
||||
*/
|
||||
bool std::equal_to<irc::string>::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return s1.irc_str() == s2.irc_str();
|
||||
}
|
||||
|
||||
/** Compare two Anope::strings as ci::strings and find which one is less
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return true if s1 < s2, else false
|
||||
*/
|
||||
bool std::less<ci::string>::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return s1.ci_str().compare(s2.ci_str()) < 0;
|
||||
}
|
||||
|
||||
/** Compare two Anope::strings as irc::strings and find which one is les
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return true if s1 < s2, else false
|
||||
*/
|
||||
bool std::less<irc::string>::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return s2.irc_str().compare(s2.irc_str()) < 0;
|
||||
}
|
||||
|
||||
sepstream::sepstream(const Anope::string &source, char seperator) : tokens(source), sep(seperator)
|
||||
{
|
||||
last_starting_position = n = tokens.begin();
|
||||
@@ -144,116 +241,3 @@ bool sepstream::StreamEnd()
|
||||
return n == tokens.end();
|
||||
}
|
||||
|
||||
/** Compare two std::string's values for hashing in hash_map
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return similar to strcmp, zero for equal, less than zero for str1
|
||||
* being less and greater than zero for str1 being greater than str2.
|
||||
*/
|
||||
bool hash_compare_std_string::operator()(const std::string &s1, const std::string &s2) const
|
||||
{
|
||||
register int i = std::char_traits<char>::compare(s1.c_str(), s2.c_str(), s1.length() < s2.length() ? s1.length() : s2.length());
|
||||
if (!i)
|
||||
return s1.length() < s2.length();
|
||||
return i < 0;
|
||||
}
|
||||
|
||||
bool hash_compare_std_string::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return operator()(s1.str(), s2.str());
|
||||
}
|
||||
|
||||
/** Return a hash value for a string
|
||||
* @param s The string
|
||||
* @return The hash value
|
||||
*/
|
||||
size_t hash_compare_std_string::operator()(const std::string &s) const
|
||||
{
|
||||
register size_t t = 0;
|
||||
|
||||
for (std::string::const_iterator it = s.begin(), it_end = s.end(); it != it_end; ++it)
|
||||
t = 5 * t + static_cast<const unsigned char>(*it);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t hash_compare_std_string::operator()(const Anope::string &s) const
|
||||
{
|
||||
return operator()(s.str());
|
||||
}
|
||||
|
||||
/** Compare two ci::string's values for hashing in hash_map
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return similar to strcmp, zero for equal, less than zero for str1
|
||||
* being less and greater than zero for str1 being greater than str2.
|
||||
*/
|
||||
bool hash_compare_ci_string::operator()(const ci::string &s1, const ci::string &s2) const
|
||||
{
|
||||
register int i = ci::ci_char_traits::compare(s1.c_str(), s2.c_str(), s1.length() < s2.length() ? s1.length() : s2.length());
|
||||
if (!i)
|
||||
return s1.length() < s2.length();
|
||||
return i < 0;
|
||||
}
|
||||
|
||||
bool hash_compare_ci_string::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return operator()(s1.ci_str(), s2.ci_str());
|
||||
}
|
||||
|
||||
/** Return a hash value for a string using case insensitivity
|
||||
* @param s The string
|
||||
* @return The hash value
|
||||
*/
|
||||
size_t hash_compare_ci_string::operator()(const ci::string &s) const
|
||||
{
|
||||
register size_t t = 0;
|
||||
|
||||
for (ci::string::const_iterator it = s.begin(), it_end = s.end(); it != it_end; ++it)
|
||||
t = 5 * t + ascii_case_insensitive_map[static_cast<const unsigned char>(*it)];
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t hash_compare_ci_string::operator()(const Anope::string &s) const
|
||||
{
|
||||
return operator()(s.ci_str());
|
||||
}
|
||||
|
||||
/** Compare two irc::string's values for hashing in hash_map
|
||||
* @param s1 The first string
|
||||
* @param s2 The second string
|
||||
* @return similar to strcmp, zero for equal, less than zero for str1
|
||||
* being less and greater than zero for str1 being greater than str2.
|
||||
*/
|
||||
bool hash_compare_irc_string::operator()(const irc::string &s1, const irc::string &s2) const
|
||||
{
|
||||
register int i = irc::irc_char_traits::compare(s1.c_str(), s2.c_str(), s1.length() < s2.length() ? s1.length() : s2.length());
|
||||
if (!i)
|
||||
return s1.length() < s2.length();
|
||||
return i < 0;
|
||||
}
|
||||
|
||||
bool hash_compare_irc_string::operator()(const Anope::string &s1, const Anope::string &s2) const
|
||||
{
|
||||
return operator()(s1.irc_str(), s2.irc_str());
|
||||
}
|
||||
|
||||
/** Return a hash value for a string using RFC1459 case sensitivity rules
|
||||
* @param s The string
|
||||
* @return The hash value
|
||||
*/
|
||||
size_t hash_compare_irc_string::operator()(const irc::string &s) const
|
||||
{
|
||||
register size_t t = 0;
|
||||
|
||||
for (irc::string::const_iterator it = s.begin(), it_end = s.end(); it != it_end; ++it)
|
||||
t = 5 * t + rfc_case_insensitive_map[static_cast<const unsigned char>(*it)];
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
size_t hash_compare_irc_string::operator()(const Anope::string &s) const
|
||||
{
|
||||
return operator()(s.irc_str());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user