From bb50dc8f134be308fcc9126d1a686465e4fd467a Mon Sep 17 00:00:00 2001 From: galloj Date: Sat, 19 Oct 2019 14:58:38 +0200 Subject: [PATCH] Using unordered map + making code more readable --- emoji.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/emoji.h b/emoji.h index d8c7a52..72799c5 100644 --- a/emoji.h +++ b/emoji.h @@ -1,8 +1,8 @@ -#include +#include namespace emojicpp { - static std::map EMOJIS = { + static std::unordered_map EMOJIS = { {":admission_tickets:" , "\U0001F39F"}, {":aerial_tramway:" , "\U0001F6A1"}, {":airplane:" , "\U00002708"}, @@ -1313,26 +1313,27 @@ namespace emojicpp { int index = -1; int sLen = s.size(); for (int i = 0; i < sLen; i++) { - if (s[i] == *L":") { + if (s[i] == ':') { if (index == -1) { index = i; } else { - if (i - index==1) { + int textEmojiLen = i - index + 1; + if (textEmojiLen == 2) { index = i; continue; } - std::map::iterator it; - it = EMOJIS.find(s.substr(index, i - index + 1)); + std::unordered_map::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;