anope- supernets anope source code & configuration |
git clone git://git.acid.vegas/anope.git |
Log | Files | Refs | Archive | README |
hashcomp.h (5094B)
1 /* 2 * 3 * (C) 2002-2011 InspIRCd Development Team 4 * (C) 2009-2022 Anope Team <team@anope.org> 5 * 6 * Please read COPYING and README for further details. 7 */ 8 9 #ifndef HASHCOMP_H 10 #define HASHCOMP_H 11 12 #include <string> 13 #include <locale> 14 15 #if defined _LIBCPP_VERSION || defined _WIN32 16 #include <unordered_map> 17 #define TR1NS std 18 #else 19 #include <tr1/unordered_map> 20 #define TR1NS std::tr1 21 #endif 22 23 #include "services.h" 24 25 namespace Anope 26 { 27 class string; 28 29 /* Casemap in use by Anope. ci::string's comparison functions use this (and thus Anope::string) */ 30 extern std::locale casemap; 31 32 extern void CaseMapRebuild(); 33 extern unsigned char tolower(unsigned char); 34 extern unsigned char toupper(unsigned char); 35 36 /* ASCII case insensitive ctype. */ 37 template<typename char_type> 38 class ascii_ctype : public std::ctype<char_type> 39 { 40 public: 41 char_type do_toupper(char_type c) const anope_override 42 { 43 if (c >= 'a' && c <= 'z') 44 return c - 32; 45 else 46 return c; 47 } 48 49 char_type do_tolower(char_type c) const anope_override 50 { 51 if (c >= 'A' && c <= 'Z') 52 return c + 32; 53 else 54 return c; 55 } 56 }; 57 58 /* rfc1459 case insensitive ctype, { = [, } = ], and | = \ */ 59 template<typename char_type> 60 class rfc1459_ctype : public ascii_ctype<char_type> 61 { 62 public: 63 char_type do_toupper(char_type c) const anope_override 64 { 65 if (c == '{' || c == '}' || c == '|') 66 return c - 32; 67 else 68 return ascii_ctype<char_type>::do_toupper(c); 69 } 70 71 char_type do_tolower(char_type c) const anope_override 72 { 73 if (c == '[' || c == ']' || c == '\\') 74 return c + 32; 75 else 76 return ascii_ctype<char_type>::do_tolower(c); 77 } 78 }; 79 } 80 81 /** The ci namespace contains a number of helper classes relevant to case insensitive strings. 82 */ 83 namespace ci 84 { 85 /** The ci_char_traits class is used for ASCII-style comparison of strings. 86 * This class is used to implement ci::string, a case-insensitive, ASCII- 87 * comparing string class. 88 */ 89 struct CoreExport ci_char_traits : std::char_traits<char> 90 { 91 /** Check if two chars match. 92 * @param c1st First character 93 * @param c2nd Second character 94 * @return true if the characters are equal 95 */ 96 static bool eq(char c1st, char c2nd); 97 98 /** Check if two chars do NOT match. 99 * @param c1st First character 100 * @param c2nd Second character 101 * @return true if the characters are unequal 102 */ 103 static bool ne(char c1st, char c2nd); 104 105 /** Check if one char is less than another. 106 * @param c1st First character 107 * @param c2nd Second character 108 * @return true if c1st is less than c2nd 109 */ 110 static bool lt(char c1st, char c2nd); 111 112 /** Compare two strings of size n. 113 * @param str1 First string 114 * @param str2 Second string 115 * @param n Length to compare to 116 * @return similar to strcmp, zero for equal, less than zero for str1 117 * being less and greater than zero for str1 being greater than str2. 118 */ 119 static int compare(const char *str1, const char *str2, size_t n); 120 121 /** Find a char within a string up to position n. 122 * @param s1 String to find in 123 * @param n Position to search up to 124 * @param c Character to search for 125 * @return Pointer to the first occurrence of c in s1 126 */ 127 static const char *find(const char *s1, int n, char c); 128 }; 129 130 /** This typedef declares ci::string based upon ci_char_traits. 131 */ 132 typedef std::basic_string<char, ci_char_traits, std::allocator<char> > string; 133 134 struct CoreExport less 135 { 136 /** Compare two Anope::strings as ci::strings and find which one is less 137 * @param s1 The first string 138 * @param s2 The second string 139 * @return true if s1 < s2, else false 140 */ 141 bool operator()(const Anope::string &s1, const Anope::string &s2) const; 142 }; 143 } 144 145 /* Define operators for + and == with ci::string to std::string for easy assignment 146 * and comparison 147 * 148 * Operator + 149 */ 150 inline std::string operator+(std::string &leftval, ci::string &rightval) 151 { 152 return leftval + std::string(rightval.c_str()); 153 } 154 155 /* Define operators for + and == with ci::string to std::string for easy assignment 156 * and comparison 157 * 158 * Operator + 159 */ 160 inline ci::string operator+(ci::string &leftval, std::string &rightval) 161 { 162 return leftval + ci::string(rightval.c_str()); 163 } 164 165 /* Define operators for + and == with ci::string to std::string for easy assignment 166 * and comparison 167 * 168 * Operator == 169 */ 170 inline bool operator==(const std::string &leftval, const ci::string &rightval) 171 { 172 return leftval.c_str() == rightval; 173 } 174 175 /* Define operators for + and == with ci::string to std::string for easy assignment 176 * and comparison 177 * 178 * Operator == 179 */ 180 inline bool operator==(const ci::string &leftval, const std::string &rightval) 181 { 182 return leftval == rightval.c_str(); 183 } 184 185 /* Define operators != for ci::string to std::string for easy comparison 186 */ 187 inline bool operator!=(const ci::string &leftval, const std::string &rightval) 188 { 189 return !(leftval == rightval.c_str()); 190 } 191 192 /* Define operators != for std::string to ci::string for easy comparison 193 */ 194 inline bool operator!=(const std::string &leftval, const ci::string &rightval) 195 { 196 return !(leftval.c_str() == rightval); 197 } 198 199 #endif // HASHCOMP_H