22 explicit Parser(
const std::string&
text) : s_(
text) {}
24 bool parse(std::unique_ptr<Node>&
out, std::string*
error) {
26 if (!parseElement(
out,
nullptr, 0)) {
28 *
error =
"invalid XML near offset " + std::to_string(pos_);
32 if (pos_ != s_.size()) {
33 if (
error) *
error =
"trailing data at offset " + std::to_string(pos_);
40 static constexpr size_t kMaxDepth = 256;
41 const std::string& s_;
45 while (pos_ < s_.size() &&
46 (s_[pos_] ==
' ' || s_[pos_] ==
'\t' || s_[pos_] ==
'\n' || s_[pos_] ==
'\r'))
50 bool startsWith(
const char* lit)
const {
51 const size_t n = std::char_traits<char>::length(lit);
52 return pos_ +
n <= s_.size() && s_.compare(pos_,
n, lit) == 0;
56 if (!startsWith(
"<!--"))
return false;
58 while (pos_ + 2 < s_.size()) {
59 if (s_[pos_] ==
'-' && s_[pos_ + 1] ==
'-' && s_[pos_ + 2] ==
'>') {
68 bool skipProcessingInstruction() {
69 if (!startsWith(
"<?"))
return false;
71 while (pos_ + 1 < s_.size()) {
72 if (s_[pos_] ==
'?' && s_[pos_ + 1] ==
'>') {
82 if (!startsWith(
"<!DOCTYPE") && !startsWith(
"<!doctype"))
return false;
85 while (pos_ < s_.size() &&
depth > 0) {
88 else if (s_[pos_] ==
'>')
98 if (skipComment() || skipProcessingInstruction() || skipDoctype())
continue;
103 bool parseName(std::string&
out) {
104 if (pos_ >= s_.size())
return false;
105 const unsigned char c0 =
static_cast<unsigned char>(s_[pos_]);
106 if (!(std::isalpha(c0) || c0 ==
'_' || c0 ==
':'))
return false;
107 const size_t start = pos_++;
108 while (pos_ < s_.size()) {
109 const unsigned char c =
static_cast<unsigned char>(s_[pos_]);
110 if (std::isalnum(
c) ||
c ==
'_' ||
c ==
'-' ||
c ==
'.' ||
c ==
':')
119 bool decodeEntity(std::string&
out) {
120 if (pos_ >= s_.size() || s_[pos_] !=
'&')
return false;
122 if (startsWith(
"amp;")) {
127 if (startsWith(
"lt;")) {
132 if (startsWith(
"gt;")) {
137 if (startsWith(
"quot;")) {
142 if (startsWith(
"apos;")) {
147 if (pos_ < s_.size() && s_[pos_] ==
'#') {
149 unsigned long code = 0;
150 if (pos_ < s_.size() && (s_[pos_] ==
'x' || s_[pos_] ==
'X')) {
152 const size_t start = pos_;
153 while (pos_ < s_.size() && std::isxdigit(
static_cast<unsigned char>(s_[pos_]))) ++pos_;
154 if (pos_ ==
start || pos_ >= s_.size() || s_[pos_] !=
';')
return false;
155 code = std::strtoul(s_.substr(
start, pos_ -
start).c_str(),
nullptr, 16);
157 const size_t start = pos_;
158 while (pos_ < s_.size() && std::isdigit(
static_cast<unsigned char>(s_[pos_]))) ++pos_;
159 if (pos_ ==
start || pos_ >= s_.size() || s_[pos_] !=
';')
return false;
160 code = std::strtoul(s_.substr(
start, pos_ -
start).c_str(),
nullptr, 10);
163 if (
code == 0 ||
code > 0x10FFFFul)
return false;
165 out +=
static_cast<char>(
code);
166 }
else if (
code < 0x800) {
167 out +=
static_cast<char>(0xC0 | (
code >> 6));
168 out +=
static_cast<char>(0x80 | (
code & 0x3F));
169 }
else if (
code < 0x10000) {
170 out +=
static_cast<char>(0xE0 | (
code >> 12));
171 out +=
static_cast<char>(0x80 | ((
code >> 6) & 0x3F));
172 out +=
static_cast<char>(0x80 | (
code & 0x3F));
174 out +=
static_cast<char>(0xF0 | (
code >> 18));
175 out +=
static_cast<char>(0x80 | ((
code >> 12) & 0x3F));
176 out +=
static_cast<char>(0x80 | ((
code >> 6) & 0x3F));
177 out +=
static_cast<char>(0x80 | (
code & 0x3F));
184 bool parseAttributeValue(std::string&
out) {
185 if (pos_ >= s_.size())
return false;
186 const char quote = s_[pos_];
187 if (
quote !=
'"' &&
quote !=
'\'')
return false;
190 while (pos_ < s_.size()) {
191 const char c = s_[pos_];
197 if (!decodeEntity(
out))
return false;
200 if (
c ==
'<')
return false;
210 if (pos_ >= s_.size())
return false;
211 if (s_[pos_] ==
'/' || s_[pos_] ==
'>')
return true;
213 if (!parseName(
name))
return false;
215 if (pos_ >= s_.size() || s_[pos_] !=
'=')
return false;
219 if (!parseAttributeValue(
value))
return false;
224 bool skipTextUntilTag() {
225 while (pos_ < s_.size() && s_[pos_] !=
'<') {
226 if (s_[pos_] ==
'&') {
228 if (!decodeEntity(discard))
return false;
237 if (
depth > kMaxDepth)
return false;
239 if (pos_ >= s_.size() || s_[pos_] !=
'<')
return false;
240 if (startsWith(
"</") || startsWith(
"<?") || startsWith(
"<!"))
return false;
242 auto node = std::make_unique<Node>();
244 if (!parseName(
node->tag))
return false;
245 if (!parseAttributes(*
node))
return false;
247 if (pos_ >= s_.size())
return false;
248 if (s_[pos_] ==
'/') {
250 if (pos_ >= s_.size() || s_[pos_] !=
'>')
return false;
255 if (s_[pos_] !=
'>')
return false;
259 if (!skipTextUntilTag())
return false;
260 if (pos_ >= s_.size() || s_[pos_] !=
'<')
return false;
261 if (startsWith(
"</")) {
264 if (!parseName(endTag))
return false;
266 if (pos_ >= s_.size() || s_[pos_] !=
'>')
return false;
268 if (endTag !=
node->tag)
return false;
272 if (startsWith(
"<!--")) {
273 if (!skipComment())
return false;
276 if (startsWith(
"<?")) {
277 if (!skipProcessingInstruction())
return false;
280 if (startsWith(
"<![CDATA[")) {
282 while (pos_ + 2 < s_.size()) {
283 if (s_[pos_] ==
']' && s_[pos_ + 1] ==
']' && s_[pos_ + 2] ==
'>') {
291 std::unique_ptr<Node> child;
292 if (!parseElement(child,
node.get(),
depth + 1))
return false;
293 node->children.push_back(std::move(child));
303Document& Document::operator=(Document&&) noexcept = default;
309 if (!parser.parse(doc.root_,
error)) {
327 if (!node_ || !
name)
return fallback;
329 return it == node_->
attributes.end() ? fallback : it->second;
342 std::vector<Element>
out;
343 if (!node_)
return out;
344 for (
const auto& child : node_->
children) {
351 std::vector<Element>
out;
352 if (!node_ || !
tag)
return out;
353 std::function<void(
const Node*)> walk = [&](
const Node*
node) {
354 for (
const auto& child :
node->children) {
355 if (child->tag ==
tag)
out.emplace_back(
Element(child.get()));