Skip to content
Snippets Groups Projects
Commit d1dbefc0 authored by Kitty Barnett's avatar Kitty Barnett
Browse files

Refactor emoji matching

parent fb1553d8
No related branches found
No related tags found
No related merge requests found
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include "llsdserialize.h" #include "llsdserialize.h"
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/algorithm/transform.hpp>
// ============================================================================ // ============================================================================
// Constants // Constants
...@@ -89,6 +91,41 @@ bool LLEmojiDescriptor::isValid() const ...@@ -89,6 +91,41 @@ bool LLEmojiDescriptor::isValid() const
!Categories.empty(); !Categories.empty();
} }
struct emoji_filter_base
{
emoji_filter_base(const std::string& needle)
{
// Search without the colon (if present) so the user can type ':food' and see all emojis in the 'Food' category
mNeedle = (boost::starts_with(needle, ":")) ? needle.substr(1) : needle;
LLStringUtil::toLower(mNeedle);
}
protected:
std::string mNeedle;
};
struct emoji_filter_shortcode_or_category_contains : public emoji_filter_base
{
emoji_filter_shortcode_or_category_contains(const std::string& needle) : emoji_filter_base(needle) {}
bool operator()(const LLEmojiDescriptor& descr) const
{
for (const auto& short_code : descr.ShortCodes)
{
if (boost::icontains(short_code, mNeedle))
return true;
}
for (const auto& category : descr.Categories)
{
if (boost::icontains(category, mNeedle))
return true;
}
return false;
}
};
// ============================================================================ // ============================================================================
// LLEmojiDictionary class // LLEmojiDictionary class
// //
...@@ -130,35 +167,12 @@ void LLEmojiDictionary::initClass() ...@@ -130,35 +167,12 @@ void LLEmojiDictionary::initClass()
} }
} }
LLWString LLEmojiDictionary::findMatchingEmojis(std::string needle) LLWString LLEmojiDictionary::findMatchingEmojis(const std::string& needle) const
{ {
// Search without the colon (if present) so the user can type ':food' and see all emojis in the 'Food' category LLWString result;
LLStringUtil::toLower(needle); boost::transform(mEmojis | boost::adaptors::filtered(emoji_filter_shortcode_or_category_contains(needle)),
const auto kitty_needle = boost::make_iterator_range((boost::starts_with(needle, ":")) ? needle.begin() + 1 : needle.begin(), needle.end()); std::back_inserter(result), [](const auto& descr) { return descr.Character; });
return result;
LLWString wstr;
for (const auto& descr : mEmojis)
{
for (const auto& short_code : descr.ShortCodes)
{
if (boost::icontains(short_code, kitty_needle))
{
wstr.push_back(descr.Character);
continue;
}
}
for (const auto& category : descr.Categories)
{
if (boost::icontains(category, kitty_needle))
{
wstr.push_back(descr.Character);
continue;
}
}
}
return wstr;
} }
std::string LLEmojiDictionary::getNameFromEmoji(llwchar ch) std::string LLEmojiDictionary::getNameFromEmoji(llwchar ch)
......
...@@ -57,7 +57,7 @@ class LLEmojiDictionary : public LLParamSingleton<LLEmojiDictionary>, public LLI ...@@ -57,7 +57,7 @@ class LLEmojiDictionary : public LLParamSingleton<LLEmojiDictionary>, public LLI
public: public:
static void initClass(); static void initClass();
LLWString findMatchingEmojis(std::string needle); LLWString findMatchingEmojis(const std::string& needle) const;
std::string getNameFromEmoji(llwchar ch); std::string getNameFromEmoji(llwchar ch);
private: private:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment