-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem_1410_entityParser.cc
More file actions
55 lines (49 loc) · 1.09 KB
/
Copy pathProblem_1410_entityParser.cc
File metadata and controls
55 lines (49 loc) · 1.09 KB
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
53
54
55
#include <iostream>
#include <string>
#include <vector>
#include "UnitTest.h"
using namespace std;
class Solution
{
using Entity = std::pair<string, char>;
public:
string entityParser(string text)
{
vector<Entity> EntryList = {{""", '"'}, {"'", '\''}, {"&", '&'}, {">", '>'}, {"<", '<'}, {"⁄", '/'}};
string ans;
for (int i = 0; i < text.length();)
{
bool isEntity = false;
if (text[i] == '&')
{
for (auto& [e, c] : EntryList)
{
if (text.substr(i, e.size()) == e)
{
ans.push_back(c);
i += e.size();
isEntity = true;
break;
}
}
}
if (!isEntity)
{
ans.push_back(text[i++]);
}
}
return ans;
}
};
void test()
{
Solution s;
EXPECT_TRUE("& is an HTML entity but &ambassador; is not." == s.entityParser("& is an HTML entity but &ambassador; is not."));
EXPECT_TRUE("and I quote: \"...\"" == s.entityParser("and I quote: "...""));
EXPECT_SUMMARY;
}
int main()
{
test();
return 0;
}