summaryrefslogtreecommitdiff
path: root/src/hashcomp.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/hashcomp.cpp')
-rw-r--r--src/hashcomp.cpp152
1 files changed, 125 insertions, 27 deletions
diff --git a/src/hashcomp.cpp b/src/hashcomp.cpp
index 507bb2929..1a2feaa9d 100644
--- a/src/hashcomp.cpp
+++ b/src/hashcomp.cpp
@@ -1,40 +1,46 @@
/*
+ * Anope IRC Services
*
- * (C) 2002-2011 InspIRCd Development Team
- * (C) 2008-2017 Anope Team <team@anope.org>
+ * Copyright (C) 2002-2011 InspIRCd Development Team
+ * Copyright (C) 2008-2017 Anope Team <team@anope.org>
*
- * Please read COPYING and README for further details.
+ * This file is part of Anope. Anope is free software; you can
+ * redistribute it and/or modify it under the terms of the GNU
+ * General Public License as published by the Free Software
+ * Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see see <http://www.gnu.org/licenses/>.
*/
#include "services.h"
#include "hashcomp.h"
#include "anope.h"
+#include "config.h"
-/* Case map in use by Anope */
-std::locale Anope::casemap = std::locale(std::locale(), new Anope::ascii_ctype<char>());
-/* Cache of the above case map, forced upper */
-static unsigned char case_map_upper[256], case_map_lower[256];
-
-/* called whenever Anope::casemap is modified to rebuild the casemap cache */
-void Anope::CaseMapRebuild()
-{
- const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(Anope::casemap);
-
- for (unsigned i = 0; i < sizeof(case_map_upper); ++i)
- {
- case_map_upper[i] = ct.toupper(i);
- case_map_lower[i] = ct.tolower(i);
- }
-}
+#ifdef Boost_FOUND
+#include <boost/locale.hpp>
+#endif
unsigned char Anope::tolower(unsigned char c)
{
- return case_map_lower[c];
+ if (!Config || !Config->CaseMapLower[c])
+ return std::tolower(c);
+
+ return Config->CaseMapLower[c];
}
unsigned char Anope::toupper(unsigned char c)
{
- return case_map_upper[c];
+ if (!Config || !Config->CaseMapUpper[c])
+ return std::toupper(c);
+
+ return Config->CaseMapUpper[c];
}
/*
@@ -43,10 +49,10 @@ unsigned char Anope::toupper(unsigned char c)
* which is a case-insensitive equivalent to std::string.
*
*/
-
+
bool ci::ci_char_traits::eq(char c1st, char c2nd)
{
- return case_map_upper[static_cast<unsigned char>(c1st)] == case_map_upper[static_cast<unsigned char>(c2nd)];
+ return Anope::toupper(c1st) == Anope::toupper(c2nd);
}
bool ci::ci_char_traits::ne(char c1st, char c2nd)
@@ -56,15 +62,15 @@ bool ci::ci_char_traits::ne(char c1st, char c2nd)
bool ci::ci_char_traits::lt(char c1st, char c2nd)
{
- return case_map_upper[static_cast<unsigned char>(c1st)] < case_map_upper[static_cast<unsigned char>(c2nd)];
+ return Anope::toupper(c1st) < Anope::toupper(c2nd);
}
int ci::ci_char_traits::compare(const char *str1, const char *str2, size_t n)
{
for (unsigned i = 0; i < n; ++i)
{
- register unsigned char c1 = case_map_upper[static_cast<unsigned char>(*str1)],
- c2 = case_map_upper[static_cast<unsigned char>(*str2)];
+ unsigned char c1 = Anope::toupper(*str1),
+ c2 = Anope::toupper(*str2);
if (c1 > c2)
return 1;
@@ -81,7 +87,7 @@ int ci::ci_char_traits::compare(const char *str1, const char *str2, size_t n)
const char *ci::ci_char_traits::find(const char *s1, int n, char c)
{
- while (n-- > 0 && case_map_upper[static_cast<unsigned char>(*s1)] != case_map_upper[static_cast<unsigned char>(c)])
+ while (n-- > 0 && Anope::toupper(*s1) != Anope::toupper(c))
++s1;
return n >= 0 ? s1 : NULL;
}
@@ -162,3 +168,95 @@ bool sepstream::StreamEnd()
return this->pos > this->tokens.length();
}
+size_t Anope::hash::operator()(const Anope::string &s) const
+{
+ return std::hash<std::string>()(s.str());
+}
+
+bool Anope::compare::operator()(const Anope::string &s1, const Anope::string &s2) const
+{
+ return s1.equals_cs(s2);
+}
+
+size_t Anope::hash_ci::operator()(const Anope::string &s) const
+{
+ return std::hash<std::string>()(s.lower().str());
+}
+
+bool Anope::compare_ci::operator()(const Anope::string &s1, const Anope::string &s2) const
+{
+ return s1.equals_ci(s2);
+}
+
+size_t Anope::hash_locale::operator()(const Anope::string &s) const
+{
+#ifdef Boost_FOUND
+ if (Config != nullptr && Config->locale != nullptr)
+ {
+ return Anope::locale::hash(s.str());
+ }
+#endif
+
+ return Anope::hash_ci()(s);
+}
+
+bool Anope::compare_locale::operator()(const Anope::string &s1, const Anope::string &s2) const
+{
+#ifdef Boost_FOUND
+ if (Config != nullptr && Config->locale != nullptr)
+ {
+ return Anope::locale::compare(s1.str(), s2.str()) == 0;
+ }
+#endif
+
+ return Anope::compare_ci()(s1, s2);
+}
+
+Anope::string Anope::transform(const Anope::string &s)
+{
+#ifdef Boost_FOUND
+ if (Config != nullptr && Config->locale != nullptr)
+ {
+ return Anope::locale::transform(s.str());
+ }
+#endif
+
+ return s.lower();
+}
+
+#ifdef Boost_FOUND
+
+std::locale Anope::locale::generate(const std::string &name)
+{
+ boost::locale::generator gen;
+ return gen.generate(name);
+}
+
+int Anope::locale::compare(const std::string &s1, const std::string &s2)
+{
+ std::string c1 = boost::locale::conv::between(s1.data(), s1.data() + s1.length(),
+ Config->locale->name(),
+ "");
+
+ std::string c2 = boost::locale::conv::between(s2.data(), s2.data() + s2.length(),
+ Config->locale->name(),
+ "");
+
+ const boost::locale::collator<char> &ct = std::use_facet<boost::locale::collator<char>>(*Config->locale);
+ return ct.compare(boost::locale::collator_base::secondary, c1, c2);
+}
+
+long Anope::locale::hash(const std::string &s)
+{
+ const boost::locale::collator<char> &ct = std::use_facet<boost::locale::collator<char>>(*Config->locale);
+ return ct.hash(boost::locale::collator_base::secondary, s.data(), s.data() + s.length());
+}
+
+std::string Anope::locale::transform(const std::string &s)
+{
+ const boost::locale::collator<char> &ct = std::use_facet<boost::locale::collator<char>>(*Config->locale);
+ return ct.transform(boost::locale::collator_base::secondary, s);
+}
+
+#endif
+