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
| class Solution { public: bool strneq(const string& str, int index, int strlen, const char *cmp, int len) { if(strlen - index < len) return false; for(int i = 0; i < len; i++) { if(str[index + i] != cmp[i]) return false; } return true; } string entityParser(string text) { string res; int len = text.length(), i = 0; while(i < len) { if(text[i] != '&') res.push_back(text[i]); else { if(strneq(text, i, len, """, 6)) { i += 6; res.push_back('"'); continue; } else if(strneq(text, i, len, "'", 6)) { i += 6; res.push_back('\''); continue; } else if(strneq(text, i, len, "&", 5)) { i += 5; res.push_back('&'); continue; } else if(strneq(text, i, len, ">", 4)) { i += 4; res.push_back('>'); continue; } else if(strneq(text, i, len, "<", 4)) { i += 4; res.push_back('<'); continue; } else if(strneq(text, i, len, "⁄", 7)) { i += 7; res.push_back('/'); continue; } else { res.push_back(text[i]); } } i++; } return res; } };
|