-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1410.html-实体解析器.cpp
61 lines (58 loc) · 1.83 KB
/
1410.html-实体解析器.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
53
54
55
56
57
58
59
60
/*
* @lc app=leetcode.cn id=1410 lang=cpp
*
* [1410] HTML 实体解析器
*/
// @lc code=start
#include <string>
#include <optional>
#include <string_view>
class Solution {
public:
std::string entityParser(const std::string& text) {
std::string result;
result.reserve(text.size());
int i = 0;
while (i < text.size()) {
if (text[i] == '&') {
auto last_i = i;
i++;
while (last_i >= 0) {
for (;i < text.size() && text[i] != ';' && text[i] != '&'; ++i);
if (i == text.size() || text[i] == '&') {
result.append(text.begin() + last_i, text.begin() + i);
if (i == text.size()) {
return result;
} else {
last_i = i;
i++;
continue;
}
}
break;
}
std::string_view sv(text.begin() + last_i, text.begin() + i + 1);
if (sv == """) {
result.push_back('"');
} else if (sv == "'") {
result.push_back('\'');
} else if (sv == "&") {
result.push_back('&');
} else if (sv == ">") {
result.push_back('>');
} else if (sv == "<") {
result.push_back('<');
} else if (sv == "⁄") {
result.push_back('/');
} else {
result.append(sv);
}
} else {
result.push_back(text[i]);
}
++i;
}
return result;
}
};
// @lc code=end