mirror of
https://github.com/anope/anope.git
synced 2026-06-30 00:16:39 +02:00
Optimizations of much of the more commonly used code
This commit is contained in:
+46
-34
@@ -13,6 +13,30 @@
|
||||
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char Anope::tolower(unsigned char c)
|
||||
{
|
||||
return case_map_lower[c];
|
||||
}
|
||||
|
||||
unsigned char Anope::toupper(unsigned char c)
|
||||
{
|
||||
return case_map_upper[c];
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
@@ -23,29 +47,25 @@ std::locale Anope::casemap = std::locale(std::locale(), new Anope::ascii_ctype<c
|
||||
|
||||
bool ci::ci_char_traits::eq(char c1st, char c2nd)
|
||||
{
|
||||
const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(Anope::casemap);
|
||||
return ct.toupper(c1st) == ct.toupper(c2nd);
|
||||
return case_map_upper[static_cast<unsigned char>(c1st)] == case_map_upper[static_cast<unsigned char>(c2nd)];
|
||||
}
|
||||
|
||||
bool ci::ci_char_traits::ne(char c1st, char c2nd)
|
||||
{
|
||||
const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(Anope::casemap);
|
||||
return ct.toupper(c1st) != ct.toupper(c2nd);
|
||||
return !eq(c1st, c2nd);
|
||||
}
|
||||
|
||||
bool ci::ci_char_traits::lt(char c1st, char c2nd)
|
||||
{
|
||||
const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(Anope::casemap);
|
||||
return ct.toupper(c1st) < ct.toupper(c2nd);
|
||||
return case_map_upper[static_cast<unsigned char>(c1st)] < case_map_upper[static_cast<unsigned char>(c2nd)];
|
||||
}
|
||||
|
||||
int ci::ci_char_traits::compare(const char *str1, const char *str2, size_t n)
|
||||
{
|
||||
const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(Anope::casemap);
|
||||
|
||||
for (unsigned i = 0; i < n; ++i)
|
||||
{
|
||||
register char c1 = ct.toupper(*str1), c2 = ct.toupper(*str2);
|
||||
register unsigned char c1 = case_map_upper[static_cast<unsigned char>(*str1)],
|
||||
c2 = case_map_upper[static_cast<unsigned char>(*str2)];
|
||||
|
||||
if (c1 > c2)
|
||||
return 1;
|
||||
@@ -62,9 +82,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)
|
||||
{
|
||||
const std::ctype<char> &ct = std::use_facet<std::ctype<char> >(Anope::casemap);
|
||||
register char c_u = ct.toupper(c);
|
||||
while (n-- > 0 && ct.toupper(*s1) != c_u)
|
||||
while (n-- > 0 && case_map_upper[static_cast<unsigned char>(*s1)] != static_cast<unsigned char>(c))
|
||||
++s1;
|
||||
return n >= 0 ? s1 : NULL;
|
||||
}
|
||||
@@ -74,35 +92,29 @@ bool ci::less::operator()(const Anope::string &s1, const Anope::string &s2) cons
|
||||
return s1.ci_str().compare(s2.ci_str()) < 0;
|
||||
}
|
||||
|
||||
sepstream::sepstream(const Anope::string &source, char seperator) : tokens(source), sep(seperator)
|
||||
sepstream::sepstream(const Anope::string &source, char seperator) : tokens(source), sep(seperator), pos(0)
|
||||
{
|
||||
last_starting_position = n = tokens.begin();
|
||||
}
|
||||
|
||||
bool sepstream::GetToken(Anope::string &token)
|
||||
{
|
||||
Anope::string::iterator lsp = last_starting_position;
|
||||
size_t p = this->pos;
|
||||
|
||||
while (n != tokens.end())
|
||||
while (p < this->tokens.length() && this->tokens[p] == this->sep)
|
||||
++p;
|
||||
|
||||
if (this->StreamEnd())
|
||||
{
|
||||
if (*n == sep || n + 1 == tokens.end())
|
||||
{
|
||||
last_starting_position = n + 1;
|
||||
token = Anope::string(lsp, n + 1 == tokens.end() ? n + 1 : n);
|
||||
|
||||
while (token.length() && token.rfind(sep) == token.length() - 1)
|
||||
token.erase(token.end() - 1);
|
||||
|
||||
++n;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
++n;
|
||||
token.clear();
|
||||
return false;
|
||||
}
|
||||
|
||||
token.clear();
|
||||
return false;
|
||||
while (p < this->tokens.length() && this->tokens[p] != this->sep)
|
||||
++p;
|
||||
|
||||
token = this->tokens.substr(this->pos, p - this->pos);
|
||||
this->pos = p + 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool sepstream::GetToken(Anope::string &token, int num)
|
||||
@@ -135,11 +147,11 @@ bool sepstream::GetTokenRemainder(Anope::string &token, int num)
|
||||
|
||||
const Anope::string sepstream::GetRemaining()
|
||||
{
|
||||
return Anope::string(n, tokens.end());
|
||||
return !this->StreamEnd() ? this->tokens.substr(this->pos) : "";
|
||||
}
|
||||
|
||||
bool sepstream::StreamEnd()
|
||||
{
|
||||
return n == tokens.end();
|
||||
return this->pos >= this->tokens.length();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user