-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemojis.cpp
53 lines (47 loc) · 1.33 KB
/
emojis.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <map>
#include <string>
namespace emojicpp {
static std::map<std::string, std::string> emojis = {
#include "emoji_map.h"
};
std::string emojize(std::string s, bool escape=true) {
int index = -1;
int sLen = s.size();
for (int i = 0; i < sLen; i++) {
if (s[i] == *L":") {
// check if colon is escaped
if(escape && i!=0 && s[i-1]=='\\')
continue;
if (index == -1) {
index = i;
}
else {
if (i - index==1) {
index = i;
continue;
}
std::map<std::string, std::string>::iterator it;
it = emojis.find(s.substr(index, i - index + 1));
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();
sLen -= goBack;
i -= goBack;
index = -1;
}
}
}
return s;
}
}
int main() {
for (auto e:emojicpp::emojis) std::cout << e.first << "=" << e.second << std::endl;
return 0;
}