Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using unordered map + making code more readable #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions emoji.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <map>
#include <unordered_map>
#include <string>

namespace emojicpp {

static std::map<std::string, std::string> EMOJIS = {
static std::unordered_map<std::string, std::string> EMOJIS = {
{":admission_tickets:" , u8"\U0001F39F"},
{":aerial_tramway:" , u8"\U0001F6A1"},
{":airplane:" , u8"\U00002708"},
Expand Down Expand Up @@ -1057,29 +1057,30 @@ namespace emojicpp {
int index = -1;
int sLen = s.size();
for (int i = 0; i < sLen; i++) {
if (s[i] == *L":") {
if (s[i] == ':') {
// check if colon is escaped
if(escape && i!=0 && s[i-1]=='\\')
continue;
if (index == -1) {
index = i;
}
else {
if (i - index==1) {
int textEmojiLen = i - index + 1;
if (textEmojiLen == 2) {
index = i;
continue;
}
std::map<std::string, std::string>::iterator it;
it = EMOJIS.find(s.substr(index, i - index + 1));
std::unordered_map<std::string, std::string>::iterator it;
it = EMOJIS.find(s.substr(index, textEmojiLen));
if (it == EMOJIS.end()) {
index = i;
continue;
}
std::string emo = it->second;
// replace from index to i
//std::cout << s.substr(index, i - index + 1) << std::endl; // <---- uncomment to see what text is replaced, might be good for debugging
s.replace(index, i - index + 1 , emo);
int goBack = i - index + 1 - emo.size();
//std::cout << s.substr(index, textEmojiLen) << std::endl; // <---- uncomment to see what text is replaced, might be good for debugging
s.replace(index, textEmojiLen, emo);
int goBack = textEmojiLen - emo.size();
sLen -= goBack;
i -= goBack;
index = -1;
Expand Down