mirror-chatterino2/src/common/UsernameSet.cpp

121 lines
2.5 KiB
C++
Raw Normal View History

2018-08-13 13:54:39 +02:00
#include "UsernameSet.hpp"
#include <tuple>
namespace chatterino {
//
// UsernameSet
//
UsernameSet::ConstIterator UsernameSet::begin() const
{
return this->items.begin();
}
UsernameSet::ConstIterator UsernameSet::end() const
{
return this->items.end();
}
UsernameSet::Range UsernameSet::subrange(const Prefix &prefix) const
{
auto it = this->firstKeyForPrefix.find(prefix);
2018-10-21 13:43:02 +02:00
if (it != this->firstKeyForPrefix.end())
{
2018-08-13 13:54:39 +02:00
auto start = this->items.find(it->second);
auto end = start;
2018-10-21 13:43:02 +02:00
while (end != this->items.end() && prefix.isStartOf(*end))
{
2018-08-13 13:54:39 +02:00
end++;
}
return {start, end};
}
return {this->items.end(), this->items.end()};
}
std::set<QString>::size_type UsernameSet::size() const
{
return this->items.size();
}
std::pair<UsernameSet::Iterator, bool> UsernameSet::insert(const QString &value)
{
this->insertPrefix(value);
return this->items.insert(value);
}
std::pair<UsernameSet::Iterator, bool> UsernameSet::insert(QString &&value)
{
this->insertPrefix(value);
return this->items.insert(std::move(value));
}
void UsernameSet::insertPrefix(const QString &value)
{
auto &string = this->firstKeyForPrefix[Prefix(value)];
if (string.isNull() || value.compare(string, Qt::CaseInsensitive) < 0)
2018-10-21 13:43:02 +02:00
string = value;
2018-08-13 13:54:39 +02:00
}
//
// Range
//
UsernameSet::Range::Range(ConstIterator start, ConstIterator end)
: start_(start)
, end_(end)
{
}
UsernameSet::ConstIterator UsernameSet::Range::begin()
{
return this->start_;
}
UsernameSet::ConstIterator UsernameSet::Range::end()
{
return this->end_;
}
//
// Prefix
//
Prefix::Prefix(const QString &string)
2018-08-14 17:34:45 +02:00
: first(string.size() >= 1 ? string[0].toLower() : '\0')
, second(string.size() >= 2 ? string[1].toLower() : '\0')
2018-08-13 13:54:39 +02:00
{
}
bool Prefix::operator==(const Prefix &other) const
{
return std::tie(this->first, this->second) ==
std::tie(other.first, other.second);
}
bool Prefix::isStartOf(const QString &string) const
{
2018-10-21 13:43:02 +02:00
if (string.size() == 0)
{
2018-08-13 13:54:39 +02:00
return this->first == QChar('\0') && this->second == QChar('\0');
2018-10-21 13:43:02 +02:00
}
else if (string.size() == 1)
{
2018-08-14 17:34:45 +02:00
return this->first == string[0].toLower() &&
this->second == QChar('\0');
2018-10-21 13:43:02 +02:00
}
else
{
2018-08-14 17:34:45 +02:00
return this->first == string[0].toLower() &&
this->second == string[1].toLower();
2018-08-13 13:54:39 +02:00
}
}
} // namespace chatterino